~sircmpwn/koio

feccec6dc9f79ea8a89138fbc560a55e0c1892f0 — Drew DeVault 4 years ago 71d6bbd
Gen pkgconfig file and namespace hashtable
6 files changed, 32 insertions(+), 24 deletions(-)

M include/koio_private.h
M lib/hashtable.c
M lib/ko_add_file.c
M lib/ko_del_file.c
M lib/ko_fopen.c
M meson.build
M include/koio_private.h => include/koio_private.h +8 -9
@@ 2,23 2,22 @@
#define _KOIO_PRIVATE_H
#include <stdbool.h>

struct hashtable_entry {
	struct hashtable_entry *next;
struct ko_hashtable_entry {
	struct ko_hashtable_entry *next;
	unsigned int hash;
	char *key;
	void *value;
};

struct hashtable {
	unsigned int (*hash)(const void *);
	struct hashtable_entry *buckets[256];
struct ko_hashtable {
	struct ko_hashtable_entry *buckets[256];
};

extern struct hashtable koio_vfs;
extern struct ko_hashtable koio_vfs;

void *hashtable_get(struct hashtable *table, const char *key);
void *hashtable_set(struct hashtable *table, const char *key, void *value);
void *hashtable_del(struct hashtable *table, const void *key);
void *ko_hashtable_get(struct ko_hashtable *table, const char *key);
void *ko_hashtable_set(struct ko_hashtable *table, const char *key, void *value);
void *ko_hashtable_del(struct ko_hashtable *table, const void *key);

struct file_entry {
	char *data;

M lib/hashtable.c => lib/hashtable.c +11 -11
@@ 4,8 4,8 @@
#include <string.h>
#include "koio_private.h"

static const int buckets_size = sizeof(((struct hashtable *)0)->buckets) /
	sizeof(struct hashtable_entry );
static const int buckets_size = sizeof(((struct ko_hashtable *)0)->buckets) /
	sizeof(struct ko_hashtable_entry );

static unsigned int djb2(const char *str) {
	unsigned int hash = 5381;


@@ 16,10 16,10 @@ static unsigned int djb2(const char *str) {
	return hash;
}

void *hashtable_get(struct hashtable *table, const char *key) {
void *ko_hashtable_get(struct ko_hashtable *table, const char *key) {
	unsigned int hash = djb2(key);
	unsigned int bucket = hash % buckets_size;
	struct hashtable_entry *entry = table->buckets[bucket];
	struct ko_hashtable_entry *entry = table->buckets[bucket];
	if (entry) {
		if (entry->hash != hash) {
			while (entry->next) {


@@ 36,11 36,11 @@ void *hashtable_get(struct hashtable *table, const char *key) {
	return entry->value;
}

void *hashtable_set(struct hashtable *table, const char *key, void *value) {
void *ko_hashtable_set(struct ko_hashtable *table, const char *key, void *value) {
	unsigned int hash = djb2(key);
	unsigned int bucket = hash % buckets_size;
	struct hashtable_entry *entry = table->buckets[bucket];
	struct hashtable_entry *previous = NULL;
	struct ko_hashtable_entry *entry = table->buckets[bucket];
	struct ko_hashtable_entry *previous = NULL;

	if (entry) {
		if (entry->hash != hash) {


@@ 56,7 56,7 @@ void *hashtable_set(struct hashtable *table, const char *key, void *value) {
	}

	if (entry == NULL) {
		entry = calloc(1, sizeof(struct hashtable_entry));
		entry = calloc(1, sizeof(struct ko_hashtable_entry));
		entry->hash = hash;
		entry->key = strdup(key);
		table->buckets[bucket] = entry;


@@ 70,11 70,11 @@ void *hashtable_set(struct hashtable *table, const char *key, void *value) {
	return old;
}

void *hashtable_del(struct hashtable *table, const void *key) {
void *ko_hashtable_del(struct ko_hashtable *table, const void *key) {
	unsigned int hash = djb2(key);
	unsigned int bucket = hash % buckets_size;
	struct hashtable_entry *entry = table->buckets[bucket];
	struct hashtable_entry *previous = NULL;
	struct ko_hashtable_entry *entry = table->buckets[bucket];
	struct ko_hashtable_entry *previous = NULL;

	if (entry) {
		if (entry->hash != hash) {

M lib/ko_add_file.c => lib/ko_add_file.c +2 -2
@@ 1,13 1,13 @@
#include <stdlib.h>
#include "koio_private.h"

struct hashtable koio_vfs = {0};
struct ko_hashtable koio_vfs = {0};

void ko_add_file(const char *path, char *data, size_t len) {
	struct file_entry *entry = calloc(1, sizeof(struct file_entry));
	entry->data = data;
	entry->len = len;
	struct file_entry *prev = hashtable_set(&koio_vfs, path, entry);
	struct file_entry *prev = ko_hashtable_set(&koio_vfs, path, entry);
	if (prev) {
		free(prev);
	}

M lib/ko_del_file.c => lib/ko_del_file.c +1 -1
@@ 2,7 2,7 @@
#include "koio_private.h"

void ko_del_file(const char *path) {
	struct file_entry *prev = hashtable_del(&koio_vfs, path);
	struct file_entry *prev = ko_hashtable_del(&koio_vfs, path);
	if (prev) {
		free(prev);
	}

M lib/ko_fopen.c => lib/ko_fopen.c +1 -1
@@ 34,7 34,7 @@ FILE *ko_fopen(const char *path, const char *mode) {
	if (f) {
		return f;
	}
	struct file_entry *entry = hashtable_get(&koio_vfs, path);
	struct file_entry *entry = ko_hashtable_get(&koio_vfs, path);
	if (!entry) {
		return NULL;
	}

M meson.build => meson.build +9 -0
@@ 39,6 39,15 @@ lib_koio = library(meson.project_name(),

install_headers('include/koio.h')

pkgconfig = import('pkgconfig')
pkgconfig.generate(
	libraries: lib_koio,
	version: meson.project_version(),
	filebase: meson.project_name(),
	name: meson.project_name(),
	description: 'statically embedded filesystem',
)

koio = declare_dependency(
	link_with: lib_koio,
	include_directories: koio_inc,