diff options
| author | Mikhail Romanko <me@blankhex.com> | 2024-04-23 23:45:43 +0300 |
|---|---|---|
| committer | Mikhail Romanko <me@blankhex.com> | 2024-04-23 23:45:43 +0300 |
| commit | 692b5b42974e2f81dc4ad708a4593b1e8cb2bd3d (patch) | |
| tree | b7eec97f4a12e0090b9421e7dfe8a6116b7ef4d8 | |
| parent | ec499b6cfc3fe196757d4467ca044b5915198d45 (diff) | |
| download | bhlib-old-692b5b42974e2f81dc4ad708a4593b1e8cb2bd3d.tar.gz | |
Sync to the latest version.
Can be broken or partially implemented.
| -rw-r--r-- | CMakeLists.txt | 7 | ||||
| -rw-r--r-- | include/bh/algo.h | 6 | ||||
| -rw-r--r-- | include/bh/bh.h | 10 | ||||
| -rw-r--r-- | include/bh/deflate.h | 5 | ||||
| -rw-r--r-- | include/bh/file.h | 37 | ||||
| -rw-r--r-- | include/bh/hashmap.h | 22 | ||||
| -rw-r--r-- | include/bh/internal/file_posix.h | 0 | ||||
| -rw-r--r-- | include/bh/internal/file_win.h | 0 | ||||
| -rw-r--r-- | include/bh/internal/io.h | 12 | ||||
| -rw-r--r-- | include/bh/internal/thread_null.h | 28 | ||||
| -rw-r--r-- | include/bh/internal/thread_posix.h | 3 | ||||
| -rw-r--r-- | include/bh/internal/thread_win.h | 48 | ||||
| -rw-r--r-- | include/bh/io.h | 182 | ||||
| -rw-r--r-- | include/bh/platform.h | 46 | ||||
| -rw-r--r-- | include/bh/queue.h | 13 | ||||
| -rw-r--r-- | include/bh/thread.h | 56 | ||||
| -rw-r--r-- | main.c | 16 | ||||
| -rwxr-xr-x | scripts/coverage.sh | 2 | ||||
| -rw-r--r-- | src/deflate.c | 1 | ||||
| -rw-r--r-- | src/file_posix.c | 0 | ||||
| -rw-r--r-- | src/file_win.c | 0 | ||||
| -rw-r--r-- | src/io.c | 83 | ||||
| -rw-r--r-- | src/thread.c | 2 | ||||
| -rw-r--r-- | src/thread_null.c | 113 | ||||
| -rw-r--r-- | src/thread_posix.c | 33 | ||||
| -rw-r--r-- | src/thread_win.c | 120 |
26 files changed, 785 insertions, 60 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b2ceff..c275f67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,12 +28,19 @@ set(BHLIB_SOURCE src/hashmap.c src/queue.c src/thread.c + src/io.c + src/deflate.c ) set(BHLIB_HEADER + include/bh/bh.h + include/bh/platform.h include/bh/algo.h include/bh/hashmap.h include/bh/queue.h + include/bh/thread.h + include/bh/io.h + include/bh/deflate.h ) set(BHLIB_INCLUDE_DIRS diff --git a/include/bh/algo.h b/include/bh/algo.h index 9934306..b859b1f 100644 --- a/include/bh/algo.h +++ b/include/bh/algo.h @@ -23,10 +23,10 @@ void bh_swap(void *lhs, * @param element Element size * @param equal Equal/compare function * - * @return Pointer to the first element that are greater-or-equal to pivot - * element. + * @return The return value is the pointer to the first element of the second + * partition. * - * @warning Pivot element can be an element of the partitioned array itself. + * @warning Pivot element can be a part of the partitioned array. */ void *bh_partition(void *pivot, void *array, diff --git a/include/bh/bh.h b/include/bh/bh.h index d00dc5c..f761518 100644 --- a/include/bh/bh.h +++ b/include/bh/bh.h @@ -3,18 +3,20 @@ #include <bh/config.h> #include <stddef.h> +#include "platform.h" + #define BH_INT_TO_PTR(x) \ - ((void *)((long)(x))) + ((void *)((bh_intptr_t)(x))) #define BH_UINT_TO_PTR(x) \ - ((void *)((unsigned long)(x))) + ((void *)((bh_uintptr_t)(x))) #define BH_PTR_TO_INT(x) \ - ((long)(x)) + ((bh_intptr_t)(x)) #define BH_PTR_TO_UINT(x) \ - ((unsigned long)(x)) + ((bh_uintptr_t)(x)) typedef int (*bh_equal_cb_t)(const void *, const void *); typedef size_t (*bh_hash_cb_t)(const void *); diff --git a/include/bh/deflate.h b/include/bh/deflate.h new file mode 100644 index 0000000..6b0696b --- /dev/null +++ b/include/bh/deflate.h @@ -0,0 +1,5 @@ +#ifndef BHLIB_DEFLATE_H +#define BHLIB_DEFLATE_H + + +#endif /* BHLIB_DEFLATE_H */ diff --git a/include/bh/file.h b/include/bh/file.h new file mode 100644 index 0000000..7718c80 --- /dev/null +++ b/include/bh/file.h @@ -0,0 +1,37 @@ +#ifndef BHLIB_FILE_H +#define BHLIB_FILE_H + +#include "io.h" + +typedef struct bh_file_s bh_file_t; + +bh_file_t *bh_file_new(const char *path, + const char *mode); + +void bh_file_free(bh_file_t *file); + +size_t bh_file_read(bh_file_t *file, + char *buffer, + size_t size); + +size_t bh_file_write(bh_file_t *file, + const char *buffer, + size_t size); + +void bh_file_flush(bh_file_t *file); + +void bh_file_seek(bh_file_t *file, + bh_off_t pos, + int dir); + +bh_off_t bh_file_tell(bh_file_t *file); + +bh_off_t bh_file_available(bh_file_t *file); + +int bh_file_error(bh_file_t *file); + +int bh_file_eof(bh_file_t *file); + +void bh_file_clear(bh_file_t *file); + +#endif /* BHLIB_FILE_H */ diff --git a/include/bh/hashmap.h b/include/bh/hashmap.h index b8c2dc5..6e8634e 100644 --- a/include/bh/hashmap.h +++ b/include/bh/hashmap.h @@ -1,7 +1,7 @@ #ifndef BHLIB_HASHMAP_H #define BHLIB_HASHMAP_H -#include <bh/bh.h> +#include "bh.h" typedef struct bh_hashmap_s bh_hashmap_t; @@ -15,9 +15,6 @@ typedef struct bh_hashmap_s bh_hashmap_t; * hashmap object. * @return If the function fails, the return value is NULL. * - * @warning The quality of the supplied hash function will affect performance - * of the hashmap. - * * @sa bh_hashmap_free, bh_hashmap_reserve, bh_hashmap_insert */ bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal, @@ -37,7 +34,7 @@ void bh_hashmap_free(bh_hashmap_t *hashmap); * * @param hashmap Valid pointer to the hashmap object. * - * @warning Clearing the hashmap does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_hashmap_remove */ @@ -52,7 +49,7 @@ void bh_hashmap_clear(bh_hashmap_t *hashmap); * @return If the function succeeds, the return value is zero. * @return If the function fails, the return value is non-zero. * - * @warning Reserving hashmap space does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_hashmap_capacity, bh_hashmap_insert */ @@ -70,9 +67,9 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap, * @return If the function fails, the return value is non-zero. * * @warning Inserted element is owned by the caller of the function. - * @warning Inserting elements into the hashmap does invalidate iterators. + * @warning Calling this function will invalidate iterators. * - * @sa bbh_hashmap_remove, bh_hashmap_at + * @sa bh_hashmap_remove, bh_hashmap_at */ int bh_hashmap_insert(bh_hashmap_t *hashmap, void *key, @@ -84,7 +81,7 @@ int bh_hashmap_insert(bh_hashmap_t *hashmap, * @param hashmap Valid pointer to the hashmap object. * @param key Key. * - * @warning Removing elements from the hashmap does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_hashmap_insert, bh_hashmap_at */ @@ -96,15 +93,12 @@ void bh_hashmap_remove(bh_hashmap_t *hashmap, * * @param hashmap Valid pointer to the hashmap. * @param key Key. - * @param exists Pointer to the exists flag (optional). + * @param exists Optional pointer to the exists flag. * * @return If the function succeeds, the return value is a valid pointer to * the element value. * @return If the function fails, the return value is NULL. * - * @note If the hashmap does not contain any element with the key, the - * function will fail. - * * @sa bh_hashmap_empty, bh_hashmap_insert */ void *bh_hashmap_at(bh_hashmap_t *hashmap, @@ -205,7 +199,7 @@ void *bh_hashmap_iter_next(bh_hashmap_t *hashmap, * @param hashmap Valid pointer to the hashmap object. * @param key Valid iterator. * - * @warning Removing elements from the hashmap does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_hashmap_insert, bh_hashmap_at */ diff --git a/include/bh/internal/file_posix.h b/include/bh/internal/file_posix.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/include/bh/internal/file_posix.h diff --git a/include/bh/internal/file_win.h b/include/bh/internal/file_win.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/include/bh/internal/file_win.h diff --git a/include/bh/internal/io.h b/include/bh/internal/io.h new file mode 100644 index 0000000..6a1f7ed --- /dev/null +++ b/include/bh/internal/io.h @@ -0,0 +1,12 @@ +#ifndef BHLIB_INTERNAL_IO_H +#define BHLIB_INTERNAL_IO_H + +#include <bh/io.h> + +void bh_io_init(bh_io_t *io, + bh_io_table_t *table); + +void bh_io_destroy(bh_io_t *io); + +#endif /* BHLIB_INTERNAL_IO_H */ + diff --git a/include/bh/internal/thread_null.h b/include/bh/internal/thread_null.h new file mode 100644 index 0000000..9a22da5 --- /dev/null +++ b/include/bh/internal/thread_null.h @@ -0,0 +1,28 @@ +struct bh_thread_s +{ + void *handle; +}; + +struct bh_mutex_s +{ + void *handle; +}; + +struct bh_cond_s +{ + void *handle; +}; + +int bh_thread_init(bh_thread_t *thread, + bh_task_t *task); + +int bh_mutex_init(bh_mutex_t *mutex); +void bh_mutex_destroy(bh_mutex_t *mutex); + +int bh_cond_init(bh_cond_t *cond); +void bh_cond_destroy(bh_cond_t *cond); + +int bh_thread_pool_init(bh_thread_pool_t *pool, + size_t size); + +void bh_thread_pool_destroy(bh_thread_pool_t *pool); diff --git a/include/bh/internal/thread_posix.h b/include/bh/internal/thread_posix.h index 311d0ae..bef12a9 100644 --- a/include/bh/internal/thread_posix.h +++ b/include/bh/internal/thread_posix.h @@ -3,6 +3,7 @@ struct bh_thread_s { pthread_t handle; + int allocated; }; struct bh_mutex_s @@ -27,4 +28,4 @@ void bh_cond_destroy(bh_cond_t *cond); int bh_thread_pool_init(bh_thread_pool_t *pool, size_t size); -void bh_thread_pool_destroy(bh_thread_pool_t *pool);
\ No newline at end of file +void bh_thread_pool_destroy(bh_thread_pool_t *pool); diff --git a/include/bh/internal/thread_win.h b/include/bh/internal/thread_win.h new file mode 100644 index 0000000..a84cbdb --- /dev/null +++ b/include/bh/internal/thread_win.h @@ -0,0 +1,48 @@ +#include <process.h> + +struct bh_thread_s +{ + HANDLE handle; + int allocated; +}; + +struct bh_thread_data_s +{ + bh_task_t *task; + bh_thread_end_cb_t end; +} bh_thread_data_t; + +struct bh_mutex_s +{ + CRITICAL_SECTION handle; +}; + +struct bh_cond_s +{ + void *handle; +}; + +int bh_thread_init_base(bh_thread_t *thread, + bh_task_t *task, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end); + +#define bh_thread_init(thread, task) \ + bh_thread_init_base((thread), (task), _beginthreadex, _endthreadex) + +int bh_mutex_init(bh_mutex_t *mutex); +void bh_mutex_destroy(bh_mutex_t *mutex); + +int bh_cond_init(bh_cond_t *cond); +void bh_cond_destroy(bh_cond_t *cond); + +int bh_thread_pool_init_base(bh_thread_pool_t *pool, + size_t size, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end); + +#define bh_thread_pool_init(pool, size) \ + bh_thread_pool_init_base((pool), (size), _beginthreadex, _endthreadex) + +void bh_thread_pool_destroy(bh_thread_pool_t *pool); + diff --git a/include/bh/io.h b/include/bh/io.h new file mode 100644 index 0000000..cdf442a --- /dev/null +++ b/include/bh/io.h @@ -0,0 +1,182 @@ +#ifndef BHLIB_IO_H +#define BHLIB_IO_H + +#include "bh.h" + +#define BH_IO_ERROR (1 << 0) +#define BH_IO_EOF (1 << 1) + +#define BH_IO_CAST(x) \ + ((bh_io_t *)(x)) + +struct bh_io_s; + +typedef struct bh_io_table_s +{ + size_t (*read)(struct bh_io_s *io, + char *data, + size_t size); + + size_t (*write)(struct bh_io_s *io, + const char *data, + size_t size); + + void (*flush)(struct bh_io_s *io); + + void (*seek)(struct bh_io_s *io, + bh_off_t offset, + int dir); + + bh_off_t (*tell)(struct bh_io_s *io); + + bh_off_t (*available)(struct bh_io_s *io); + + void (*clear)(struct bh_io_s *io); + + void (*destroy)(struct bh_io_s *io); +} bh_io_table_t; + +typedef struct bh_io_s +{ + bh_io_table_t *table; + int flags; +} bh_io_t; + +/** + * @brief Create new IO object. + * + * @param table Valid pointer to the IO table. + * @param size Size of the IO object. + * + * @return If the function succeeds, the return value is a pointer to the + * new, semi-initalized, IO object. + * @return If the function fails, the return value is NULL. + * + * @warning This function should be used in context of implementing child + * IO objects (files, sockets, streaming compression, etc). + * + * @sa bh_io_free + */ +bh_io_t *bh_io_new(bh_io_table_t *table, + size_t size); + +/** + * @brief Free IO object. + * + * @param io Valid pointer to the IO object. + */ +void bh_io_free(bh_io_t *io); + +/** + * @brief Read data from IO. + * + * @param io Valid pointer to the IO object. + * @param data Valid pointer to the buffer. + * @param size Size of the buffer. + * + * @return If the function succeeds, the return value is the amount + * of bytes read from the IO object. + * @return If the function fails, the return value is zero and error + * flag is set. + * + * @sa bh_io_write, bh_io_eof, bh_io_error + */ +size_t bh_io_read(bh_io_t *io, + char *data, + size_t size); + +/** + * @brief Write data to IO. + * + * @param io Valid pointer to the IO object. + * @param data Valid pointer to the buffer. + * @param size Size of the buffer. + * + * @return If the function succeeds, the return value is the amount + * of bytes written to the IO object. + * @return If the function fails, the return value is zero and error + * flag is set. + * + * @sa bh_io_read, bh_io_error, bh_io_flush + */ +size_t bh_io_write(bh_io_t *io, + const char* data, + size_t size); + +/** + * @brief Writes any uncommited changes (if possible). + * + * @param io Valid pointer to the IO object. + */ +void bh_io_flush(bh_io_t *io); + +/** + * @brief Seeks IO object to the specified position (if possible). + * + * @param io Valid pointer to the IO object. + * @param offset Position + * @param dir Direction + * + * @sa bh_io_tell + */ +void bh_io_seek(bh_io_t *io, + bh_off_t offset, + int dir); + +/** + * @brief Return current position in IO. + * + * @param io Valid pointer to the IO object. + * + * @return If the function succeeds, the return value is current + * position in the IO. + * @return If the function fails, the return value is -1. + * + * @sa bh_io_seek + */ +bh_off_t bh_io_tell(bh_io_t *io); + +/** + * @brief Return available bytes in the IO. + * + * @param io Valid pointer to the IO object. + * + * @return If the function succeeds, the return value is the amount + * of the available bytes for reading. + * @return If the function fails, the return value is zero. + */ +bh_off_t bh_io_available(bh_io_t *io); + +/** + * @brief Return error flag of the IO. + * + * @param io Valid pointer to the IO object. + * + * @return The return value is error flag. + * + * @sa bh_io_eof, bh_io_clear + */ +int bh_io_error(bh_io_t *io); + +/** + * @brief Return end-of-file flag of the IO. + * + * @param io Valid pointer to the IO object. + * + * @return The return value is end-of-file flag. + * + * @sa bh_io_error, bh_io_clear + */ +int bh_io_eof(bh_io_t *io); + +/** + * @brief Crear IO object state. + * + * @param io Valid pointer to the IO object. + * + * @sa bh_io_error, bh_io_eof + */ +void bh_io_clear(bh_io_t *io); + + +#endif /* BHLIB_IO_H */ diff --git a/include/bh/platform.h b/include/bh/platform.h new file mode 100644 index 0000000..c48149c --- /dev/null +++ b/include/bh/platform.h @@ -0,0 +1,46 @@ +#ifndef BHLIB_PLATFORM_H +#define BHLIB_PLATFORM_H + +#include <stddef.h> + +/* Common type definitions */ +typedef signed char bh_int8_t; +typedef unsigned char bh_uint8_t; +typedef signed short bh_int16_t; +typedef unsigned short bh_uint16_t; +typedef float bh_float32_t; +typedef double bh_float64_t; + +/* Platform specific type definition */ +#if __STDC_VERSION__ >= 199901L || defined(__GNUC__) +#include <stdint.h> + +typedef int32_t bh_int32_t; +typedef uint32_t bh_uint32_t; +typedef int64_t bh_int64_t; +typedef uint64_t bh_uint64_t; +typedef intptr_t bh_intptr_t; +typedef uintptr_t bh_uintptr_t; + +#elif defined(_WIN32) +typedef __int32 bh_int32_t; +typedef unsigned __int32 bh_uint32_t; +typedef __int64 bh_int64_t; +typedef unsigned __int64 bh_uint64_t; + +#if defined(_WIN64) +typedef __int64 bh_intptr_t; +typedef unsigned __int64 bh_uintptr_t; +#else +typedef __int32 bh_intptr_t; +typedef unsigned __int32 bh_uintptr_t; +#endif + +#else + +#error "Unsupported platform" +#endif + +typedef bh_int64_t bh_off_t; + +#endif /* BHLIB_PLATFORM_H */ diff --git a/include/bh/queue.h b/include/bh/queue.h index c5185ef..d25af65 100644 --- a/include/bh/queue.h +++ b/include/bh/queue.h @@ -1,7 +1,7 @@ #ifndef BH_QUEUE_H #define BH_QUEUE_H -#include <bh/bh.h> +#include "bh.h" typedef struct bh_queue_s bh_queue_t; @@ -30,7 +30,7 @@ void bh_queue_free(bh_queue_t *queue); * * @param queue Valid pointer to the queue object. * - * @warning Clearing the queue does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_queue_remove */ @@ -45,7 +45,7 @@ void bh_queue_clear(bh_queue_t *queue); * @return If the function succeeds, the return value is zero. * @return If the function fails, the return value is non-zero. * - * @warning Reserving queue space does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_queue_capacity, bh_queue_insert */ @@ -62,7 +62,7 @@ int bh_queue_reserve(bh_queue_t *queue, * @return If the function fails, the return value is non-zero. * * @warning Inserted element is owned by the caller of the function. - * @warning Inserting elements into the queue does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_queue_remove, bh_queue_front */ @@ -74,7 +74,7 @@ int bh_queue_insert(bh_queue_t *queue, * * @param queue Valid pointer to the queue object. * - * @warning Removing elements from the queue does invalidate iterators. + * @warning Calling this function will invalidate iterators. * * @sa bh_queue_insert, bh_queue_front */ @@ -89,9 +89,6 @@ void bh_queue_remove(bh_queue_t *queue); * the element. * @return If the function fails, the return value is NULL. * - * @note If the queue is empty, function will fail to return element from - * the queue. - * * @sa bh_queue_empty, bh_queue_insert */ void *bh_queue_front(bh_queue_t *queue); diff --git a/include/bh/thread.h b/include/bh/thread.h index 21edc1c..45a0e6e 100644 --- a/include/bh/thread.h +++ b/include/bh/thread.h @@ -43,6 +43,50 @@ bh_thread_pool_t *bh_thread_pool_new_base(size_t size, #endif /** + * @brief Create new task + * + * @param func Function + * @param data Function data + * @param flags Task flags + * + * @return Pointer to the new task + * + * @sa bh_task_free, bh_task_reuse, bh_task_done + */ +bh_task_t *bh_task_new(void (*func)(void *), + void *data, + int flags); + +/** + * @brief Free the task. + * + * @param task Pointer to the task + */ +void bh_task_free(bh_task_t *task); + +/** + * @brief Reuse task. + * + * @param task Pointer to the task + * @param func Function + * @param data Data + * + * @sa bh_task_free, bh_task_done + */ +void bh_task_reuse(bh_task_t *task, + void (*func)(void *), + void *data); + +/** + * @brief Check if task is done. + * + * @param task Pointer to the task + * + * @return The return value is boolean flag, indicating if the task is done. + */ +int bh_task_done(bh_task_t *task); + +/** * @function bh_thread_new * * @brief Create new thread. @@ -205,18 +249,6 @@ int bh_cond_signal(bh_cond_t *cond); */ int bh_cond_broadcast(bh_cond_t *cond); -bh_task_t *bh_task_new(void (*func)(void *), - void *data, - int flags); - -void bh_task_free(bh_task_t *task); - -void bh_task_reuse(bh_task_t *task, - void (*func)(void *), - void *data); - -int bh_task_done(bh_task_t *task); - /** * @brief Submit task to the thread pool. * @@ -4,18 +4,6 @@ #include <stdio.h> #include <stdint.h> -#define BH_INT_TO_PTR(x) \ - ((void *)((long)(x))) - -#define BH_UINT_TO_PTR(x) \ - ((void *)((unsigned long)(x))) - -#define BH_PTR_TO_INT(x) \ - ((long)(x)) - -#define BH_PTR_TO_UINT(x) \ - ((unsigned long)(x)) - size_t ptr_hash(const void *item) { return BH_PTR_TO_INT(item); @@ -59,7 +47,7 @@ void factor_task(void *arg) { printf("Task start\n"); fflush(stdout); - printf("Factor: %d\n", factor(48)); + printf("Factor: %d\n", factor(42)); fflush(stdout); } @@ -119,7 +107,7 @@ int main() iter = bh_queue_iter_next(queue, NULL); while (iter) { - printf("%d\n", (int)bh_queue_iter_value(iter)); + printf("%d\n", BH_PTR_TO_INT(bh_queue_iter_value(iter))); iter = bh_queue_iter_next(queue, iter); } diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 9597cf6..6e858b6 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -1,5 +1,5 @@ #!/bin/sh -ulimit -Sv 320000 +#ulimit -Sv 320000 rm -r build coverage mkdir build coverage cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS_DEBUG="-g -fprofile-arcs -ftest-coverage" diff --git a/src/deflate.c b/src/deflate.c new file mode 100644 index 0000000..182fae0 --- /dev/null +++ b/src/deflate.c @@ -0,0 +1 @@ +#include <bh/deflate.h> diff --git a/src/file_posix.c b/src/file_posix.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/file_posix.c diff --git a/src/file_win.c b/src/file_win.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/file_win.c diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..e4d7833 --- /dev/null +++ b/src/io.c @@ -0,0 +1,83 @@ +#include <bh/internal/io.h> +#include <stdlib.h> + +bh_io_t *bh_io_new(bh_io_table_t *table, + size_t size) +{ + bh_io_t *result; + + result = malloc(size); + if (result) + bh_io_init(result, table); + + return result; +} + +void bh_io_free(bh_io_t *io) +{ + bh_io_destroy(io); + free(io); +} + +void bh_io_init(bh_io_t *io, + bh_io_table_t *table) +{ + io->table = table; + io->flags = 0; +} + +void bh_io_destroy(bh_io_t *io) +{ + io->table->destroy(io); +} + +size_t bh_io_read(bh_io_t *io, + char *data, + size_t size) +{ + return io->table->read(io, data, size); +} + +size_t bh_io_write(bh_io_t *io, + const char* data, + size_t size) +{ + return io->table->write(io, data, size); +} + +void bh_io_flush(bh_io_t *io) +{ + io->table->flush(io); +} + +void bh_io_seek(bh_io_t *io, + bh_off_t offset, + int dir) +{ + io->table->seek(io, offset, dir); +} + +bh_off_t bh_io_tell(bh_io_t *io) +{ + return io->table->tell(io); +} + +bh_off_t bh_io_available(bh_io_t *io) +{ + return io->table->available(io); +} + +int bh_io_error(bh_io_t *io) +{ + return (io->flags & BH_IO_ERROR) > 0; +} + +int bh_io_eof(bh_io_t *io) +{ + return (io->flags & BH_IO_EOF) > 0; +} + +void bh_io_clear(bh_io_t *io) +{ + io->table->clear(io); +} diff --git a/src/thread.c b/src/thread.c index 645dd0b..77f36bb 100644 --- a/src/thread.c +++ b/src/thread.c @@ -216,4 +216,4 @@ void bh_thread_pool_worker(void *arg) bh_mutex_unlock(&pool->lock); bh_cond_broadcast(&pool->done_task); } -}
\ No newline at end of file +} diff --git a/src/thread_null.c b/src/thread_null.c new file mode 100644 index 0000000..494e0b6 --- /dev/null +++ b/src/thread_null.c @@ -0,0 +1,113 @@ +#include <bh/internal/thread.h> + +int bh_thread_init(bh_thread_t *thread, + bh_task_t *task) +{ + (void)thread; + (void)task; + return -1; +} + +bh_thread_t *bh_thread_new(bh_task_t *task) +{ + (void)task; + return NULL; +} + +int bh_thread_join(bh_thread_t *thread) +{ + (void)thread; + return -1; +} + +int bh_thread_detach(bh_thread_t *thread) +{ + (void)thread; + return -1; +} + +int bh_mutex_init(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +void bh_mutex_destroy(bh_mutex_t *mutex) +{ + (void)mutex; +} + +int bh_mutex_lock(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +int bh_mutex_try_lock(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +int bh_mutex_unlock(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +int bh_cond_init(bh_cond_t *cond) +{ + (void)cond; + return -1; +} + +void bh_cond_destroy(bh_cond_t *cond) +{ + (void)cond; +} + +int bh_cond_wait(bh_cond_t *cond, + bh_mutex_t *mutex) +{ + (void)cond; + (void)mutex; + return -1; +} + +int bh_cond_wait_for(bh_cond_t *cond, + bh_mutex_t *mutex, + unsigned long timeout) +{ + (void)cond; + (void)mutex; + (void)timeout; + return -1; +} + +int bh_cond_signal(bh_cond_t *cond) +{ + (void)cond; + return -1; +} + +int bh_cond_broadcast(bh_cond_t *cond) +{ + (void)cond; + return -1; +} + +int bh_thread_pool_init(bh_thread_pool_t *pool, + size_t size) +{ + (void)pool; + (void)size; + return -1; +} + +bh_thread_pool_t *bh_thread_pool_new(size_t size) +{ + (void)size; + return NULL; +} + + diff --git a/src/thread_posix.c b/src/thread_posix.c index 2ed3ab8..22d4c62 100644 --- a/src/thread_posix.c +++ b/src/thread_posix.c @@ -20,17 +20,46 @@ static void *bh_thread_run(void *arg) int bh_thread_init(bh_thread_t *thread, bh_task_t *task) { + thread->allocated = 0; return pthread_create(&thread->handle, NULL, bh_thread_run, task); } +bh_thread_t *bh_thread_new(bh_task_t *task) +{ + bh_thread_t *result; + + result = malloc(sizeof(*result)); + + if (result && bh_thread_init(result, task)) + { + free(result); + result = NULL; + } + + if (result) + result->allocated = 1; + + return result; +} + int bh_thread_join(bh_thread_t *thread) { - return pthread_join(thread->handle, NULL); + int result = pthread_join(thread->handle, NULL); + + if (thread->allocated) + free(thread); + + return result; } int bh_thread_detach(bh_thread_t *thread) { - return pthread_detach(thread->handle); + int result = pthread_detach(thread->handle); + + if (thread->allocated) + free(thread); + + return result; } int bh_mutex_init(bh_mutex_t *mutex) diff --git a/src/thread_win.c b/src/thread_win.c new file mode 100644 index 0000000..ae5f6a9 --- /dev/null +++ b/src/thread_win.c @@ -0,0 +1,120 @@ +#include <bh/internal/thread.h> + +int bh_thread_init_base(bh_thread_t *thread, + bh_task_t *task, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end) +{ + (void)thread; + (void)task; + return -1; +} + +bh_thread_t *bh_thread_new_base(bh_task_t *task, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end) +{ + (void)task; + return NULL; +} + +int bh_thread_join(bh_thread_t *thread) +{ + (void)thread; + return -1; +} + +int bh_thread_detach(bh_thread_t *thread) +{ + (void)thread; + return -1; +} + +int bh_mutex_init(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +void bh_mutex_destroy(bh_mutex_t *mutex) +{ + (void)mutex; +} + +int bh_mutex_lock(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +int bh_mutex_try_lock(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +int bh_mutex_unlock(bh_mutex_t *mutex) +{ + (void)mutex; + return -1; +} + +int bh_cond_init(bh_cond_t *cond) +{ + (void)cond; + return -1; +} + +void bh_cond_destroy(bh_cond_t *cond) +{ + (void)cond; +} + +int bh_cond_wait(bh_cond_t *cond, + bh_mutex_t *mutex) +{ + (void)cond; + (void)mutex; + return -1; +} + +int bh_cond_wait_for(bh_cond_t *cond, + bh_mutex_t *mutex, + unsigned long timeout) +{ + (void)cond; + (void)mutex; + (void)timeout; + return -1; +} + +int bh_cond_signal(bh_cond_t *cond) +{ + (void)cond; + return -1; +} + +int bh_cond_broadcast(bh_cond_t *cond) +{ + (void)cond; + return -1; +} + +int bh_thread_pool_init_base(bh_thread_pool_t *pool, + size_t size, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end) +{ + (void)pool; + (void)size; + return -1; +} + +bh_thread_pool_t *bh_thread_pool_new_base(size_t size, + bh_thread_begin_cb_t begin, + bh_thread_end_cb_t end) +{ + (void)size; + return NULL; +} + |
