From 312947b34f2827ca163e359e9d68bae7aa5fe945 Mon Sep 17 00:00:00 2001 From: fc_botelho Date: Mon, 10 Oct 2005 17:43:21 +0000 Subject: [PATCH] thread-safe vector adapter --- examples/file_adapter_ex2.c | 2 +- examples/vector_adapter_ex1.c | 2 +- src/cmph.c | 81 +++++++++++++++++++++++++---------- src/cmph.h | 5 +++ tests/graph_tests.c | 6 +-- 5 files changed, 68 insertions(+), 28 deletions(-) diff --git a/examples/file_adapter_ex2.c b/examples/file_adapter_ex2.c index 57ec5b6..92a9770 100644 --- a/examples/file_adapter_ex2.c +++ b/examples/file_adapter_ex2.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) fprintf(stderr, "Id:%u\n", id); //Destroy hash cmph_destroy(hash); - free(source); + cmph_io_nlfile_adapter_destroy(source); fclose(keys_fd); return 0; } diff --git a/examples/vector_adapter_ex1.c b/examples/vector_adapter_ex1.c index 1ae8abb..3e0bd85 100755 --- a/examples/vector_adapter_ex1.c +++ b/examples/vector_adapter_ex1.c @@ -21,6 +21,6 @@ int main(int argc, char **argv) fprintf(stderr, "Id:%u\n", id); //Destroy hash cmph_destroy(hash); - free(source); + cmph_io_vector_adapter_destroy(source); return 0; } diff --git a/src/cmph.c b/src/cmph.c index d03bc74..07e888a 100644 --- a/src/cmph.c +++ b/src/cmph.c @@ -13,7 +13,14 @@ const char *cmph_names[] = { "bmz", "bmz8", "chm", "brz", NULL }; /* included -- Fabiano */ -static cmph_uint32 position; // access position when data is a vector +typedef struct +{ + void *vector; + cmph_uint32 position; // access position when data is a vector +} cmph_vector_t; + +static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys); +static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source); static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen) { @@ -42,19 +49,14 @@ static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen) static int key_vector_read(void *data, char **key, cmph_uint32 *keylen) { - char **keys_vd = (char **)data; - if (keys_vd + position == NULL) return -1; - *keylen = strlen(*(keys_vd + position)); - *key = (char *)malloc(*keylen + 1); - strcpy(*key, *(keys_vd + position)); - position ++; + cmph_vector_t *cmph_vector = (cmph_vector_t *)data; + char **keys_vd = (char **)cmph_vector->vector; -/* char **keys_vd = (char **)data; - if (keys_vd[position] == NULL) return -1; - *keylen = strlen(keys_vd[position]); + if (keys_vd + cmph_vector->position == NULL) return -1; + *keylen = strlen(*(keys_vd + cmph_vector->position)); *key = (char *)malloc(*keylen + 1); - strcpy(*key, keys_vd[position]); - position ++;*/ + strcpy(*key, *(keys_vd + cmph_vector->position)); + cmph_vector->position = cmph_vector->position + 1; return *keylen; } @@ -66,7 +68,7 @@ static void key_nlfile_dispose(void *data, char *key, cmph_uint32 keylen) static void key_vector_dispose(void *data, char *key, cmph_uint32 keylen) { - free(key); + key_nlfile_dispose(data, key, keylen); } static void key_nlfile_rewind(void *data) @@ -77,10 +79,10 @@ static void key_nlfile_rewind(void *data) static void key_vector_rewind(void *data) { - position = 0; + cmph_vector_t *cmph_vector = (cmph_vector_t *)data; + cmph_vector->position = 0; } - static cmph_uint32 count_nlfile_keys(FILE *fd) { cmph_uint32 count = 0; @@ -109,6 +111,11 @@ cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd) return key_source; } +void cmph_io_nlfile_adapter_destroy(cmph_io_adapter_t * key_source) +{ + free(key_source); +} + cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys) { cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t)); @@ -121,16 +128,44 @@ cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys) return key_source; } +void cmph_io_nlnkfile_adapter_destroy(cmph_io_adapter_t * key_source) +{ + free(key_source); +} + +static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys) +{ + cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t)); + cmph_vector_t * cmph_vector = malloc(sizeof(cmph_vector_t)); + assert(key_source); + assert(cmph_vector); + cmph_vector->vector = vector; + cmph_vector->position = 0; + key_source->data = (void *)cmph_vector; + key_source->nkeys = nkeys; + return key_source; +} + +static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source) +{ + cmph_vector_t *cmph_vector = (cmph_vector_t *)key_source->data; + cmph_vector->vector = NULL; + free(cmph_vector); + free(key_source); +} + cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys) { - cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t)); - assert(key_source); - key_source->data = (void *)vector; - key_source->nkeys = nkeys; - key_source->read = key_vector_read; - key_source->dispose = key_vector_dispose; - key_source->rewind = key_vector_rewind; - return key_source; + cmph_io_adapter_t * key_source = cmph_io_vector_new(vector, nkeys); + key_source->read = key_vector_read; + key_source->dispose = key_vector_dispose; + key_source->rewind = key_vector_rewind; + return key_source; +} + +void cmph_io_vector_adapter_destroy(cmph_io_adapter_t * key_source) +{ + cmph_io_vector_destroy(key_source); } cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source) diff --git a/src/cmph.h b/src/cmph.h index 88ed317..50b2d66 100644 --- a/src/cmph.h +++ b/src/cmph.h @@ -26,8 +26,13 @@ typedef struct /** Adapter pattern API **/ /* please call free() in the created adapters */ cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd); +void cmph_io_nlfile_adapter_destroy(cmph_io_adapter_t * key_source); + cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys); +void cmph_io_nlnkfile_adapter_destroy(cmph_io_adapter_t * key_source); + cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys); +void cmph_io_vector_adapter_destroy(cmph_io_adapter_t * key_source); /** Hash configuration API **/ cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source); diff --git a/tests/graph_tests.c b/tests/graph_tests.c index 39e9b3e..74dd32e 100644 --- a/tests/graph_tests.c +++ b/tests/graph_tests.c @@ -7,7 +7,7 @@ int main(int argc, char **argv) { graph_iterator_t it; cmph_uint32 i, neighbor; - graph_t *g = graph_new(5, 10, 0); + graph_t *g = graph_new(5, 10); fprintf(stderr, "Building random graph\n"); for (i = 0; i < 10; ++i) @@ -26,7 +26,7 @@ int main(int argc, char **argv) graph_destroy(g); fprintf(stderr, "Building cyclic graph\n"); - g = graph_new(4, 5, 0); + g = graph_new(4, 5); graph_add_edge(g, 0, 3); graph_add_edge(g, 0, 1); graph_add_edge(g, 1, 2); @@ -38,7 +38,7 @@ int main(int argc, char **argv) graph_destroy(g); fprintf(stderr, "Building non-cyclic graph\n"); - g = graph_new(5, 4, 0); + g = graph_new(5, 4); graph_add_edge(g, 0, 1); graph_add_edge(g, 1, 2); graph_add_edge(g, 2, 3);