Implement -XStaticValues
[ghc.git] / rts / StaticPtrTable.c
1 /*
2 * (c)2014 Tweag I/O
3 *
4 * The Static Pointer Table implementation.
5 *
6 * https://ghc.haskell.org/trac/ghc/wiki/StaticPointers
7 * https://ghc.haskell.org/trac/ghc/wiki/StaticPointers/ImplementationPlan
8 *
9 */
10
11 #include "Rts.h"
12 #include "StaticPtrTable.h"
13 #include "Hash.h"
14
15 static HashTable * spt = NULL;
16
17 /// Hash function for the SPT.
18 static int hashFingerprint(HashTable *table, StgWord64 key[2]) {
19 // Take half of the key to compute the hash.
20 return hashWord(table, (StgWord)key[1]);
21 }
22
23 /// Comparison function for the SPT.
24 static int compareFingerprint(StgWord64 ptra[2], StgWord64 ptrb[2]) {
25 return ptra[0] == ptrb[0] && ptra[1] == ptrb[1];
26 }
27
28 void hs_spt_insert(StgWord64 key[2],void *spe_closure) {
29 // hs_spt_insert is called from constructor functions, so
30 // the SPT needs to be initialized here.
31 if (spt == NULL)
32 spt = allocHashTable_( (HashFunction *)hashFingerprint
33 , (CompareFunction *)compareFingerprint
34 );
35
36 getStablePtr(spe_closure);
37 insertHashTable(spt, (StgWord)key, spe_closure);
38 }
39
40 StgPtr hs_spt_lookup(StgWord64 key[2]) {
41 return spt ? lookupHashTable(spt, (StgWord)key) : NULL;
42 }
43
44 int hs_spt_keys(StgPtr keys[], int szKeys) {
45 return spt ? keysHashTable(spt, (StgWord*)keys, szKeys) : 0;
46 }
47
48 int hs_spt_key_count() {
49 return spt ? keyCountHashTable(spt) : 0;
50 }
51
52 void exitStaticPtrTable() {
53 if (spt) {
54 freeHashTable(spt, NULL);
55 spt = NULL;
56 }
57 }