diff --git a/src/arch/x86_64/main.c b/src/arch/x86_64/main.c index 3275beb6..baac279a 100644 --- a/src/arch/x86_64/main.c +++ b/src/arch/x86_64/main.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include static volatile struct limine_terminal_request liminettyr = { .id = LIMINE_TERMINAL_REQUEST, @@ -37,13 +39,16 @@ void kmain(){ //set from 0xA0000 to 0x100000 as used //as sometimes the bootloader doesn't pass this region as being used - pmm_setused(0xA0000, 0x60); + pmm_setused((void*)0xA0000, 0x60); arch_mmu_init(); vmm_init(); alloc_init(); + + _panic("End of kmain()", 0); + } diff --git a/src/include/Makefile b/src/include/Makefile index 52813c5b..f33edbe7 100644 --- a/src/include/Makefile +++ b/src/include/Makefile @@ -1,5 +1,5 @@ objects := $(patsubst %.c,%.o,$(wildcard *.c)) -subdirs := stdio string +subdirs := stdio string hashtable OBJPATH=$(OBJDIR)/include export diff --git a/src/include/hashtable.h b/src/include/hashtable.h new file mode 100644 index 00000000..4ece3c4d --- /dev/null +++ b/src/include/hashtable.h @@ -0,0 +1,23 @@ +#ifndef _HASHTABLE_H_INCLUDE +#define _HASHTABLE_H_INCLUDE + +#include +#include + +typedef struct{ + void* next; + char* key; + void* val; +} hashtableentry; + +typedef struct{ + size_t size; + hashtableentry* entries; +} hashtable; + +bool hashtable_init(hashtable*, size_t); +bool hashtable_insert(hashtable*, char*, void*); +bool hashtable_remove(hashtable*, char*); +void* hashtable_get(hashtable*, char*); + +#endif diff --git a/src/include/hashtable/Makefile b/src/include/hashtable/Makefile new file mode 100644 index 00000000..9b282026 --- /dev/null +++ b/src/include/hashtable/Makefile @@ -0,0 +1,11 @@ +objects := $(patsubst %.c,%.o,$(wildcard *.c)) +subdirs := +OBJPATH=$(OBJDIR)/include/hashtable/ +export + +all: $(subdirs) $(objects) + +%.o: %.c + $(CC) $(CFLAGS) -o $@ $< + mkdir -p $(OBJPATH) + cp $@ $(OBJPATH)/$@ diff --git a/src/include/hashtable/hashtable.c b/src/include/hashtable/hashtable.c new file mode 100644 index 00000000..cbf126d8 --- /dev/null +++ b/src/include/hashtable/hashtable.c @@ -0,0 +1,114 @@ +#include +#include +#include + +// temporary for testing + +size_t hash(char* ptr, size_t size){ + size_t hash = 0; + for(size_t i = 0; i < size; ++i){ + hash += ptr[i] * 9; + } + + return hash % 883; + +} + +bool hashtable_init(hashtable* table, size_t size){ + table->entries = alloc(size*sizeof(hashtableentry)); + if(!table->entries) return false; + table->size = size; + return true; +} + +static inline hashtableentry* getentry(hashtable* table, char* key){ + size_t entryn = hash(key, strlen(key)) % table->size; + + return &table->entries[entryn]; +} + +bool hashtable_insert(hashtable* table, char* key, void* val){ + + + hashtableentry* entry = getentry(table, key); + + char* keysave = alloc(strlen(key)+1); + + memcpy(keysave, key, strlen(key)+1); + + if(!keysave) return false; + + if(entry->key){ + while(entry->next) + entry = entry->next; + + entry->next = alloc(sizeof(hashtableentry)); + + if(!entry->next){ + free(keysave); + return false; + } + + entry = entry->next; + } + + entry->key = keysave; + entry->val = val; + + return true; + +} + +bool hashtable_remove(hashtable* table, char* key){ + + hashtableentry* entry = getentry(table, key); + + if(!entry->key) return false; + + hashtableentry* prev = NULL; + + while(entry){ + if(!strcmp(entry->key, key)) + break; + prev = entry; + entry = entry->next; + } + + if(!entry) return false; + + if(prev){ + prev->next = entry->next; + free(entry->key); + free(entry); + } + else{ + if(entry->next){ + void* addr = entry->next; + memcpy(entry, entry->next, sizeof(hashtableentry)); + free(addr); + } + else{ + memset(entry, 0, sizeof(hashtableentry)); + } + return true; + } + + + return true; + +} + +void* hashtable_get(hashtable* table, char* key){ + + hashtableentry* entry = getentry(table, key); + + if(!entry->key) + return NULL; + + while(entry && strcmp(entry->key, key)) + entry = entry->next; + + if(!entry) return NULL; + + return entry->val; +}