Sync to the latest version.

Can be broken or partially implemented.
This commit is contained in:
2024-04-23 23:45:43 +03:00
parent ec499b6cfc
commit 692b5b4297
26 changed files with 785 additions and 60 deletions

View File

@@ -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,

View File

@@ -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 *);

5
include/bh/deflate.h Normal file
View File

@@ -0,0 +1,5 @@
#ifndef BHLIB_DEFLATE_H
#define BHLIB_DEFLATE_H
#endif /* BHLIB_DEFLATE_H */

37
include/bh/file.h Normal file
View File

@@ -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 */

View File

@@ -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
*/

View File

View File

12
include/bh/internal/io.h Normal file
View File

@@ -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 */

View File

@@ -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);

View File

@@ -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);
void bh_thread_pool_destroy(bh_thread_pool_t *pool);

View File

@@ -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);

182
include/bh/io.h Normal file
View File

@@ -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 */

46
include/bh/platform.h Normal file
View File

@@ -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 */

View File

@@ -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);

View File

@@ -42,6 +42,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
*
@@ -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.
*