Changeset 59e450c65c2885ee922061f292965817dc713d66
- Timestamp:
- 08/06/09 06:40:19 (3 years ago)
- Author:
- Philip Herron <herron.philip@…>
- Parents:
- 7153655a73260d072e02aa583e2f4294cdda31f5
- Children:
- ba029258d080b283d5f177522a2c61ea35f387ed
- git-committer:
- Philip Herron <herron.philip@googlemail.com> / 2009-08-06T06:40:19Z+0100
- Message:
-
On branch master
Working hash tables based off git-core :) thanks!
modified: COPYRIGHT
modified: src/crulesParser.y
modified: src/gc.c
modified: src/hashTable.c
modified: src/symbols.h
modified: src/util.c
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
rdcf9f50
|
r59e450c
|
|
| 5 | 5 | Contact us (see: AUTHORS) and we'll add you. |
| 6 | 6 | |
| 7 | | Philip Herron |
| | 7 | Original Developer: |
| | 8 | |
| | 9 | * Philip Herron - herron.philip@googlemail.com |
| 8 | 10 | |
| 9 | 11 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
-
|
ra270d0b
|
r59e450c
|
|
| 37 | 37 | _SYM_STACK_ *_BLK_STK_PTR_= NULL; |
| 38 | 38 | |
| | 39 | /* Declare the tables */ |
| | 40 | extern struct hash_table *_func_table_; |
| | 41 | extern struct hash_table *_rule_table_; |
| | 42 | //Variable table stack |
| | 43 | extern struct hash_tbl_stk *stk_table; |
| | 44 | |
| 39 | 45 | //to prototype to get rid of gcc warnings... |
| 40 | 46 | extern int yylex( ); |
-
|
r7153655
|
r59e450c
|
|
| 33 | 33 | #include <sys/types.h> |
| 34 | 34 | |
| 35 | | #include <pthread.h> |
| 36 | | |
| 37 | 35 | #include "crules.h" |
| 38 | 36 | #include "symbols.h" |
| 39 | 37 | |
| 40 | 38 | /* Declare the tables */ |
| 41 | | extern _hash_table_ *_func_table_; |
| 42 | | extern _hash_table_ *_rule_table_; |
| | 39 | extern struct hash_table *_func_table_; |
| | 40 | extern struct hash_table *_rule_table_; |
| 43 | 41 | //Variable table stack |
| 44 | 42 | extern struct hash_tbl_stk *stk_table; |
| 45 | 43 | |
| 46 | | static void freesymbol( struct symtab *sym ); |
| | 44 | static void free_symbol( struct symtab *sym ); |
| | 45 | |
| | 46 | /* Invoke the GC to search for garbage flags in symbol tables */ |
| | 47 | int invoke_gc() |
| | 48 | { |
| | 49 | error("Not implemented yet!\n\n"); |
| | 50 | return ERROR; |
| | 51 | } |
| | 52 | |
| | 53 | /*Cleanup the program for exit!*/ |
| | 54 | int cleanup( ) |
| | 55 | { |
| | 56 | error("Not implemented yet!\n\n"); |
| | 57 | return ERROR; |
| | 58 | } |
| | 59 | |
| | 60 | /*Push any symbols into the garbage table*/ |
| | 61 | void push_garbage( struct symtab **sym ) |
| | 62 | { |
| | 63 | error("Not implemented yet!\n\n"); |
| | 64 | } |
| | 65 | |
| | 66 | static |
| | 67 | void free_symbol( struct symtab *sym ) |
| | 68 | { |
| | 69 | error("not implemented yet!\n\n"); |
| | 70 | xfree( sym ); |
| | 71 | } |
| | 72 | |
| | 73 | void free_hash_stk_node( struct hash_tbl_stk* node ) |
| | 74 | { |
| | 75 | //.. |
| | 76 | } |
| 47 | 77 | |
| 48 | 78 | /** |
| 49 | 79 | *\param stack the stack you want to free |
| 50 | 80 | **/ |
| 51 | | void freeStack( _SYM_STACK_** stack ) |
| | 81 | void free_stack( _SYM_STACK_** stack ) |
| 52 | 82 | { |
| 53 | 83 | _SYM_STACK_* curr= (*stack); |
| … |
… |
|
| 58 | 88 | if( curr ) |
| 59 | 89 | { |
| 60 | | freesymbol( curr->symbol ); |
| | 90 | free_symbol( curr->symbol ); |
| 61 | 91 | xfree( curr ); |
| 62 | 92 | } |
| … |
… |
|
| 70 | 100 | *\return returns macro SUCCESS or ERROR from crules.h |
| 71 | 101 | **/ |
| 72 | | int freeList( _SYM_LIST_ **node ) |
| | 102 | int free_list( _SYM_LIST_ **node ) |
| 73 | 103 | { |
| 74 | 104 | if( *node ) |
| … |
… |
|
| 99 | 129 | } |
| 100 | 130 | |
| 101 | | /* Invoke the GC to search for garbage flags in symbol tables */ |
| 102 | | int invoke_gc() |
| | 131 | void free_hash_table( struct hash_table *table ) |
| 103 | 132 | { |
| 104 | | error("Not implemented yet!\n\n"); |
| 105 | | return ERROR; |
| | 133 | if( table->array ) |
| | 134 | { |
| | 135 | //go through the array and free symbols |
| | 136 | uint32_t i; |
| | 137 | struct hash_entry *array= table->array; |
| | 138 | for( i=0; i<table->size; ++i ) |
| | 139 | { |
| | 140 | struct symtab *sym= array[i].symbol; |
| | 141 | if( sym ) |
| | 142 | free_symbol(sym); |
| | 143 | } |
| | 144 | xfree(table->array); |
| | 145 | table->array = NULL; |
| | 146 | table->size = 0; |
| | 147 | table->nr = 0; |
| | 148 | } |
| | 149 | else |
| | 150 | { |
| | 151 | error("Warning: null hash table!\n\n"); |
| | 152 | } |
| 106 | 153 | } |
| 107 | | |
| 108 | | /*Cleanup the program for exit!*/ |
| 109 | | int cleanup( ) |
| 110 | | { |
| 111 | | error("Not implemented yet!\n\n"); |
| 112 | | return ERROR; |
| 113 | | } |
| 114 | | |
| 115 | | /*Push any symbols into the garbage table*/ |
| 116 | | void push_garbage( struct symtab **sym ) |
| 117 | | { |
| 118 | | error("Not implemented yet!\n\n"); |
| 119 | | } |
| 120 | | |
| 121 | | static |
| 122 | | void freesymbol( struct symtab *sym ) |
| 123 | | { |
| 124 | | error("not implemented yet!\n\n"); |
| 125 | | xfree( sym ); |
| 126 | | } |
| 127 | | |
| 128 | | void pushStkTable_garbage( struct hash_tbl_stk* node ) |
| 129 | | { |
| 130 | | //.. |
| 131 | | } |
-
|
r7153655
|
r59e450c
|
|
| 2 | 2 | * hashTable.c -> Part of Crules Programming language |
| 3 | 3 | * hashTable implementation used for a many different things |
| | 4 | * |
| | 5 | * Based of hash.c from Git-core! |
| 4 | 6 | * |
| 5 | 7 | * Crules is the legal property of its developers. Please refer to the |
| … |
… |
|
| 37 | 39 | #include "symbols.h" |
| 38 | 40 | |
| | 41 | //threshold size alloctor to scale faster |
| | 42 | #define threshold_alloc(x) (((x)+16)*3/2) |
| | 43 | |
| 39 | 44 | /* Declare the tables */ |
| 40 | | _hash_table_ *_func_table_; |
| 41 | | _hash_table_ *_rule_table_; |
| | 45 | struct hash_table *_func_table_; |
| | 46 | struct hash_table *_rule_table_; |
| 42 | 47 | //Variable table stack |
| 43 | 48 | struct hash_tbl_stk *stk_table; |
| | 49 | |
| | 50 | static void grow_hash_table( struct hash_table* table ); |
| 44 | 51 | |
| 45 | 52 | /** |
| … |
… |
|
| 51 | 58 | * http://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function |
| 52 | 59 | **/ |
| 53 | | uint32_t hash( unsigned char *str ) |
| | 60 | uint32_t hash( const char *str ) |
| 54 | 61 | { |
| 55 | 62 | uint32_t hash= 0x811C9DC5, i; |
| 56 | | for( i=0; i<strlen(str); ++i ) |
| | 63 | while( *str != '\0' ) |
| 57 | 64 | { |
| 58 | | uint8_t oct= str[i]; |
| 59 | | hash^= oct; |
| | 65 | hash^= (uint8_t) *str; |
| 60 | 66 | hash= hash*(0x1000193); |
| | 67 | *str++; |
| 61 | 68 | } |
| 62 | 69 | return hash; |
| … |
… |
|
| 75 | 82 | **/ |
| 76 | 83 | |
| 77 | | struct hash_tbl_stk* const_tbl_stk_node( ) |
| | 84 | struct hash_entry* |
| | 85 | lookup_table( struct hash_table *table, |
| | 86 | uint32_t hash ) |
| 78 | 87 | { |
| 79 | | return NULL; |
| | 88 | if( table->array ) |
| | 89 | { |
| | 90 | uint32_t size= table->size, nr= hash%size; |
| | 91 | printf("nr == %i\n", nr); |
| | 92 | struct hash_entry *array= table->array; |
| | 93 | |
| | 94 | while( array[nr].symbol ) |
| | 95 | { |
| | 96 | printf("Pass, nr = %i\n", nr); |
| | 97 | if (array[nr].hash == hash) |
| | 98 | break; |
| | 99 | nr++; |
| | 100 | if (nr >= size) |
| | 101 | nr = 0; |
| | 102 | } |
| | 103 | return array+nr; |
| | 104 | } |
| | 105 | else |
| | 106 | { |
| | 107 | fatal("FATAL: looking up null table!\n\n"); |
| | 108 | return NULL; |
| | 109 | } |
| 80 | 110 | } |
| 81 | 111 | |
| 82 | | bool search_table_glb( const char* identifier, |
| 83 | | uint8_t table ) |
| | 112 | struct symtab** insert_hash( uint32_t hash, struct symtab* sym, |
| | 113 | struct hash_table *table ) |
| 84 | 114 | { |
| 85 | | return false; |
| | 115 | uint32_t nr= table->nr; |
| | 116 | if( nr >= table->size/2 ) |
| | 117 | grow_hash_table( table ); |
| | 118 | |
| | 119 | struct hash_entry *entry= lookup_table(table, hash); |
| | 120 | if( entry->symbol ) |
| | 121 | return &entry->symbol; |
| | 122 | else |
| | 123 | { |
| | 124 | entry->symbol = sym; |
| | 125 | entry->hash = hash; |
| | 126 | table->nr++; |
| | 127 | return NULL; |
| | 128 | } |
| 86 | 129 | } |
| 87 | 130 | |
| 88 | | struct symtab* get_symbol_glb( const char* identifier, |
| 89 | | uint8_t table ) |
| | 131 | static |
| | 132 | void grow_hash_table(struct hash_table* table ) |
| 90 | 133 | { |
| 91 | | return NULL; |
| | 134 | uint32_t prev_size= table->size, size= 0, i= 0; |
| | 135 | struct hash_entry *prev_array= table->array, *array; |
| | 136 | |
| | 137 | size= threshold_alloc( prev_size ); |
| | 138 | array= (struct hash_entry*) |
| | 139 | xcalloc( size, sizeof(struct hash_entry) ); |
| | 140 | |
| | 141 | //reset nr |
| | 142 | table->nr = 0; |
| | 143 | table->size= size; |
| | 144 | table->array= array; |
| | 145 | |
| | 146 | for ( i= 0; i<prev_size; i++ ) |
| | 147 | { |
| | 148 | uint32_t hash= prev_array[i].hash; |
| | 149 | struct symtab *sym= prev_array[i].symbol; |
| | 150 | |
| | 151 | if ( sym ) |
| | 152 | insert_hash(hash, sym, table); |
| | 153 | } |
| | 154 | |
| | 155 | if( prev_array ) |
| | 156 | xfree(prev_array); |
| 92 | 157 | } |
| 93 | | |
| 94 | | bool search_table( const char* identifier, |
| 95 | | _hash_table_ *table ) |
| 96 | | { |
| 97 | | return false; |
| 98 | | } |
| 99 | | |
| 100 | | struct symtab* get_symbol( const char* identifier, |
| 101 | | _hash_table_ *table ) |
| 102 | | { |
| 103 | | return NULL; |
| 104 | | } |
| 105 | | |
| 106 | | void push_symbol( struct symtab* sym, |
| 107 | | _hash_table_ *table ) |
| 108 | | { |
| 109 | | } |
| 110 | | |
| 111 | | struct hash_tbl_stk* |
| 112 | | pop_stk_table( struct hash_tbl_stk *tbl_stk ) |
| 113 | | { |
| 114 | | return NULL; |
| 115 | | } |
-
|
r7153655
|
r59e450c
|
|
| 68 | 68 | #define _STK_TYPE_ 7 |
| 69 | 69 | |
| | 70 | /* === === === === === === */ |
| | 71 | //table defines |
| | 72 | #define _FUNC_TBL_ 111 |
| | 73 | #define _RULE_TBL_ 222 |
| | 74 | |
| 70 | 75 | /* This is the list type */ |
| 71 | 76 | struct list_sym_node { |
| … |
… |
|
| 111 | 116 | typedef struct symstack _SYM_STACK_; |
| 112 | 117 | |
| 113 | | struct hash_tbl_entry { |
| 114 | | unsigned long hash; |
| | 118 | struct hash_entry { |
| | 119 | uint32_t hash; |
| 115 | 120 | struct symtab *symbol; |
| 116 | 121 | } __attribute__((aligned)); |
| 117 | | typedef struct hash_tbl_entry _hash_table_; |
| | 122 | |
| | 123 | struct hash_table { |
| | 124 | uint32_t size, nr; |
| | 125 | struct hash_entry *array; |
| | 126 | } __attribute__((aligned)); |
| 118 | 127 | |
| 119 | 128 | struct hash_tbl_stk { |
| 120 | 129 | uint32_t size; |
| 121 | | struct hash_tbl_entry *table; |
| | 130 | struct hash_tbl *table; |
| 122 | 131 | struct hash_tbl_stk *next; |
| 123 | 132 | } __attribute__((aligned)); |
| … |
… |
|
| 127 | 136 | extern struct symstack* pop( _SYM_STACK_** stack ); |
| 128 | 137 | extern void push( _SYM_STACK_** stack, struct symtab *sym ); |
| 129 | | extern void freeStack( _SYM_STACK_** stack ); |
| | 138 | extern void free_stack( _SYM_STACK_** stack ); |
| 130 | 139 | |
| 131 | 140 | /* hashTable.c */ |
| 132 | 141 | //32-bit hash function |
| 133 | | extern uint32_t hash( unsigned char *str ); |
| 134 | | extern struct hash_tbl_stk* const_tbl_stk_node( ); |
| | 142 | extern uint32_t hash( const char *str ); |
| | 143 | extern struct symtab** insert_hash( uint32_t hash, struct symtab* sym, |
| | 144 | struct hash_table *table ); |
| | 145 | extern struct hash_entry* lookup_table( struct hash_table *table, |
| | 146 | uint32_t hash ); |
| 135 | 147 | |
| 136 | | extern bool search_table_glb( const char* identifier, |
| 137 | | uint8_t table ); |
| 138 | | extern struct symtab* get_symbol_glb( const char* identifier, |
| 139 | | uint8_t table ); |
| | 148 | static inline void init_hash_table( struct hash_table *table ) |
| | 149 | { |
| | 150 | table->size= 0; |
| | 151 | table->nr= 0; |
| | 152 | table->array= NULL; |
| | 153 | } |
| 140 | 154 | |
| 141 | | extern bool search_table( const char* identifier, |
| 142 | | _hash_table_ *table ); |
| 143 | | extern struct symtab* get_symbol( const char* identifier, |
| 144 | | _hash_table_ *table ); |
| 145 | | extern void push_symbol( struct symtab* sym, |
| 146 | | _hash_table_ *table ); |
| 147 | | extern struct hash_tbl_stk* pop_stk_table( struct hash_tbl_stk *tbl_stk ); |
| 148 | 155 | |
| 149 | 156 | /* listType.c */ |
| … |
… |
|
| 153 | 160 | extern _SYM_LIST_* constructListNode( ); |
| 154 | 161 | extern void deleteListItem( uint32_t point, _SYM_LIST_** node ); |
| | 162 | int free_list( _SYM_LIST_ **node ); |
| 155 | 163 | |
| 156 | 164 | /* symTab.c */ |
| | 165 | //... |
| 157 | 166 | |
| 158 | 167 | #endif //__SYMBOLS_H_ |
-
|
r7153655
|
r59e450c
|
|
| 34 | 34 | #include "symbols.h" |
| 35 | 35 | |
| 36 | | #define _DEFAULT_TBL_SIZE_ 64 |
| 37 | | |
| 38 | 36 | /* Declare the tables */ |
| 39 | | extern _hash_table_ *_func_table_; |
| 40 | | extern _hash_table_ *_rule_table_; |
| | 37 | extern struct hash_table _func_table_; |
| | 38 | extern struct hash_table _rule_table_; |
| 41 | 39 | //Variable table stack |
| 42 | 40 | extern struct hash_tbl_stk *stk_table; |
| … |
… |
|
| 48 | 46 | int init_tables( ) |
| 49 | 47 | { |
| 50 | | _func_table_= (_hash_table_*) |
| 51 | | xcalloc( _DEFAULT_TBL_SIZE_, sizeof(_hash_table_) ); |
| 52 | | _rule_table_= (_hash_table_*) |
| 53 | | xcalloc( _DEFAULT_TBL_SIZE_, sizeof(_hash_table_) ); |
| 54 | | |
| 55 | | //stk_table= |
| | 48 | init_hash_table( &_func_table_ ); |
| | 49 | init_hash_table( &_rule_table_ ); |
| | 50 | //... |
| 56 | 51 | |
| 57 | 52 | return SUCCESS; |