📄 jsdhash.h
字号:
* Called when a table (whether allocated dynamically by itself, or nested in * a larger structure, or allocated on the stack) is finished. This callback * allows table->ops-specific code to finalize table->data. */typedef void(* JS_DLL_CALLBACK JSDHashFinalize) (JSDHashTable *table);/* * Initialize a new entry, apart from keyHash. This function is called when * JS_DHashTableOperate's JS_DHASH_ADD case finds no existing entry for the * given key, and must add a new one. At that point, entry->keyHash is not * set yet, to avoid claiming the last free entry in a severely overloaded * table. */typedef JSBool(* JS_DLL_CALLBACK JSDHashInitEntry)(JSDHashTable *table, JSDHashEntryHdr *entry, const void *key);/* * Finally, the "vtable" structure for JSDHashTable. The first eight hooks * must be provided by implementations; they're called unconditionally by the * generic jsdhash.c code. Hooks after these may be null. * * Summary of allocation-related hook usage with C++ placement new emphasis: * allocTable Allocate raw bytes with malloc, no ctors run. * freeTable Free raw bytes with free, no dtors run. * initEntry Call placement new using default key-based ctor. * Return JS_TRUE on success, JS_FALSE on error. * moveEntry Call placement new using copy ctor, run dtor on old * entry storage. * clearEntry Run dtor on entry. * finalize Stub unless table->data was initialized and needs to * be finalized. * * Note the reason why initEntry is optional: the default hooks (stubs) clear * entry storage: On successful JS_DHashTableOperate(tbl, key, JS_DHASH_ADD), * the returned entry pointer addresses an entry struct whose keyHash member * has been set non-zero, but all other entry members are still clear (null). * JS_DHASH_ADD callers can test such members to see whether the entry was * newly created by the JS_DHASH_ADD call that just succeeded. If placement * new or similar initialization is required, define an initEntry hook. Of * course, the clearEntry hook must zero or null appropriately. * * XXX assumes 0 is null for pointer types. */struct JSDHashTableOps { /* Mandatory hooks. All implementations must provide these. */ JSDHashAllocTable allocTable; JSDHashFreeTable freeTable; JSDHashGetKey getKey; JSDHashHashKey hashKey; JSDHashMatchEntry matchEntry; JSDHashMoveEntry moveEntry; JSDHashClearEntry clearEntry; JSDHashFinalize finalize; /* Optional hooks start here. If null, these are not called. */ JSDHashInitEntry initEntry;};/* * Default implementations for the above ops. */extern JS_PUBLIC_API(void *)JS_DHashAllocTable(JSDHashTable *table, uint32 nbytes);extern JS_PUBLIC_API(void)JS_DHashFreeTable(JSDHashTable *table, void *ptr);extern JS_PUBLIC_API(JSDHashNumber)JS_DHashStringKey(JSDHashTable *table, const void *key);/* A minimal entry contains a keyHash header and a void key pointer. */struct JSDHashEntryStub { JSDHashEntryHdr hdr; const void *key;};extern JS_PUBLIC_API(const void *)JS_DHashGetKeyStub(JSDHashTable *table, JSDHashEntryHdr *entry);extern JS_PUBLIC_API(JSDHashNumber)JS_DHashVoidPtrKeyStub(JSDHashTable *table, const void *key);extern JS_PUBLIC_API(JSBool)JS_DHashMatchEntryStub(JSDHashTable *table, const JSDHashEntryHdr *entry, const void *key);extern JS_PUBLIC_API(JSBool)JS_DHashMatchStringKey(JSDHashTable *table, const JSDHashEntryHdr *entry, const void *key);extern JS_PUBLIC_API(void)JS_DHashMoveEntryStub(JSDHashTable *table, const JSDHashEntryHdr *from, JSDHashEntryHdr *to);extern JS_PUBLIC_API(void)JS_DHashClearEntryStub(JSDHashTable *table, JSDHashEntryHdr *entry);extern JS_PUBLIC_API(void)JS_DHashFreeStringKey(JSDHashTable *table, JSDHashEntryHdr *entry);extern JS_PUBLIC_API(void)JS_DHashFinalizeStub(JSDHashTable *table);/* * If you use JSDHashEntryStub or a subclass of it as your entry struct, and * if your entries move via memcpy and clear via memset(0), you can use these * stub operations. */extern JS_PUBLIC_API(const JSDHashTableOps *)JS_DHashGetStubOps(void);/* * Dynamically allocate a new JSDHashTable using malloc, initialize it using * JS_DHashTableInit, and return its address. Return null on malloc failure. * Note that the entry storage at table->entryStore will be allocated using * the ops->allocTable callback. */extern JS_PUBLIC_API(JSDHashTable *)JS_NewDHashTable(const JSDHashTableOps *ops, void *data, uint32 entrySize, uint32 capacity);/* * Finalize table's data, free its entry storage (via table->ops->freeTable), * and return the memory starting at table to the malloc heap. */extern JS_PUBLIC_API(void)JS_DHashTableDestroy(JSDHashTable *table);/* * Initialize table with ops, data, entrySize, and capacity. Capacity is a * guess for the smallest table size at which the table will usually be less * than 75% loaded (the table will grow or shrink as needed; capacity serves * only to avoid inevitable early growth from JS_DHASH_MIN_SIZE). */extern JS_PUBLIC_API(JSBool)JS_DHashTableInit(JSDHashTable *table, const JSDHashTableOps *ops, void *data, uint32 entrySize, uint32 capacity);/* * Set maximum and minimum alpha for table. The defaults are 0.75 and .25. * maxAlpha must be in [0.5, 0.9375] for the default JS_DHASH_MIN_SIZE; or if * MinSize=JS_DHASH_MIN_SIZE <= 256, in [0.5, (float)(MinSize-1)/MinSize]; or * else in [0.5, 255.0/256]. minAlpha must be in [0, maxAlpha / 2), so that * we don't shrink on the very next remove after growing a table upon adding * an entry that brings entryCount past maxAlpha * tableSize. */extern JS_PUBLIC_API(void)JS_DHashTableSetAlphaBounds(JSDHashTable *table, float maxAlpha, float minAlpha);/* * Call this macro with k, the number of pointer-sized words wasted per entry * under chaining, to compute the minimum alpha at which double hashing still * beats chaining. */#define JS_DHASH_MIN_ALPHA(table, k) \ ((float)((table)->entrySize / sizeof(void *) - 1) \ / ((table)->entrySize / sizeof(void *) + (k)))/* * Finalize table's data, free its entry storage using table->ops->freeTable, * and leave its members unchanged from their last live values (which leaves * pointers dangling). If you want to burn cycles clearing table, it's up to * your code to call memset. */extern JS_PUBLIC_API(void)JS_DHashTableFinish(JSDHashTable *table);/* * To consolidate keyHash computation and table grow/shrink code, we use a * single entry point for lookup, add, and remove operations. The operation * codes are declared here, along with codes returned by JSDHashEnumerator * functions, which control JS_DHashTableEnumerate's behavior. */typedef enum JSDHashOperator { JS_DHASH_LOOKUP = 0, /* lookup entry */ JS_DHASH_ADD = 1, /* add entry */ JS_DHASH_REMOVE = 2, /* remove entry, or enumerator says remove */ JS_DHASH_NEXT = 0, /* enumerator says continue */ JS_DHASH_STOP = 1 /* enumerator says stop */} JSDHashOperator;/* * To lookup a key in table, call: * * entry = JS_DHashTableOperate(table, key, JS_DHASH_LOOKUP); * * If JS_DHASH_ENTRY_IS_BUSY(entry) is true, key was found and it identifies * entry. If JS_DHASH_ENTRY_IS_FREE(entry) is true, key was not found. * * To add an entry identified by key to table, call: * * entry = JS_DHashTableOperate(table, key, JS_DHASH_ADD); * * If entry is null upon return, then either the table is severely overloaded, * and memory can't be allocated for entry storage via table->ops->allocTable; * Or if table->ops->initEntry is non-null, the table->ops->initEntry op may * have returned false. * * Otherwise, entry->keyHash has been set so that JS_DHASH_ENTRY_IS_BUSY(entry) * is true, and it is up to the caller to initialize the key and value parts * of the entry sub-type, if they have not been set already (i.e. if entry was * not already in the table, and if the optional initEntry hook was not used). * * To remove an entry identified by key from table, call: * * (void) JS_DHashTableOperate(table, key, JS_DHASH_REMOVE); * * If key's entry is found, it is cleared (via table->ops->clearEntry) and * the entry is marked so that JS_DHASH_ENTRY_IS_FREE(entry). This operation * returns null unconditionally; you should ignore its return value. */extern JS_PUBLIC_API(JSDHashEntryHdr *) JS_DHASH_FASTCALLJS_DHashTableOperate(JSDHashTable *table, const void *key, JSDHashOperator op);/* * Remove an entry already accessed via LOOKUP or ADD. * * NB: this is a "raw" or low-level routine, intended to be used only where * the inefficiency of a full JS_DHashTableOperate (which rehashes in order * to find the entry given its key) is not tolerable. This function does not * shrink the table if it is underloaded. It does not update stats #ifdef * JS_DHASHMETER, either. */extern JS_PUBLIC_API(void)JS_DHashTableRawRemove(JSDHashTable *table, JSDHashEntryHdr *entry);/* * Enumerate entries in table using etor: * * count = JS_DHashTableEnumerate(table, etor, arg); * * JS_DHashTableEnumerate calls etor like so: * * op = etor(table, entry, number, arg); * * where number is a zero-based ordinal assigned to live entries according to * their order in table->entryStore. * * The return value, op, is treated as a set of flags. If op is JS_DHASH_NEXT, * then continue enumerating. If op contains JS_DHASH_REMOVE, then clear (via * table->ops->clearEntry) and free entry. Then we check whether op contains * JS_DHASH_STOP; if so, stop enumerating and return the number of live entries * that were enumerated so far. Return the total number of live entries when * enumeration completes normally. * * If etor calls JS_DHashTableOperate on table with op != JS_DHASH_LOOKUP, it * must return JS_DHASH_STOP; otherwise undefined behavior results. * * If any enumerator returns JS_DHASH_REMOVE, table->entryStore may be shrunk * or compressed after enumeration, but before JS_DHashTableEnumerate returns. * Such an enumerator therefore can't safely set aside entry pointers, but an * enumerator that never returns JS_DHASH_REMOVE can set pointers to entries * aside, e.g., to avoid copying live entries into an array of the entry type. * Copying entry pointers is cheaper, and safe so long as the caller of such a * "stable" Enumerate doesn't use the set-aside pointers after any call either * to PL_DHashTableOperate, or to an "unstable" form of Enumerate, which might * grow or shrink entryStore. * * If your enumerator wants to remove certain entries, but set aside pointers * to other entries that it retains, it can use JS_DHashTableRawRemove on the * entries to be removed, returning JS_DHASH_NEXT to skip them. Likewise, if * you want to remove entries, but for some reason you do not want entryStore * to be shrunk or compressed, you can call JS_DHashTableRawRemove safely on * the entry being enumerated, rather than returning JS_DHASH_REMOVE. */typedef JSDHashOperator(* JS_DLL_CALLBACK JSDHashEnumerator)(JSDHashTable *table, JSDHashEntryHdr *hdr, uint32 number, void *arg);extern JS_PUBLIC_API(uint32)JS_DHashTableEnumerate(JSDHashTable *table, JSDHashEnumerator etor, void *arg);#ifdef JS_DHASHMETER#include <stdio.h>extern JS_PUBLIC_API(void)JS_DHashTableDumpMeter(JSDHashTable *table, JSDHashEnumerator dump, FILE *fp);#endifJS_END_EXTERN_C#endif /* jsdhash_h___ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -