split array and hashtable code out into a new file
authorDan Winship <danw@gnome.org>
Thu Feb 07 09:58:48 2008 -0500 (2008-02-07)
changeset 11526edeea5c95a
parent 114 1d54922ff920
child 116 4ec6e2a55c34
split array and hashtable code out into a new file
Makefile
razor.c
types.c
types.h
     1.1 --- a/Makefile	Wed Feb 06 13:02:50 2008 -0500
     1.2 +++ b/Makefile	Thu Feb 07 09:58:48 2008 -0500
     1.3 @@ -3,11 +3,12 @@
     1.4  
     1.5  all : razor test-driver
     1.6  
     1.7 -razor : razor.o yum.o main.o rpm.o util.o
     1.8 +razor : razor.o yum.o main.o rpm.o types.o util.o
     1.9  
    1.10  *.o : razor.h razor-internal.h
    1.11 +razor.o : types.h
    1.12  
    1.13 -test-driver : razor.o util.o test-driver.o
    1.14 +test-driver : razor.o types.o util.o test-driver.o
    1.15  
    1.16  test : test-driver
    1.17  	./test-driver test.xml
     2.1 --- a/razor.c	Wed Feb 06 13:02:50 2008 -0500
     2.2 +++ b/razor.c	Thu Feb 07 09:58:48 2008 -0500
     2.3 @@ -16,11 +16,7 @@
     2.4  
     2.5  #include "razor.h"
     2.6  #include "razor-internal.h"
     2.7 -
     2.8 -struct array {
     2.9 -	void *data;
    2.10 -	int size, alloc;
    2.11 -};
    2.12 +#include "types.h"
    2.13  
    2.14  struct razor_set_section {
    2.15  	uint32_t type;
    2.16 @@ -92,11 +88,6 @@
    2.17  	struct import_directory *last;
    2.18  };
    2.19  
    2.20 -struct hashtable {
    2.21 -	struct array buckets;
    2.22 -	struct array *pool;
    2.23 -};
    2.24 -
    2.25  struct razor_importer {
    2.26  	struct razor_set *set;
    2.27  	struct hashtable table;
    2.28 @@ -105,46 +96,6 @@
    2.29  	struct array files;
    2.30  };
    2.31  
    2.32 -static void
    2.33 -array_init(struct array *array)
    2.34 -{
    2.35 -	memset(array, 0, sizeof *array);
    2.36 -}
    2.37 -
    2.38 -static void
    2.39 -array_release(struct array *array)
    2.40 -{
    2.41 -	free(array->data);
    2.42 -}
    2.43 -
    2.44 -static void *
    2.45 -array_add(struct array *array, int size)
    2.46 -{
    2.47 -	int alloc;
    2.48 -	void *data, *p;
    2.49 -
    2.50 -	if (array->alloc > 0)
    2.51 -		alloc = array->alloc;
    2.52 -	else
    2.53 -		alloc = 16;
    2.54 -
    2.55 -	while (alloc < array->size + size)
    2.56 -		alloc *= 2;
    2.57 -
    2.58 -	if (array->alloc < alloc) {
    2.59 -		data = realloc(array->data, alloc);
    2.60 -		if (data == NULL)
    2.61 -			return 0;
    2.62 -		array->data = data;
    2.63 -		array->alloc = alloc;
    2.64 -	}
    2.65 -
    2.66 -	p = array->data + array->size;
    2.67 -	array->size += size;
    2.68 -
    2.69 -	return p;
    2.70 -}
    2.71 -
    2.72  static void *
    2.73  zalloc(size_t size)
    2.74  {
    2.75 @@ -275,55 +226,6 @@
    2.76  	return 0;
    2.77  }
    2.78  
    2.79 -static unsigned int
    2.80 -hash_string(const char *key)
    2.81 -{
    2.82 -	const char *p;
    2.83 -	unsigned int hash = 0;
    2.84 -
    2.85 -	for (p = key; *p; p++)
    2.86 -		hash = (hash * 617) ^ *p;
    2.87 -
    2.88 -	return hash;
    2.89 -}
    2.90 -
    2.91 -static uint32_t
    2.92 -hashtable_lookup(struct hashtable *table, const char *key)
    2.93 -{
    2.94 -	unsigned int mask, start, i;
    2.95 -	uint32_t *b;
    2.96 -	char *pool;
    2.97 -
    2.98 -	pool = table->pool->data;
    2.99 -	mask = table->buckets.alloc - 1;
   2.100 -	start = hash_string(key) * sizeof(uint32_t);
   2.101 -
   2.102 -	for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
   2.103 -		b = table->buckets.data + ((start + i) & mask);
   2.104 -
   2.105 -		if (*b == 0)
   2.106 -			return 0;
   2.107 -
   2.108 -		if (strcmp(key, &pool[*b]) == 0)
   2.109 -			return *b;
   2.110 -	}
   2.111 -
   2.112 -	return 0;
   2.113 -}
   2.114 -
   2.115 -static uint32_t
   2.116 -add_to_string_pool(struct hashtable *table, const char *key)
   2.117 -{
   2.118 -	int len;
   2.119 -	char *p;
   2.120 -
   2.121 -	len = strlen(key) + 1;
   2.122 -	p = array_add(table->pool, len);
   2.123 -	memcpy(p, key, len);
   2.124 -
   2.125 -	return p - (char *) table->pool->data;
   2.126 -}
   2.127 -
   2.128  static uint32_t
   2.129  add_to_property_pool(struct array *pool, struct array *properties)
   2.130  {
   2.131 @@ -341,80 +243,6 @@
   2.132  	return p - (uint32_t *) pool->data;
   2.133  }
   2.134  
   2.135 -static void
   2.136 -do_insert(struct hashtable *table, uint32_t value)
   2.137 -{
   2.138 -	unsigned int mask, start, i;
   2.139 -	uint32_t *b;
   2.140 -	const char *key;
   2.141 -
   2.142 -	key = (char *) table->pool->data + value;
   2.143 -	mask = table->buckets.alloc - 1;
   2.144 -	start = hash_string(key) * sizeof(uint32_t);
   2.145 -
   2.146 -	for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
   2.147 -		b = table->buckets.data + ((start + i) & mask);
   2.148 -		if (*b == 0) {
   2.149 -			*b = value;
   2.150 -			break;
   2.151 -		}
   2.152 -	}
   2.153 -}
   2.154 -
   2.155 -static uint32_t
   2.156 -hashtable_insert(struct hashtable *table, const char *key)
   2.157 -{
   2.158 -	uint32_t value, *buckets, *b, *end;
   2.159 -	int alloc;
   2.160 -
   2.161 -	alloc = table->buckets.alloc;
   2.162 -	array_add(&table->buckets, 4 * sizeof *buckets);
   2.163 -	if (alloc != table->buckets.alloc) {
   2.164 -		end = table->buckets.data + alloc;
   2.165 -		memset(end, 0, table->buckets.alloc - alloc);
   2.166 -		for (b = table->buckets.data; b < end; b++) {
   2.167 -			value = *b;
   2.168 -			if (value != 0) {
   2.169 -				*b = 0;
   2.170 -				do_insert(table, value);
   2.171 -			}
   2.172 -		}
   2.173 -	}
   2.174 -
   2.175 -	value = add_to_string_pool(table, key);
   2.176 -	do_insert (table, value);
   2.177 -
   2.178 -	return value;
   2.179 -}
   2.180 -
   2.181 -static void
   2.182 -hashtable_init(struct hashtable *table, struct array *pool)
   2.183 -{
   2.184 -	array_init(&table->buckets);
   2.185 -	table->pool = pool;
   2.186 -}
   2.187 -
   2.188 -static void
   2.189 -hashtable_release(struct hashtable *table)
   2.190 -{
   2.191 -	array_release(&table->buckets);
   2.192 -}
   2.193 -
   2.194 -static uint32_t
   2.195 -hashtable_tokenize(struct hashtable *table, const char *string)
   2.196 -{
   2.197 -	uint32_t token;
   2.198 -
   2.199 -	if (string == NULL)
   2.200 -		string = "";
   2.201 -
   2.202 -	token = hashtable_lookup(table, string);
   2.203 -	if (token != 0)
   2.204 -		return token;
   2.205 -
   2.206 -	return hashtable_insert(table, string);
   2.207 -}
   2.208 -
   2.209  void
   2.210  razor_importer_begin_package(struct razor_importer *importer,
   2.211  			     const char *name, const char *version)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/types.c	Thu Feb 07 09:58:48 2008 -0500
     3.3 @@ -0,0 +1,168 @@
     3.4 +#include <stdlib.h>
     3.5 +#include <string.h>
     3.6 +
     3.7 +#include "types.h"
     3.8 +
     3.9 +void
    3.10 +array_init(struct array *array)
    3.11 +{
    3.12 +	memset(array, 0, sizeof *array);
    3.13 +}
    3.14 +
    3.15 +void
    3.16 +array_release(struct array *array)
    3.17 +{
    3.18 +	free(array->data);
    3.19 +}
    3.20 +
    3.21 +void *
    3.22 +array_add(struct array *array, int size)
    3.23 +{
    3.24 +	int alloc;
    3.25 +	void *data, *p;
    3.26 +
    3.27 +	if (array->alloc > 0)
    3.28 +		alloc = array->alloc;
    3.29 +	else
    3.30 +		alloc = 16;
    3.31 +
    3.32 +	while (alloc < array->size + size)
    3.33 +		alloc *= 2;
    3.34 +
    3.35 +	if (array->alloc < alloc) {
    3.36 +		data = realloc(array->data, alloc);
    3.37 +		if (data == NULL)
    3.38 +			return 0;
    3.39 +		array->data = data;
    3.40 +		array->alloc = alloc;
    3.41 +	}
    3.42 +
    3.43 +	p = array->data + array->size;
    3.44 +	array->size += size;
    3.45 +
    3.46 +	return p;
    3.47 +}
    3.48 +
    3.49 +
    3.50 +void
    3.51 +hashtable_init(struct hashtable *table, struct array *pool)
    3.52 +{
    3.53 +	array_init(&table->buckets);
    3.54 +	table->pool = pool;
    3.55 +}
    3.56 +
    3.57 +void
    3.58 +hashtable_release(struct hashtable *table)
    3.59 +{
    3.60 +	array_release(&table->buckets);
    3.61 +}
    3.62 +
    3.63 +static unsigned int
    3.64 +hash_string(const char *key)
    3.65 +{
    3.66 +	const char *p;
    3.67 +	unsigned int hash = 0;
    3.68 +
    3.69 +	for (p = key; *p; p++)
    3.70 +		hash = (hash * 617) ^ *p;
    3.71 +
    3.72 +	return hash;
    3.73 +}
    3.74 +
    3.75 +uint32_t
    3.76 +hashtable_lookup(struct hashtable *table, const char *key)
    3.77 +{
    3.78 +	unsigned int mask, start, i;
    3.79 +	uint32_t *b;
    3.80 +	char *pool;
    3.81 +
    3.82 +	pool = table->pool->data;
    3.83 +	mask = table->buckets.alloc - 1;
    3.84 +	start = hash_string(key) * sizeof(uint32_t);
    3.85 +
    3.86 +	for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
    3.87 +		b = table->buckets.data + ((start + i) & mask);
    3.88 +
    3.89 +		if (*b == 0)
    3.90 +			return 0;
    3.91 +
    3.92 +		if (strcmp(key, &pool[*b]) == 0)
    3.93 +			return *b;
    3.94 +	}
    3.95 +
    3.96 +	return 0;
    3.97 +}
    3.98 +
    3.99 +static void
   3.100 +do_insert(struct hashtable *table, uint32_t value)
   3.101 +{
   3.102 +	unsigned int mask, start, i;
   3.103 +	uint32_t *b;
   3.104 +	const char *key;
   3.105 +
   3.106 +	key = (char *) table->pool->data + value;
   3.107 +	mask = table->buckets.alloc - 1;
   3.108 +	start = hash_string(key) * sizeof(uint32_t);
   3.109 +
   3.110 +	for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
   3.111 +		b = table->buckets.data + ((start + i) & mask);
   3.112 +		if (*b == 0) {
   3.113 +			*b = value;
   3.114 +			break;
   3.115 +		}
   3.116 +	}
   3.117 +}
   3.118 +
   3.119 +static uint32_t
   3.120 +add_to_string_pool(struct hashtable *table, const char *key)
   3.121 +{
   3.122 +	int len;
   3.123 +	char *p;
   3.124 +
   3.125 +	len = strlen(key) + 1;
   3.126 +	p = array_add(table->pool, len);
   3.127 +	memcpy(p, key, len);
   3.128 +
   3.129 +	return p - (char *) table->pool->data;
   3.130 +}
   3.131 +
   3.132 +uint32_t
   3.133 +hashtable_insert(struct hashtable *table, const char *key)
   3.134 +{
   3.135 +	uint32_t value, *buckets, *b, *end;
   3.136 +	int alloc;
   3.137 +
   3.138 +	alloc = table->buckets.alloc;
   3.139 +	array_add(&table->buckets, 4 * sizeof *buckets);
   3.140 +	if (alloc != table->buckets.alloc) {
   3.141 +		end = table->buckets.data + alloc;
   3.142 +		memset(end, 0, table->buckets.alloc - alloc);
   3.143 +		for (b = table->buckets.data; b < end; b++) {
   3.144 +			value = *b;
   3.145 +			if (value != 0) {
   3.146 +				*b = 0;
   3.147 +				do_insert(table, value);
   3.148 +			}
   3.149 +		}
   3.150 +	}
   3.151 +
   3.152 +	value = add_to_string_pool(table, key);
   3.153 +	do_insert (table, value);
   3.154 +
   3.155 +	return value;
   3.156 +}
   3.157 +
   3.158 +uint32_t
   3.159 +hashtable_tokenize(struct hashtable *table, const char *string)
   3.160 +{
   3.161 +	uint32_t token;
   3.162 +
   3.163 +	if (string == NULL)
   3.164 +		string = "";
   3.165 +
   3.166 +	token = hashtable_lookup(table, string);
   3.167 +	if (token != 0)
   3.168 +		return token;
   3.169 +
   3.170 +	return hashtable_insert(table, string);
   3.171 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/types.h	Thu Feb 07 09:58:48 2008 -0500
     4.3 @@ -0,0 +1,27 @@
     4.4 +#ifndef _RAZOR_TYPES_H_
     4.5 +#define _RAZOR_TYPES_H_
     4.6 +
     4.7 +#include <stdint.h>
     4.8 +
     4.9 +struct array {
    4.10 +	void *data;
    4.11 +	int size, alloc;
    4.12 +};
    4.13 +
    4.14 +void array_init(struct array *array);
    4.15 +void array_release(struct array *array);
    4.16 +void *array_add(struct array *array, int size);
    4.17 +
    4.18 +
    4.19 +struct hashtable {
    4.20 +	struct array buckets;
    4.21 +	struct array *pool;
    4.22 +};
    4.23 +
    4.24 +void hashtable_init(struct hashtable *table, struct array *pool);
    4.25 +void hashtable_release(struct hashtable *table);
    4.26 +uint32_t hashtable_insert(struct hashtable *table, const char *key);
    4.27 +uint32_t hashtable_lookup(struct hashtable *table, const char *key);
    4.28 +uint32_t hashtable_tokenize(struct hashtable *table, const char *string);
    4.29 +
    4.30 +#endif /* _RAZOR_TYPES_H_ */