diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2012-04-11 08:06:11 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2012-04-11 08:06:11 +0200 |
commit | 745588d87d439a2c9ff5acc5cc3dc4a68db57cf9 (patch) | |
tree | bf937191b819910f856520241fa4619273e6bd8e | |
parent | d937bf0d32fc77b60d796faa8baa0b84c77db00b (diff) | |
download | pgfuse-745588d87d439a2c9ff5acc5cc3dc4a68db57cf9.tar.gz pgfuse-745588d87d439a2c9ff5acc5cc3dc4a68db57cf9.tar.bz2 |
no hash structure, using the array with a prime
-rw-r--r-- | Makefile | 15 | ||||
-rw-r--r-- | config.h | 6 | ||||
-rw-r--r-- | hash.c | 143 | ||||
-rw-r--r-- | hash.h | 50 | ||||
-rw-r--r-- | testhash.c | 32 |
5 files changed, 6 insertions, 240 deletions
@@ -13,7 +13,7 @@ CFLAGS += `pkg-config fuse --cflags` LDFLAGS = `pkg-config fuse --libs` -lpq clean: - rm -f pgfuse pgfuse.o pgsql.o hash.o testhash testhash.o + rm -f pgfuse pgfuse.o pgsql.o test: pgfuse psql < test.sql @@ -28,20 +28,11 @@ test: pgfuse -ls -al mnt/dir/dir2 fusermount -u mnt -pgfuse: pgfuse.o pgsql.o hash.o - gcc -o pgfuse $(LDFLAGS) pgfuse.o pgsql.o hash.o +pgfuse: pgfuse.o pgsql.o + gcc -o pgfuse $(LDFLAGS) pgfuse.o pgsql.o pgfuse.o: pgfuse.c pgsql.h gcc -c $(CFLAGS) -o pgfuse.o pgfuse.c pgsql.o: pgsql.c pgsql.h gcc -c $(CFLAGS) -o pgsql.o pgsql.c - -hash.o: hash.c hash.h - gcc -c $(CFLAGS) -o hash.o hash.c - -testhash: testhash.o hash.o - gcc -o testhash $(LDFLAGS) testhash.o hash.o - -testhash.o: testhash.c hash.h - gcc -c $(CFLAGS) -o testhash.o testhash.c @@ -21,9 +21,9 @@ /* standard block size, rather a simulation currently */ #define STANDARD_BLOCK_SIZE 512 -/* maximal number of open files, limited currently due to a too simple - * hash table implementation of open file handles */ -#define MAX_NOF_OPEN_FILES 256 +/* maximal number of open files, may vary as there may be hashtable + * collitions. Must be a prime */ +#define MAX_NOF_OPEN_FILES 257 /* maximum size of a file, rather arbitrary, 2^31 is a current implementation * limit, before fixing this, the storing and efficiency has to be rethought @@ -1,143 +0,0 @@ -/* - Copyright (C) 2012 Andreas Baumann <abaumann@yahoo.com> - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include "hash.h" - -#include <stdlib.h> /* for malloc, free */ -#include <string.h> /* for memset */ -#include <assert.h> /* for assert */ - -hashtable_t *hashtable_init_mem( hashtable_t *h, void *mem, size_t size, hashfunc_t hash, comparefunc_t compare ) -{ - h->mem = NULL; - h->size = size; - h->hash = hash; - h->compare = compare; - h->nodes = (struct hashnode_t *)mem; - h->self_allocated = false; - - memset( (void *)h->nodes, 0, sizeof( struct hashnode_t ) * size ); - - return h; -} - -hashtable_t *hashtable_init( hashtable_t *h, size_t size, hashfunc_t hash, comparefunc_t compare ) -{ - void *mem = malloc( size * sizeof( struct hashnode_t ) ); - if( mem == NULL ) { - free( h ); - return NULL; - } - - (void)hashtable_init_mem( h, mem, size, hash, compare ); - - h->mem = mem; - - return h; -} - -hashtable_t *hashtable_create( size_t size, hashfunc_t hash, comparefunc_t compare ) -{ - hashtable_t *h; - - h = (hashtable_t *)malloc( sizeof( hashtable_t ) ); - if( h == NULL ) return NULL; - - hashtable_init( h, size, hash, compare ); - - h->self_allocated = true; - - return h; -} - -void hashtable_destroy( hashtable_t *h ) -{ - if( h->mem != NULL ) { - free( h->mem ); - } - - if( h->self_allocated ) { - free( h ); - } -} - -int hashtable_insert( hashtable_t *h, uintptr_t key, uintptr_t value ) -{ - struct hashnode_t *prev; - struct hashnode_t *node; - size_t hash; - - hash = h->hash( key ) % h->size; - - node = &h->nodes[hash]; - - /* free slot in nodes array, put it directly there, if a key */ - if( node->key == 0 ) { - node->key = key; - node->value = value; - return 0; - } - - /* not free, is it the same key? If yes, we replace the data only */ - if( h->compare( node->key, key ) != 0 ) { - node->value = value; - return 0; - } - - /* find last element in overflow list, again handle - * matching keys */ - do { - if( h->compare( node->key, key ) != 0 ) { - node->value = value; - return 0; - } - prev = node; - node = node->next; - } while( node != NULL ); - assert( node == NULL ); - assert( prev != NULL ); - - /* allocate a new child element for the overflow list */ - node = (struct hashnode_t *)malloc( sizeof( struct hashnode_t ) ); - if( node == NULL ) { - return -1; - } - - node->value = value; - node->key = key; - - prev->next = node; - - return 0; -} - -int hashtable_remove( hashtable_t *h, uintptr_t key ) -{ - return 0; -} - -size_t hashtable_int_hash( uintptr_t key ) -{ - return key; -} - -int hashtable_int_compare( uintptr_t a, uintptr_t b ) -{ - if( a < b ) return -1; - else if( a > b ) return 1; - else return 0; -} @@ -1,50 +0,0 @@ -/* - Copyright (C) 2012 Andreas Baumann <abaumann@yahoo.com> - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef HASH_H - -#include <stdint.h> /* for uintptr_t */ -#include <stddef.h> /* for size_t */ -#include <stdbool.h> /* for bool */ - -struct hashnode_t { - uintptr_t key; - uintptr_t value; - struct hashnode_t *next; /* overflow link */ -}; - -typedef size_t (*hashfunc_t)( uintptr_t ); -typedef int (*comparefunc_t)( uintptr_t a, uintptr_t b ); - -typedef struct hashtable_t { - size_t size; /* size of hash table */ - struct hashnode_t *nodes; /* memory buffer, allocated outside */ - hashfunc_t hash; /* hash function */ - comparefunc_t compare; /* compare function */ - void *mem; /* optional allocated memory */ - bool self_allocated; /* self allocated or not? */ -} hashtable_t; - -hashtable_t *hashtable_init_mem( hashtable_t *h, void *mem, size_t size, hashfunc_t hash, comparefunc_t compare); -hashtable_t *hashtable_init( hashtable_t *h, size_t size, hashfunc_t hash, comparefunc_t compare ); -hashtable_t *hashtable_create( size_t size, hashfunc_t hash, comparefunc_t compare ); -void hashtable_destroy( hashtable_t *h ); - -size_t hashtable_int_hash( uintptr_t key ); -int hashtable_int_compare( uintptr_t a, uintptr_t b ); - -#endif diff --git a/testhash.c b/testhash.c deleted file mode 100644 index e5037a4..0000000 --- a/testhash.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "hash.h" - -#include <stdbool.h> - -static bool alloc1( ) -{ - hashtable_t *h; - - h = hashtable_create( 100, hashtable_int_hash, hashtable_int_compare ); - if( h == NULL ) return false; - - hashtable_destroy( h ); - - return true; -} - -static bool alloc2( ) -{ - hashtable_t h; - - hashtable_init( &h, 100, hashtable_int_hash, hashtable_int_compare ); - - return true; -} - -int main( void ) -{ - if( !alloc1( ) ) return 1; - if( !alloc2( ) ) return 1; - - return 0; -} |