2013-04-16 13:47:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef jshash_h___
|
|
|
|
#define jshash_h___
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* API to portable hash table code.
|
|
|
|
*/
|
|
|
|
#include <stddef.h>
|
2013-08-17 15:50:18 -07:00
|
|
|
#include <stdint.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-06-26 18:35:11 -07:00
|
|
|
extern "C" {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
typedef uint32_t JSHashNumber;
|
2007-03-22 10:30:00 -07:00
|
|
|
typedef struct JSHashEntry JSHashEntry;
|
|
|
|
typedef struct JSHashTable JSHashTable;
|
|
|
|
|
|
|
|
#define JS_HASH_BITS 32
|
|
|
|
#define JS_GOLDEN_RATIO 0x9E3779B9U
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
typedef JSHashNumber (* JSHashFunction)(const void *key);
|
2012-02-28 15:11:11 -08:00
|
|
|
typedef int (* JSHashComparator)(const void *v1, const void *v2);
|
|
|
|
typedef int (* JSHashEnumerator)(JSHashEntry *he, int i, void *arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Flag bits in JSHashEnumerator's return value */
|
|
|
|
#define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
|
|
|
|
#define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
|
|
|
|
#define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
|
|
|
|
|
|
|
|
typedef struct JSHashAllocOps {
|
|
|
|
void * (*allocTable)(void *pool, size_t size);
|
2009-04-05 21:17:22 -07:00
|
|
|
void (*freeTable)(void *pool, void *item, size_t size);
|
2007-03-22 10:30:00 -07:00
|
|
|
JSHashEntry * (*allocEntry)(void *pool, const void *key);
|
2012-02-28 15:11:11 -08:00
|
|
|
void (*freeEntry)(void *pool, JSHashEntry *he, unsigned flag);
|
2007-03-22 10:30:00 -07:00
|
|
|
} JSHashAllocOps;
|
|
|
|
|
|
|
|
#define HT_FREE_VALUE 0 /* just free the entry's value */
|
|
|
|
#define HT_FREE_ENTRY 1 /* free value and entire entry */
|
|
|
|
|
|
|
|
struct JSHashEntry {
|
|
|
|
JSHashEntry *next; /* hash chain linkage */
|
|
|
|
JSHashNumber keyHash; /* key hash function result */
|
|
|
|
const void *key; /* ptr to opaque key */
|
|
|
|
void *value; /* ptr to opaque value */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JSHashTable {
|
|
|
|
JSHashEntry **buckets; /* vector of hash buckets */
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint32_t nentries; /* number of entries in table */
|
|
|
|
uint32_t shift; /* multiplicative hash shift */
|
2007-03-22 10:30:00 -07:00
|
|
|
JSHashFunction keyHash; /* key hash function */
|
|
|
|
JSHashComparator keyCompare; /* key comparison function */
|
|
|
|
JSHashComparator valueCompare; /* value comparison function */
|
2013-09-24 08:21:22 -07:00
|
|
|
const JSHashAllocOps *allocOps; /* allocation operations */
|
2007-03-22 10:30:00 -07:00
|
|
|
void *allocPriv; /* allocation private data */
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
#ifdef JS_HASHMETER
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint32_t nlookups; /* total number of lookups */
|
|
|
|
uint32_t nsteps; /* number of hash chains traversed */
|
|
|
|
uint32_t ngrows; /* number of table expansions */
|
|
|
|
uint32_t nshrinks; /* number of table contractions */
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new hash table.
|
|
|
|
* If allocOps is null, use default allocator ops built on top of malloc().
|
|
|
|
*/
|
2012-07-18 17:38:10 -07:00
|
|
|
extern JSHashTable *
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
JS_NewHashTable(uint32_t n, JSHashFunction keyHash,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSHashComparator keyCompare, JSHashComparator valueCompare,
|
2013-09-24 08:21:22 -07:00
|
|
|
const JSHashAllocOps *allocOps, void *allocPriv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-18 17:38:10 -07:00
|
|
|
extern void
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableDestroy(JSHashTable *ht);
|
|
|
|
|
|
|
|
/* Low level access methods */
|
2012-07-18 17:38:10 -07:00
|
|
|
extern JSHashEntry **
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key);
|
|
|
|
|
2012-07-18 17:38:10 -07:00
|
|
|
extern JSHashEntry *
|
2009-07-16 13:16:27 -07:00
|
|
|
JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **&hep, JSHashNumber keyHash,
|
2007-03-22 10:30:00 -07:00
|
|
|
const void *key, void *value);
|
|
|
|
|
2012-07-18 17:38:10 -07:00
|
|
|
extern void
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he);
|
|
|
|
|
|
|
|
/* Higher level access methods */
|
2012-07-18 17:38:10 -07:00
|
|
|
extern JSHashEntry *
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableAdd(JSHashTable *ht, const void *key, void *value);
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
extern bool
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableRemove(JSHashTable *ht, const void *key);
|
|
|
|
|
2012-07-18 17:38:10 -07:00
|
|
|
extern int
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg);
|
|
|
|
|
2012-07-18 17:38:10 -07:00
|
|
|
extern void *
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableLookup(JSHashTable *ht, const void *key);
|
|
|
|
|
2012-07-18 17:38:10 -07:00
|
|
|
extern int
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp);
|
|
|
|
|
|
|
|
/* General-purpose C string hash function. */
|
2012-07-18 17:38:10 -07:00
|
|
|
extern JSHashNumber
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashString(const void *key);
|
|
|
|
|
|
|
|
/* Stub function just returns v1 == v2 */
|
2012-07-18 17:38:10 -07:00
|
|
|
extern int
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_CompareValues(const void *v1, const void *v2);
|
|
|
|
|
2013-06-26 18:35:11 -07:00
|
|
|
} // extern "C"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif /* jshash_h___ */
|