xpcom/glue/pldhash.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial