PageRenderTime 35ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsdhash.h

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