PageRenderTime 83ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/pldhash.h

http://firefox-mac-pdf.googlecode.com/
C Header | 593 lines | 183 code | 53 blank | 357 comment | 5 complexity | 50218bc8f7e62095be27ad6da6f87f3f MD5 | raw file
  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Mozilla JavaScript code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1999-2001
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Brendan Eich <brendan@mozilla.org> (Original Author)
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #ifndef pldhash_h___
  39. #define pldhash_h___
  40. /*
  41. * Double hashing, a la Knuth 6.
  42. * GENERATED BY js/src/plify_jsdhash.sed -- DO NOT EDIT!!!
  43. */
  44. #include "nscore.h"
  45. PR_BEGIN_EXTERN_C
  46. #if defined(__GNUC__) && defined(__i386__) && (__GNUC__ >= 3) && !defined(XP_OS2)
  47. #define PL_DHASH_FASTCALL __attribute__ ((regparm (3),stdcall))
  48. #elif defined(XP_WIN)
  49. #define PL_DHASH_FASTCALL __fastcall
  50. #else
  51. #define PL_DHASH_FASTCALL
  52. #endif
  53. #ifdef DEBUG_XXXbrendan
  54. #define PL_DHASHMETER 1
  55. #endif
  56. /* Table size limit, do not equal or exceed (see min&maxAlphaFrac, below). */
  57. #undef PL_DHASH_SIZE_LIMIT
  58. #define PL_DHASH_SIZE_LIMIT PR_BIT(24)
  59. /* Minimum table size, or gross entry count (net is at most .75 loaded). */
  60. #ifndef PL_DHASH_MIN_SIZE
  61. #define PL_DHASH_MIN_SIZE 16
  62. #elif (PL_DHASH_MIN_SIZE & (PL_DHASH_MIN_SIZE - 1)) != 0
  63. #error "PL_DHASH_MIN_SIZE must be a power of two!"
  64. #endif
  65. /*
  66. * Multiplicative hash uses an unsigned 32 bit integer and the golden ratio,
  67. * expressed as a fixed-point 32-bit fraction.
  68. */
  69. #define PL_DHASH_BITS 32
  70. #define PL_DHASH_GOLDEN_RATIO 0x9E3779B9U
  71. /* Primitive and forward-struct typedefs. */
  72. typedef PRUint32 PLDHashNumber;
  73. typedef struct PLDHashEntryHdr PLDHashEntryHdr;
  74. typedef struct PLDHashEntryStub PLDHashEntryStub;
  75. typedef struct PLDHashTable PLDHashTable;
  76. typedef struct PLDHashTableOps PLDHashTableOps;
  77. /*
  78. * Table entry header structure.
  79. *
  80. * In order to allow in-line allocation of key and value, we do not declare
  81. * either here. Instead, the API uses const void *key as a formal parameter.
  82. * The key need not be stored in the entry; it may be part of the value, but
  83. * need not be stored at all.
  84. *
  85. * Callback types are defined below and grouped into the PLDHashTableOps
  86. * structure, for single static initialization per hash table sub-type.
  87. *
  88. * Each hash table sub-type should nest the PLDHashEntryHdr structure at the
  89. * front of its particular entry type. The keyHash member contains the result
  90. * of multiplying the hash code returned from the hashKey callback (see below)
  91. * by PL_DHASH_GOLDEN_RATIO, then constraining the result to avoid the magic 0
  92. * and 1 values. The stored keyHash value is table size invariant, and it is
  93. * maintained automatically by PL_DHashTableOperate -- users should never set
  94. * it, and its only uses should be via the entry macros below.
  95. *
  96. * The PL_DHASH_ENTRY_IS_LIVE macro tests whether entry is neither free nor
  97. * removed. An entry may be either busy or free; if busy, it may be live or
  98. * removed. Consumers of this API should not access members of entries that
  99. * are not live.
  100. *
  101. * However, use PL_DHASH_ENTRY_IS_BUSY for faster liveness testing of entries
  102. * returned by PL_DHashTableOperate, as PL_DHashTableOperate never returns a
  103. * non-live, busy (i.e., removed) entry pointer to its caller. See below for
  104. * more details on PL_DHashTableOperate's calling rules.
  105. */
  106. struct PLDHashEntryHdr {
  107. PLDHashNumber keyHash; /* every entry must begin like this */
  108. };
  109. #define PL_DHASH_ENTRY_IS_FREE(entry) ((entry)->keyHash == 0)
  110. #define PL_DHASH_ENTRY_IS_BUSY(entry) (!PL_DHASH_ENTRY_IS_FREE(entry))
  111. #define PL_DHASH_ENTRY_IS_LIVE(entry) ((entry)->keyHash >= 2)
  112. /*
  113. * A PLDHashTable is currently 8 words (without the PL_DHASHMETER overhead)
  114. * on most architectures, and may be allocated on the stack or within another
  115. * structure or class (see below for the Init and Finish functions to use).
  116. *
  117. * To decide whether to use double hashing vs. chaining, we need to develop a
  118. * trade-off relation, as follows:
  119. *
  120. * Let alpha be the load factor, esize the entry size in words, count the
  121. * entry count, and pow2 the power-of-two table size in entries.
  122. *
  123. * (PLDHashTable overhead) > (PLHashTable overhead)
  124. * (unused table entry space) > (malloc and .next overhead per entry) +
  125. * (buckets overhead)
  126. * (1 - alpha) * esize * pow2 > 2 * count + pow2
  127. *
  128. * Notice that alpha is by definition (count / pow2):
  129. *
  130. * (1 - alpha) * esize * pow2 > 2 * alpha * pow2 + pow2
  131. * (1 - alpha) * esize > 2 * alpha + 1
  132. *
  133. * esize > (1 + 2 * alpha) / (1 - alpha)
  134. *
  135. * This assumes both tables must keep keyHash, key, and value for each entry,
  136. * where key and value point to separately allocated strings or structures.
  137. * If key and value can be combined into one pointer, then the trade-off is:
  138. *
  139. * esize > (1 + 3 * alpha) / (1 - alpha)
  140. *
  141. * If the entry value can be a subtype of PLDHashEntryHdr, rather than a type
  142. * that must be allocated separately and referenced by an entry.value pointer
  143. * member, and provided key's allocation can be fused with its entry's, then
  144. * k (the words wasted per entry with chaining) is 4.
  145. *
  146. * To see these curves, feed gnuplot input like so:
  147. *
  148. * gnuplot> f(x,k) = (1 + k * x) / (1 - x)
  149. * gnuplot> plot [0:.75] f(x,2), f(x,3), f(x,4)
  150. *
  151. * For k of 2 and a well-loaded table (alpha > .5), esize must be more than 4
  152. * words for chaining to be more space-efficient than double hashing.
  153. *
  154. * Solving for alpha helps us decide when to shrink an underloaded table:
  155. *
  156. * esize > (1 + k * alpha) / (1 - alpha)
  157. * esize - alpha * esize > 1 + k * alpha
  158. * esize - 1 > (k + esize) * alpha
  159. * (esize - 1) / (k + esize) > alpha
  160. *
  161. * alpha < (esize - 1) / (esize + k)
  162. *
  163. * Therefore double hashing should keep alpha >= (esize - 1) / (esize + k),
  164. * assuming esize is not too large (in which case, chaining should probably be
  165. * used for any alpha). For esize=2 and k=3, we want alpha >= .2; for esize=3
  166. * and k=2, we want alpha >= .4. For k=4, esize could be 6, and alpha >= .5
  167. * would still obtain. See the PL_DHASH_MIN_ALPHA macro further below.
  168. *
  169. * The current implementation uses a configurable lower bound on alpha, which
  170. * defaults to .25, when deciding to shrink the table (while still respecting
  171. * PL_DHASH_MIN_SIZE).
  172. *
  173. * Note a qualitative difference between chaining and double hashing: under
  174. * chaining, entry addresses are stable across table shrinks and grows. With
  175. * double hashing, you can't safely hold an entry pointer and use it after an
  176. * ADD or REMOVE operation, unless you sample table->generation before adding
  177. * or removing, and compare the sample after, dereferencing the entry pointer
  178. * only if table->generation has not changed.
  179. *
  180. * The moral of this story: there is no one-size-fits-all hash table scheme,
  181. * but for small table entry size, and assuming entry address stability is not
  182. * required, double hashing wins.
  183. */
  184. struct PLDHashTable {
  185. const PLDHashTableOps *ops; /* virtual operations, see below */
  186. void *data; /* ops- and instance-specific data */
  187. PRInt16 hashShift; /* multiplicative hash shift */
  188. uint8 maxAlphaFrac; /* 8-bit fixed point max alpha */
  189. uint8 minAlphaFrac; /* 8-bit fixed point min alpha */
  190. PRUint32 entrySize; /* number of bytes in an entry */
  191. PRUint32 entryCount; /* number of entries in table */
  192. PRUint32 removedCount; /* removed entry sentinels in table */
  193. PRUint32 generation; /* entry storage generation number */
  194. char *entryStore; /* entry storage */
  195. #ifdef PL_DHASHMETER
  196. struct PLDHashStats {
  197. PRUint32 searches; /* total number of table searches */
  198. PRUint32 steps; /* hash chain links traversed */
  199. PRUint32 hits; /* searches that found key */
  200. PRUint32 misses; /* searches that didn't find key */
  201. PRUint32 lookups; /* number of PL_DHASH_LOOKUPs */
  202. PRUint32 addMisses; /* adds that miss, and do work */
  203. PRUint32 addOverRemoved; /* adds that recycled a removed entry */
  204. PRUint32 addHits; /* adds that hit an existing entry */
  205. PRUint32 addFailures; /* out-of-memory during add growth */
  206. PRUint32 removeHits; /* removes that hit, and do work */
  207. PRUint32 removeMisses; /* useless removes that miss */
  208. PRUint32 removeFrees; /* removes that freed entry directly */
  209. PRUint32 removeEnums; /* removes done by Enumerate */
  210. PRUint32 grows; /* table expansions */
  211. PRUint32 shrinks; /* table contractions */
  212. PRUint32 compresses; /* table compressions */
  213. PRUint32 enumShrinks; /* contractions after Enumerate */
  214. } stats;
  215. #endif
  216. };
  217. /*
  218. * Size in entries (gross, not net of free and removed sentinels) for table.
  219. * We store hashShift rather than sizeLog2 to optimize the collision-free case
  220. * in SearchTable.
  221. */
  222. #define PL_DHASH_TABLE_SIZE(table) PR_BIT(PL_DHASH_BITS - (table)->hashShift)
  223. /*
  224. * Table space at entryStore is allocated and freed using these callbacks.
  225. * The allocator should return null on error only (not if called with nbytes
  226. * equal to 0; but note that pldhash.c code will never call with 0 nbytes).
  227. */
  228. typedef void *
  229. (* PR_CALLBACK PLDHashAllocTable)(PLDHashTable *table, PRUint32 nbytes);
  230. typedef void
  231. (* PR_CALLBACK PLDHashFreeTable) (PLDHashTable *table, void *ptr);
  232. /*
  233. * Compute the hash code for a given key to be looked up, added, or removed
  234. * from table. A hash code may have any PLDHashNumber value.
  235. */
  236. typedef PLDHashNumber
  237. (* PR_CALLBACK PLDHashHashKey) (PLDHashTable *table, const void *key);
  238. /*
  239. * Compare the key identifying entry in table with the provided key parameter.
  240. * Return PR_TRUE if keys match, PR_FALSE otherwise.
  241. */
  242. typedef PRBool
  243. (* PR_CALLBACK PLDHashMatchEntry)(PLDHashTable *table,
  244. const PLDHashEntryHdr *entry,
  245. const void *key);
  246. /*
  247. * Copy the data starting at from to the new entry storage at to. Do not add
  248. * reference counts for any strong references in the entry, however, as this
  249. * is a "move" operation: the old entry storage at from will be freed without
  250. * any reference-decrementing callback shortly.
  251. */
  252. typedef void
  253. (* PR_CALLBACK PLDHashMoveEntry)(PLDHashTable *table,
  254. const PLDHashEntryHdr *from,
  255. PLDHashEntryHdr *to);
  256. /*
  257. * Clear the entry and drop any strong references it holds. This callback is
  258. * invoked during a PL_DHASH_REMOVE operation (see below for operation codes),
  259. * but only if the given key is found in the table.
  260. */
  261. typedef void
  262. (* PR_CALLBACK PLDHashClearEntry)(PLDHashTable *table,
  263. PLDHashEntryHdr *entry);
  264. /*
  265. * Called when a table (whether allocated dynamically by itself, or nested in
  266. * a larger structure, or allocated on the stack) is finished. This callback
  267. * allows table->ops-specific code to finalize table->data.
  268. */
  269. typedef void
  270. (* PR_CALLBACK PLDHashFinalize) (PLDHashTable *table);
  271. /*
  272. * Initialize a new entry, apart from keyHash. This function is called when
  273. * PL_DHashTableOperate's PL_DHASH_ADD case finds no existing entry for the
  274. * given key, and must add a new one. At that point, entry->keyHash is not
  275. * set yet, to avoid claiming the last free entry in a severely overloaded
  276. * table.
  277. */
  278. typedef PRBool
  279. (* PR_CALLBACK PLDHashInitEntry)(PLDHashTable *table,
  280. PLDHashEntryHdr *entry,
  281. const void *key);
  282. /*
  283. * Finally, the "vtable" structure for PLDHashTable. The first eight hooks
  284. * must be provided by implementations; they're called unconditionally by the
  285. * generic pldhash.c code. Hooks after these may be null.
  286. *
  287. * Summary of allocation-related hook usage with C++ placement new emphasis:
  288. * allocTable Allocate raw bytes with malloc, no ctors run.
  289. * freeTable Free raw bytes with free, no dtors run.
  290. * initEntry Call placement new using default key-based ctor.
  291. * Return PR_TRUE on success, PR_FALSE on error.
  292. * moveEntry Call placement new using copy ctor, run dtor on old
  293. * entry storage.
  294. * clearEntry Run dtor on entry.
  295. * finalize Stub unless table->data was initialized and needs to
  296. * be finalized.
  297. *
  298. * Note the reason why initEntry is optional: the default hooks (stubs) clear
  299. * entry storage: On successful PL_DHashTableOperate(tbl, key, PL_DHASH_ADD),
  300. * the returned entry pointer addresses an entry struct whose keyHash member
  301. * has been set non-zero, but all other entry members are still clear (null).
  302. * PL_DHASH_ADD callers can test such members to see whether the entry was
  303. * newly created by the PL_DHASH_ADD call that just succeeded. If placement
  304. * new or similar initialization is required, define an initEntry hook. Of
  305. * course, the clearEntry hook must zero or null appropriately.
  306. *
  307. * XXX assumes 0 is null for pointer types.
  308. */
  309. struct PLDHashTableOps {
  310. /* Mandatory hooks. All implementations must provide these. */
  311. PLDHashAllocTable allocTable;
  312. PLDHashFreeTable freeTable;
  313. PLDHashHashKey hashKey;
  314. PLDHashMatchEntry matchEntry;
  315. PLDHashMoveEntry moveEntry;
  316. PLDHashClearEntry clearEntry;
  317. PLDHashFinalize finalize;
  318. /* Optional hooks start here. If null, these are not called. */
  319. PLDHashInitEntry initEntry;
  320. };
  321. /*
  322. * Default implementations for the above ops.
  323. */
  324. NS_COM_GLUE void *
  325. PL_DHashAllocTable(PLDHashTable *table, PRUint32 nbytes);
  326. NS_COM_GLUE void
  327. PL_DHashFreeTable(PLDHashTable *table, void *ptr);
  328. NS_COM_GLUE PLDHashNumber
  329. PL_DHashStringKey(PLDHashTable *table, const void *key);
  330. /* A minimal entry contains a keyHash header and a void key pointer. */
  331. struct PLDHashEntryStub {
  332. PLDHashEntryHdr hdr;
  333. const void *key;
  334. };
  335. NS_COM_GLUE PLDHashNumber
  336. PL_DHashVoidPtrKeyStub(PLDHashTable *table, const void *key);
  337. NS_COM_GLUE PRBool
  338. PL_DHashMatchEntryStub(PLDHashTable *table,
  339. const PLDHashEntryHdr *entry,
  340. const void *key);
  341. NS_COM_GLUE PRBool
  342. PL_DHashMatchStringKey(PLDHashTable *table,
  343. const PLDHashEntryHdr *entry,
  344. const void *key);
  345. NS_COM_GLUE void
  346. PL_DHashMoveEntryStub(PLDHashTable *table,
  347. const PLDHashEntryHdr *from,
  348. PLDHashEntryHdr *to);
  349. NS_COM_GLUE void
  350. PL_DHashClearEntryStub(PLDHashTable *table, PLDHashEntryHdr *entry);
  351. NS_COM_GLUE void
  352. PL_DHashFreeStringKey(PLDHashTable *table, PLDHashEntryHdr *entry);
  353. NS_COM_GLUE void
  354. PL_DHashFinalizeStub(PLDHashTable *table);
  355. /*
  356. * If you use PLDHashEntryStub or a subclass of it as your entry struct, and
  357. * if your entries move via memcpy and clear via memset(0), you can use these
  358. * stub operations.
  359. */
  360. NS_COM_GLUE const PLDHashTableOps *
  361. PL_DHashGetStubOps(void);
  362. /*
  363. * Dynamically allocate a new PLDHashTable using malloc, initialize it using
  364. * PL_DHashTableInit, and return its address. Return null on malloc failure.
  365. * Note that the entry storage at table->entryStore will be allocated using
  366. * the ops->allocTable callback.
  367. */
  368. NS_COM_GLUE PLDHashTable *
  369. PL_NewDHashTable(const PLDHashTableOps *ops, void *data, PRUint32 entrySize,
  370. PRUint32 capacity);
  371. /*
  372. * Finalize table's data, free its entry storage (via table->ops->freeTable),
  373. * and return the memory starting at table to the malloc heap.
  374. */
  375. NS_COM_GLUE void
  376. PL_DHashTableDestroy(PLDHashTable *table);
  377. /*
  378. * Initialize table with ops, data, entrySize, and capacity. Capacity is a
  379. * guess for the smallest table size at which the table will usually be less
  380. * than 75% loaded (the table will grow or shrink as needed; capacity serves
  381. * only to avoid inevitable early growth from PL_DHASH_MIN_SIZE).
  382. */
  383. NS_COM_GLUE PRBool
  384. PL_DHashTableInit(PLDHashTable *table, const PLDHashTableOps *ops, void *data,
  385. PRUint32 entrySize, PRUint32 capacity);
  386. /*
  387. * Set maximum and minimum alpha for table. The defaults are 0.75 and .25.
  388. * maxAlpha must be in [0.5, 0.9375] for the default PL_DHASH_MIN_SIZE; or if
  389. * MinSize=PL_DHASH_MIN_SIZE <= 256, in [0.5, (float)(MinSize-1)/MinSize]; or
  390. * else in [0.5, 255.0/256]. minAlpha must be in [0, maxAlpha / 2), so that
  391. * we don't shrink on the very next remove after growing a table upon adding
  392. * an entry that brings entryCount past maxAlpha * tableSize.
  393. */
  394. NS_COM_GLUE void
  395. PL_DHashTableSetAlphaBounds(PLDHashTable *table,
  396. float maxAlpha,
  397. float minAlpha);
  398. /*
  399. * Call this macro with k, the number of pointer-sized words wasted per entry
  400. * under chaining, to compute the minimum alpha at which double hashing still
  401. * beats chaining.
  402. */
  403. #define PL_DHASH_MIN_ALPHA(table, k) \
  404. ((float)((table)->entrySize / sizeof(void *) - 1) \
  405. / ((table)->entrySize / sizeof(void *) + (k)))
  406. /*
  407. * Default max/min alpha, and macros to compute the value for the |capacity|
  408. * parameter to PL_NewDHashTable and PL_DHashTableInit, given default or any
  409. * max alpha, such that adding entryCount entries right after initializing the
  410. * table will not require a reallocation (so PL_DHASH_ADD can't fail for those
  411. * PL_DHashTableOperate calls).
  412. *
  413. * NB: PL_DHASH_CAP is a helper macro meant for use only in PL_DHASH_CAPACITY.
  414. * Don't use it directly!
  415. */
  416. #define PL_DHASH_DEFAULT_MAX_ALPHA 0.75
  417. #define PL_DHASH_DEFAULT_MIN_ALPHA 0.25
  418. #define PL_DHASH_CAP(entryCount, maxAlpha) \
  419. ((PRUint32)((double)(entryCount) / (maxAlpha)))
  420. #define PL_DHASH_CAPACITY(entryCount, maxAlpha) \
  421. (PL_DHASH_CAP(entryCount, maxAlpha) + \
  422. (((PL_DHASH_CAP(entryCount, maxAlpha) * (uint8)(0x100 * (maxAlpha))) \
  423. >> 8) < (entryCount)))
  424. #define PL_DHASH_DEFAULT_CAPACITY(entryCount) \
  425. PL_DHASH_CAPACITY(entryCount, PL_DHASH_DEFAULT_MAX_ALPHA)
  426. /*
  427. * Finalize table's data, free its entry storage using table->ops->freeTable,
  428. * and leave its members unchanged from their last live values (which leaves
  429. * pointers dangling). If you want to burn cycles clearing table, it's up to
  430. * your code to call memset.
  431. */
  432. NS_COM_GLUE void
  433. PL_DHashTableFinish(PLDHashTable *table);
  434. /*
  435. * To consolidate keyHash computation and table grow/shrink code, we use a
  436. * single entry point for lookup, add, and remove operations. The operation
  437. * codes are declared here, along with codes returned by PLDHashEnumerator
  438. * functions, which control PL_DHashTableEnumerate's behavior.
  439. */
  440. typedef enum PLDHashOperator {
  441. PL_DHASH_LOOKUP = 0, /* lookup entry */
  442. PL_DHASH_ADD = 1, /* add entry */
  443. PL_DHASH_REMOVE = 2, /* remove entry, or enumerator says remove */
  444. PL_DHASH_NEXT = 0, /* enumerator says continue */
  445. PL_DHASH_STOP = 1 /* enumerator says stop */
  446. } PLDHashOperator;
  447. /*
  448. * To lookup a key in table, call:
  449. *
  450. * entry = PL_DHashTableOperate(table, key, PL_DHASH_LOOKUP);
  451. *
  452. * If PL_DHASH_ENTRY_IS_BUSY(entry) is true, key was found and it identifies
  453. * entry. If PL_DHASH_ENTRY_IS_FREE(entry) is true, key was not found.
  454. *
  455. * To add an entry identified by key to table, call:
  456. *
  457. * entry = PL_DHashTableOperate(table, key, PL_DHASH_ADD);
  458. *
  459. * If entry is null upon return, then either the table is severely overloaded,
  460. * and memory can't be allocated for entry storage via table->ops->allocTable;
  461. * Or if table->ops->initEntry is non-null, the table->ops->initEntry op may
  462. * have returned false.
  463. *
  464. * Otherwise, entry->keyHash has been set so that PL_DHASH_ENTRY_IS_BUSY(entry)
  465. * is true, and it is up to the caller to initialize the key and value parts
  466. * of the entry sub-type, if they have not been set already (i.e. if entry was
  467. * not already in the table, and if the optional initEntry hook was not used).
  468. *
  469. * To remove an entry identified by key from table, call:
  470. *
  471. * (void) PL_DHashTableOperate(table, key, PL_DHASH_REMOVE);
  472. *
  473. * If key's entry is found, it is cleared (via table->ops->clearEntry) and
  474. * the entry is marked so that PL_DHASH_ENTRY_IS_FREE(entry). This operation
  475. * returns null unconditionally; you should ignore its return value.
  476. */
  477. NS_COM_GLUE PLDHashEntryHdr * PL_DHASH_FASTCALL
  478. PL_DHashTableOperate(PLDHashTable *table, const void *key, PLDHashOperator op);
  479. /*
  480. * Remove an entry already accessed via LOOKUP or ADD.
  481. *
  482. * NB: this is a "raw" or low-level routine, intended to be used only where
  483. * the inefficiency of a full PL_DHashTableOperate (which rehashes in order
  484. * to find the entry given its key) is not tolerable. This function does not
  485. * shrink the table if it is underloaded. It does not update stats #ifdef
  486. * PL_DHASHMETER, either.
  487. */
  488. NS_COM_GLUE void
  489. PL_DHashTableRawRemove(PLDHashTable *table, PLDHashEntryHdr *entry);
  490. /*
  491. * Enumerate entries in table using etor:
  492. *
  493. * count = PL_DHashTableEnumerate(table, etor, arg);
  494. *
  495. * PL_DHashTableEnumerate calls etor like so:
  496. *
  497. * op = etor(table, entry, number, arg);
  498. *
  499. * where number is a zero-based ordinal assigned to live entries according to
  500. * their order in table->entryStore.
  501. *
  502. * The return value, op, is treated as a set of flags. If op is PL_DHASH_NEXT,
  503. * then continue enumerating. If op contains PL_DHASH_REMOVE, then clear (via
  504. * table->ops->clearEntry) and free entry. Then we check whether op contains
  505. * PL_DHASH_STOP; if so, stop enumerating and return the number of live entries
  506. * that were enumerated so far. Return the total number of live entries when
  507. * enumeration completes normally.
  508. *
  509. * If etor calls PL_DHashTableOperate on table with op != PL_DHASH_LOOKUP, it
  510. * must return PL_DHASH_STOP; otherwise undefined behavior results.
  511. *
  512. * If any enumerator returns PL_DHASH_REMOVE, table->entryStore may be shrunk
  513. * or compressed after enumeration, but before PL_DHashTableEnumerate returns.
  514. * Such an enumerator therefore can't safely set aside entry pointers, but an
  515. * enumerator that never returns PL_DHASH_REMOVE can set pointers to entries
  516. * aside, e.g., to avoid copying live entries into an array of the entry type.
  517. * Copying entry pointers is cheaper, and safe so long as the caller of such a
  518. * "stable" Enumerate doesn't use the set-aside pointers after any call either
  519. * to PL_DHashTableOperate, or to an "unstable" form of Enumerate, which might
  520. * grow or shrink entryStore.
  521. *
  522. * If your enumerator wants to remove certain entries, but set aside pointers
  523. * to other entries that it retains, it can use PL_DHashTableRawRemove on the
  524. * entries to be removed, returning PL_DHASH_NEXT to skip them. Likewise, if
  525. * you want to remove entries, but for some reason you do not want entryStore
  526. * to be shrunk or compressed, you can call PL_DHashTableRawRemove safely on
  527. * the entry being enumerated, rather than returning PL_DHASH_REMOVE.
  528. */
  529. typedef PLDHashOperator
  530. (* PR_CALLBACK PLDHashEnumerator)(PLDHashTable *table, PLDHashEntryHdr *hdr,
  531. PRUint32 number, void *arg);
  532. NS_COM_GLUE PRUint32
  533. PL_DHashTableEnumerate(PLDHashTable *table, PLDHashEnumerator etor, void *arg);
  534. #ifdef PL_DHASHMETER
  535. #include <stdio.h>
  536. NS_COM_GLUE void
  537. PL_DHashTableDumpMeter(PLDHashTable *table, PLDHashEnumerator dump, FILE *fp);
  538. #endif
  539. PR_END_EXTERN_C
  540. #endif /* pldhash_h___ */