Changeset 1654

Show
Ignore:
Timestamp:
07/16/08 19:42:16 (3 months ago)
Author:
common
Message:

libemu

  • introduce emu_hashmap_{ptr,string}_{hash,cmp} functions, to avoid further duplicated code, remove duplicate hashtable hash and cmp functions
  • fix sctest Makefile
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libemu/trunk/include/emu/emu_hashtable.h

    r1507 r1654  
    161161bool emu_hashtable_delete(struct emu_hashtable *eh, void *key); 
    162162 
     163 
     164uint32_t emu_hashtable_string_hash(void *data); 
     165bool emu_hashtable_string_cmp(void *a, void *b); 
     166 
     167uint32_t emu_hashtable_ptr_hash(void *data); 
     168bool emu_hashtable_ptr_cmp(void *a, void *b); 
     169 
     170 
    163171#endif // EMU_HASHTABLE_H 
  • libemu/trunk/src/emu_hashtable.c

    r1507 r1654  
    211211        free(ehbi); 
    212212} 
     213 
     214 
     215 
     216/** 
     217 * string hashing function to avoid duplicate code 
     218 * algo is djb2 taken from http://www.cse.yorku.ca/~oz/hash.html 
     219 *  
     220 * @param data   the string to hash 
     221 *  
     222 * @return the hash 
     223 */ 
     224uint32_t emu_hashtable_string_hash(void *key) 
     225{ 
     226#ifdef LSOD_HASH 
     227    uint32_t hash = 0; 
     228    char *c = (char *)key; 
     229 
     230        while (*c != 0) 
     231        { 
     232                hash = hash << 19 | hash >> 13; 
     233                hash += *c; 
     234                c++; 
     235        } 
     236 
     237    return hash; 
     238#else 
     239        unsigned char *str = key; 
     240        unsigned long hash = 5381; 
     241        int c; 
     242 
     243        while ( (c = *str++) != 0 ) 
     244                hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 
     245 
     246        return hash; 
     247#endif 
     248} 
     249 
     250bool emu_hashtable_string_cmp(void *a, void *b) 
     251{ 
     252        if ( strcmp(a, b) == 0 ) 
     253                return true; 
     254 
     255        return false; 
     256} 
     257 
     258uint32_t emu_hashtable_ptr_hash(void *key) 
     259{ 
     260        uint32_t ukey = (uint32_t)key; 
     261        ukey++; 
     262        return ukey; 
     263} 
     264 
     265bool emu_hashtable_ptr_cmp(void *a, void *b) 
     266{ 
     267        if ( (uint32_t)a == (uint32_t)b ) 
     268                return true; 
     269 
     270        return false; 
     271} 
     272 
  • libemu/trunk/src/emu_shellcode.c

    r1607 r1654  
    5252 
    5353 
    54 bool cmp_uint32_t(void *a, void *b) 
    55 { 
    56         if ( (uint32_t)a == (uint32_t)b ) 
    57                 return true; 
    58  
    59         return false; 
    60 } 
    61  
    62 uint32_t hash_uint32_t(void *key) 
    63 { 
    64         uint32_t ukey = (uint32_t)key; 
    65         ukey++; 
    66         return ukey; 
    67 } 
    68  
    69  
    7054int tested_positions_cmp(struct emu_list_item *a, struct emu_list_item *b) 
    7155{ 
     
    386370        emu_memory_mode_rw(emu_memory_get(e)); 
    387371 
    388         struct emu_hashtable *eh = emu_hashtable_new(size+4/4, hash_uint32_t, cmp_uint32_t); 
     372        struct emu_hashtable *eh = emu_hashtable_new(size+4/4, emu_hashtable_ptr_hash, emu_hashtable_ptr_cmp); 
    389373        struct emu_list_item *eli; 
    390374//      struct emu_env_w32 *env = emu_env_w32_new(e); 
  • libemu/trunk/src/environment/linux/emu_env_linux.c

    r1607 r1654  
    3636#include "emu/environment/linux/env_linux_syscalls.h" 
    3737 
    38 extern uint32_t dll_export_fnname_hash(void *key); 
    39 extern bool dll_export_fnname_cmp(void *a, void *b); 
    40  
    4138struct emu_env_linux *emu_env_linux_new(struct emu *e) 
    4239{ 
     
    4542        eel->emu = e; 
    4643 
    47         eel->syscall_hooks_by_name = emu_hashtable_new(256, dll_export_fnname_hash,  dll_export_fnname_cmp); 
     44        eel->syscall_hooks_by_name = emu_hashtable_new(256, emu_hashtable_string_hash,  emu_hashtable_string_cmp); 
    4845        int i; 
    4946        eel->syscall_hookx = malloc(sizeof(syscall_hooks)); 
  • libemu/trunk/src/environment/win32/emu_env_w32_dll.c

    r1607 r1654  
    5252} 
    5353 
    54 bool dll_export_fnptr_cmp(void *a, void *b) 
    55 { 
    56         if ((uint32_t)a == (uint32_t)b) 
    57                 return true; 
    58  
    59         return false; 
    60 } 
    61  
    62 uint32_t dll_export_fnptr_hash(void *key) 
    63 { 
    64         uint32_t ukey = (uint32_t)key; 
    65         ukey++; 
    66         return ukey; 
    67 } 
    68  
    69  
    70 bool dll_export_fnname_cmp(void *a, void *b) 
    71 { 
    72         if (strcmp((char *)a, (char *)b) == 0) 
    73                 return true; 
    74  
    75         return false; 
    76 } 
    77  
    78 #define HASH_KEY                       13 
    79 uint32_t dll_export_fnname_hash(void *key) 
    80 { 
    81     uint32_t hash = 0; 
    82     char *c = (char *)key; 
    83 /* 
    84     while ( *c != 0 ) 
    85     { 
    86         asm ("ror DWORD hash,13"); 
    87          
    88         hash += *c++; 
    89     } 
    90 */ 
    91         while (*c != 0) 
    92         { 
    93                 hash = hash << 19 | hash >> 13; 
    94                 hash += *c; 
    95                 c++; 
    96         } 
    97  
    98     return hash; 
    99 } 
    100  
    101  
    10254void emu_env_w32_dll_exports_copy(struct emu_env_w32_dll *to,struct emu_env_w32_dll_export *from) 
    10355{ 
     
    11365 
    11466 
    115         to->exports_by_fnptr = emu_hashtable_new(size, dll_export_fnptr_hash, dll_export_fnptr_cmp); 
    116         to->exports_by_fnname = emu_hashtable_new(size, dll_export_fnname_hash, dll_export_fnname_cmp); 
     67        to->exports_by_fnptr = emu_hashtable_new(size, emu_hashtable_ptr_hash,emu_hashtable_ptr_cmp); 
     68        to->exports_by_fnname = emu_hashtable_new(size, emu_hashtable_string_hash, emu_hashtable_string_cmp); 
    11769 
    11870        for (i=0;from[i].fnname != 0; i++) 
  • libemu/trunk/tools/sctest/Makefile.am

    r1653 r1654  
    1111sctest_LDADD = ../../src/libemu.la 
    1212sctest_SOURCES = dot.c 
    13 sctest_SOURCES = dot.h 
     13sctest_SOURCES += dot.h 
    1414sctest_SOURCES += sctestmain.c 
    1515sctest_SOURCES += sctestmain.h 
     
    1818sctest_SOURCES += userhooks.c 
    1919sctest_SOURCES += userhooks.h 
     20#sctest_SOURCES += nanny.c 
     21#sctest_SOURCES += nanny.h 
     22 
  • libemu/trunk/tools/sctest/dot.c

    r1653 r1654  
    119119 
    120120        struct emu_graph *sgraph = emu_graph_new(); 
    121         struct emu_hashtable *ht = emu_hashtable_new(2047, hash, cmp); 
     121        struct emu_hashtable *ht = emu_hashtable_new(2047, emu_hashtable_ptr_hash, emu_hashtable_ptr_cmp); 
    122122 
    123123        struct emu_vertex *nev; 
  • libemu/trunk/tools/sctest/sctestmain.c

    r1653 r1654  
    118118 
    119119 
    120 bool cmp(void *a, void *b) 
    121 { 
    122         if ( (uint32_t)a == (uint32_t)b ) 
    123                 return true; 
    124  
    125         return false; 
    126 } 
    127  
    128 uint32_t hash(void *key) 
    129 { 
    130         uint32_t ukey = (uint32_t)key; 
    131         ukey++; 
    132         return ukey; 
    133 } 
    134  
    135 bool string_cmp(void *a, void *b) 
    136 { 
    137         if ( strcmp(a, b)  ) 
    138                 return true; 
    139  
    140         return false; 
    141 } 
    142  
    143 uint32_t string_hash(void *key) 
    144 { 
    145     uint32_t hash = 0; 
    146     char *c = (char *)key; 
    147         uint8_t num = 0x13; 
    148  
    149         while (*c != 0) 
    150         { 
    151                 hash = hash << (32-num) | hash >> (num); 
    152 //              hash = hash >> (32-num) | hash << (num); 
    153                 hash += *c; 
    154                 c++; 
    155         } 
    156  
    157     return hash; 
    158 } 
    159  
    160  
    161  
    162  
    163120int graph_draw(struct emu_graph *graph); 
    164121 
     
    252209        { 
    253210                graph = emu_graph_new(); 
    254                 eh = emu_hashtable_new(2047, hash, cmp); 
     211                eh = emu_hashtable_new(2047, emu_hashtable_ptr_hash, emu_hashtable_ptr_cmp); 
    255212        } 
    256213 
     
    803760        opts.offset = 0; 
    804761 
    805         opts.override.commands.commands = emu_hashtable_new(16, string_hash, string_cmp); 
     762        opts.override.commands.commands = emu_hashtable_new(16, emu_hashtable_string_hash, emu_hashtable_string_cmp); 
    806763 
    807764        while ( 1 )