From 16aaaa19a3ef4220726007d3e644ced0c9e06513 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 9 Sep 2019 01:08:34 +0200 Subject: Allow referencing code in imported file (right now for function calls, allow calling a function in another file) --- src/std/buffer.c | 13 +++++++++++++ src/std/buffer_view.c | 2 +- src/std/hash_map.c | 32 ++++++++++++++++++++++++++++++++ src/std/log.c | 4 ++-- src/std/mem.c | 3 +-- src/std/thread.c | 8 ++++---- 6 files changed, 53 insertions(+), 9 deletions(-) (limited to 'src/std') diff --git a/src/std/buffer.c b/src/std/buffer.c index a482bb9..021fce8 100644 --- a/src/std/buffer.c +++ b/src/std/buffer.c @@ -94,6 +94,19 @@ void buffer_clear(Buffer *self) { self->size = 0; } +int buffer_set_capacity(Buffer *self, usize new_capacity) { + if(am_realloc(self->data, new_capacity, (void**)&self->data) != ALLOC_OK) + return BUFFER_ALLOC_FAIL; + + self->capacity = new_capacity; + if(self->size < self->capacity) + self->size = self->capacity; + /* Update list of buffers in the allocator with the new address of the buffer data */ + if(self->allocator) + am_memcpy(self->allocator->mems.data + sizeof(void*) * self->allocator_index, &self->data, sizeof(void*)); + return BUFFER_OK; +} + void* buffer_begin(Buffer *self) { return self->data; } diff --git a/src/std/buffer_view.c b/src/std/buffer_view.c index f2d79c0..249928b 100644 --- a/src/std/buffer_view.c +++ b/src/std/buffer_view.c @@ -1,7 +1,7 @@ #include "../../include/std/buffer_view.h" #include "../../include/std/mem.h" -BufferView create_buffer_view_null() { +BufferView create_buffer_view_null(void) { BufferView buffer_view; buffer_view.data = NULL; buffer_view.size = 0; diff --git a/src/std/hash_map.c b/src/std/hash_map.c index 98ebf40..234f3e3 100644 --- a/src/std/hash_map.c +++ b/src/std/hash_map.c @@ -178,6 +178,7 @@ int hash_map_insert(HashMap *self, BufferView key, void *value) { } bool hash_map_get(HashMap *self, BufferView key, void *value) { +#if 0 usize bucket_size; usize bucket_index; usize hash; @@ -199,6 +200,37 @@ bool hash_map_get(HashMap *self, BufferView key, void *value) { } } + return bool_false; +#endif + void *ref; + if(!hash_map_get_ref(self, key, &ref)) + return bool_false; + am_memcpy(value, ref, self->value_type_size); + return bool_true; +} + +bool hash_map_get_ref(HashMap *self, BufferView key, void **value) { + usize bucket_size; + usize bucket_index; + usize hash; + HashMapBucket *bucket; + HashMapBucketNode *bucket_node; + + bucket_size = buffer_get_size(&self->buckets, HashMapBucket); + hash = self->hash_func((const u8*)key.data, key.size); + bucket_index = hash % bucket_size; + + bucket = ((HashMapBucket*)self->buckets.data) + bucket_index; + for(bucket_node = bucket->start; bucket_node; bucket_node = bucket_node_get_next(bucket_node)) { + BufferView bucket_key; + bucket_key = bucket_node_get_key(bucket_node); + if(hash == bucket_node_get_hash(bucket_node) && self->compare_func(&key, &bucket_key) == 0) { + if(value) + *value = bucket_node_get_value(bucket_node); + return bool_true; + } + } + return bool_false; } diff --git a/src/std/log.c b/src/std/log.c index 59e0319..68f1f2e 100644 --- a/src/std/log.c +++ b/src/std/log.c @@ -10,14 +10,14 @@ static amal_mutex mutex; static bool mutex_initialized = bool_false; /* Safe to call multiple times */ -static void mutex_init() { +static void mutex_init(void) { if(!mutex_initialized) { ignore_result_int(amal_mutex_init(&mutex)); mutex_initialized = bool_true; } } -amal_mutex* amal_log_get_mutex() { +amal_mutex* amal_log_get_mutex(void) { mutex_init(); return &mutex; } diff --git a/src/std/mem.c b/src/std/mem.c index 95edcb9..8658781 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -18,7 +18,6 @@ void am_memset(void *dest, int value, usize size) { memset(dest, value, size); } -long am_pagesize() { +long am_pagesize(void) { return sysconf(_SC_PAGESIZE); } - diff --git a/src/std/thread.c b/src/std/thread.c index 87362d2..9b9b764 100644 --- a/src/std/thread.c +++ b/src/std/thread.c @@ -120,11 +120,11 @@ void amal_mutex_deinit(amal_mutex *self) { pthread_mutex_destroy(&self->mutex); } -static long amal_process_get_id() { +static long amal_process_get_id(void) { return getpid(); } -static long amal_thread_get_id() { +static long amal_thread_get_id(void) { return syscall(SYS_gettid); } @@ -173,11 +173,11 @@ void amal_mutex_tryunlock(amal_mutex *self) { ignore_result_int(amal_mutex_unlock(self)); } -bool amal_thread_is_main() { +bool amal_thread_is_main(void) { /* TODO: This only works for linux, use equivalent functions on other platforms */ return amal_thread_get_id() == amal_process_get_id(); } -int amal_get_usable_thread_count() { +int amal_get_usable_thread_count(void) { return get_nprocs(); } -- cgit v1.2.3