aboutsummaryrefslogtreecommitdiff
path: root/src/std
diff options
context:
space:
mode:
Diffstat (limited to 'src/std')
-rw-r--r--src/std/file.c62
-rw-r--r--src/std/thread.c10
2 files changed, 64 insertions, 8 deletions
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() {