标签:code arrays 构造 tab 关键字 stat struct 位置 数组
struct ram_tuple *ram_hash[256];
#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
static unsigned int hash(const char *s) { unsigned int hash = 0; while(*s) { hash = 31 * hash + *s++; } return hash; }
char *_ram_get(const char *name) { unsigned int i; struct ram_tuple *t; char *value; if(name == NULL) return NULL; i = hash(name) % ARRAYSIZE(ram_hash); for(t=ram_hash[i]; t && strcmp(t->name, name); t=t->next); value = t ? t->value : NULL; return value; }
void _ram_free(void) { unsigned int i; struct ram_tuple *t, *next; for(i = 0; i < ARRAYSIZE(ram_hash); i++) { for(t= ram_hash[i]; t; t=next) { next = t->next; free(t->name); free(t->value); free(t); } ram_hash[i] = NULL; } }
int _ram_set(const char *name, const char *value) { unsigned int i; struct ram_tuple *t, *u, **prev; i = hash(name) % ARRAYSIZE(ram_hash); for(prev = &ram_hash[i], t = *prev; t && strcmp(t->name, name); prev = &(t->next), t = *prev); if( (u = _ram_realloc(t, name, value)) == NULL) { return -12; /* -ENOMEM*/ } if(t && t == u) { return 0; } u->next = ram_hash[i]; ram_hash[i] = u; return 0; }
标签:code arrays 构造 tab 关键字 stat struct 位置 数组
原文地址:http://www.cnblogs.com/embedded-linux/p/6106478.html