aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorAlex Auvolat <alex.auvolat@ens.fr>2015-02-09 17:56:59 +0100
committerAlex Auvolat <alex.auvolat@ens.fr>2015-02-09 17:56:59 +0100
commit002a1b035e2464c11b17f1bfd3835deccef7652a (patch)
tree640baf35dce60566a8f14eec54212af848e5ad71 /src/common
parentf2c51bc81d2aa618b29ddbeaae5ac1c5308821f0 (diff)
downloadkogata-002a1b035e2464c11b17f1bfd3835deccef7652a.tar.gz
kogata-002a1b035e2464c11b17f1bfd3835deccef7652a.zip
Change readme, remove unused code, changed hashtbl to add key freeing function.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/Makefile2
-rw-r--r--src/common/buffer.c167
-rw-r--r--src/common/common.libbin41450 -> 35275 bytes
-rw-r--r--src/common/hashtbl.c7
-rw-r--r--src/common/include/buffer.h41
-rw-r--r--src/common/include/hashtbl.h9
-rw-r--r--src/common/include/string.h2
-rw-r--r--src/common/string.c11
8 files changed, 26 insertions, 213 deletions
diff --git a/src/common/Makefile b/src/common/Makefile
index d27c6d5..40b8ba9 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -1,4 +1,4 @@
-OBJ = string.o printf.o slab_alloc.o mutex.o hashtbl.o buffer.o
+OBJ = string.o printf.o slab_alloc.o mutex.o hashtbl.o
LIB =
diff --git a/src/common/buffer.c b/src/common/buffer.c
deleted file mode 100644
index 21f1ec4..0000000
--- a/src/common/buffer.c
+++ /dev/null
@@ -1,167 +0,0 @@
-#include <malloc.h>
-
-#include <buffer.h>
-#include <string.h>
-
-#include <debug.h>
-
-// three types of buffers
-#define T_BYTES 1
-#define T_SLICE 2
-#define T_CONCAT 3
-
-struct buffer {
- uint16_t rc, type; // takes less space like this
- size_t len;
- union {
- struct {
- const char* data;
- bool owned;
- } bytes;
- struct {
- struct buffer *buf;
- size_t begin;
- } slice;
- struct {
- struct buffer *a, *b;
- } concat;
- };
-};
-
-void buffer_ref(buffer_t *b) {
- b->rc++;
-}
-
-void buffer_unref(buffer_t *b) {
- b->rc--;
- if (b->rc == 0) {
- switch (b->type) {
- case T_BYTES:
- if (b->bytes.owned) free((void*)b->bytes.data);
- break;
- case T_SLICE:
- buffer_unref(b->slice.buf);
- break;
- case T_CONCAT:
- buffer_unref(b->concat.a);
- buffer_unref(b->concat.b);
- break;
- default:
- ASSERT(false);
- }
- free(b);
- }
-}
-
-size_t buffer_size(buffer_t *b) {
- return b->len;
-}
-
-size_t read_buffer(buffer_t *b, char* dest, size_t begin, size_t n) {
- if (begin >= b->len) return 0;
- if (begin + n >= b->len) n = b->len - begin;
-
- switch (b->type) {
- case T_BYTES:
- memcpy(dest, b->bytes.data + begin, n);
- break;
- case T_SLICE:
- read_buffer(b->slice.buf, dest, begin + b->slice.begin, n);
- break;
- case T_CONCAT: {
- size_t la = b->concat.a->len;
- if (begin < la) {
- size_t r = read_buffer(b->concat.a, dest, begin, n);
- if (r < n) {
- ASSERT(read_buffer(b->concat.b, dest, 0, n - r) == n - r);
- }
- } else {
- ASSERT(read_buffer(b->concat.b, dest, begin - la, n) == n);
- }
- break;
- }
- default:
- ASSERT(false);
- }
-
- return n;
-}
-
-// ========================= //
-// BUFFER CREATION FUNCTIONS //
-// ========================= //
-
-buffer_t *buffer_from_bytes_nocopy(const char* data, size_t n, bool own_bytes) {
- buffer_t *b = (buffer_t*)malloc(sizeof(buffer_t));
- if (b == 0) return 0;
-
- b->rc = 1;
- b->type = T_BYTES;
- b->len = n;
- b->bytes.data = data;
- b->bytes.owned = own_bytes;
-
- return b;
-}
-buffer_t *buffer_from_bytes(const char* data, size_t n) {
- char* data2 = (char*)malloc(n);
- if (data2 == 0) return 0;
-
- memcpy(data2, data, n);
-
- buffer_t *b = buffer_from_bytes_nocopy(data2, n, true);
- if (b == 0) {
- free(data2);
- return 0;
- }
-
- return b;
-}
-
-
-buffer_t* buffer_slice(buffer_t* src, size_t begin, size_t n) {
- if (begin + n > src->len) return 0; // invalid request
-
- buffer_t *b = (buffer_t*)malloc(sizeof(buffer_t));
- if (b == 0) return 0;
-
- b->rc = 1;
- b->type = T_SLICE;
- b->len = n;
- b->slice.buf = src;
- b->slice.begin = begin;
-
- return b;
-}
-
-buffer_t* buffer_concat(buffer_t* a, buffer_t* b) {
- buffer_t *r = (buffer_t*)malloc(sizeof(buffer_t));
- if (r == 0) return r;
-
- r->rc = 1;
- r->type = T_CONCAT;
- r->len = a->len + b->len;
- r->concat.a = a;
- r->concat.b = b;
-
- return r;
-}
-
-buffer_t* buffer_slice_k(buffer_t *b, size_t begin, size_t n) {
- buffer_t *r = buffer_slice(b, begin, n);
- if (r != 0) {
- buffer_ref(b);
- }
- return r;
-}
-
-buffer_t* buffer_concat_k(buffer_t *a, buffer_t *b) {
- buffer_t *r = buffer_concat(a, b);
- if (r != 0) {
- buffer_ref(a);
- buffer_ref(b);
- }
- return r;
-}
-
-/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/common/common.lib b/src/common/common.lib
index 16259e6..058abeb 100644
--- a/src/common/common.lib
+++ b/src/common/common.lib
Binary files differ
diff --git a/src/common/hashtbl.c b/src/common/hashtbl.c
index 47072b4..d034ff5 100644
--- a/src/common/hashtbl.c
+++ b/src/common/hashtbl.c
@@ -16,16 +16,18 @@ typedef struct hashtbl_item {
struct hashtbl {
key_eq_fun_t ef;
hash_fun_t hf;
+ key_free_fun_t kff;
size_t size, nitems;
hashtbl_item_t **items;
};
-hashtbl_t *create_hashtbl(key_eq_fun_t ef, hash_fun_t hf, size_t initial_size) {
+hashtbl_t *create_hashtbl(key_eq_fun_t ef, hash_fun_t hf, key_free_fun_t kff, size_t initial_size) {
hashtbl_t *ht = (hashtbl_t*)malloc(sizeof(hashtbl_t));
if (ht == 0) return 0;
ht->ef = ef;
ht->hf = hf;
+ ht->kff = kff;
ht->size = (initial_size == 0 ? DEFAULT_INIT_SIZE : initial_size);
ht->nitems = 0;
@@ -46,6 +48,7 @@ void delete_hashtbl(hashtbl_t *ht) {
for (size_t i = 0; i < ht->size; i++) {
while (ht->items[i] != 0) {
hashtbl_item_t *n = ht->items[i]->next;
+ if (ht->kff) ht->kff(ht->items[i]->key);
free(ht->items[i]);
ht->items[i] = n;
}
@@ -122,6 +125,7 @@ void hashtbl_remove(hashtbl_t* ht, void* key) {
if (ht->ef(ht->items[slot]->key, key)) {
hashtbl_item_t *x = ht->items[slot];
ht->items[slot] = x->next;
+ if (ht->kff) ht->kff(x->key);
free(x);
ht->nitems--;
} else {
@@ -129,6 +133,7 @@ void hashtbl_remove(hashtbl_t* ht, void* key) {
if (ht->ef(i->next->key, key)) {
hashtbl_item_t *x = i->next;
i->next = x->next;
+ if (ht->kff) ht->kff(x->key);
free(x);
ht->nitems--;
break;
diff --git a/src/common/include/buffer.h b/src/common/include/buffer.h
deleted file mode 100644
index 0d6cfbf..0000000
--- a/src/common/include/buffer.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#pragma once
-
-#include <stdint.h>
-#include <stddef.h>
-#include <stdbool.h>
-
-// The buffer_t type is a simple reference-counted buffer type
-// enabling the creation, sharing, slicing and concatenation of buffers
-// without having to copy everything each time
-
-// Buffers are always allocated on the core kernel heap (kmalloc/kfree)
-
-// Once a buffer is allocated, its contents is immutable
-
-// Encoding and decoding functions for buffer contents are provided in
-// a separate file (encode.h)
-
-struct buffer;
-typedef struct buffer buffer_t;
-
-void buffer_ref(buffer_t*); // increase reference counter
-void buffer_unref(buffer_t*); // decrease reference counter
-
-size_t buffer_len(buffer_t* buf);
-size_t read_buffer(buffer_t* buf, char* to, size_t begin, size_t n); // returns num of bytes read
-
-buffer_t* buffer_from_bytes(const char* data, size_t n); // bytes are COPIED
-buffer_t* buffer_from_bytes_nocopy(const char* data, size_t n, bool own_bytes); // bytes are NOT COPIED
-
-// these functions GIVE the buffer in order to create the new buffer, ie they do not increase RC
-// the buffer is NOT GIVED if the new buffer could not be created (ie retval == 0)
-buffer_t* buffer_slice(buffer_t* b, size_t begin, size_t n);
-buffer_t* buffer_concat(buffer_t* a, buffer_t* b);
-
-// these functions KEEP a reference on the buffer (ie RC is incremented)
-// the RC is NOT INCREMENTED if the new buffer cannot be created
-buffer_t* buffer_slice_k(buffer_t* b, size_t begin, size_t n);
-buffer_t* buffer_concat_k(buffer_t* a, buffer_t* b);
-
-
-/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/common/include/hashtbl.h b/src/common/include/hashtbl.h
index 16dfefb..3b5a44d 100644
--- a/src/common/include/hashtbl.h
+++ b/src/common/include/hashtbl.h
@@ -8,8 +8,10 @@
// Supports adding, seeking, removing
// When adding a binding to the table, the previous binding for same key (if exists) is removed
-// TODO : possibility to allocate the hashtbl structure on any heap
-// (currently uses kmalloc/kfree)
+// The hashtbl is allocated with malloc/free
+// The keys are not copied in any way by the hashtbl, but there might still be something
+// to free, so the create_hashtbl function is given a key freeing function, usually
+// null when no freeing is required, or the standard free function.
struct hashtbl;
typedef struct hashtbl hashtbl_t;
@@ -17,8 +19,9 @@ typedef struct hashtbl hashtbl_t;
typedef size_t hash_t;
typedef hash_t (*hash_fun_t)(const void*);
typedef bool (*key_eq_fun_t)(const void*, const void*);
+typedef void (*key_free_fun_t)(void*);
-hashtbl_t* create_hashtbl(key_eq_fun_t ef, hash_fun_t hf, size_t initial_size); // 0 -> default size
+hashtbl_t* create_hashtbl(key_eq_fun_t ef, hash_fun_t hf, key_free_fun_t ff, size_t initial_size); // 0 -> default size
void delete_hashtbl(hashtbl_t* ht);
int hashtbl_add(hashtbl_t* ht, void* key, void* v); // non-null on error (OOM for instance)
diff --git a/src/common/include/string.h b/src/common/include/string.h
index 682b25a..a7a5253 100644
--- a/src/common/include/string.h
+++ b/src/common/include/string.h
@@ -14,4 +14,6 @@ char *strcpy(char *dest, const char *src);
char *strcat(char *dest, const char *src);
int strcmp(const char *s1, const char *s2);
+char *strdup(const char* str);
+
/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/common/string.c b/src/common/string.c
index 9dce27b..2c73900 100644
--- a/src/common/string.c
+++ b/src/common/string.c
@@ -1,4 +1,5 @@
#include <string.h>
+#include <malloc.h>
size_t strlen(const char* str) {
@@ -87,4 +88,14 @@ void *memset(void *dest, int val, size_t count) {
return dest;
}
+char *strdup(const char* str) {
+ int len = strlen(str) + 1;
+
+ char* ret = (char*)malloc(len);
+ if (ret == 0) return 0;
+
+ memcpy(ret, str, len);
+ return ret;
+}
+
/* vim: set ts=4 sw=4 tw=0 noet :*/