Back to index

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