Back to index

lightning-sunbird  0.9+nobinonly
jsdhash.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla JavaScript code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999-2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Brendan Eich <brendan@mozilla.org> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef jsdhash_h___
00040 #define jsdhash_h___
00041 /*
00042  * Double hashing, a la Knuth 6.
00043  */
00044 #include "jstypes.h"
00045 
00046 JS_BEGIN_EXTERN_C
00047 
00048 #if defined(__GNUC__) && defined(__i386__) && (__GNUC__ >= 3) && !defined(XP_OS2)
00049 #define JS_DHASH_FASTCALL __attribute__ ((regparm (3),stdcall))
00050 #elif defined(XP_WIN)
00051 #define JS_DHASH_FASTCALL __fastcall
00052 #else
00053 #define JS_DHASH_FASTCALL
00054 #endif
00055 
00056 #ifdef DEBUG_XXXbrendan
00057 #define JS_DHASHMETER 1
00058 #endif
00059 
00060 /* Table size limit, do not equal or exceed (see min&maxAlphaFrac, below). */
00061 #undef JS_DHASH_SIZE_LIMIT
00062 #define JS_DHASH_SIZE_LIMIT     JS_BIT(24)
00063 
00064 /* Minimum table size, or gross entry count (net is at most .75 loaded). */
00065 #ifndef JS_DHASH_MIN_SIZE
00066 #define JS_DHASH_MIN_SIZE 16
00067 #elif (JS_DHASH_MIN_SIZE & (JS_DHASH_MIN_SIZE - 1)) != 0
00068 #error "JS_DHASH_MIN_SIZE must be a power of two!"
00069 #endif
00070 
00071 /*
00072  * Multiplicative hash uses an unsigned 32 bit integer and the golden ratio,
00073  * expressed as a fixed-point 32-bit fraction.
00074  */
00075 #define JS_DHASH_BITS           32
00076 #define JS_DHASH_GOLDEN_RATIO   0x9E3779B9U
00077 
00078 /* Primitive and forward-struct typedefs. */
00079 typedef uint32                  JSDHashNumber;
00080 typedef struct JSDHashEntryHdr  JSDHashEntryHdr;
00081 typedef struct JSDHashEntryStub JSDHashEntryStub;
00082 typedef struct JSDHashTable     JSDHashTable;
00083 typedef struct JSDHashTableOps  JSDHashTableOps;
00084 
00085 /*
00086  * Table entry header structure.
00087  *
00088  * In order to allow in-line allocation of key and value, we do not declare
00089  * either here.  Instead, the API uses const void *key as a formal parameter,
00090  * and asks each entry for its key when necessary via a getKey callback, used
00091  * when growing or shrinking the table.  Other callback types are defined
00092  * below and grouped into the JSDHashTableOps structure, for single static
00093  * initialization per hash table sub-type.
00094  *
00095  * Each hash table sub-type should nest the JSDHashEntryHdr structure at the
00096  * front of its particular entry type.  The keyHash member contains the result
00097  * of multiplying the hash code returned from the hashKey callback (see below)
00098  * by JS_DHASH_GOLDEN_RATIO, then constraining the result to avoid the magic 0
00099  * and 1 values.  The stored keyHash value is table size invariant, and it is
00100  * maintained automatically by JS_DHashTableOperate -- users should never set
00101  * it, and its only uses should be via the entry macros below.
00102  *
00103  * The JS_DHASH_ENTRY_IS_LIVE macro tests whether entry is neither free nor
00104  * removed.  An entry may be either busy or free; if busy, it may be live or
00105  * removed.  Consumers of this API should not access members of entries that
00106  * are not live.
00107  *
00108  * However, use JS_DHASH_ENTRY_IS_BUSY for faster liveness testing of entries
00109  * returned by JS_DHashTableOperate, as JS_DHashTableOperate never returns a
00110  * non-live, busy (i.e., removed) entry pointer to its caller.  See below for
00111  * more details on JS_DHashTableOperate's calling rules.
00112  */
00113 struct JSDHashEntryHdr {
00114     JSDHashNumber       keyHash;        /* every entry must begin like this */
00115 };
00116 
00117 #define JS_DHASH_ENTRY_IS_FREE(entry)   ((entry)->keyHash == 0)
00118 #define JS_DHASH_ENTRY_IS_BUSY(entry)   (!JS_DHASH_ENTRY_IS_FREE(entry))
00119 #define JS_DHASH_ENTRY_IS_LIVE(entry)   ((entry)->keyHash >= 2)
00120 
00121 /*
00122  * A JSDHashTable is currently 8 words (without the JS_DHASHMETER overhead)
00123  * on most architectures, and may be allocated on the stack or within another
00124  * structure or class (see below for the Init and Finish functions to use).
00125  *
00126  * To decide whether to use double hashing vs. chaining, we need to develop a
00127  * trade-off relation, as follows:
00128  *
00129  * Let alpha be the load factor, esize the entry size in words, count the
00130  * entry count, and pow2 the power-of-two table size in entries.
00131  *
00132  *   (JSDHashTable overhead)    > (JSHashTable overhead)
00133  *   (unused table entry space) > (malloc and .next overhead per entry) +
00134  *                                (buckets overhead)
00135  *   (1 - alpha) * esize * pow2 > 2 * count + pow2
00136  *
00137  * Notice that alpha is by definition (count / pow2):
00138  *
00139  *   (1 - alpha) * esize * pow2 > 2 * alpha * pow2 + pow2
00140  *   (1 - alpha) * esize        > 2 * alpha + 1
00141  *
00142  *   esize > (1 + 2 * alpha) / (1 - alpha)
00143  *
00144  * This assumes both tables must keep keyHash, key, and value for each entry,
00145  * where key and value point to separately allocated strings or structures.
00146  * If key and value can be combined into one pointer, then the trade-off is:
00147  *
00148  *   esize > (1 + 3 * alpha) / (1 - alpha)
00149  *
00150  * If the entry value can be a subtype of JSDHashEntryHdr, rather than a type
00151  * that must be allocated separately and referenced by an entry.value pointer
00152  * member, and provided key's allocation can be fused with its entry's, then
00153  * k (the words wasted per entry with chaining) is 4.
00154  *
00155  * To see these curves, feed gnuplot input like so:
00156  *
00157  *   gnuplot> f(x,k) = (1 + k * x) / (1 - x)
00158  *   gnuplot> plot [0:.75] f(x,2), f(x,3), f(x,4)
00159  *
00160  * For k of 2 and a well-loaded table (alpha > .5), esize must be more than 4
00161  * words for chaining to be more space-efficient than double hashing.
00162  *
00163  * Solving for alpha helps us decide when to shrink an underloaded table:
00164  *
00165  *   esize                     > (1 + k * alpha) / (1 - alpha)
00166  *   esize - alpha * esize     > 1 + k * alpha
00167  *   esize - 1                 > (k + esize) * alpha
00168  *   (esize - 1) / (k + esize) > alpha
00169  *
00170  *   alpha < (esize - 1) / (esize + k)
00171  *
00172  * Therefore double hashing should keep alpha >= (esize - 1) / (esize + k),
00173  * assuming esize is not too large (in which case, chaining should probably be
00174  * used for any alpha).  For esize=2 and k=3, we want alpha >= .2; for esize=3
00175  * and k=2, we want alpha >= .4.  For k=4, esize could be 6, and alpha >= .5
00176  * would still obtain.  See the JS_DHASH_MIN_ALPHA macro further below.
00177  *
00178  * The current implementation uses a configurable lower bound on alpha, which
00179  * defaults to .25, when deciding to shrink the table (while still respecting
00180  * JS_DHASH_MIN_SIZE).
00181  *
00182  * Note a qualitative difference between chaining and double hashing: under
00183  * chaining, entry addresses are stable across table shrinks and grows.  With
00184  * double hashing, you can't safely hold an entry pointer and use it after an
00185  * ADD or REMOVE operation, unless you sample table->generation before adding
00186  * or removing, and compare the sample after, dereferencing the entry pointer
00187  * only if table->generation has not changed.
00188  *
00189  * The moral of this story: there is no one-size-fits-all hash table scheme,
00190  * but for small table entry size, and assuming entry address stability is not
00191  * required, double hashing wins.
00192  */
00193 struct JSDHashTable {
00194     const JSDHashTableOps *ops;         /* virtual operations, see below */
00195     void                *data;          /* ops- and instance-specific data */
00196     int16               hashShift;      /* multiplicative hash shift */
00197     uint8               maxAlphaFrac;   /* 8-bit fixed point max alpha */
00198     uint8               minAlphaFrac;   /* 8-bit fixed point min alpha */
00199     uint32              entrySize;      /* number of bytes in an entry */
00200     uint32              entryCount;     /* number of entries in table */
00201     uint32              removedCount;   /* removed entry sentinels in table */
00202     uint32              generation;     /* entry storage generation number */
00203     char                *entryStore;    /* entry storage */
00204 #ifdef JS_DHASHMETER
00205     struct JSDHashStats {
00206         uint32          searches;       /* total number of table searches */
00207         uint32          steps;          /* hash chain links traversed */
00208         uint32          hits;           /* searches that found key */
00209         uint32          misses;         /* searches that didn't find key */
00210         uint32          lookups;        /* number of JS_DHASH_LOOKUPs */
00211         uint32          addMisses;      /* adds that miss, and do work */
00212         uint32          addOverRemoved; /* adds that recycled a removed entry */
00213         uint32          addHits;        /* adds that hit an existing entry */
00214         uint32          addFailures;    /* out-of-memory during add growth */
00215         uint32          removeHits;     /* removes that hit, and do work */
00216         uint32          removeMisses;   /* useless removes that miss */
00217         uint32          removeFrees;    /* removes that freed entry directly */
00218         uint32          removeEnums;    /* removes done by Enumerate */
00219         uint32          grows;          /* table expansions */
00220         uint32          shrinks;        /* table contractions */
00221         uint32          compresses;     /* table compressions */
00222         uint32          enumShrinks;    /* contractions after Enumerate */
00223     } stats;
00224 #endif
00225 };
00226 
00227 /*
00228  * Size in entries (gross, not net of free and removed sentinels) for table.
00229  * We store hashShift rather than sizeLog2 to optimize the collision-free case
00230  * in SearchTable.
00231  */
00232 #define JS_DHASH_TABLE_SIZE(table)  JS_BIT(JS_DHASH_BITS - (table)->hashShift)
00233 
00234 /*
00235  * Table space at entryStore is allocated and freed using these callbacks.
00236  * The allocator should return null on error only (not if called with nbytes
00237  * equal to 0; but note that jsdhash.c code will never call with 0 nbytes).
00238  */
00239 typedef void *
00240 (* JS_DLL_CALLBACK JSDHashAllocTable)(JSDHashTable *table, uint32 nbytes);
00241 
00242 typedef void
00243 (* JS_DLL_CALLBACK JSDHashFreeTable) (JSDHashTable *table, void *ptr);
00244 
00245 /*
00246  * When a table grows or shrinks, each entry is queried for its key using this
00247  * callback.  NB: in that event, entry is not in table any longer; it's in the
00248  * old entryStore vector, which is due to be freed once all entries have been
00249  * moved via moveEntry callbacks.
00250  */
00251 typedef const void *
00252 (* JS_DLL_CALLBACK JSDHashGetKey)    (JSDHashTable *table,
00253                                       JSDHashEntryHdr *entry);
00254 
00255 /*
00256  * Compute the hash code for a given key to be looked up, added, or removed
00257  * from table.  A hash code may have any JSDHashNumber value.
00258  */
00259 typedef JSDHashNumber
00260 (* JS_DLL_CALLBACK JSDHashHashKey)   (JSDHashTable *table, const void *key);
00261 
00262 /*
00263  * Compare the key identifying entry in table with the provided key parameter.
00264  * Return JS_TRUE if keys match, JS_FALSE otherwise.
00265  */
00266 typedef JSBool
00267 (* JS_DLL_CALLBACK JSDHashMatchEntry)(JSDHashTable *table,
00268                                       const JSDHashEntryHdr *entry,
00269                                       const void *key);
00270 
00271 /*
00272  * Copy the data starting at from to the new entry storage at to.  Do not add
00273  * reference counts for any strong references in the entry, however, as this
00274  * is a "move" operation: the old entry storage at from will be freed without
00275  * any reference-decrementing callback shortly.
00276  */
00277 typedef void
00278 (* JS_DLL_CALLBACK JSDHashMoveEntry)(JSDHashTable *table,
00279                                      const JSDHashEntryHdr *from,
00280                                      JSDHashEntryHdr *to);
00281 
00282 /*
00283  * Clear the entry and drop any strong references it holds.  This callback is
00284  * invoked during a JS_DHASH_REMOVE operation (see below for operation codes),
00285  * but only if the given key is found in the table.
00286  */
00287 typedef void
00288 (* JS_DLL_CALLBACK JSDHashClearEntry)(JSDHashTable *table,
00289                                       JSDHashEntryHdr *entry);
00290 
00291 /*
00292  * Called when a table (whether allocated dynamically by itself, or nested in
00293  * a larger structure, or allocated on the stack) is finished.  This callback
00294  * allows table->ops-specific code to finalize table->data.
00295  */
00296 typedef void
00297 (* JS_DLL_CALLBACK JSDHashFinalize)  (JSDHashTable *table);
00298 
00299 /*
00300  * Initialize a new entry, apart from keyHash.  This function is called when
00301  * JS_DHashTableOperate's JS_DHASH_ADD case finds no existing entry for the
00302  * given key, and must add a new one.  At that point, entry->keyHash is not
00303  * set yet, to avoid claiming the last free entry in a severely overloaded
00304  * table.
00305  */
00306 typedef JSBool
00307 (* JS_DLL_CALLBACK JSDHashInitEntry)(JSDHashTable *table,
00308                                      JSDHashEntryHdr *entry,
00309                                      const void *key);
00310 
00311 /*
00312  * Finally, the "vtable" structure for JSDHashTable.  The first eight hooks
00313  * must be provided by implementations; they're called unconditionally by the
00314  * generic jsdhash.c code.  Hooks after these may be null.
00315  *
00316  * Summary of allocation-related hook usage with C++ placement new emphasis:
00317  *  allocTable          Allocate raw bytes with malloc, no ctors run.
00318  *  freeTable           Free raw bytes with free, no dtors run.
00319  *  initEntry           Call placement new using default key-based ctor.
00320  *                      Return JS_TRUE on success, JS_FALSE on error.
00321  *  moveEntry           Call placement new using copy ctor, run dtor on old
00322  *                      entry storage.
00323  *  clearEntry          Run dtor on entry.
00324  *  finalize            Stub unless table->data was initialized and needs to
00325  *                      be finalized.
00326  *
00327  * Note the reason why initEntry is optional: the default hooks (stubs) clear
00328  * entry storage:  On successful JS_DHashTableOperate(tbl, key, JS_DHASH_ADD),
00329  * the returned entry pointer addresses an entry struct whose keyHash member
00330  * has been set non-zero, but all other entry members are still clear (null).
00331  * JS_DHASH_ADD callers can test such members to see whether the entry was
00332  * newly created by the JS_DHASH_ADD call that just succeeded.  If placement
00333  * new or similar initialization is required, define an initEntry hook.  Of
00334  * course, the clearEntry hook must zero or null appropriately.
00335  *
00336  * XXX assumes 0 is null for pointer types.
00337  */
00338 struct JSDHashTableOps {
00339     /* Mandatory hooks.  All implementations must provide these. */
00340     JSDHashAllocTable   allocTable;
00341     JSDHashFreeTable    freeTable;
00342     JSDHashGetKey       getKey;
00343     JSDHashHashKey      hashKey;
00344     JSDHashMatchEntry   matchEntry;
00345     JSDHashMoveEntry    moveEntry;
00346     JSDHashClearEntry   clearEntry;
00347     JSDHashFinalize     finalize;
00348 
00349     /* Optional hooks start here.  If null, these are not called. */
00350     JSDHashInitEntry    initEntry;
00351 };
00352 
00353 /*
00354  * Default implementations for the above ops.
00355  */
00356 extern JS_PUBLIC_API(void *)
00357 JS_DHashAllocTable(JSDHashTable *table, uint32 nbytes);
00358 
00359 extern JS_PUBLIC_API(void)
00360 JS_DHashFreeTable(JSDHashTable *table, void *ptr);
00361 
00362 extern JS_PUBLIC_API(JSDHashNumber)
00363 JS_DHashStringKey(JSDHashTable *table, const void *key);
00364 
00365 /* A minimal entry contains a keyHash header and a void key pointer. */
00366 struct JSDHashEntryStub {
00367     JSDHashEntryHdr hdr;
00368     const void      *key;
00369 };
00370 
00371 extern JS_PUBLIC_API(const void *)
00372 JS_DHashGetKeyStub(JSDHashTable *table, JSDHashEntryHdr *entry);
00373 
00374 extern JS_PUBLIC_API(JSDHashNumber)
00375 JS_DHashVoidPtrKeyStub(JSDHashTable *table, const void *key);
00376 
00377 extern JS_PUBLIC_API(JSBool)
00378 JS_DHashMatchEntryStub(JSDHashTable *table,
00379                        const JSDHashEntryHdr *entry,
00380                        const void *key);
00381 
00382 extern JS_PUBLIC_API(JSBool)
00383 JS_DHashMatchStringKey(JSDHashTable *table,
00384                        const JSDHashEntryHdr *entry,
00385                        const void *key);
00386 
00387 extern JS_PUBLIC_API(void)
00388 JS_DHashMoveEntryStub(JSDHashTable *table,
00389                       const JSDHashEntryHdr *from,
00390                       JSDHashEntryHdr *to);
00391 
00392 extern JS_PUBLIC_API(void)
00393 JS_DHashClearEntryStub(JSDHashTable *table, JSDHashEntryHdr *entry);
00394 
00395 extern JS_PUBLIC_API(void)
00396 JS_DHashFreeStringKey(JSDHashTable *table, JSDHashEntryHdr *entry);
00397 
00398 extern JS_PUBLIC_API(void)
00399 JS_DHashFinalizeStub(JSDHashTable *table);
00400 
00401 /*
00402  * If you use JSDHashEntryStub or a subclass of it as your entry struct, and
00403  * if your entries move via memcpy and clear via memset(0), you can use these
00404  * stub operations.
00405  */
00406 extern JS_PUBLIC_API(const JSDHashTableOps *)
00407 JS_DHashGetStubOps(void);
00408 
00409 /*
00410  * Dynamically allocate a new JSDHashTable using malloc, initialize it using
00411  * JS_DHashTableInit, and return its address.  Return null on malloc failure.
00412  * Note that the entry storage at table->entryStore will be allocated using
00413  * the ops->allocTable callback.
00414  */
00415 extern JS_PUBLIC_API(JSDHashTable *)
00416 JS_NewDHashTable(const JSDHashTableOps *ops, void *data, uint32 entrySize,
00417                  uint32 capacity);
00418 
00419 /*
00420  * Finalize table's data, free its entry storage (via table->ops->freeTable),
00421  * and return the memory starting at table to the malloc heap.
00422  */
00423 extern JS_PUBLIC_API(void)
00424 JS_DHashTableDestroy(JSDHashTable *table);
00425 
00426 /*
00427  * Initialize table with ops, data, entrySize, and capacity.  Capacity is a
00428  * guess for the smallest table size at which the table will usually be less
00429  * than 75% loaded (the table will grow or shrink as needed; capacity serves
00430  * only to avoid inevitable early growth from JS_DHASH_MIN_SIZE).
00431  */
00432 extern JS_PUBLIC_API(JSBool)
00433 JS_DHashTableInit(JSDHashTable *table, const JSDHashTableOps *ops, void *data,
00434                   uint32 entrySize, uint32 capacity);
00435 
00436 /*
00437  * Set maximum and minimum alpha for table.  The defaults are 0.75 and .25.
00438  * maxAlpha must be in [0.5, 0.9375] for the default JS_DHASH_MIN_SIZE; or if
00439  * MinSize=JS_DHASH_MIN_SIZE <= 256, in [0.5, (float)(MinSize-1)/MinSize]; or
00440  * else in [0.5, 255.0/256].  minAlpha must be in [0, maxAlpha / 2), so that
00441  * we don't shrink on the very next remove after growing a table upon adding
00442  * an entry that brings entryCount past maxAlpha * tableSize.
00443  */
00444 extern JS_PUBLIC_API(void)
00445 JS_DHashTableSetAlphaBounds(JSDHashTable *table,
00446                             float maxAlpha,
00447                             float minAlpha);
00448 
00449 /*
00450  * Call this macro with k, the number of pointer-sized words wasted per entry
00451  * under chaining, to compute the minimum alpha at which double hashing still
00452  * beats chaining.
00453  */
00454 #define JS_DHASH_MIN_ALPHA(table, k)                                          \
00455     ((float)((table)->entrySize / sizeof(void *) - 1)                         \
00456      / ((table)->entrySize / sizeof(void *) + (k)))
00457 
00458 /*
00459  * Finalize table's data, free its entry storage using table->ops->freeTable,
00460  * and leave its members unchanged from their last live values (which leaves
00461  * pointers dangling).  If you want to burn cycles clearing table, it's up to
00462  * your code to call memset.
00463  */
00464 extern JS_PUBLIC_API(void)
00465 JS_DHashTableFinish(JSDHashTable *table);
00466 
00467 /*
00468  * To consolidate keyHash computation and table grow/shrink code, we use a
00469  * single entry point for lookup, add, and remove operations.  The operation
00470  * codes are declared here, along with codes returned by JSDHashEnumerator
00471  * functions, which control JS_DHashTableEnumerate's behavior.
00472  */
00473 typedef enum JSDHashOperator {
00474     JS_DHASH_LOOKUP = 0,        /* lookup entry */
00475     JS_DHASH_ADD = 1,           /* add entry */
00476     JS_DHASH_REMOVE = 2,        /* remove entry, or enumerator says remove */
00477     JS_DHASH_NEXT = 0,          /* enumerator says continue */
00478     JS_DHASH_STOP = 1           /* enumerator says stop */
00479 } JSDHashOperator;
00480 
00481 /*
00482  * To lookup a key in table, call:
00483  *
00484  *  entry = JS_DHashTableOperate(table, key, JS_DHASH_LOOKUP);
00485  *
00486  * If JS_DHASH_ENTRY_IS_BUSY(entry) is true, key was found and it identifies
00487  * entry.  If JS_DHASH_ENTRY_IS_FREE(entry) is true, key was not found.
00488  *
00489  * To add an entry identified by key to table, call:
00490  *
00491  *  entry = JS_DHashTableOperate(table, key, JS_DHASH_ADD);
00492  *
00493  * If entry is null upon return, then either the table is severely overloaded,
00494  * and memory can't be allocated for entry storage via table->ops->allocTable;
00495  * Or if table->ops->initEntry is non-null, the table->ops->initEntry op may
00496  * have returned false.
00497  *
00498  * Otherwise, entry->keyHash has been set so that JS_DHASH_ENTRY_IS_BUSY(entry)
00499  * is true, and it is up to the caller to initialize the key and value parts
00500  * of the entry sub-type, if they have not been set already (i.e. if entry was
00501  * not already in the table, and if the optional initEntry hook was not used).
00502  *
00503  * To remove an entry identified by key from table, call:
00504  *
00505  *  (void) JS_DHashTableOperate(table, key, JS_DHASH_REMOVE);
00506  *
00507  * If key's entry is found, it is cleared (via table->ops->clearEntry) and
00508  * the entry is marked so that JS_DHASH_ENTRY_IS_FREE(entry).  This operation
00509  * returns null unconditionally; you should ignore its return value.
00510  */
00511 extern JS_PUBLIC_API(JSDHashEntryHdr *) JS_DHASH_FASTCALL
00512 JS_DHashTableOperate(JSDHashTable *table, const void *key, JSDHashOperator op);
00513 
00514 /*
00515  * Remove an entry already accessed via LOOKUP or ADD.
00516  *
00517  * NB: this is a "raw" or low-level routine, intended to be used only where
00518  * the inefficiency of a full JS_DHashTableOperate (which rehashes in order
00519  * to find the entry given its key) is not tolerable.  This function does not
00520  * shrink the table if it is underloaded.  It does not update stats #ifdef
00521  * JS_DHASHMETER, either.
00522  */
00523 extern JS_PUBLIC_API(void)
00524 JS_DHashTableRawRemove(JSDHashTable *table, JSDHashEntryHdr *entry);
00525 
00526 /*
00527  * Enumerate entries in table using etor:
00528  *
00529  *   count = JS_DHashTableEnumerate(table, etor, arg);
00530  *
00531  * JS_DHashTableEnumerate calls etor like so:
00532  *
00533  *   op = etor(table, entry, number, arg);
00534  *
00535  * where number is a zero-based ordinal assigned to live entries according to
00536  * their order in table->entryStore.
00537  *
00538  * The return value, op, is treated as a set of flags.  If op is JS_DHASH_NEXT,
00539  * then continue enumerating.  If op contains JS_DHASH_REMOVE, then clear (via
00540  * table->ops->clearEntry) and free entry.  Then we check whether op contains
00541  * JS_DHASH_STOP; if so, stop enumerating and return the number of live entries
00542  * that were enumerated so far.  Return the total number of live entries when
00543  * enumeration completes normally.
00544  *
00545  * If etor calls JS_DHashTableOperate on table with op != JS_DHASH_LOOKUP, it
00546  * must return JS_DHASH_STOP; otherwise undefined behavior results.
00547  *
00548  * If any enumerator returns JS_DHASH_REMOVE, table->entryStore may be shrunk
00549  * or compressed after enumeration, but before JS_DHashTableEnumerate returns.
00550  * Such an enumerator therefore can't safely set aside entry pointers, but an
00551  * enumerator that never returns JS_DHASH_REMOVE can set pointers to entries
00552  * aside, e.g., to avoid copying live entries into an array of the entry type.
00553  * Copying entry pointers is cheaper, and safe so long as the caller of such a
00554  * "stable" Enumerate doesn't use the set-aside pointers after any call either
00555  * to PL_DHashTableOperate, or to an "unstable" form of Enumerate, which might
00556  * grow or shrink entryStore.
00557  *
00558  * If your enumerator wants to remove certain entries, but set aside pointers
00559  * to other entries that it retains, it can use JS_DHashTableRawRemove on the
00560  * entries to be removed, returning JS_DHASH_NEXT to skip them.  Likewise, if
00561  * you want to remove entries, but for some reason you do not want entryStore
00562  * to be shrunk or compressed, you can call JS_DHashTableRawRemove safely on
00563  * the entry being enumerated, rather than returning JS_DHASH_REMOVE.
00564  */
00565 typedef JSDHashOperator
00566 (* JS_DLL_CALLBACK JSDHashEnumerator)(JSDHashTable *table, JSDHashEntryHdr *hdr,
00567                                       uint32 number, void *arg);
00568 
00569 extern JS_PUBLIC_API(uint32)
00570 JS_DHashTableEnumerate(JSDHashTable *table, JSDHashEnumerator etor, void *arg);
00571 
00572 #ifdef JS_DHASHMETER
00573 #include <stdio.h>
00574 
00575 extern JS_PUBLIC_API(void)
00576 JS_DHashTableDumpMeter(JSDHashTable *table, JSDHashEnumerator dump, FILE *fp);
00577 #endif
00578 
00579 JS_END_EXTERN_C
00580 
00581 #endif /* jsdhash_h___ */