Trigger another mozilla-central build. a=breakage
[mozilla-central.git] / js / jsd / jsd_atom.c
blob66f09ffd76032b97182318eb16e42079e94c3f4f
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * JavaScript Debugging support - Atom support
42 #include "jsd.h"
44 /* #define TEST_ATOMS 1 */
46 #ifdef TEST_ATOMS
47 static void
48 _testAtoms(JSDContext*jsdc)
50 JSDAtom* atom0 = jsd_AddAtom(jsdc, "foo");
51 JSDAtom* atom1 = jsd_AddAtom(jsdc, "foo");
52 JSDAtom* atom2 = jsd_AddAtom(jsdc, "bar");
53 JSDAtom* atom3 = jsd_CloneAtom(jsdc, atom1);
54 JSDAtom* atom4 = jsd_CloneAtom(jsdc, atom2);
56 const char* c0 = JSD_ATOM_TO_STRING(atom0);
57 const char* c1 = JSD_ATOM_TO_STRING(atom1);
58 const char* c2 = JSD_ATOM_TO_STRING(atom2);
59 const char* c3 = JSD_ATOM_TO_STRING(atom3);
60 const char* c4 = JSD_ATOM_TO_STRING(atom4);
62 jsd_DropAtom(jsdc, atom0);
63 jsd_DropAtom(jsdc, atom1);
64 jsd_DropAtom(jsdc, atom2);
65 jsd_DropAtom(jsdc, atom3);
66 jsd_DropAtom(jsdc, atom4);
68 #endif
70 static intN
71 _atom_smasher(JSHashEntry *he, intN i, void *arg)
73 JS_ASSERT(he);
74 JS_ASSERT(he->value);
75 JS_ASSERT(((JSDAtom*)(he->value))->str);
77 free(((JSDAtom*)(he->value))->str);
78 free(he->value);
79 he->value = NULL;
80 he->key = NULL;
81 return HT_ENUMERATE_NEXT;
84 static intN
85 _compareAtomKeys(const void *v1, const void *v2)
87 return 0 == strcmp((const char*)v1, (const char*)v2);
90 static intN
91 _compareAtoms(const void *v1, const void *v2)
93 return 0 == strcmp(((JSDAtom*)v1)->str, ((JSDAtom*)v2)->str);
97 JSBool
98 jsd_CreateAtomTable(JSDContext* jsdc)
100 jsdc->atoms = JS_NewHashTable(256, JS_HashString,
101 _compareAtomKeys, _compareAtoms,
102 NULL, NULL);
103 #ifdef TEST_ATOMS
104 _testAtoms(jsdc);
105 #endif
106 return !!jsdc->atoms;
109 void
110 jsd_DestroyAtomTable(JSDContext* jsdc)
112 if( jsdc->atoms )
114 JS_HashTableEnumerateEntries(jsdc->atoms, _atom_smasher, NULL);
115 JS_HashTableDestroy(jsdc->atoms);
116 jsdc->atoms = NULL;
120 JSDAtom*
121 jsd_AddAtom(JSDContext* jsdc, const char* str)
123 JSDAtom* atom;
125 if(!str)
127 JS_ASSERT(0);
128 return NULL;
131 JSD_LOCK_ATOMS(jsdc);
133 atom = (JSDAtom*) JS_HashTableLookup(jsdc->atoms, str);
135 if( atom )
136 atom->refcount++;
137 else
139 atom = (JSDAtom*) malloc(sizeof(JSDAtom));
140 if( atom )
142 atom->str = strdup(str);
143 atom->refcount = 1;
144 if(!JS_HashTableAdd(jsdc->atoms, atom->str, atom))
146 free(atom->str);
147 free(atom);
148 atom = NULL;
153 JSD_UNLOCK_ATOMS(jsdc);
154 return atom;
157 JSDAtom*
158 jsd_CloneAtom(JSDContext* jsdc, JSDAtom* atom)
160 JSD_LOCK_ATOMS(jsdc);
161 atom->refcount++;
162 JSD_UNLOCK_ATOMS(jsdc);
163 return atom;
166 void
167 jsd_DropAtom(JSDContext* jsdc, JSDAtom* atom)
169 JSD_LOCK_ATOMS(jsdc);
170 if(! --atom->refcount)
172 JS_HashTableRemove(jsdc->atoms, atom->str);
173 free(atom->str);
174 free(atom);
176 JSD_UNLOCK_ATOMS(jsdc);