LCOV - code coverage report
Current view: directory - js/jsd - jsd_atom.c (source / functions) Found Hit Coverage
Test: app.info Lines: 53 9 17.0 %
Date: 2012-06-02 Functions: 8 2 25.0 %

       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
       4                 :  *
       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/
       9                 :  *
      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.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      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.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *
      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.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : /*
      39                 :  * JavaScript Debugging support - Atom support
      40                 :  */
      41                 : 
      42                 : #include "jsd.h"
      43                 : 
      44                 : /* #define TEST_ATOMS 1 */
      45                 : 
      46                 : #ifdef TEST_ATOMS
      47                 : static void 
      48                 : _testAtoms(JSDContext*jsdc)
      49                 : {
      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);
      55                 : 
      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);
      61                 : 
      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);
      67                 : }        
      68                 : #endif    
      69                 : 
      70                 : static int
      71               0 : _atom_smasher(JSHashEntry *he, int i, void *arg)
      72                 : {
      73               0 :     JS_ASSERT(he);
      74               0 :     JS_ASSERT(he->value);
      75               0 :     JS_ASSERT(((JSDAtom*)(he->value))->str);
      76                 : 
      77               0 :     free(((JSDAtom*)(he->value))->str);
      78               0 :     free(he->value);
      79               0 :     he->value = NULL;
      80               0 :     he->key   = NULL;
      81               0 :     return HT_ENUMERATE_NEXT;
      82                 : }
      83                 : 
      84                 : static int
      85               0 : _compareAtomKeys(const void *v1, const void *v2)
      86                 : {
      87               0 :     return 0 == strcmp((const char*)v1, (const char*)v2);
      88                 : }        
      89                 : 
      90                 : static int
      91               0 : _compareAtoms(const void *v1, const void *v2)
      92                 : {
      93               0 :     return 0 == strcmp(((JSDAtom*)v1)->str, ((JSDAtom*)v2)->str);
      94                 : }        
      95                 : 
      96                 : 
      97                 : JSBool
      98             280 : jsd_CreateAtomTable(JSDContext* jsdc)
      99                 : {
     100             280 :     jsdc->atoms = JS_NewHashTable(256, JS_HashString,
     101                 :                                   _compareAtomKeys, _compareAtoms,
     102                 :                                   NULL, NULL);
     103                 : #ifdef TEST_ATOMS
     104                 :     _testAtoms(jsdc);
     105                 : #endif    
     106             280 :     return !!jsdc->atoms;
     107                 : }
     108                 : 
     109                 : void
     110             280 : jsd_DestroyAtomTable(JSDContext* jsdc)
     111                 : {
     112             280 :     if( jsdc->atoms )
     113                 :     {
     114             280 :         JS_HashTableEnumerateEntries(jsdc->atoms, _atom_smasher, NULL);
     115             280 :         JS_HashTableDestroy(jsdc->atoms);
     116             280 :         jsdc->atoms = NULL;
     117                 :     }
     118             280 : }
     119                 : 
     120                 : JSDAtom*
     121               0 : jsd_AddAtom(JSDContext* jsdc, const char* str)
     122                 : {
     123                 :     JSDAtom* atom;
     124                 : 
     125               0 :     if(!str)
     126                 :     {
     127               0 :         JS_ASSERT(0);
     128               0 :         return NULL;
     129                 :     }
     130                 : 
     131               0 :     JSD_LOCK_ATOMS(jsdc);
     132                 :     
     133               0 :     atom = (JSDAtom*) JS_HashTableLookup(jsdc->atoms, str);
     134                 : 
     135               0 :     if( atom )
     136               0 :         atom->refcount++;
     137                 :     else
     138                 :     {
     139               0 :         atom = (JSDAtom*) malloc(sizeof(JSDAtom));
     140               0 :         if( atom )
     141                 :         {
     142               0 :             atom->str = strdup(str);
     143               0 :             atom->refcount = 1;
     144               0 :             if(!JS_HashTableAdd(jsdc->atoms, atom->str, atom))
     145                 :             {
     146               0 :                 free(atom->str);
     147               0 :                 free(atom);
     148               0 :                 atom = NULL;
     149                 :             }
     150                 :         }
     151                 :     }
     152                 : 
     153               0 :     JSD_UNLOCK_ATOMS(jsdc);
     154               0 :     return atom;
     155                 : }        
     156                 : 
     157                 : JSDAtom*
     158               0 : jsd_CloneAtom(JSDContext* jsdc, JSDAtom* atom)
     159                 : {
     160               0 :     JSD_LOCK_ATOMS(jsdc);
     161               0 :     atom->refcount++;
     162               0 :     JSD_UNLOCK_ATOMS(jsdc);
     163               0 :     return atom;
     164                 : }        
     165                 : 
     166                 : void
     167               0 : jsd_DropAtom(JSDContext* jsdc, JSDAtom* atom)
     168                 : {
     169               0 :     JSD_LOCK_ATOMS(jsdc);
     170               0 :     if(! --atom->refcount)
     171                 :     {
     172               0 :         JS_HashTableRemove(jsdc->atoms, atom->str);
     173               0 :         free(atom->str);
     174               0 :         free(atom);
     175                 :     }
     176               0 :     JSD_UNLOCK_ATOMS(jsdc);
     177               0 : }        
     178                 : 

Generated by: LCOV version 1.7