Add documentation, expand error handling, implement file and buffer io

This commit is contained in:
2024-06-03 22:11:05 +03:00
parent fdbabab0e0
commit 79874622a2
37 changed files with 5531 additions and 986 deletions

View File

@@ -1,154 +1,63 @@
#ifndef BHLIB_ALGO_H
#define BHLIB_ALGO_H
#ifndef BH_ALGO_H
#define BH_ALGO_H
#include "bh.h"
/**
* @brief Swap two elements.
*
* @param lhs Pointer to the first element
* @param rhs Pointer to the second element
* @param size Element size
*/
void bh_swap(void *lhs,
void *rhs,
void bh_swap(void *dest,
void *src,
size_t size);
/**
* @brief Partition the array.
*
* @param pivot Pointer to the pivot element
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*
* @return The return value is the pointer to the first element of the second
* partition.
*
* @warning Pivot element can be a part of the partitioned array.
*/
void *bh_partition(void *pivot,
void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Sort the array.
*
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*/
void bh_sort(void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Sort the array using insert sort.
*
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*/
void bh_sort_insert(void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Sort the array using shell sort.
*
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*/
void bh_sort_shell(void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Sort the array using intro sort.
*
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*/
void bh_sort_intro(void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Sort the array using heap sort.
*
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*/
void bh_sort_heap(void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Make heap from the array.
*
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*
* @sa bh_heap_remove, bh_heap_insert
*/
void bh_heap_make(void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Remove element from the heap.
*
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*
* @warning Removed element is placed at the end of the array
*
* @sa bh_heap_make, bh_heap_remove
*/
void bh_heap_remove(void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
/**
* @brief Insert element to the heap.
*
* @param value Pointer to the inserted value (optional)
* @param array Pointer to the array
* @param size Array size
* @param element Element size
* @param equal Equal/compare function
*
* @warning If value is not passed, function assumes inserted element
* is already placed at the end of the array.
*
* @sa bh_heap_make, bh_heap_remove
*/
void bh_heap_insert(void *value,
void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
#endif /* BHLIB_ALGO_H */
void bh_heap_replace(void *value,
void *array,
size_t size,
size_t element,
bh_equal_cb_t equal);
#endif /* BH_ALGO_H */

View File

@@ -1,25 +1,34 @@
#ifndef BHLIB_H
#define BHLIB_H
#ifndef BH_H
#define BH_H
#include <bh/config.h>
#include <stddef.h>
#include "platform.h"
#define BH_INT_TO_PTR(x) \
#define BH_INT_TO_PTR(x) \
((void *)((bh_intptr_t)(x)))
#define BH_UINT_TO_PTR(x) \
#define BH_UINT_TO_PTR(x) \
((void *)((bh_uintptr_t)(x)))
#define BH_PTR_TO_INT(x) \
#define BH_PTR_TO_INT(x) \
((bh_intptr_t)(x))
#define BH_PTR_TO_UINT(x) \
#define BH_PTR_TO_UINT(x) \
((bh_uintptr_t)(x))
#define BH_OK 0x0000
#define BH_ERROR 0x0001
#define BH_OOM 0x0002
#define BH_INVALID 0x0003
#define BH_NO_IMPL 0x0004
#define BH_FOUND 0x0005
#define BH_NOT_FOUND 0x0006
#define BH_TIMEOUT 0x0007
typedef int (*bh_equal_cb_t)(const void *, const void *);
typedef size_t (*bh_hash_cb_t)(const void *);
#endif /* BHLIB_H */
#endif /* BH_H */

91
include/bh/buffer.h Normal file
View File

@@ -0,0 +1,91 @@
#ifndef BH_BUFFER_H
#define BH_BUFFER_H
#include "io.h"
typedef struct bh_buffer_s bh_buffer_t;
bh_buffer_t *bh_buffer_new(void);
void bh_buffer_free(bh_buffer_t *buffer);
const char *bh_buffer_data(bh_buffer_t *buffer);
void bh_buffer_set_data(bh_buffer_t *buffer,
const char *data,
size_t size);
size_t bh_buffer_capacity(bh_buffer_t *buffer);
int bh_buffer_reserve(bh_buffer_t *buffer,
size_t size);
int bh_buffer_open_base(bh_buffer_t *buffer,
int mode);
void bh_buffer_close_base(bh_buffer_t *buffer);
int bh_buffer_is_open_base(bh_buffer_t *buffer);
size_t bh_buffer_read_base(bh_buffer_t *buffer,
char *data,
size_t size);
size_t bh_buffer_write_base(bh_buffer_t *buffer,
const char *data,
size_t size);
void bh_buffer_flush_base(bh_buffer_t *buffer);
int bh_buffer_seek_base(bh_buffer_t *buffer,
bh_off_t pos,
int dir);
bh_off_t bh_buffer_size_base(bh_buffer_t *buffer);
bh_off_t bh_buffer_tell_base(bh_buffer_t *buffer);
bh_off_t bh_buffer_available_base(bh_buffer_t *buffer);
void bh_buffer_clear_base(bh_buffer_t *buffer);
#define bh_buffer_open(buffer, mode) \
bh_io_open((bh_io_t *)(buffer), (mode))
#define bh_buffer_close(buffer) \
bh_io_close((bh_io_t *)(buffer))
#define bh_buffer_is_open(buffer) \
bh_io_is_open((bh_io_t *)(buffer))
#define bh_buffer_read(buffer, data, size) \
bh_io_read((bh_io_t *)(buffer), (data), (size))
#define bh_buffer_write(buffer, data, size) \
bh_io_write((bh_io_t *)(buffer), (data), (size))
#define bh_buffer_flush(buffer) \
bh_io_flush((bh_io_t *)(buffer))
#define bh_buffer_seek(buffer, pos, dir) \
bh_io_seek((bh_io_t *)(buffer), (pos), (dir))
#define bh_buffer_size(buffer) \
bh_io_size((bh_io_t *)(buffer))
#define bh_buffer_tell(buffer) \
bh_io_tell((bh_io_t *)(buffer))
#define bh_buffer_available(buffer) \
bh_io_available((bh_io_t *)(buffer))
#define bh_buffer_clear(buffer) \
bh_io_clear((bh_io_t *)(buffer))
#define bh_buffer_error(buffer) \
bh_io_error((bh_io_t *)(buffer))
#define bh_buffer_eof(buffer) \
bh_io_eof((bh_io_t *)(buffer))
#endif /* BH_BUFFER_H */

View File

@@ -1,8 +1,10 @@
#ifndef BHLIB_CONFIG_H
#define BHLIB_CONFIG_H
#ifndef BH_CONFIG_H
#define BH_CONFIG_H
#cmakedefine BHLIB_USE_WINTHREAD
#cmakedefine BHLIB_USE_PTHREAD
#cmakedefine BHLIB_NO_WINXP
#cmakedefine BH_USE_WINTHREAD
#cmakedefine BH_USE_PTHREAD
#cmakedefine BH_NO_WINXP
#cmakedefine BH_PLATFORM_POSIX
#cmakedefine BH_PLATFORM_WIN
#endif /* BHLIB_CONFIG_H */
#endif /* BH_CONFIG_H */

View File

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

View File

@@ -1,37 +1,80 @@
#ifndef BHLIB_FILE_H
#define BHLIB_FILE_H
#ifndef BH_FILE_H
#define BH_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);
bh_file_t *bh_file_new(const char *path);
void bh_file_free(bh_file_t *file);
size_t bh_file_read(bh_file_t *file,
char *buffer,
size_t size);
int bh_file_open_base(bh_file_t *file,
int mode);
size_t bh_file_write(bh_file_t *file,
const char *buffer,
size_t size);
void bh_file_close_base(bh_file_t *file);
void bh_file_flush(bh_file_t *file);
int bh_file_is_open_base(bh_file_t *file);
void bh_file_seek(bh_file_t *file,
bh_off_t pos,
int dir);
size_t bh_file_read_base(bh_file_t *file,
char *data,
size_t size);
bh_off_t bh_file_tell(bh_file_t *file);
size_t bh_file_write_base(bh_file_t *file,
const char *data,
size_t size);
bh_off_t bh_file_available(bh_file_t *file);
void bh_file_flush_base(bh_file_t *file);
int bh_file_error(bh_file_t *file);
int bh_file_seek_base(bh_file_t *file,
bh_off_t pos,
int dir);
int bh_file_eof(bh_file_t *file);
bh_off_t bh_file_size_base(bh_file_t *file);
void bh_file_clear(bh_file_t *file);
bh_off_t bh_file_tell_base(bh_file_t *file);
#endif /* BHLIB_FILE_H */
bh_off_t bh_file_available_base(bh_file_t *file);
void bh_file_clear_base(bh_file_t *file);
#define bh_file_open(file, mode) \
bh_io_open((bh_io_t *)(file), (mode))
#define bh_file_close(file) \
bh_io_close((bh_io_t *)(file))
#define bh_file_is_open(file) \
bh_io_is_open((bh_io_t *)(file))
#define bh_file_read(file, data, size) \
bh_io_read((bh_io_t *)(file), (data), (size))
#define bh_file_write(file, data, size) \
bh_io_write((bh_io_t *)(file), (data), (size))
#define bh_file_flush(file) \
bh_io_flush((bh_io_t *)(file))
#define bh_file_seek(file, pos, dir) \
bh_io_seek((bh_io_t *)(file), (pos), (dir))
#define bh_file_size(file) \
bh_io_size((bh_io_t *)(file))
#define bh_file_tell(file) \
bh_io_tell((bh_io_t *)(file))
#define bh_file_available(file) \
bh_io_available((bh_io_t *)(file))
#define bh_file_clear(file) \
bh_io_clear((bh_io_t *)(file))
#define bh_file_error(file) \
bh_io_error((bh_io_t *)(file))
#define bh_file_eof(file) \
bh_io_eof((bh_io_t *)(file))
#endif /* BH_FILE_H */

View File

@@ -1,231 +1,53 @@
#ifndef BHLIB_HASHMAP_H
#define BHLIB_HASHMAP_H
#ifndef BH_HASHMAP_H
#define BH_HASHMAP_H
#include "bh.h"
typedef struct bh_hashmap_s bh_hashmap_t;
/**
* @brief Create new hashmap object.
*
* @param equal Function used for comparing keys
* @param hash Function used to calculate hash value of the key
*
* @return If the function succeeds, the return value is a pointer to the new
* hashmap object.
* @return If the function fails, the return value is NULL.
*
* @sa bh_hashmap_free, bh_hashmap_reserve, bh_hashmap_insert
*/
bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal,
bh_hash_cb_t hash);
/**
* @brief Free hashmap object.
*
* @param hashmap Valid pointer to the hashmap object.
*
* @sa bh_hashmap_clear
*/
void bh_hashmap_free(bh_hashmap_t *hashmap);
/**
* @brief Clear the hashmap.
*
* @param hashmap Valid pointer to the hashmap object.
*
* @warning Calling this function will invalidate iterators.
*
* @sa bh_hashmap_remove
*/
void bh_hashmap_clear(bh_hashmap_t *hashmap);
/**
* @brief Reserve space for the specified amount of elements.
*
* @param hashmap Valid pointer to the hashmap object.
* @param size The amount of elements.
*
* @return If the function succeeds, the return value is zero.
* @return If the function fails, the return value is non-zero.
*
* @warning Calling this function will invalidate iterators.
*
* @sa bh_hashmap_capacity, bh_hashmap_insert
*/
int bh_hashmap_reserve(bh_hashmap_t *hashmap,
size_t size);
/**
* @brief Insert key/value into the hashmap.
*
* @param hashmap Valid pointer to the hashmap object.
* @param key Key
* @param value Value
*
* @return If the function succeeds, the return value is zero.
* @return If the function fails, the return value is non-zero.
*
* @warning Inserted element is owned by the caller of the function.
* @warning Calling this function will invalidate iterators.
*
* @sa bh_hashmap_remove, bh_hashmap_at
*/
int bh_hashmap_insert(bh_hashmap_t *hashmap,
void *key,
void *value);
/**
* @brief Remove element from the hashmap.
*
* @param hashmap Valid pointer to the hashmap object.
* @param key Key.
*
* @warning Calling this function will invalidate iterators.
*
* @sa bh_hashmap_insert, bh_hashmap_at
*/
void bh_hashmap_remove(bh_hashmap_t *hashmap,
void *key);
/**
* @brief Return element value by the key from the hashmap.
*
* @param hashmap Valid pointer to the hashmap.
* @param key Key.
* @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.
*
* @sa bh_hashmap_empty, bh_hashmap_insert
*/
void *bh_hashmap_at(bh_hashmap_t *hashmap,
void *key,
int *exists);
/**
* @brief Check if the hashmap is empty.
*
* @param hashmap Valid pointer to the hashmap object.
*
* @return The return value is non-zero if the hashmap is empty, otherwise
* zero.
*
* @sa bh_hashmap_size
*/
int bh_hashmap_empty(bh_hashmap_t *hashmap);
/**
* @brief Return hashmap size.
*
* @param hashmap Valid pointer to the hashmap object.
*
* @return The return value is current hashmap size.
*
* @sa bh_hashmap_empty, bh_hashmap_capacity
*/
size_t bh_hashmap_size(bh_hashmap_t *hashmap);
/**
* @brief Return hashmap capacity.
*
* @param hashmap Valid pointer to the hashmap object.
*
* @return The return value is current hashmap capacity.
*
* @sa bh_hashmap_reserve, bh_hashmap_size
*/
size_t bh_hashmap_capacity(bh_hashmap_t *hashmap);
/**
* @brief Return hashmap load factor.
*
* @param hashmap Valid pointer to the hashmap object.
*
* @return The return value is current hashmap load factor.
*
* @sa bh_hashmap_set_factor, bh_hashmap_capacity
*/
float bh_hashmap_factor(bh_hashmap_t *hashmap);
/**
* @brief Set hashmap load factor.
*
* @param hashmap Valid pointer to the hashmap object.
* @param factor Load factor.
*
* @sa bh_hashmap_factor
*/
void bh_hashmap_set_factor(bh_hashmap_t *hashmap,
float factor);
/**
* @brief Return iterator for the element by the key.
*
* @param hashmap Valid pointer to the hashmap object.
* @param key Key
*
* @return The return value is the valid iterator for the element in the
* hashmap.
* @return The return value is the NULL iterator if there is no element with
* specified key.
*
* @sa bh_hashmap_iter_key, bh_hashmap_iter_value
*/
void *bh_hashmap_iter_at(bh_hashmap_t *hashmap,
void *key);
/**
* @brief Return iterator for the next element in the hashmap.
*
* @param hashmap Valid pointer to the hashmap object.
* @param iter Valid or NULL iterator.
*
* @return The return value is the valid iterator for the next element in the
* hashmap.
* @return The return value is the NULL iterator if there is no more elements
* in the hashmap.
*
* @sa bh_hashmap_iter_key, bh_hashmap_iter_value
*/
void *bh_hashmap_iter_next(bh_hashmap_t *hashmap,
void *iter);
/**
* @brief Remove element from the hashmap by the iterator.
*
* @param hashmap Valid pointer to the hashmap object.
* @param key Valid iterator.
*
* @warning Calling this function will invalidate iterators.
*
* @sa bh_hashmap_insert, bh_hashmap_at
*/
void bh_hashmap_iter_remove(bh_hashmap_t *hashmap,
void *iter);
/**
* @brief Return pointer to the element's key.
*
* @param iter Valid iterator.
*
* @return The return value is the stored element key.
*
* @sa bh_hashmap_iter_value, bh_hashmap_iter_next
*/
void *bh_hashmap_iter_key(void *iter);
/**
* @brief Return pointer to the element's value.
*
* @param iter Valid iterator.
*
* @return The return value is the stored element value.
*
* @sa bh_hashmap_iter_key, bh_hashmap_iter_next
*/
void *bh_hashmap_iter_value(void *iter);
#endif /* BHLIB_HASHMAP_H */
#endif /* BH_HASHMAP_H */

View File

@@ -0,0 +1,20 @@
#ifndef BH_INTERNAL_BUFFER_H
#define BH_INTERNAL_BUFFER_H
#include <bh/buffer.h>
struct bh_buffer_s
{
bh_io_t base;
char *data;
size_t capacity;
size_t size;
size_t at;
int mode;
};
int bh_buffer_init(bh_buffer_t *buffer);
void bh_buffer_destroy(bh_buffer_t *buffer);
#endif /* BH_INTERNAL_BUFFER_H */

View File

@@ -0,0 +1,20 @@
#ifndef BH_INTERNAL_FILE_H
#define BH_INTERNAL_FILE_H
#include "io.h"
#include <bh/file.h>
#if defined(BH_PLATFORM_POSIX)
#include "file_posix.h"
#elif defined(BH_PLATFORM_WIN)
#include "file_win.h"
#else
#include "file_null.h"
#endif
int bh_file_init(bh_file_t *file,
const char *path);
void bh_file_destroy(bh_file_t *file);
#endif /* BH_INTERNAL_FILE_H */

View File

@@ -0,0 +1,4 @@
struct bh_file_s
{
bh_io_t base;
};

View File

@@ -0,0 +1,9 @@
#include <unistd.h>
struct bh_file_s
{
bh_io_t base;
char *path;
int mode;
int handle;
};

View File

@@ -0,0 +1,9 @@
#include <windows.h>
struct bh_file_s
{
bh_io_t base;
char *path;
int mode;
HANDLE handle;
};

View File

@@ -1,5 +1,5 @@
#ifndef BHLIB_INTERNAL_HASHMAP_H
#define BHLIB_INTERNAL_HASHMAP_H
#ifndef BH_INTERNAL_HASHMAP_H
#define BH_INTERNAL_HASHMAP_H
#include <bh/hashmap.h>
@@ -27,4 +27,4 @@ void bh_hashmap_init(bh_hashmap_t *hashmap,
void bh_hashmap_destroy(bh_hashmap_t *hashmap);
#endif /* BHLIB_INTERNAL_HASHMAP_H */
#endif /* BH_INTERNAL_HASHMAP_H */

View File

@@ -1,12 +1,12 @@
#ifndef BHLIB_INTERNAL_IO_H
#define BHLIB_INTERNAL_IO_H
#ifndef BH_INTERNAL_IO_H
#define BH_INTERNAL_IO_H
#include <bh/io.h>
void bh_io_init(bh_io_t *io,
bh_io_table_t *table);
const bh_io_table_t *table);
void bh_io_destroy(bh_io_t *io);
#endif /* BHLIB_INTERNAL_IO_H */
#endif /* BH_INTERNAL_IO_H */

View File

@@ -1,5 +1,5 @@
#ifndef BHLIB_INTERNAL_QUEUE_H
#define BHLIB_INTERNAL_QUEUE_H
#ifndef BH_INTERNAL_QUEUE_H
#define BH_INTERNAL_QUEUE_H
#include <bh/queue.h>
@@ -12,24 +12,8 @@ struct bh_queue_s
size_t capacity;
};
/**
* @internal
* @brief Initialize embedded queue object.
*
* @param queue Valid pointer to the queue object.
*
* @sa bh_queue_destroy
*/
void bh_queue_init(bh_queue_t *queue);
/**
* @internal
* @brief Destroy embedded queue object.
*
* @param queue Valid pointer to the queue object.
*
* @sa bh_queue_init
*/
void bh_queue_destroy(bh_queue_t *queue);
#endif /* BHLIB_INTERNAL_QUEUE_H */
#endif /* BH_INTERNAL_QUEUE_H */

View File

@@ -1,14 +1,14 @@
#ifndef BHLIB_INTERNAL_THREAD_H
#define BHLIB_INTERNAL_THREAD_H
#ifndef BH_INTERNAL_THREAD_H
#define BH_INTERNAL_THREAD_H
#include <bh/thread.h>
#include "queue.h"
#define BH_THREAD_DONE (1 << 8)
#define BH_THREAD_DONE 0x0100
#if defined(BHLIB_USE_PTHREAD)
#if defined(BH_USE_PTHREAD)
#include "thread_posix.h"
#elif defined(BHLIB_USE_WINTHREAD)
#elif defined(BH_USE_WINTHREAD)
#include "thread_win.h"
#else
#include "thread_null.h"
@@ -42,4 +42,4 @@ void bh_task_destroy(bh_task_t *task);
void bh_thread_pool_worker(void *arg);
#endif /* BHLIB_INTERNAL_THREAD_H */
#endif /* BH_INTERNAL_THREAD_H */

View File

@@ -50,7 +50,7 @@ int bh_thread_init_base(bh_thread_t *thread,
int bh_mutex_init(bh_mutex_t *mutex);
void bh_mutex_destroy(bh_mutex_t *mutex);
int bh_semaphore_init(bh_semaphore_t *semaphore,
int bh_semaphore_init(bh_semaphore_t *semaphore,
int count);
void bh_semaphore_destroy(bh_semaphore_t *semaphore);

View File

@@ -1,10 +1,24 @@
#ifndef BHLIB_IO_H
#define BHLIB_IO_H
#ifndef BH_IO_H
#define BH_IO_H
#include "bh.h"
#define BH_IO_ERROR (1 << 0)
#define BH_IO_EOF (1 << 1)
#define BH_IO_ERROR 0x0001
#define BH_IO_EOF 0x0002
#define BH_IO_READ 0x0001
#define BH_IO_WRITE 0x0002
#define BH_IO_READ_WRITE (BH_IO_READ | BH_IO_WRITE)
#define BH_IO_OPEN 0x0000
#define BH_IO_CREATE 0x0100
#define BH_IO_APPEND 0x0200
#define BH_IO_TRUNCATE 0x0300
#define BH_IO_MASK 0xFF00
#define BH_IO_SET 0x0000
#define BH_IO_CURRENT 0x0001
#define BH_IO_END 0x0002
#define BH_IO_CAST(x) \
((bh_io_t *)(x))
@@ -13,6 +27,13 @@ struct bh_io_s;
typedef struct bh_io_table_s
{
int (*open)(struct bh_io_s *io,
int mode);
void (*close)(struct bh_io_s *io);
int (*is_open)(struct bh_io_s *io);
size_t (*read)(struct bh_io_s *io,
char *data,
size_t size);
@@ -23,9 +44,11 @@ typedef struct bh_io_table_s
void (*flush)(struct bh_io_s *io);
void (*seek)(struct bh_io_s *io,
bh_off_t offset,
int dir);
int (*seek)(struct bh_io_s *io,
bh_off_t offset,
int dir);
bh_off_t (*size)(struct bh_io_s *io);
bh_off_t (*tell)(struct bh_io_s *io);
@@ -38,145 +61,49 @@ typedef struct bh_io_table_s
typedef struct bh_io_s
{
bh_io_table_t *table;
const 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
*/
int bh_io_open(bh_io_t *io,
int mode);
void bh_io_close(bh_io_t *io);
int bh_io_is_open(bh_io_t *io);
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
*/
int bh_io_seek(bh_io_t *io,
bh_off_t offset,
int dir);
bh_off_t bh_io_size(bh_io_t *io);
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 */
#endif /* BH_IO_H */

View File

@@ -1,5 +1,5 @@
#ifndef BHLIB_PLATFORM_H
#define BHLIB_PLATFORM_H
#ifndef BH_PLATFORM_H
#define BH_PLATFORM_H
#include <stddef.h>
@@ -43,4 +43,4 @@ typedef unsigned __int32 bh_uintptr_t;
typedef bh_int64_t bh_off_t;
#endif /* BHLIB_PLATFORM_H */
#endif /* BH_PLATFORM_H */

View File

@@ -5,152 +5,31 @@
typedef struct bh_queue_s bh_queue_t;
/**
* @brief Create new queue object.
*
* @return If the function succeeds, the return value is a pointer to the new
* queue object.
* @return If the function fails, the return value is NULL.
*
* @sa bh_queue_free, bh_queue_reserve, bh_queue_insert
*/
bh_queue_t *bh_queue_new(void);
/**
* @brief Free queue object.
*
* @param queue Valid pointer to the queue object.
*
* @sa bh_queue_clear
*/
void bh_queue_free(bh_queue_t *queue);
/**
* @brief Clear the queue.
*
* @param queue Valid pointer to the queue object.
*
* @warning Calling this function will invalidate iterators.
*
* @sa bh_queue_remove
*/
void bh_queue_clear(bh_queue_t *queue);
/**
* @brief Reserve space for the specified amount of elements.
*
* @param queue Valid pointer to the queue object.
* @param size The amount of elements.
*
* @return If the function succeeds, the return value is zero.
* @return If the function fails, the return value is non-zero.
*
* @warning Calling this function will invalidate iterators.
*
* @sa bh_queue_capacity, bh_queue_insert
*/
int bh_queue_reserve(bh_queue_t *queue,
size_t size);
/**
* @brief Insert element at the end of the queue.
*
* @param queue Valid pointer to the queue object.
* @param value Element.
*
* @return If the function succeeds, the return value is zero.
* @return If the function fails, the return value is non-zero.
*
* @warning Inserted element is owned by the caller of the function.
* @warning Calling this function will invalidate iterators.
*
* @sa bh_queue_remove, bh_queue_front
*/
int bh_queue_insert(bh_queue_t *queue,
void *value);
/**
* @brief Remove element from the front of the queue.
*
* @param queue Valid pointer to the queue object.
*
* @warning Calling this function will invalidate iterators.
*
* @sa bh_queue_insert, bh_queue_front
*/
void bh_queue_remove(bh_queue_t *queue);
/**
* @brief Return element from the front of the queue.
*
* @param queue Valid pointer to the queue object.
*
* @return If the function succeeds, the return value is a valid pointer to
* the element.
* @return If the function fails, the return value is NULL.
*
* @sa bh_queue_empty, bh_queue_insert
*/
void *bh_queue_front(bh_queue_t *queue);
/**
* @brief Check if the queue is empty.
*
* @param queue Valid pointer to the queue object.
*
* @return The return value is non-zero if the queue is empty, otherwise zero.
*
* @sa bh_queue_size
*/
int bh_queue_empty(bh_queue_t *queue);
/**
* @brief Return queue size.
*
* @param queue Valid pointer to the queue object.
*
* @return The return value is current queue size.
*
* @sa bh_queue_empty, bh_queue_capacity
*/
size_t bh_queue_size(bh_queue_t *queue);
/**
* @brief Return queue capacity.
*
* @param queue Valid pointer to the queue object.
*
* @return The return value is current queue capacity.
*
* @sa bh_queue_reserve, bh_queue_size
*/
size_t bh_queue_capacity(bh_queue_t *queue);
/**
* @brief Return iterator for the next element in the queue.
*
* @param queue Valid pointer to the queue object.
* @param iter Valid or NULL iterator.
*
* @return The return value is the valid iterator for the next element in the
* queue.
* @return The return value is the NULL iterator if there is no more elements
* in the queue.
*
* @sa bh_queue_iter_value
*/
void *bh_queue_iter_next(bh_queue_t *queue,
void *iter);
/**
* @brief Return pointer to the element's value.
*
* @param iter Valid iterator.
*
* @return The return value is the stored element.
*
* @sa bh_queue_iter_next
*/
void *bh_queue_iter_value(void *iter);
#endif /* BH_QUEUE_H */

View File

@@ -1,9 +1,9 @@
#ifndef BHLIB_THREAD_H
#define BHLIB_THREAD_H
#ifndef BH_THREAD_H
#define BH_THREAD_H
#include "bh.h"
#define BH_THREAD_CLEANUP (1 << 0)
#define BH_THREAD_CLEANUP 0x0001
typedef void (*bh_thread_cb_t)(void *);
typedef struct bh_thread_s bh_thread_t;
@@ -13,12 +13,12 @@ typedef struct bh_cond_s bh_cond_t;
typedef struct bh_task_s bh_task_t;
typedef struct bh_thread_pool_s bh_thread_pool_t;
#if defined(BHLIB_USE_PTHREAD)
#if defined(BH_USE_PTHREAD)
bh_thread_t *bh_thread_new(bh_task_t *task);
bh_thread_pool_t *bh_thread_pool_new(size_t size);
#elif defined(BHLIB_USE_WINTHREAD)
#elif defined(BH_USE_WINTHREAD)
#include <windows.h>
#include <process.h>
@@ -47,250 +47,65 @@ 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.
*
* @param task Thread task
*
* @return Pointer to thread
*
* @sa bh_thread_join, bh_thread_detach
*/
/**
* @function bh_thread_pool_new
* @brief Create new thread pool.
*
* @param pool Pointer to the thread pool
* @param size Amount of threads
*
* @return Pointer to thread pool
*
* @sa bh_thread_pool_add, bh_thread_pool_join, bh_thread_pool_free
*/
/**
* @brief Join thread.
*
* @param thread Pointer to the thread
*
* @return 0 on success, non-zero otherwise
*
* @sa bh_thread_detach
*/
int bh_thread_join(bh_thread_t *thread);
/**
* @brief Detach thread.
*
* @param thread Pointer to the thread
*
* @return 0 on success, non-zero otherwise
*
* @sa bh_thread_join
*/
int bh_thread_detach(bh_thread_t *thread);
/**
* @brief Create mutex.
*
* @return Pointer to the mutex
*
* @sa bh_mutex_lock, bh_mutex_try_lock, bh_mutex_destroy
*/
bh_mutex_t *bh_mutex_new(void);
/**
* @brief Free mutex.
*
* @param mutex Pointer ot the mutex
*/
void bh_mutex_free(bh_mutex_t *mutex);
/**
* @brief Lock mutex.
*
* @param mutex Pointer to the mutex
*
* @return 0 on success, non-zero otherwise
*
* @note Locking already locked mutex will block thread until mutex is
* released
*
* @sa bh_mutex_try_lock, bh_mutex_unlock
*/
int bh_mutex_lock(bh_mutex_t *mutex);
/**
* @brief Try to lock mutex.
*
* @param mutex Pointer to the mutex
*
* @return 0 on success, positive value if mutex is locked, negative value on
* error
*
* @sa bh_mutex_lock, bh_mutex_unlock
*/
int bh_mutex_try_lock(bh_mutex_t *mutex);
/**
* @brief Unlock mutex.
*
* @param mutex Pointer to the mutex
*
* @return 0 on success, non-zero otherwise
*
* @sa bh_mutex_lock, bh_mutex_try_lock
*/
int bh_mutex_unlock(bh_mutex_t *mutex);
bh_semaphore_t *bh_semaphore_new(int count);
void bh_semaphore_free(bh_semaphore_t *semaphore);
int bh_semaphore_post(bh_semaphore_t *semaphore);
int bh_semaphore_wait(bh_semaphore_t *semaphore);
int bh_semaphore_wait_for(bh_semaphore_t *semaphore,
unsigned long timeout);
int bh_semaphore_try_wait(bh_semaphore_t *semaphore);
/**
* @brief Create condition variable.
*
* @return Pointer to the condition variable
*/
bh_cond_t *bh_cond_new(void);
/**
* @brief Destroy condition variable.
*
* @param cond Pointer to the conditional variable
*/
void bh_cond_free(bh_cond_t *cond);
/**
* @brief Block on conditional variable.
*
* @param cond Pointer to the condition variable
* @param mutex Pointer to the mutex
*
* @return 0 on success, non-zero otherwise
*
* @sa bh_cond_wait_for, bh_cond_signal, bh_cond_broadcast
*/
int bh_cond_wait(bh_cond_t *cond,
bh_mutex_t *mutex);
/**
* @brief Block on conditional variable for a period of the time.
*
* @param cond Pointer to the conditional variable
* @param mutex Pointer to the mutex
* @param timeout Timeout in miliseconds
*
* @return 0 on success, positive value on timeout, negative on error
*
* @sa bh_cond_wait, bh_cond_signal, bh_cond_broadcast
*/
int bh_cond_wait_for(bh_cond_t *cond,
bh_mutex_t *mutex,
unsigned long timeout);
/**
* @brief Unblock (notify) thread.
*
* @param cond Pointer to the condition variable
*
* @return 0 on success, non-zero otherwise
*
* @sa bh_cond_broadcast, bh_cond_wait, bh_cond_wait_for
*/
int bh_cond_signal(bh_cond_t *cond);
/**
* @brief Unblock all threads.
*
* @param cond Pointer to the conditional variable
*
* @return 0 on success, non-zero otherwise
*
* @sa bh_cond_signal, bh_cond_wait, bh_cond_wait_for
*/
int bh_cond_broadcast(bh_cond_t *cond);
/**
* @brief Submit task to the thread pool.
*
* @param pool Pointer to the thread pool
* @param func Task function
* @param data Task data
*
* @return 0 on success, non-zero otherwise
*
* @sa bh_thread_pool_join
*/
int bh_thread_pool_add(bh_thread_pool_t *pool,
bh_task_t *task);
/**
* @brief Wait until all tasks are finished.
*
* @param pool Pointer to the thread pool
* @return 0 on success, non-zero otherwise
*
* @sa bh_thread_pool_add
*/
int bh_thread_pool_wait(bh_thread_pool_t *pool);
/**
* @brief Destroy thread pool.
*
* @param pool Pointer to the thread pool
*/
void bh_thread_pool_free(bh_thread_pool_t *pool);
#endif /* BHLIB_THREAD_H */
#endif /* BH_THREAD_H */