js/jsd/jsd_atom.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/jsd/jsd_atom.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/*
    1.11 + * JavaScript Debugging support - Atom support
    1.12 + */
    1.13 +
    1.14 +#include "jsd.h"
    1.15 +
    1.16 +/* #define TEST_ATOMS 1 */
    1.17 +
    1.18 +#ifdef TEST_ATOMS
    1.19 +static void 
    1.20 +_testAtoms(JSDContext*jsdc)
    1.21 +{
    1.22 +    JSDAtom* atom0 = jsd_AddAtom(jsdc, "foo");
    1.23 +    JSDAtom* atom1 = jsd_AddAtom(jsdc, "foo");
    1.24 +    JSDAtom* atom2 = jsd_AddAtom(jsdc, "bar");
    1.25 +    JSDAtom* atom3 = jsd_CloneAtom(jsdc, atom1);
    1.26 +    JSDAtom* atom4 = jsd_CloneAtom(jsdc, atom2);
    1.27 +
    1.28 +    const char* c0 = JSD_ATOM_TO_STRING(atom0);
    1.29 +    const char* c1 = JSD_ATOM_TO_STRING(atom1);
    1.30 +    const char* c2 = JSD_ATOM_TO_STRING(atom2);
    1.31 +    const char* c3 = JSD_ATOM_TO_STRING(atom3);
    1.32 +    const char* c4 = JSD_ATOM_TO_STRING(atom4);
    1.33 +
    1.34 +    jsd_DropAtom(jsdc, atom0);
    1.35 +    jsd_DropAtom(jsdc, atom1);
    1.36 +    jsd_DropAtom(jsdc, atom2);
    1.37 +    jsd_DropAtom(jsdc, atom3);
    1.38 +    jsd_DropAtom(jsdc, atom4);
    1.39 +}        
    1.40 +#endif    
    1.41 +
    1.42 +static int
    1.43 +_atom_smasher(JSHashEntry *he, int i, void *arg)
    1.44 +{
    1.45 +    MOZ_ASSERT(he);
    1.46 +    MOZ_ASSERT(he->value);
    1.47 +    MOZ_ASSERT(((JSDAtom*)(he->value))->str);
    1.48 +
    1.49 +    free(((JSDAtom*)(he->value))->str);
    1.50 +    free(he->value);
    1.51 +    he->value = nullptr;
    1.52 +    he->key   = nullptr;
    1.53 +    return HT_ENUMERATE_NEXT;
    1.54 +}
    1.55 +
    1.56 +static int
    1.57 +_compareAtomKeys(const void *v1, const void *v2)
    1.58 +{
    1.59 +    return 0 == strcmp((const char*)v1, (const char*)v2);
    1.60 +}        
    1.61 +
    1.62 +static int
    1.63 +_compareAtoms(const void *v1, const void *v2)
    1.64 +{
    1.65 +    return 0 == strcmp(((JSDAtom*)v1)->str, ((JSDAtom*)v2)->str);
    1.66 +}        
    1.67 +
    1.68 +
    1.69 +bool
    1.70 +jsd_CreateAtomTable(JSDContext* jsdc)
    1.71 +{
    1.72 +    jsdc->atoms = JS_NewHashTable(256, JS_HashString,
    1.73 +                                  _compareAtomKeys, _compareAtoms,
    1.74 +                                  nullptr, nullptr);
    1.75 +#ifdef TEST_ATOMS
    1.76 +    _testAtoms(jsdc);
    1.77 +#endif    
    1.78 +    return !!jsdc->atoms;
    1.79 +}
    1.80 +
    1.81 +void
    1.82 +jsd_DestroyAtomTable(JSDContext* jsdc)
    1.83 +{
    1.84 +    if( jsdc->atoms )
    1.85 +    {
    1.86 +        JS_HashTableEnumerateEntries(jsdc->atoms, _atom_smasher, nullptr);
    1.87 +        JS_HashTableDestroy(jsdc->atoms);
    1.88 +        jsdc->atoms = nullptr;
    1.89 +    }
    1.90 +}
    1.91 +
    1.92 +JSDAtom*
    1.93 +jsd_AddAtom(JSDContext* jsdc, const char* str)
    1.94 +{
    1.95 +    JSDAtom* atom;
    1.96 +
    1.97 +    if(!str)
    1.98 +    {
    1.99 +        MOZ_ASSERT(0);
   1.100 +        return nullptr;
   1.101 +    }
   1.102 +
   1.103 +    JSD_LOCK_ATOMS(jsdc);
   1.104 +    
   1.105 +    atom = (JSDAtom*) JS_HashTableLookup(jsdc->atoms, str);
   1.106 +
   1.107 +    if( atom )
   1.108 +        atom->refcount++;
   1.109 +    else
   1.110 +    {
   1.111 +        atom = (JSDAtom*) malloc(sizeof(JSDAtom));
   1.112 +        if( atom )
   1.113 +        {
   1.114 +            atom->str = strdup(str);
   1.115 +            atom->refcount = 1;
   1.116 +            if(!JS_HashTableAdd(jsdc->atoms, atom->str, atom))
   1.117 +            {
   1.118 +                free(atom->str);
   1.119 +                free(atom);
   1.120 +                atom = nullptr;
   1.121 +            }
   1.122 +        }
   1.123 +    }
   1.124 +
   1.125 +    JSD_UNLOCK_ATOMS(jsdc);
   1.126 +    return atom;
   1.127 +}        
   1.128 +
   1.129 +JSDAtom*
   1.130 +jsd_CloneAtom(JSDContext* jsdc, JSDAtom* atom)
   1.131 +{
   1.132 +    JSD_LOCK_ATOMS(jsdc);
   1.133 +    atom->refcount++;
   1.134 +    JSD_UNLOCK_ATOMS(jsdc);
   1.135 +    return atom;
   1.136 +}        
   1.137 +
   1.138 +void
   1.139 +jsd_DropAtom(JSDContext* jsdc, JSDAtom* atom)
   1.140 +{
   1.141 +    JSD_LOCK_ATOMS(jsdc);
   1.142 +    if(! --atom->refcount)
   1.143 +    {
   1.144 +        JS_HashTableRemove(jsdc->atoms, atom->str);
   1.145 +        free(atom->str);
   1.146 +        free(atom);
   1.147 +    }
   1.148 +    JSD_UNLOCK_ATOMS(jsdc);
   1.149 +}        
   1.150 +

mercurial