Kannel: Open Source WAP and SMS gateway  svn-r5335
wshash.c File Reference
#include "wsint.h"
#include "wshash.h"

Go to the source code of this file.

Data Structures

struct  WsHashItemRec
 
struct  WsHashRec
 

Macros

#define WS_HASH_TABLE_SIZE   256
 

Typedefs

typedef struct WsHashItemRec WsHashItem
 

Functions

static size_t count_hash (const char *string)
 
WsHashPtr ws_hash_create (WsHashItemDestructor destructor, void *context)
 
void ws_hash_destroy (WsHashPtr hash)
 
WsBool ws_hash_put (WsHashPtr hash, const char *name, void *data)
 
void * ws_hash_get (WsHashPtr hash, const char *name)
 
void ws_hash_clear (WsHashPtr hash)
 

Macro Definition Documentation

◆ WS_HASH_TABLE_SIZE

#define WS_HASH_TABLE_SIZE   256

Definition at line 76 of file wshash.c.

Referenced by count_hash(), and ws_hash_clear().

Typedef Documentation

◆ WsHashItem

typedef struct WsHashItemRec WsHashItem

Definition at line 86 of file wshash.c.

Function Documentation

◆ count_hash()

static size_t count_hash ( const char *  string)
static

Definition at line 200 of file wshash.c.

References WS_HASH_TABLE_SIZE.

Referenced by ws_hash_get(), and ws_hash_put().

201 {
202  size_t val = 0;
203  int i;
204 
205  for (i = 0; string[i]; i++) {
206  val <<= 3;
207  val ^= string[i];
208  val ^= (val & 0xff00) >> 5;
209  val ^= (val & 0xff0000) >> 16;
210  }
211 
212  return val % WS_HASH_TABLE_SIZE;
213 }
#define WS_HASH_TABLE_SIZE
Definition: wshash.c:76

◆ ws_hash_clear()

void ws_hash_clear ( WsHashPtr  hash)

Definition at line 180 of file wshash.c.

References WsHashItemRec::data, WsHashRec::destructor, WsHashRec::destructor_context, WsHashRec::items, WsHashItemRec::name, WsHashItemRec::next, ws_free(), and WS_HASH_TABLE_SIZE.

Referenced by ws_hash_destroy().

181 {
182  WsHashItem *i, *n;
183  size_t j;
184 
185  for (j = 0; j < WS_HASH_TABLE_SIZE; j++) {
186  for (i = hash->items[j]; i; i = n) {
187  n = i->next;
188  if (hash->destructor)
189  (*hash->destructor)(i->data, hash->destructor_context);
190 
191  ws_free(i->name);
192  ws_free(i);
193  }
194  hash->items[j] = NULL;
195  }
196 }
void ws_free(void *ptr)
Definition: wsalloc.c:139
#define WS_HASH_TABLE_SIZE
Definition: wshash.c:76
WsHashItemDestructor destructor
Definition: wshash.c:92
struct WsHashItemRec * next
Definition: wshash.c:81
WsHashItem * items[WS_HASH_TABLE_SIZE]
Definition: wshash.c:91
void * data
Definition: wshash.c:83
void * destructor_context
Definition: wshash.c:93
char * name
Definition: wshash.c:82

◆ ws_hash_create()

WsHashPtr ws_hash_create ( WsHashItemDestructor  destructor,
void *  context 
)

Definition at line 103 of file wshash.c.

References WsHashRec::destructor, WsHashRec::destructor_context, and ws_calloc().

Referenced by ws_function_hash_create(), ws_pragma_use_hash_create(), and ws_variable_hash_create().

104 {
105  WsHashPtr hash = ws_calloc(1, sizeof(*hash));
106 
107  if (hash) {
108  hash->destructor = destructor;
109  hash->destructor_context = context;
110  }
111 
112  return hash;
113 }
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
Definition: parse.c:65
WsHashItemDestructor destructor
Definition: wshash.c:92
void * destructor_context
Definition: wshash.c:93

◆ ws_hash_destroy()

void ws_hash_destroy ( WsHashPtr  hash)

Definition at line 116 of file wshash.c.

References ws_free(), and ws_hash_clear().

Referenced by compile_stream().

117 {
118  if (hash == NULL)
119  return;
120 
121  ws_hash_clear(hash);
122  ws_free(hash);
123 }
void ws_hash_clear(WsHashPtr hash)
Definition: wshash.c:180
void ws_free(void *ptr)
Definition: wsalloc.c:139

◆ ws_hash_get()

void* ws_hash_get ( WsHashPtr  hash,
const char *  name 
)

Definition at line 167 of file wshash.c.

References count_hash(), WsHashItemRec::data, WsHashRec::items, WsHashItemRec::name, name, and WsHashItemRec::next.

Referenced by ws_expr_linearize(), ws_function_hash(), ws_pragma_use(), ws_variable_define(), and ws_variable_lookup().

168 {
169  WsHashItem *i;
170  size_t h = count_hash(name);
171 
172  for (i = hash->items[h]; i; i = i->next)
173  if (strcmp(i->name, name) == 0)
174  return i->data;
175 
176  return NULL;
177 }
struct WsHashItemRec * next
Definition: wshash.c:81
char * name
Definition: smsc_cimd2.c:212
WsHashItem * items[WS_HASH_TABLE_SIZE]
Definition: wshash.c:91
void * data
Definition: wshash.c:83
static size_t count_hash(const char *string)
Definition: wshash.c:200
char * name
Definition: wshash.c:82

◆ ws_hash_put()

WsBool ws_hash_put ( WsHashPtr  hash,
const char *  name,
void *  data 
)

Definition at line 126 of file wshash.c.

References count_hash(), WsHashItemRec::data, WsHashRec::destructor, WsHashRec::destructor_context, WsHashRec::items, WsHashItemRec::name, name, WsHashItemRec::next, ws_calloc(), WS_FALSE, ws_free(), ws_strdup(), and WS_TRUE.

Referenced by ws_function_hash(), ws_pragma_use(), and ws_variable_define().

127 {
128  WsHashItem *i;
129  size_t h = count_hash(name);
130 
131  for (i = hash->items[h]; i; i = i->next) {
132  if (strcmp(i->name, name) == 0) {
133  /* Found it. */
134 
135  /* Destroy the old item */
136  if (hash->destructor)
137  (*hash->destructor)(i->data, hash->destructor_context);
138 
139  i->data = data;
140 
141  return WS_FALSE;
142  }
143  }
144 
145  /* Must create a new mapping. */
146  i = ws_calloc(1, sizeof(*i));
147 
148  if (i == NULL)
149  return WS_FALSE;
150 
151  i->name = ws_strdup(name);
152  if (i->name == NULL) {
153  ws_free(i);
154  return WS_FALSE;
155  }
156 
157  i->data = data;
158 
159  /* Link it to our hash. */
160  i->next = hash->items[h];
161  hash->items[h] = i;
162 
163  return WS_TRUE;
164 }
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
Definition: wsint.h:131
void ws_free(void *ptr)
Definition: wsalloc.c:139
WsHashItemDestructor destructor
Definition: wshash.c:92
struct WsHashItemRec * next
Definition: wshash.c:81
char * name
Definition: smsc_cimd2.c:212
WsHashItem * items[WS_HASH_TABLE_SIZE]
Definition: wshash.c:91
void * ws_strdup(const char *str)
Definition: wsalloc.c:119
void * data
Definition: wshash.c:83
void * destructor_context
Definition: wshash.c:93
static size_t count_hash(const char *string)
Definition: wshash.c:200
char * name
Definition: wshash.c:82
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.