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