diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-03-18 23:47:45 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 2323ca6c9ec3c8ee76b9acf13745b80b92952a6a (patch) | |
tree | 93013237dbcb0fa96ceb5f3c026fd040aff464cf /src/std | |
parent | 5a93c32a59775cd1be4b4f450e8230016b434366 (diff) |
Add struct, import caching, binop ops etc
Diffstat (limited to 'src/std')
-rw-r--r-- | src/std/alloc.c | 9 | ||||
-rw-r--r-- | src/std/file.c | 22 | ||||
-rw-r--r-- | src/std/log.c | 17 | ||||
-rw-r--r-- | src/std/scoped_allocator.c | 23 | ||||
-rw-r--r-- | src/std/thread.c | 39 |
5 files changed, 83 insertions, 27 deletions
diff --git a/src/std/alloc.c b/src/std/alloc.c index 93dcb98..d07e94f 100644 --- a/src/std/alloc.c +++ b/src/std/alloc.c @@ -1,10 +1,13 @@ #include "../../include/std/alloc.h" +#include "../../include/std/log.h" #include <stdlib.h> int am_malloc(usize size, void **mem) { void *allocated_data = malloc(size); - if(!allocated_data) + if(!allocated_data) { + amal_log_error("am_malloc: failed to allocate memory of size %lu", size); return ALLOC_FAIL; + } *mem = allocated_data; return ALLOC_OK; @@ -12,8 +15,10 @@ int am_malloc(usize size, void **mem) { int am_realloc(void *mem, usize new_size, void **new_mem) { void *new_allocated_data = realloc(mem, new_size); - if(!new_allocated_data) + if(!new_allocated_data) { + amal_log_error("am_malloc: failed to reallocate memory to size %lu", new_size); return ALLOC_FAIL; + } *new_mem = new_allocated_data; return ALLOC_OK; diff --git a/src/std/file.c b/src/std/file.c index 177ccbc..7de2b6c 100644 --- a/src/std/file.c +++ b/src/std/file.c @@ -6,6 +6,8 @@ #include <errno.h> #include <assert.h> #include <string.h> +#include <limits.h> +#include <stdlib.h> #include <dirent.h> #include <sys/stat.h> @@ -177,7 +179,7 @@ typedef enum { static CHECK_RESULT int file_get_type(const char *filepath, FileType *type) { struct stat file_stats; if(stat(filepath, &file_stats) == -1) { - amal_log_perror(filepath); + amal_log_error("file_get_type: %s failed: ", filepath, strerror(errno)); return -1; } @@ -202,12 +204,11 @@ int read_whole_file(const char *filepath, char **data, usize *size) { return -2; } - result = 0; file = fopen(filepath, "rb"); if(!file) { int error; error = errno; - amal_log_perror(filepath); + amal_log_error("read_whole_file: %s failed: ", filepath, strerror(error)); return error; } @@ -241,6 +242,21 @@ int read_whole_file(const char *filepath, char **data, usize *size) { return result; } +int file_get_canonical_path(const char *filepath, char **result_path, usize *result_path_size) { + char result_path_tmp[PATH_MAX]; + if(!realpath(filepath, result_path_tmp)) { + int ret; + ret = errno; + amal_log_error("file_get_canonical_path: realpath failed for path %s, error: %s", filepath, strerror(ret)); + return ret; + } + *result_path_size = strnlen(result_path_tmp, PATH_MAX); + return_if_error(am_malloc(*result_path_size + 1, (void**)result_path)); + am_memcpy(*result_path, result_path_tmp, *result_path_size); + (*result_path)[*result_path_size] = '\0'; + return 0; +} + BufferView file_get_parent_directory(BufferView filepath) { int i; for(i = filepath.size - 1; i >= 0; --i) { diff --git a/src/std/log.c b/src/std/log.c index 2563bdc..2059445 100644 --- a/src/std/log.c +++ b/src/std/log.c @@ -4,6 +4,8 @@ #include <stdio.h> #include <stdarg.h> +/*#define LOG_DEBUG*/ + static amal_mutex mutex; static bool mutex_initialized = bool_false; @@ -21,6 +23,7 @@ amal_mutex* amal_log_get_mutex() { } void amal_log_debug(const char *fmt, ...) { +#ifdef LOG_DEBUG va_list args; mutex_init(); ignore_result_int(amal_mutex_lock(&mutex, NULL)); @@ -30,13 +33,16 @@ void amal_log_debug(const char *fmt, ...) { va_end(args); fprintf(stderr, "\n"); ignore_result_int(amal_mutex_unlock(&mutex)); +#else + (void)fmt; +#endif } void amal_log_error(const char *fmt, ...) { va_list args; mutex_init(); ignore_result_int(amal_mutex_lock(&mutex, NULL)); - fprintf(stderr, "Error: "); + fprintf(stderr, "\x1b[1;31mError:\x1b[0m "); va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); @@ -66,11 +72,4 @@ void amal_log_warning(const char *fmt, ...) { va_end(args); fprintf(stderr, "\n"); ignore_result_int(amal_mutex_unlock(&mutex)); -} - -void amal_log_perror(const char *prefix) { - mutex_init(); - ignore_result_int(amal_mutex_lock(&mutex, NULL)); - perror(prefix); - ignore_result_int(amal_mutex_unlock(&mutex)); -} +}
\ No newline at end of file diff --git a/src/std/scoped_allocator.c b/src/std/scoped_allocator.c index dfed0b7..c66639a 100644 --- a/src/std/scoped_allocator.c +++ b/src/std/scoped_allocator.c @@ -1,5 +1,6 @@ #include "../../include/std/scoped_allocator.h" #include "../../include/std/alloc.h" +#include "../../include/std/log.h" #include <assert.h> #define ALLOC_NODE_SIZE 4096 @@ -36,18 +37,18 @@ static void buffer_deinit(Buffer *self) { } void scoped_allocator_deinit(ScopedAllocator *self) { - Buffer *buffer; - Buffer *buffer_end; + Buffer **buffer; + Buffer **buffers_end; - scoped_allocator_node_deinit(&self->head); self->current = NULL; - buffer = (Buffer*)self->buffers.data; - buffer_end = buffer + self->buffers.size / sizeof(Buffer); - while(buffer != buffer_end) { - buffer_deinit(buffer); + buffer = buffer_start(&self->buffers); + buffers_end = buffer_end(&self->buffers); + while(buffer != buffers_end) { + buffer_deinit(*buffer); ++buffer; } buffer_deinit(&self->buffers); + scoped_allocator_node_deinit(&self->head); } static CHECK_RESULT int scoped_allocator_ensure_capacity_for(ScopedAllocator *self, usize size) { @@ -82,9 +83,13 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) { ScopedAllocatorNode *current; usize alloc_size; assert(self->current); - assert(size <= ALLOC_NODE_SIZE); current = self->current; + if(size >= ALLOC_NODE_SIZE) { + amal_log_error("scoped_allocator_alloc: tried to alloc memory of size %lu. Max allowed alloc size is %lu", size, ALLOC_NODE_SIZE); + return -1; + } + alloc_size = size + align_ptr_ceil_offset(self->current->data + self->current->size, 16); return_if_error(scoped_allocator_ensure_capacity_for(self, alloc_size)); /* Reallocated (new node created) */ @@ -99,5 +104,5 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) { } int scoped_allocator_add_buffer(ScopedAllocator *self, Buffer *buffer) { - return buffer_append(&self->buffers, buffer, sizeof(Buffer)); + return buffer_append(&self->buffers, &buffer, sizeof(Buffer*)); } diff --git a/src/std/thread.c b/src/std/thread.c index 64a7f1b..ba67463 100644 --- a/src/std/thread.c +++ b/src/std/thread.c @@ -3,6 +3,7 @@ #include <stdio.h> #include <assert.h> #include <errno.h> +#include <string.h> #include <sys/sysinfo.h> #include <sys/types.h> #include <sys/syscall.h> @@ -48,12 +49,21 @@ int amal_thread_deinit(amal_thread *self) { if(self->cancellable) { r1 = pthread_cancel(self->thread_id); + /* thread deinit on a thread that has not been started shouldn't be an error */ + if(r1 == ESRCH) + r1 = 0; + if(r1 != 0) + amal_log_error("amal_thread_deinit: failed to cancel thread, error: %s", strerror(r1)); self->cancellable = bool_false; } + if(self->destroyable) { r2 = pthread_attr_destroy(&self->thread_attr); + if(r2 != 0) + amal_log_error("amal_thread_deinit: failed to destroy thread attributes, error: %s", strerror(r2)); self->destroyable = bool_false; } + return r1 != 0 ? r1 : r2; } @@ -73,20 +83,35 @@ int amal_thread_detach(amal_thread *self) { int amal_thread_join(amal_thread *self, void **result) { int result_err; int thread_type; - if((result_err = pthread_attr_getdetachstate(&self->thread_attr, &thread_type)) != 0) + + if((result_err = pthread_attr_getdetachstate(&self->thread_attr, &thread_type)) != 0) { + amal_log_error("amal_thread_join: failed to get detach state, error: %d", result_err); return result_err; - if(thread_type != PTHREAD_CREATE_JOINABLE) + } + + if(thread_type != PTHREAD_CREATE_JOINABLE) { + amal_log_error("amal_thread_join: thread not joinable type"); return AMAL_THREAD_NOT_JOINABLE; - if((result_err = pthread_join(self->thread_id, result)) != 0) + } + + if((result_err = pthread_join(self->thread_id, result)) != 0) { + /* Joining a thread that is not joinable shouldn't be an error */ + if(result_err == ESRCH) + goto cleanup; + amal_log_error("amal_thread_join: failed to join thread %llu, error: %s", self->thread_id, strerror(result_err)); return result_err == EINVAL ? AMAL_THREAD_NOT_JOINABLE : result_err; + } + + cleanup: self->cancellable = bool_false; return 0; } void amal_mutex_init(amal_mutex *self) { pthread_mutex_init(&self->mutex, NULL); - /* TODO: pthread_mutex_destroy in amal_mutex_deinit */ self->lock_identifier = NULL; + self->locked = bool_false; + self->owner_thread = 0; } void amal_mutex_deinit(amal_mutex *self) { @@ -105,6 +130,8 @@ int amal_mutex_lock(amal_mutex *self, const char *lock_identifier) { int result; result = pthread_mutex_lock(&self->mutex); self->lock_identifier = lock_identifier; + self->owner_thread = pthread_self(); + self->locked = bool_true; #ifdef AMAL_MUTEX_DEBUG if(result == 0 && self->lock_identifier) { amal_log_debug("amal_mutex_lock: mutex locked by thread %lu (%s), identification: %s", @@ -122,6 +149,10 @@ int amal_mutex_unlock(amal_mutex *self) { const char *identifier; identifier = self->lock_identifier; #endif + if(!self->locked || pthread_equal(self->owner_thread, pthread_self()) == 0) + return 0; + self->locked = bool_false; + self->owner_thread = 0; result = pthread_mutex_unlock(&self->mutex); #ifdef AMAL_MUTEX_DEBUG if(result == 0 && identifier) { |