Back to index

lightning-sunbird  0.9+nobinonly
jsd_atom.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * JavaScript Debugging support - Atom support
00040  */
00041 
00042 #include "jsd.h"
00043 
00044 /* #define TEST_ATOMS 1 */
00045 
00046 #ifdef TEST_ATOMS
00047 static void 
00048 _testAtoms(JSDContext*jsdc)
00049 {
00050     JSDAtom* atom0 = jsd_AddAtom(jsdc, "foo");
00051     JSDAtom* atom1 = jsd_AddAtom(jsdc, "foo");
00052     JSDAtom* atom2 = jsd_AddAtom(jsdc, "bar");
00053     JSDAtom* atom3 = jsd_CloneAtom(jsdc, atom1);
00054     JSDAtom* atom4 = jsd_CloneAtom(jsdc, atom2);
00055 
00056     const char* c0 = JSD_ATOM_TO_STRING(atom0);
00057     const char* c1 = JSD_ATOM_TO_STRING(atom1);
00058     const char* c2 = JSD_ATOM_TO_STRING(atom2);
00059     const char* c3 = JSD_ATOM_TO_STRING(atom3);
00060     const char* c4 = JSD_ATOM_TO_STRING(atom4);
00061 
00062     jsd_DropAtom(jsdc, atom0);
00063     jsd_DropAtom(jsdc, atom1);
00064     jsd_DropAtom(jsdc, atom2);
00065     jsd_DropAtom(jsdc, atom3);
00066     jsd_DropAtom(jsdc, atom4);
00067 }        
00068 #endif    
00069 
00070 JS_STATIC_DLL_CALLBACK(intN)
00071 _atom_smasher(JSHashEntry *he, intN i, void *arg)
00072 {
00073     JS_ASSERT(he);
00074     JS_ASSERT(he->value);
00075     JS_ASSERT(((JSDAtom*)(he->value))->str);
00076 
00077     free(((JSDAtom*)(he->value))->str);
00078     free(he->value);
00079     he->value = NULL;
00080     he->key   = NULL;
00081     return HT_ENUMERATE_NEXT;
00082 }
00083 
00084 JS_STATIC_DLL_CALLBACK(intN)
00085 _compareAtomKeys(const void *v1, const void *v2)
00086 {
00087     return 0 == strcmp((const char*)v1, (const char*)v2);
00088 }        
00089 
00090 JS_STATIC_DLL_CALLBACK(intN)
00091 _compareAtoms(const void *v1, const void *v2)
00092 {
00093     return 0 == strcmp(((JSDAtom*)v1)->str, ((JSDAtom*)v2)->str);
00094 }        
00095 
00096 
00097 JSBool
00098 jsd_CreateAtomTable(JSDContext* jsdc)
00099 {
00100     jsdc->atoms = JS_NewHashTable(256, JS_HashString,
00101                                   _compareAtomKeys, _compareAtoms,
00102                                   NULL, NULL);
00103 #ifdef TEST_ATOMS
00104     _testAtoms(jsdc);
00105 #endif    
00106     return (JSBool) jsdc->atoms;
00107 }
00108 
00109 void
00110 jsd_DestroyAtomTable(JSDContext* jsdc)
00111 {
00112     if( jsdc->atoms )
00113     {
00114         JS_HashTableEnumerateEntries(jsdc->atoms, _atom_smasher, NULL);
00115         JS_HashTableDestroy(jsdc->atoms);
00116         jsdc->atoms = NULL;
00117     }
00118 }
00119 
00120 JSDAtom*
00121 jsd_AddAtom(JSDContext* jsdc, const char* str)
00122 {
00123     JSDAtom* atom;
00124 
00125     if(!str)
00126     {
00127         JS_ASSERT(0);
00128         return NULL;
00129     }
00130 
00131     JSD_LOCK_ATOMS(jsdc);
00132     
00133     atom = (JSDAtom*) JS_HashTableLookup(jsdc->atoms, str);
00134 
00135     if( atom )
00136         atom->refcount++;
00137     else
00138     {
00139         atom = (JSDAtom*) malloc(sizeof(JSDAtom));
00140         if( atom )
00141         {
00142             atom->str = strdup(str);
00143             atom->refcount = 1;
00144             if(!JS_HashTableAdd(jsdc->atoms, atom->str, atom))
00145             {
00146                 free(atom->str);
00147                 free(atom);
00148                 atom = NULL;
00149             }
00150         }
00151     }
00152 
00153     JSD_UNLOCK_ATOMS(jsdc);
00154     return atom;
00155 }        
00156 
00157 JSDAtom*
00158 jsd_CloneAtom(JSDContext* jsdc, JSDAtom* atom)
00159 {
00160     JSD_LOCK_ATOMS(jsdc);
00161     atom->refcount++;
00162     JSD_UNLOCK_ATOMS(jsdc);
00163     return atom;
00164 }        
00165 
00166 void
00167 jsd_DropAtom(JSDContext* jsdc, JSDAtom* atom)
00168 {
00169     JSD_LOCK_ATOMS(jsdc);
00170     if(! --atom->refcount)
00171     {
00172         JS_HashTableRemove(jsdc->atoms, atom->str);
00173         free(atom->str);
00174         free(atom);
00175     }
00176     JSD_UNLOCK_ATOMS(jsdc);
00177 }        
00178