diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-02-27 22:26:35 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 76d85a10f6cda93eba29dad5372e8160af7289c8 (patch) | |
tree | cfec3043ec45a5e83494ec109e87c239dad6cc47 /include/std | |
parent | 8201cd9f40897cf6b8e6973b28a8661108702ab1 (diff) |
Use multiple threads to parse
Diffstat (limited to 'include/std')
-rw-r--r-- | include/std/alloc.h | 14 | ||||
-rw-r--r-- | include/std/buffer.h | 23 | ||||
-rw-r--r-- | include/std/buffer_view.h | 14 | ||||
-rw-r--r-- | include/std/defs.h | 7 | ||||
-rw-r--r-- | include/std/file.h | 27 | ||||
-rw-r--r-- | include/std/log.h | 13 | ||||
-rw-r--r-- | include/std/mem.h | 11 | ||||
-rw-r--r-- | include/std/misc.h | 33 | ||||
-rw-r--r-- | include/std/scoped_allocator.h | 31 | ||||
-rw-r--r-- | include/std/thread.h | 50 | ||||
-rw-r--r-- | include/std/types.h | 20 |
11 files changed, 243 insertions, 0 deletions
diff --git a/include/std/alloc.h b/include/std/alloc.h new file mode 100644 index 0000000..6809287 --- /dev/null +++ b/include/std/alloc.h @@ -0,0 +1,14 @@ +#ifndef AMALGAM_ALLOC_H +#define AMALGAM_ALLOC_H + +#include "types.h" +#include "misc.h" + +#define ALLOC_OK 0 +#define ALLOC_FAIL -1 + +CHECK_RESULT int am_malloc(usize size, void **mem); +CHECK_RESULT int am_realloc(void *mem, usize new_size, void **new_mem); +void am_free(void *mem); + +#endif diff --git a/include/std/buffer.h b/include/std/buffer.h new file mode 100644 index 0000000..ed87f29 --- /dev/null +++ b/include/std/buffer.h @@ -0,0 +1,23 @@ +#ifndef AMALGAM_BUFFER_H +#define AMALGAM_BUFFER_H + +#include "types.h" +#include "misc.h" + +#define BUFFER_OK 0 +#define BUFFER_ALLOC_FAIL -1 + +typedef struct { + char* data; + usize size; + usize capacity; +} Buffer; + +struct ScopedAllocator; +CHECK_RESULT int buffer_init(Buffer *self, struct ScopedAllocator *allocator); + +CHECK_RESULT int buffer_append(Buffer *self, void *data, usize size); +void* buffer_get(Buffer *self, usize index, usize type_size); +CHECK_RESULT int buffer_pop(Buffer *self, void *data, usize size); + +#endif
\ No newline at end of file diff --git a/include/std/buffer_view.h b/include/std/buffer_view.h new file mode 100644 index 0000000..4993dc2 --- /dev/null +++ b/include/std/buffer_view.h @@ -0,0 +1,14 @@ +#ifndef AMALGAM_BUFFER_VIEW_H +#define AMALGAM_BUFFER_VIEW_H + +#include "types.h" + +typedef struct { + const char* data; + usize size; +} BufferView; + +BufferView create_buffer_view_null(); +BufferView create_buffer_view(const char *data, usize size); + +#endif diff --git a/include/std/defs.h b/include/std/defs.h new file mode 100644 index 0000000..1376e16 --- /dev/null +++ b/include/std/defs.h @@ -0,0 +1,7 @@ +#ifndef AMALGAM_STD_DEFS_H +#define AMALGAM_STD_DEFS_H + +typedef struct amal_thread amal_thread; +typedef struct amal_mutex amal_mutex; + +#endif
\ No newline at end of file diff --git a/include/std/file.h b/include/std/file.h new file mode 100644 index 0000000..ac2abec --- /dev/null +++ b/include/std/file.h @@ -0,0 +1,27 @@ +#ifndef AMALGAM_FILE_H +#define AMALGAM_FILE_H + +#include "misc.h" +#include "types.h" + +/* Return bool_true if you want to continue scanning, otherwise return bool_false */ +typedef bool (*scan_dir_callback_func)(const char *filepath, int filepath_length, void *userdata); + +typedef struct { + const char *file_data; + usize file_size; + int fd; +} MappedFile; + +typedef enum { + MAPPED_FILE_READ, + MAPPED_FILE_WRITE, + MAPPED_FILE_READ_WRITE +} MappedFileMode; + +/* Hidden files (files starting with a dot) are skipped */ +CHECK_RESULT int scan_dir_recursive(const char *dir_path, scan_dir_callback_func callback_func, void *userdata); +CHECK_RESULT int mapped_file_init(MappedFile *self, const char *filepath, MappedFileMode file_mode); +CHECK_RESULT int mapped_file_deinit(MappedFile *self); + +#endif
\ No newline at end of file diff --git a/include/std/log.h b/include/std/log.h new file mode 100644 index 0000000..6ce9e4c --- /dev/null +++ b/include/std/log.h @@ -0,0 +1,13 @@ +#ifndef AMALGAM_LOG_H +#define AMALGAM_LOG_H + +#include "defs.h" + +amal_mutex* amal_log_get_mutex(); +void amal_log_debug(const char *fmt, ...); +void amal_log_error(const char *fmt, ...); +void amal_log_info(const char *fmt, ...); +void amal_log_warning(const char *fmt, ...); +void amal_log_perror(const char *prefix); + +#endif
\ No newline at end of file diff --git a/include/std/mem.h b/include/std/mem.h new file mode 100644 index 0000000..a5fe9b4 --- /dev/null +++ b/include/std/mem.h @@ -0,0 +1,11 @@ +#ifndef AMALGAM_MEM_H +#define AMALGAM_MEM_H + +#include "types.h" +#include "misc.h" + +void am_memcpy(void *dest, const void *src, usize size); +bool am_memeql(const void *lhs, const void *rhs, usize size); +void am_memset(void *dest, int value, usize size); + +#endif
\ No newline at end of file diff --git a/include/std/misc.h b/include/std/misc.h new file mode 100644 index 0000000..3ac524a --- /dev/null +++ b/include/std/misc.h @@ -0,0 +1,33 @@ +#ifndef AMALGAM_MISC_H +#define AMALGAM_MISC_H + +#define return_if_error(result) \ +do { \ + int return_if_result; \ + return_if_result = (result); \ + if((return_if_result) != 0) \ + return return_if_result; \ +} while(0) + +#define cleanup_if_error(result) do { if((result) != 0) goto cleanup; } while(0) + +#if defined(__GNUC__) && __GNUC__ >= 4 +#define CHECK_RESULT __attribute__ ((warn_unused_result)) +#elif defined(_MSC_VER) && _MSC_VER >= 1700 +#define CHECK_RESULT _Check_return_ +#else +#define CHECK_RESULT +#endif + +#define ignore_result_int(expr) (void)((expr)+1) + +typedef enum { + bool_false, + bool_true +} bool; + +#ifndef NULL +#define NULL ((void*)0) +#endif + +#endif
\ No newline at end of file diff --git a/include/std/scoped_allocator.h b/include/std/scoped_allocator.h new file mode 100644 index 0000000..fdaee2a --- /dev/null +++ b/include/std/scoped_allocator.h @@ -0,0 +1,31 @@ +#ifndef AMALGAM_SCOPED_ALLOCATOR_H +#define AMALGAM_SCOPED_ALLOCATOR_H + +#include "misc.h" +#include "types.h" +#include "buffer.h" + +typedef struct ScopedAllocatorNode ScopedAllocatorNode; +typedef struct ScopedAllocator ScopedAllocator; + +struct ScopedAllocatorNode { + char *data; + usize size; + ScopedAllocatorNode *next; +}; + +struct ScopedAllocator { + ScopedAllocatorNode head; + ScopedAllocatorNode *current; + Buffer buffers; +}; + +CHECK_RESULT int scoped_allocator_node_init(ScopedAllocatorNode *self); +void scoped_allocator_node_deinit(ScopedAllocatorNode *self); + +CHECK_RESULT int scoped_allocator_init(ScopedAllocator *self); +void scoped_allocator_deinit(ScopedAllocator *self); +CHECK_RESULT int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem); +CHECK_RESULT int scoped_allocator_add_buffer(ScopedAllocator *self, Buffer *buffer); + +#endif
\ No newline at end of file diff --git a/include/std/thread.h b/include/std/thread.h new file mode 100644 index 0000000..dd09039 --- /dev/null +++ b/include/std/thread.h @@ -0,0 +1,50 @@ +#ifndef AMALGAM_THREAD_H +#define AMALGAM_THREAD_H + +#include "misc.h" +#include "types.h" +#include "defs.h" +#include <pthread.h> + +typedef void* (AmalThreadCallbackFunc)(void *userdata); + +#define AMAL_THREAD_OK 0 +/* General error */ +#define AMAL_THREAD_ERR -1 +#define AMAL_THREAD_NOT_JOINABLE -23 + +struct amal_thread { + pthread_t thread_id; + pthread_attr_t thread_attr; + const char *name; + bool cancellable; + bool destroyable; +}; + +typedef enum { + AMAL_THREAD_JOINABLE, + AMAL_THREAD_DETACHED +} amal_thread_type; + +struct amal_mutex { + pthread_mutex_t mutex; + const char *lock_identifier; +}; + +CHECK_RESULT int amal_thread_create(amal_thread *self, amal_thread_type thread_type, const char *name, AmalThreadCallbackFunc callback_func, void *userdata); +/* Safe to call multiple times */ +CHECK_RESULT int amal_thread_deinit(amal_thread *self); +CHECK_RESULT int amal_thread_detach(amal_thread *self); +CHECK_RESULT int amal_thread_join(amal_thread *self, void **result); + +void amal_mutex_init(amal_mutex *self); +CHECK_RESULT int amal_mutex_lock(amal_mutex *self, const char *lock_identifier); +CHECK_RESULT int amal_mutex_unlock(amal_mutex *self); +void amal_mutex_tryunlock(amal_mutex *self); + +bool amal_thread_is_main(); + +/* Returns 0 if the number of usable threads is unknown */ +int amal_get_usable_thread_count(); + +#endif
\ No newline at end of file diff --git a/include/std/types.h b/include/std/types.h new file mode 100644 index 0000000..68e2d0f --- /dev/null +++ b/include/std/types.h @@ -0,0 +1,20 @@ +#ifndef AMALGAM_TYPES_H +#define AMALGAM_TYPES_H + +#include <stddef.h> +#include <stdint.h> + +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef ptrdiff_t isize; +typedef size_t usize; + +#endif |