aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-02-28 00:02:41 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commitcff67f93caeb3f98261860904dd232f6b551299e (patch)
tree94715ee98cc0ea62ea60da96b8b2d3a595b5276e /src
parent0ed62b9337c64a91481afd91f9e5706a36eca7b5 (diff)
fix crashes
Diffstat (limited to 'src')
-rw-r--r--src/compiler.c5
-rw-r--r--src/parser.c14
-rw-r--r--src/std/file.c62
-rw-r--r--src/std/thread.c10
4 files changed, 73 insertions, 18 deletions
diff --git a/src/compiler.c b/src/compiler.c
index 6f1c954..3e04ef2 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -88,7 +88,6 @@ static CHECK_RESULT int amal_compiler_load_first_this_thread(amal_compiler *self
return_if_error(parser_init(&parser, self, allocator));
cleanup_if_error(parser_parse_file(&parser, filepath));
- amal_log_debug("Finished parsing in thread");
cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_load_first_this_thread"));
cleanup_if_error(buffer_append(&self->parsers, &parser, sizeof(parser)));
result = AMAL_COMPILER_OK;
@@ -109,7 +108,6 @@ static void* thread_callback_parse_file(void *userdata) {
am_memcpy(&compiler_parser_userdata, userdata, sizeof(compiler_parser_userdata));
am_free(userdata);
- amal_log_debug("Thread start parse file");
cleanup_if_error(amal_compiler_load_first_this_thread(compiler_parser_userdata.compiler,
compiler_parser_userdata.filepath,
&compiler_parser_userdata.parser_thread_data->allocator));
@@ -118,7 +116,6 @@ static void* thread_callback_parse_file(void *userdata) {
_ = buffer_pop(&compiler_parser_userdata.compiler->queued_files, &next_file, sizeof(next_file));
cleanup:
- amal_log_debug("Thread finished, next file: %s", next_file.data ? "yes" : "no");
if(!next_file.data)
compiler_parser_userdata.parser_thread_data->status = PARSER_THREAD_STATUS_IDLE;
amal_mutex_tryunlock(&compiler_parser_userdata.compiler->mutex);
@@ -170,7 +167,7 @@ static CHECK_RESULT int amal_compiler_load_file_join_threads(amal_compiler *self
result = AMAL_COMPILER_ERR;
(void)_;
assert(amal_thread_is_main());
-
+
for(i = 0; i < self->usable_thread_count; ++i) {
cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_load_file_join_threads, waiting for workers"));
parser_thread_data = &self->threads[i];
diff --git a/src/parser.c b/src/parser.c
index da85292..bb7c8d3 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -5,6 +5,7 @@
#include "../include/std/file.h"
#include "../include/std/mem.h"
#include "../include/std/log.h"
+#include "../include/std/alloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
@@ -220,8 +221,8 @@ int parser_parse_buffer(Parser *self, BufferView code_buffer, BufferView buffer_
int parser_parse_file(Parser *self, BufferView filepath) {
int result;
- int mapped_file_deinit_result;
- MappedFile mapped_file;
+ char *file_data;
+ usize file_size;
char *filepath_tmp;
amal_log_debug("Parsing %.*s", (int)filepath.size, filepath.data);
@@ -232,11 +233,12 @@ int parser_parse_file(Parser *self, BufferView filepath) {
filepath_tmp = malloc(filepath.size + 1);
am_memcpy(filepath_tmp, filepath.data, filepath.size);
filepath_tmp[filepath.size] = '\0';
- result = mapped_file_init(&mapped_file, filepath_tmp, MAPPED_FILE_READ);
+ result = read_whole_file(filepath_tmp, &file_data, &file_size);
if(result != 0) return result;
- result = parser_parse_buffer(self, create_buffer_view(mapped_file.file_data, mapped_file.file_size), filepath);
- mapped_file_deinit_result = mapped_file_deinit(&mapped_file);
- return result != 0 ? result : mapped_file_deinit_result;
+ result = parser_parse_buffer(self, create_buffer_view(file_data, file_size), create_buffer_view(filepath_tmp, filepath.size));
+ /* TODO: Somehow free this.. causes issue where filepath becomes corrupt */
+ /*am_free(file_data);*/
+ return result;
}
/*
diff --git a/src/std/file.c b/src/std/file.c
index 7701be4..3cd0798 100644
--- a/src/std/file.c
+++ b/src/std/file.c
@@ -1,6 +1,7 @@
#include "../../include/std/file.h"
#include "../../include/std/mem.h"
#include "../../include/std/log.h"
+#include "../../include/std/alloc.h"
#include <stdio.h>
#include <errno.h>
#include <assert.h>
@@ -23,8 +24,10 @@ static int scan_dir_recursive_internal(char *dir_path, int path_length, scan_dir
path_length_start = path_length;
if((dir = opendir(dir_path)) == NULL) {
+ int error;
+ error = errno;
amal_log_perror("scan_dir_recursive");
- return errno;
+ return error;
}
while((entry = readdir(dir)) != NULL) {
@@ -95,6 +98,17 @@ static int mapped_file_mode_to_system_file_mode(MappedFileMode file_mode) {
}
}
+static int mapped_file_mode_to_open_mode(MappedFileMode file_mode) {
+ switch(file_mode) {
+ case MAPPED_FILE_READ: return O_RDONLY;
+ case MAPPED_FILE_WRITE: return O_WRONLY;
+ case MAPPED_FILE_READ_WRITE: return O_RDWR;
+ default:
+ assert(bool_false);
+ return O_RDONLY;
+ }
+}
+
int mapped_file_init(MappedFile *self, const char *filepath, MappedFileMode file_mode) {
struct stat file_stats;
int fd;
@@ -105,17 +119,17 @@ int mapped_file_init(MappedFile *self, const char *filepath, MappedFileMode file
self->file_size = 0;
self->fd = -1;
- fd = open(filepath, O_RDONLY);
+ fd = open(filepath, mapped_file_mode_to_open_mode(file_mode));
if(fd == -1) return errno;
if(fstat(fd, &file_stats) == -1) {
- amal_log_perror("map_file_read");
+ amal_log_perror(filepath);
goto cleanup;
}
map_file_system_mode = mapped_file_mode_to_system_file_mode(file_mode);
- memblock = mmap(NULL, file_stats.st_size, map_file_system_mode, MAP_PRIVATE, fd, 0);
+ memblock = mmap(NULL, file_stats.st_size, map_file_system_mode, MAP_SHARED, fd, 0);
if(memblock == MAP_FAILED) {
- amal_log_perror("map_file_read");
+ amal_log_perror(filepath);
goto cleanup;
}
@@ -149,4 +163,42 @@ int mapped_file_deinit(MappedFile *self) {
return -1;
else
return 0;
+}
+
+int read_whole_file(const char *filepath, char **data, usize *size) {
+ FILE *file;
+ int result;
+ usize bytes_read;
+
+ result = 0;
+ file = fopen(filepath, "rb");
+ if(!file) {
+ int error;
+ error = errno;
+ amal_log_perror(filepath);
+ return error;
+ }
+
+ result = fseek(file, 0, SEEK_END);
+ if(result != 0) {
+ result = ferror(file);
+ goto cleanup;
+ }
+
+ *size = ftell(file);
+ result = fseek(file, 0, SEEK_SET);
+ if(result != 0) {
+ result = ferror(file);
+ goto cleanup;
+ }
+
+ cleanup_if_error(am_malloc(*size, (void**)data));
+ bytes_read = fread(*data, 1, *size, file);
+ if(bytes_read != *size) {
+ result = ferror(file);
+ }
+
+ cleanup:
+ fclose(file);
+ return result;
} \ No newline at end of file
diff --git a/src/std/thread.c b/src/std/thread.c
index f717865..4f27c1d 100644
--- a/src/std/thread.c
+++ b/src/std/thread.c
@@ -101,33 +101,37 @@ int amal_mutex_lock(amal_mutex *self, const char *lock_identifier) {
int result;
result = pthread_mutex_lock(&self->mutex);
self->lock_identifier = lock_identifier;
+ #ifdef AMAL_MUTEX_DEBUG
if(result == 0 && self->lock_identifier) {
amal_log_debug("amal_mutex_lock: mutex locked by thread %lu (%s), identification: %s",
amal_thread_get_id(),
amal_thread_is_main() ? "main" : "not main",
self->lock_identifier ? self->lock_identifier : "none");
}
+ #endif
return result;
}
int amal_mutex_unlock(amal_mutex *self) {
int result;
+ #ifdef AMAL_MUTEX_DEBUG
const char *identifier;
identifier = self->lock_identifier;
+ #endif
result = pthread_mutex_unlock(&self->mutex);
+ #ifdef AMAL_MUTEX_DEBUG
if(result == 0 && identifier) {
amal_log_debug("amal_mutex_unlock: mutex unlocked by thread %lu (%s), identification: %s",
amal_thread_get_id(),
amal_thread_is_main() ? "main" : "not main",
identifier ? identifier : "none");
}
+ #endif
return result;
}
void amal_mutex_tryunlock(amal_mutex *self) {
- int _;
- (void)_;
- _ = amal_mutex_unlock(self);
+ ignore_result_int(amal_mutex_unlock(self));
}
bool amal_thread_is_main() {