|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /* |
|
8 * JavaScript Debugging support - Atom support |
|
9 */ |
|
10 |
|
11 #include "jsd.h" |
|
12 |
|
13 /* #define TEST_ATOMS 1 */ |
|
14 |
|
15 #ifdef TEST_ATOMS |
|
16 static void |
|
17 _testAtoms(JSDContext*jsdc) |
|
18 { |
|
19 JSDAtom* atom0 = jsd_AddAtom(jsdc, "foo"); |
|
20 JSDAtom* atom1 = jsd_AddAtom(jsdc, "foo"); |
|
21 JSDAtom* atom2 = jsd_AddAtom(jsdc, "bar"); |
|
22 JSDAtom* atom3 = jsd_CloneAtom(jsdc, atom1); |
|
23 JSDAtom* atom4 = jsd_CloneAtom(jsdc, atom2); |
|
24 |
|
25 const char* c0 = JSD_ATOM_TO_STRING(atom0); |
|
26 const char* c1 = JSD_ATOM_TO_STRING(atom1); |
|
27 const char* c2 = JSD_ATOM_TO_STRING(atom2); |
|
28 const char* c3 = JSD_ATOM_TO_STRING(atom3); |
|
29 const char* c4 = JSD_ATOM_TO_STRING(atom4); |
|
30 |
|
31 jsd_DropAtom(jsdc, atom0); |
|
32 jsd_DropAtom(jsdc, atom1); |
|
33 jsd_DropAtom(jsdc, atom2); |
|
34 jsd_DropAtom(jsdc, atom3); |
|
35 jsd_DropAtom(jsdc, atom4); |
|
36 } |
|
37 #endif |
|
38 |
|
39 static int |
|
40 _atom_smasher(JSHashEntry *he, int i, void *arg) |
|
41 { |
|
42 MOZ_ASSERT(he); |
|
43 MOZ_ASSERT(he->value); |
|
44 MOZ_ASSERT(((JSDAtom*)(he->value))->str); |
|
45 |
|
46 free(((JSDAtom*)(he->value))->str); |
|
47 free(he->value); |
|
48 he->value = nullptr; |
|
49 he->key = nullptr; |
|
50 return HT_ENUMERATE_NEXT; |
|
51 } |
|
52 |
|
53 static int |
|
54 _compareAtomKeys(const void *v1, const void *v2) |
|
55 { |
|
56 return 0 == strcmp((const char*)v1, (const char*)v2); |
|
57 } |
|
58 |
|
59 static int |
|
60 _compareAtoms(const void *v1, const void *v2) |
|
61 { |
|
62 return 0 == strcmp(((JSDAtom*)v1)->str, ((JSDAtom*)v2)->str); |
|
63 } |
|
64 |
|
65 |
|
66 bool |
|
67 jsd_CreateAtomTable(JSDContext* jsdc) |
|
68 { |
|
69 jsdc->atoms = JS_NewHashTable(256, JS_HashString, |
|
70 _compareAtomKeys, _compareAtoms, |
|
71 nullptr, nullptr); |
|
72 #ifdef TEST_ATOMS |
|
73 _testAtoms(jsdc); |
|
74 #endif |
|
75 return !!jsdc->atoms; |
|
76 } |
|
77 |
|
78 void |
|
79 jsd_DestroyAtomTable(JSDContext* jsdc) |
|
80 { |
|
81 if( jsdc->atoms ) |
|
82 { |
|
83 JS_HashTableEnumerateEntries(jsdc->atoms, _atom_smasher, nullptr); |
|
84 JS_HashTableDestroy(jsdc->atoms); |
|
85 jsdc->atoms = nullptr; |
|
86 } |
|
87 } |
|
88 |
|
89 JSDAtom* |
|
90 jsd_AddAtom(JSDContext* jsdc, const char* str) |
|
91 { |
|
92 JSDAtom* atom; |
|
93 |
|
94 if(!str) |
|
95 { |
|
96 MOZ_ASSERT(0); |
|
97 return nullptr; |
|
98 } |
|
99 |
|
100 JSD_LOCK_ATOMS(jsdc); |
|
101 |
|
102 atom = (JSDAtom*) JS_HashTableLookup(jsdc->atoms, str); |
|
103 |
|
104 if( atom ) |
|
105 atom->refcount++; |
|
106 else |
|
107 { |
|
108 atom = (JSDAtom*) malloc(sizeof(JSDAtom)); |
|
109 if( atom ) |
|
110 { |
|
111 atom->str = strdup(str); |
|
112 atom->refcount = 1; |
|
113 if(!JS_HashTableAdd(jsdc->atoms, atom->str, atom)) |
|
114 { |
|
115 free(atom->str); |
|
116 free(atom); |
|
117 atom = nullptr; |
|
118 } |
|
119 } |
|
120 } |
|
121 |
|
122 JSD_UNLOCK_ATOMS(jsdc); |
|
123 return atom; |
|
124 } |
|
125 |
|
126 JSDAtom* |
|
127 jsd_CloneAtom(JSDContext* jsdc, JSDAtom* atom) |
|
128 { |
|
129 JSD_LOCK_ATOMS(jsdc); |
|
130 atom->refcount++; |
|
131 JSD_UNLOCK_ATOMS(jsdc); |
|
132 return atom; |
|
133 } |
|
134 |
|
135 void |
|
136 jsd_DropAtom(JSDContext* jsdc, JSDAtom* atom) |
|
137 { |
|
138 JSD_LOCK_ATOMS(jsdc); |
|
139 if(! --atom->refcount) |
|
140 { |
|
141 JS_HashTableRemove(jsdc->atoms, atom->str); |
|
142 free(atom->str); |
|
143 free(atom); |
|
144 } |
|
145 JSD_UNLOCK_ATOMS(jsdc); |
|
146 } |
|
147 |