diff --git a/src/opr/Makefile.in b/src/opr/Makefile.in index 6d2d7fce0f..5264538029 100644 --- a/src/opr/Makefile.in +++ b/src/opr/Makefile.in @@ -3,11 +3,12 @@ include @TOP_OBJDIR@/src/config/Makefile.config include @TOP_OBJDIR@/src/config/Makefile.pthread include @TOP_OBJDIR@/src/config/Makefile.libtool -LT_objs = assert.lo casestrcpy.lo rbtree.lo uuid.lo +LT_objs = assert.lo casestrcpy.lo dict.lo rbtree.lo uuid.lo LT_libs = $(LIB_hcrypto) $(LIB_roken) HEADERS = $(TOP_INCDIR)/afs/opr.h \ $(TOP_INCDIR)/afs/opr_assert.h \ + $(TOP_INCDIR)/opr/dict.h \ $(TOP_INCDIR)/opr/jhash.h \ $(TOP_INCDIR)/opr/lock.h \ $(TOP_INCDIR)/opr/lockstub.h \ @@ -40,6 +41,9 @@ $(TOP_INCDIR)/afs/opr.h: opr.h $(TOP_INCDIR)/afs/opr_assert.h: ${srcdir}/opr_assert.h $(INSTALL_DATA) $? $@ +$(TOP_INCDIR)/opr/dict.h: ${srcdir}/dict.h + $(INSTALL_DATA) $? $@ + $(TOP_INCDIR)/opr/jhash.h: ${srcdir}/jhash.h $(INSTALL_DATA) $? $@ diff --git a/src/opr/dict.c b/src/opr/dict.c new file mode 100644 index 0000000000..bd6f68ef88 --- /dev/null +++ b/src/opr/dict.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2012 Your File System Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include + +#include + +#include "dict.h" + +static_inline int +isPowerOf2(int value) +{ + return value && !(value & (value -1)); +} + +struct opr_dict * +opr_dict_Init(unsigned int size) +{ + struct opr_dict *dict; + int i; + + if (!isPowerOf2(size)) + return NULL; + + dict = calloc(1, sizeof(struct opr_dict)); + dict->size = size; + + dict->table = malloc(dict->size * sizeof(struct opr_queue)); + for (i = 0; i < dict->size; i++) { + opr_queue_Init(&dict->table[i]); + } + + return dict; +} + +void +opr_dict_Free(struct opr_dict **dict) +{ + free((*dict)->table); + free(*dict); + *dict = NULL; +} diff --git a/src/opr/dict.h b/src/opr/dict.h new file mode 100644 index 0000000000..f6f71632f5 --- /dev/null +++ b/src/opr/dict.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2012 Your File System Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +struct opr_dict { + int size; + struct opr_queue *table; +}; + +static_inline void +opr_dict_Prepend(struct opr_dict *dict, int index, struct opr_queue *entry) +{ + opr_queue_Prepend(&dict->table[index & (dict->size-1)], entry); +} + +static_inline void +opr_dict_Append(struct opr_dict *dict, int index, struct opr_queue *entry) +{ + opr_queue_Append(&dict->table[index & (dict->size-1)], entry); +} + +static_inline void +opr_dict_Promote(struct opr_dict *dict, int index, struct opr_queue *entry) +{ + opr_queue_Remove(entry); + opr_dict_Prepend(dict, index, entry); +} + +#define opr_dict_ScanBucket(dict, index, cursor) \ + opr_queue_Scan(&(dict)->table[index & ((dict)->size-1)], cursor) + +#define opr_dict_ScanBucketSafe(dict, index, cursor, store) \ + opr_queue_ScanSafe(&(dict)->table[index & ((dict)->size-1)], \ + cursor, store) + +extern struct opr_dict *opr_dict_Init(unsigned int size); +extern void opr_dict_Free(struct opr_dict **dict); diff --git a/src/opr/liboafs_opr.la.sym b/src/opr/liboafs_opr.la.sym index 4e727e0b38..8d10b37fef 100644 --- a/src/opr/liboafs_opr.la.sym +++ b/src/opr/liboafs_opr.la.sym @@ -4,6 +4,8 @@ opr_stolower opr_stoupper opr_strcompose opr_AssertionFailed +opr_dict_Init +opr_dict_Free opr_rbtree_first opr_rbtree_next opr_rbtree_remove diff --git a/tests/TESTS b/tests/TESTS index 59c4fb3e1e..2f30cbbb27 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -5,6 +5,7 @@ auth/superuser auth/authcon auth/realms cmd/command +opr/dict opr/jhash opr/queues opr/rbtree diff --git a/tests/opr/Makefile.in b/tests/opr/Makefile.in index 3719ce0ed9..30a4d953b6 100644 --- a/tests/opr/Makefile.in +++ b/tests/opr/Makefile.in @@ -7,10 +7,13 @@ MODULE_CFLAGS = -I$(srcdir)/../.. LIBS=../tap/libtap.a $(abs_top_builddir)/src/opr/liboafs_opr.la -tests = jhash-t queues-t rbtree-t time-t uuid-t +tests = dict-t jhash-t queues-t rbtree-t time-t uuid-t all check test tests: $(tests) +dict-t: dict-t.o + $(LT_LDRULE_static) dict-t.o $(LIBS) $(XLIBS) + queues-t: queues-t.o $(LT_LDRULE_static) queues-t.o ../tap/libtap.a $(XLIBS) diff --git a/tests/opr/dict-t.c b/tests/opr/dict-t.c new file mode 100644 index 0000000000..3558cdfb61 --- /dev/null +++ b/tests/opr/dict-t.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2012 Your File System Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include +#include + +struct myentry { + int value; + struct opr_queue d; +}; + +int +find(struct opr_dict *dict, int value, struct myentry **entryp) +{ + struct myentry *ent; + struct opr_queue *cursor; + int length = 0; + + for (opr_dict_ScanBucket(dict, value, cursor)) { + ent = opr_queue_Entry(cursor, struct myentry, d); + length++; + + if (ent->value == value) { + if (entryp) + *entryp = ent; + return length; + } + } + return 0; +} + +int +main(void) +{ + struct opr_dict *dict; + struct myentry *entry; + int members[] = {1,2,3,4,5,6,7,8,9,10,17,0}; + int i; + + plan(10); + + ok(opr_dict_Init(3) == NULL, + "Initialising a dictionary with a bad size fails"); + + dict = opr_dict_Init(8); + ok(dict != NULL, + "Initialising a dictionary succeeds"); + + for (i = 0; members[i] !=0; i++) { + entry = malloc(sizeof(struct myentry)); + entry->value = members[i]; + opr_dict_Append(dict, entry->value, &entry->d); + } + ok(1, "Hash populated successfully"); + + is_int(1, find(dict, 1, NULL), + "Entry 1 is first in hash chain"); + is_int(2, find(dict, 9, NULL), + "Entry 9 is second in hash chain"); + is_int(3, find(dict, 17, NULL), + "Entry 17 is third in hash chain"); + is_int(1, find(dict, 2, NULL), + "Entry 2 is first in hash chain"); + is_int(1, find(dict, 8, NULL), + "Entry 8 is first in hash chain"); + + find(dict, 17, &entry); + ok(entry != NULL && entry->value == 17, "Retrieved entry 17"); + opr_dict_Promote(dict, entry->value, &entry->d); + is_int(1, find(dict, 17, NULL), + "Entry 17 is first in hash chain following promotion"); + + return 0; +}