rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 106437f02c135fa1ac965816acabe49c9e1c748c
parent c5a40fb440d4cbba809480eead8698663525b5ac
Author: vaplv <vaplv@free.fr>
Date:   Tue, 23 Sep 2014 17:05:26 +0200

Major refactoring. Notify status with res_T rather than int

Diffstat:
Msrc/binary_heap.h | 10++++++----
Msrc/dynamic_array.h | 78+++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/free_list.h | 2++
Msrc/hash_table.h | 66+++++++++++++++++++++++++++++++++++-------------------------------
Msrc/image.c | 12++++++------
Msrc/image.h | 2+-
Msrc/library.c | 17++++++++---------
Msrc/library.h | 2+-
Msrc/list.h | 2+-
Msrc/logger.c | 15+++++++--------
Msrc/logger.h | 4++--
Dsrc/result.h | 1-
Msrc/rsys.h | 12+++++++++++-
Msrc/str.c | 58+++++++++++++++++++++++++++++-----------------------------
Msrc/str.h | 55+++++++++++++++----------------------------------------
Msrc/test_binary_heap.c | 24++++++++++++------------
Msrc/test_hash_table.c | 28++++++++++++++--------------
Msrc/test_library.c | 4++--
Msrc/test_str.c | 40++++++++++++++++++++--------------------
19 files changed, 213 insertions(+), 219 deletions(-)

diff --git a/src/binary_heap.h b/src/binary_heap.h @@ -136,18 +136,20 @@ BHEAP_FUNC__(is_empty)(struct BHEAP_TYPE__* heap) return BHEAP_NODES_FUNC__(size_get)(&heap->nodes) == 0; } -static INLINE int +static INLINE res_T BHEAP_FUNC__(insert)(struct BHEAP_TYPE__* heap, const BHEAP_DATA* value) { BHEAP_DATA* nodes = NULL; BHEAP_DATA tmp; size_t inode = 0; + res_T res; ASSERT(heap); inode = BHEAP_NODES_FUNC__(size_get)(&heap->nodes); - if(BHEAP_NODES_FUNC__(push_back)(&heap->nodes, value)) - return -1; + res = BHEAP_NODES_FUNC__(push_back)(&heap->nodes, value); + if(res != R_OK) + return res; nodes = BHEAP_NODES_FUNC__(data_get)(&heap->nodes); BHEAP_FUNCTOR_INIT(heap->nodes.allocator, &tmp); @@ -162,7 +164,7 @@ BHEAP_FUNC__(insert)(struct BHEAP_TYPE__* heap, const BHEAP_DATA* value) inode = iparent; } BHEAP_FUNCTOR_RELEASE(&tmp); - return 0; + return R_OK; } static INLINE char diff --git a/src/dynamic_array.h b/src/dynamic_array.h @@ -55,7 +55,7 @@ struct DARRAY_TYPE__ { DARRAY_DATA* data; /* Avoids alloc on small arrays */ - char ALIGN(ALIGNOF(DARRAY_DATA)) buf[16*sizeof(DARRAY_DATA)]; + char ALIGN(ALIGNOF(DARRAY_DATA)) buf[16*sizeof(DARRAY_DATA)]; size_t size; size_t capacity; struct mem_allocator* allocator; @@ -79,17 +79,17 @@ DARRAY_FUNC__(functor_release__)(DARRAY_DATA* data) #endif #ifndef DARRAY_FUNCTOR_COPY -static FINLINE int +static FINLINE res_T DARRAY_FUNC__(functor_cp__)(DARRAY_DATA* dst, DARRAY_DATA const* src) -{ ASSERT(dst && src); *dst = *src; return 0; } +{ ASSERT(dst && src); *dst = *src; return R_OK; } #define DARRAY_FUNCTOR_COPY DARRAY_FUNC__(functor_cp__) #endif #ifndef DARRAY_FUNCTOR_COPY_AND_RELEASE -static FINLINE int +static FINLINE res_T DARRAY_FUNC__(functor_cp_and_release__)(DARRAY_DATA* dst, DARRAY_DATA* src) { - const int res = DARRAY_FUNCTOR_COPY(dst, src); + const res_T res = DARRAY_FUNCTOR_COPY(dst, src); DARRAY_FUNCTOR_RELEASE(src); return res; } @@ -133,7 +133,7 @@ DARRAY_FUNC__(release)(struct DARRAY_TYPE__* darray) MEM_FREE(darray->allocator, darray->data); } -static INLINE int +static INLINE res_T DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) { DARRAY_DATA* data = NULL; @@ -141,7 +141,7 @@ DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) ASSERT(darray); if(sz <= darray->capacity) - return 0; + return R_OK; sz_adjusted = round_up_pow2(sz); data = MEM_ALLOC_ALIGNED @@ -149,17 +149,17 @@ DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) sz_adjusted * sizeof(DARRAY_DATA), ALIGNOF(DARRAY_DATA)); if(!data) - return -1; + return R_MEM_ERR; if(darray->size) { size_t i = 0; FOR_EACH(i, 0, darray->size) { - int err = 0; + res_T res = 0; DARRAY_FUNCTOR_INIT(darray->allocator, data+i); - err = DARRAY_FUNCTOR_COPY_AND_RELEASE(data+i, darray->data+i); - if(err) { + res = DARRAY_FUNCTOR_COPY_AND_RELEASE(data+i, darray->data+i); + if(res != R_OK) { MEM_FREE(darray->allocator, data); - return err; + return res; } } } @@ -168,38 +168,42 @@ DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) darray->data = data; darray->capacity = sz_adjusted; - return 0; + return R_OK; } -static INLINE int +static INLINE res_T DARRAY_FUNC__(resize)(struct DARRAY_TYPE__* darray, const size_t sz) { size_t i; + res_T res; ASSERT(darray); - if(DARRAY_FUNC__(reserve)(darray, sz)) - return -1; + res = DARRAY_FUNC__(reserve)(darray, sz); + if(res != R_OK) + return res; FOR_EACH(i, sz, darray->size) DARRAY_FUNCTOR_RELEASE(darray->data+i); FOR_EACH(i, darray->size, sz) DARRAY_FUNCTOR_INIT(darray->allocator, darray->data+i); darray->size = sz; - return 0; + return R_OK; } -static INLINE char +static INLINE res_T DARRAY_FUNC__(push_back) (struct DARRAY_TYPE__* darray, DARRAY_DATA const* data) { size_t sz = 0; + res_T res; ASSERT(darray && data); sz = darray->size; - if(DARRAY_FUNC__(resize)(darray, sz+1)) - return -1; + res = DARRAY_FUNC__(resize)(darray, sz+1); + if(res != R_OK) + return res; DARRAY_FUNCTOR_COPY(darray->data+sz, data); - return 0; + return R_OK; } static INLINE void @@ -207,8 +211,8 @@ DARRAY_FUNC__(pop_back)(struct DARRAY_TYPE__* darray) { ASSERT(darray); if(darray->size > 0) { - const int err = DARRAY_FUNC__(resize)(darray, darray->size - 1); - ASSERT(!err); (void)err; + const res_T res = DARRAY_FUNC__(resize)(darray, darray->size - 1); + ASSERT(res == R_OK); (void)res; } } @@ -233,38 +237,38 @@ DARRAY_FUNC__(cdata_get)(const struct DARRAY_TYPE__* darray) return darray->data; } -static INLINE int +static INLINE res_T DARRAY_FUNC__(copy)(struct DARRAY_TYPE__* dst, const struct DARRAY_TYPE__* src) { DARRAY_DATA const* src_data = NULL; size_t i, src_sz; - int res; + res_T res; ASSERT(dst && src); if(dst == src) - return 0; + return R_OK; DARRAY_FUNC__(clear)(dst); src_sz = DARRAY_FUNC__(size_get)(src); res = DARRAY_FUNC__(reserve)(dst, src_sz); - if(res) return res; + if(res != R_OK) return res; src_data = DARRAY_FUNC__(cdata_get)(src); FOR_EACH(i, 0, src_sz) DARRAY_FUNC__(push_back)(dst, src_data+i); - return 0; + return R_OK; } -static INLINE int +static INLINE res_T DARRAY_FUNC__(copy_and_clear) (struct DARRAY_TYPE__* dst, struct DARRAY_TYPE__* src) { - int res = 0; + res_T res = R_OK; ASSERT(dst && src); - if( dst == src ) { + if(dst == src) { DARRAY_FUNC__(clear)(dst); - return 0; + return R_OK; } if(src->data != (DARRAY_DATA*)src->buf && src->allocator == dst->allocator) { @@ -285,31 +289,31 @@ DARRAY_FUNC__(copy_and_clear) DARRAY_FUNC__(clear)(dst); res = DARRAY_FUNC__(resize)(dst, src_sz); - if(res) return res; + if(res != R_OK) return res; src_data = DARRAY_FUNC__(data_get)(src); dst_data = DARRAY_FUNC__(data_get)(dst); FOR_EACH(i, 0, src_sz) { res = DARRAY_FUNCTOR_COPY_AND_RELEASE(dst_data+i, src_data+i); - if(res) return res; + if(res != R_OK) return res; } src->size = 0; } return res; } -static INLINE int +static INLINE res_T DARRAY_FUNC__(copy_and_release) (struct DARRAY_TYPE__* dst, struct DARRAY_TYPE__* src) { - int res = 0; + res_T res = R_OK; ASSERT(dst && src); if(dst == src) { DARRAY_FUNC__(release)(dst); } else { res = DARRAY_FUNC__(copy_and_clear)(dst, src); - if(!res) + if(res == R_OK) DARRAY_FUNC__(release)(src); } return res; diff --git a/src/free_list.h b/src/free_list.h @@ -125,6 +125,8 @@ FLIST_FUNC__(add)(struct FLIST_TYPE__* list) (list->allocator, list->items, nitems_new * sizeof(struct FITEM_TYPE)); + if(!list->items) + FATAL("Unsufficient memory\n"); FOR_EACH(iitem, list->nitems, nitems_new - 1) { list->items[iitem].fitem__.next = iitem + 1; } diff --git a/src/hash_table.h b/src/hash_table.h @@ -110,24 +110,24 @@ HTABLE_FUNC__(key_functor_release__)(HTABLE_KEY* key) #endif #ifndef HTABLE_DATA_FUNCTOR_COPY -static FINLINE int +static FINLINE res_T HTABLE_FUNC__(data_functor_cp__)(HTABLE_DATA* dst, HTABLE_DATA const* src) -{ ASSERT(dst && src); *dst = *src; return 0; } +{ ASSERT(dst && src); *dst = *src; return R_OK; } #define HTABLE_DATA_FUNCTOR_COPY HTABLE_FUNC__(data_functor_cp__) #endif #ifndef HTABLE_KEY_FUNCTOR_COPY -static FINLINE int +static FINLINE res_T HTABLE_FUNC__(key_functor_cp__)(HTABLE_KEY* dst, HTABLE_KEY const* src) -{ ASSERT(dst && src); *dst = *src; return 0; } +{ ASSERT(dst && src); *dst = *src; return R_OK; } #define HTABLE_KEY_FUNCTOR_COPY HTABLE_FUNC__(key_functor_cp__) #endif #ifndef HTABLE_DATA_FUNCTOR_COPY_AND_RELEASE -static FINLINE int +static FINLINE res_T HTABLE_FUNC__(data_functor_cp_and_release__)(HTABLE_DATA* dst, HTABLE_DATA* src) { - const int res = HTABLE_DATA_FUNCTOR_COPY(dst, src); + const res_T res = HTABLE_DATA_FUNCTOR_COPY(dst, src); HTABLE_DATA_FUNCTOR_RELEASE(src); return res; } @@ -136,10 +136,10 @@ HTABLE_FUNC__(data_functor_cp_and_release__)(HTABLE_DATA* dst, HTABLE_DATA* src) #endif #ifndef HTABLE_KEY_FUNCTOR_COPY_AND_RELEASE -static FINLINE int +static FINLINE res_T HTABLE_FUNC__(key_functor_cp_and_release__)(HTABLE_KEY* dst, HTABLE_KEY* src) { - const int res = HTABLE_KEY_FUNCTOR_COPY(dst, src); + const res_T res = HTABLE_KEY_FUNCTOR_COPY(dst, src); HTABLE_KEY_FUNCTOR_RELEASE(src); return res; } @@ -192,25 +192,25 @@ HTABLE_FUNC__(pair_release__)(struct HTABLE_PAIR__* pair) HTABLE_DATA_FUNCTOR_RELEASE(&pair->data); } -static INLINE int +static INLINE res_T HTABLE_FUNC__(pair_copy__) (struct HTABLE_PAIR__* dst, struct HTABLE_PAIR__ const* src) { - int res = 0; + res_T res; ASSERT(dst && src); - if((res = HTABLE_KEY_FUNCTOR_COPY(&dst->key, &src->key))) + if(R_OK != (res = HTABLE_KEY_FUNCTOR_COPY(&dst->key, &src->key))) return res; res = HTABLE_DATA_FUNCTOR_COPY(&dst->data, &src->data); return res; } -static INLINE int +static INLINE res_T HTABLE_FUNC__(pair_copy_and_release__) (struct HTABLE_PAIR__* dst, struct HTABLE_PAIR__* src) { - int res = 0; + res_T res; ASSERT(dst && src); - if((res = HTABLE_KEY_FUNCTOR_COPY_AND_RELEASE(&dst->key, &src->key))) + if(R_OK != (res = HTABLE_KEY_FUNCTOR_COPY_AND_RELEASE(&dst->key, &src->key))) return res; res = HTABLE_DATA_FUNCTOR_COPY_AND_RELEASE(&dst->data, &src->data); return res; @@ -320,8 +320,7 @@ HTABLE_FUNC__(release)(struct HTABLE__* htbl) darray_char_release(&htbl->table_slot_is_used); } -/* Return 0 on success and -1 otherwise */ -static int +static res_T HTABLE_FUNC__(reserve)(struct HTABLE__* htbl, const size_t size_submitted) { struct HTABLE_DATA__ tbl; @@ -333,7 +332,7 @@ HTABLE_FUNC__(reserve)(struct HTABLE__* htbl, const size_t size_submitted) size_t islot = 0; size_t in_use = 0; size_t size = 0; - int res = 0; + res_T res = R_OK; ASSERT(htbl); size = round_up_pow2(size_submitted); @@ -341,11 +340,11 @@ HTABLE_FUNC__(reserve)(struct HTABLE__* htbl, const size_t size_submitted) goto exit; HTABLE_DATA_FUNC__(init)(htbl->allocator, &tbl); - if(HTABLE_DATA_FUNC__(resize)(&tbl, size)) + if(R_OK != (res = HTABLE_DATA_FUNC__(resize)(&tbl, size))) goto error; darray_char_init(htbl->allocator, &tbl_slot_is_used); - if(darray_char_resize(&tbl_slot_is_used, size)) + if(R_OK != (res = darray_char_resize(&tbl_slot_is_used, size))) goto error; memset(darray_char_data_get(&tbl_slot_is_used), 0, size*sizeof(char)); @@ -389,12 +388,10 @@ exit: error: HTABLE_DATA_FUNC__(release)(&tbl); darray_char_release(&tbl_slot_is_used); - res = -1; goto exit; } -/* Return 0 on success and -1 otherwise */ -static INLINE int +static INLINE res_T HTABLE_FUNC__(set) ( struct HTABLE__* htbl, HTABLE_KEY const* key, @@ -403,7 +400,7 @@ HTABLE_FUNC__(set) struct HTABLE_PAIR__* pair = NULL; size_t tbl_size = 0; size_t i = 0; - int res = 0; + res_T res = R_OK; ASSERT(htbl && key && data); /* Increase hash table size when the load factor is too high */ @@ -414,22 +411,29 @@ HTABLE_FUNC__(set) } else { res = HTABLE_FUNC__(reserve)(htbl, tbl_size * 2 ); } - if(res) + if(res != R_OK) return res; } i = HTABLE_FUNC__(find_slot__)(htbl, key); pair = HTABLE_DATA_FUNC__(data_get)(&htbl->table) + i; if(darray_char_cdata_get(&htbl->table_slot_is_used)[i]) { - HTABLE_DATA_FUNCTOR_COPY(&pair->data, data); + res = HTABLE_DATA_FUNCTOR_COPY(&pair->data, data); + if(res != R_OK) { + darray_char_data_get(&htbl->table_slot_is_used)[i] = 0; + --htbl->table_size_in_use; + } } else { - HTABLE_KEY_FUNCTOR_COPY(&pair->key, key); - HTABLE_DATA_FUNCTOR_COPY(&pair->data, data); - - darray_char_data_get(&htbl->table_slot_is_used)[i] = 1; - ++htbl->table_size_in_use; + res = HTABLE_KEY_FUNCTOR_COPY(&pair->key, key); + if(res == R_OK) { + res = HTABLE_DATA_FUNCTOR_COPY(&pair->data, data); + if(res == R_OK) { + darray_char_data_get(&htbl->table_slot_is_used)[i] = 1; + ++htbl->table_size_in_use; + } + } } - return 0; + return R_OK; } /* Return the number of erased elements, i.e. 0 or 1 */ diff --git a/src/image.c b/src/image.c @@ -18,7 +18,7 @@ #include <stdio.h> #include <string.h> -int +res_T image_ppm_write (const char* path, const int width, @@ -28,25 +28,26 @@ image_ppm_write { char buf[BUFSIZ]; FILE* fp = NULL; - int err = 0; + res_T res = R_OK; if(width && height && Bpp && !buffer) { goto error; } fp = fopen(path, "w"); if(NULL == fp) { + res = R_IO_ERR; goto error; } #define FWRITE(Fp, String) \ { \ const size_t i = fwrite(String, sizeof(char), strlen(String), Fp); \ - if(i != strlen(String) * sizeof(char)) goto error; \ + if(i != strlen(String) * sizeof(char)) { res = R_MEM_ERR; goto error; } \ } (void)0 #define SNPRINTF(Buf, Sz, Str, Arg0, Arg1, Arg2) \ { \ const int i = snprintf(Buf, Sz, Str, Arg0, Arg1, Arg2); \ - if( i >= BUFSIZ ) goto error; \ + if( i >= BUFSIZ ) { res = R_MEM_ERR; goto error; } \ } (void)0 SNPRINTF(buf, BUFSIZ, "P3\n\n%i %i\n%i\n", width, height, 255); @@ -73,9 +74,8 @@ image_ppm_write exit: if(fp) fclose(fp); - return err; + return res; error: - err = -1; goto exit; } diff --git a/src/image.h b/src/image.h @@ -20,7 +20,7 @@ BEGIN_DECLS -RSYS_API int +RSYS_API res_T image_ppm_write (const char* path, int width, diff --git a/src/library.c b/src/library.c @@ -25,19 +25,18 @@ library_open(const char* filename) return (void*)LoadLibraryA(filename); } -int +res_T library_close(void* lib) { BOOL b; - if(!lib) - return -1; + return R_BAD_ARG; b = FreeLibrary((HMODULE)lib); if(!b) - return -1; + return R_UNKNOWN_ERR; - return 0; + return R_OK; } void* @@ -86,20 +85,20 @@ library_get_symbol(void* lib, const char* sym) } } -int +res_T library_close(void* handle) { int err = 0; if(!handle) - return -1; + return R_BAD_ARG; err = dlclose(handle); if(err) { fprintf(stderr, "%s\n", dlerror()); - return -1; + return R_UNKNOWN_ERR; } - return 0; + return R_OK; } #endif /* OS_<XXX> */ diff --git a/src/library.h b/src/library.h @@ -29,7 +29,7 @@ library_get_symbol (void* lib, const char* symbol); -RSYS_API int +RSYS_API res_T library_close (void* handle); diff --git a/src/list.h b/src/list.h @@ -79,7 +79,7 @@ list_init(struct list_node* node) node->prev = node; } -static FINLINE int +static FINLINE char is_list_empty(const struct list_node* node) { ASSERT(node); diff --git a/src/logger.c b/src/logger.c @@ -16,25 +16,24 @@ #define _POSIX_C_SOURCE 200112L /* vsnprintf support */ #include "logger.h" -int +res_T logger_print (struct logger* logger, const enum log_type type, const char* log, ...) { struct list_node* node = NULL; va_list vargs_list; int sz; - int err = 0; ASSERT(logger && log && type < LOG_TYPES_COUNT__); if(logger == LOGGER_DEFAULT) { va_start(vargs_list, log); vfprintf(type == LOG_OUTPUT ? stdout : stderr, log, vargs_list); va_end(vargs_list); - return 0; + return R_OK; } if(is_list_empty(logger->streams + type)) - return 0; + return R_OK; va_start(vargs_list, log); sz = vsnprintf @@ -46,9 +45,9 @@ logger_print /* If there is not sufficient space in the logger buffer, resize it */ if((size_t)sz >= darray_char_size_get(&logger->buffer)) { - err = darray_char_resize(&logger->buffer, (size_t)sz + 1/*+1<=>'\0'*/); - if(err) - return err; + res_T res = darray_char_resize(&logger->buffer, (size_t)sz+1/*+1<=>'\0'*/); + if(res != R_OK) + return res; va_start(vargs_list, log); sz = vsnprintf @@ -65,6 +64,6 @@ logger_print (node, struct log_stream, attachments[type]); stream->writer(darray_char_cdata_get(&logger->buffer), stream->ctx); } - return 0; + return R_OK; } diff --git a/src/logger.h b/src/logger.h @@ -91,7 +91,7 @@ struct logger { struct darray_char buffer; }; -static INLINE int +static INLINE res_T logger_init (struct mem_allocator* allocator, /* May be NULL <=> default allocator */ struct logger* logger) @@ -137,7 +137,7 @@ logger_attach_stream BEGIN_DECLS -RSYS_API int +RSYS_API res_T logger_print (struct logger* logger, const enum log_type type, diff --git a/src/result.h b/src/result.h @@ -1 +0,0 @@ - diff --git a/src/rsys.h b/src/rsys.h @@ -190,7 +190,7 @@ #define LIST_ARG4(A, B, C, D) A, B, C, D #define LIST_ARG5(A, B, C, D, E) A, B, C, D, E -#define COMMA_ARG0() +#define COMMA_ARG0() #define COMMA_ARG1(A) , #define COMMA_ARG2(A, B) , #define COMMA_ARG3(A, B, C) , @@ -198,6 +198,16 @@ #define COMMA_ARG5(A, B, C, D, E) , /******************************************************************************* + * Result constants + ******************************************************************************/ +typedef int res_T; +#define R_OK 0 +#define R_BAD_ARG 1 +#define R_MEM_ERR 2 +#define R_IO_ERR 3 +#define R_UNKNOWN_ERR 4 + +/******************************************************************************* * Miscellaneous ******************************************************************************/ #define BIT(Num) (1 << (Num)) diff --git a/src/str.c b/src/str.c @@ -19,7 +19,7 @@ /******************************************************************************* * helper function ******************************************************************************/ -static int +static res_T ensure_allocated(struct str* str, const size_t len, const char keep_old) { char* buf = NULL; @@ -30,14 +30,14 @@ ensure_allocated(struct str* str, const size_t len, const char keep_old) ASSERT( str ); if(len * sizeof(char) <= str->allocated) - return 0; + return R_OK; mod = len % alloc_granularity; new_len = !mod ? len : len - mod + alloc_granularity; new_size = new_len * sizeof(char); buf = MEM_ALLOC(str->allocator, new_size); - if( !buf ) - return -1; + if(!buf) + return R_MEM_ERR; if(keep_old) { strncpy( buf, str->cstr, new_len - 1); @@ -49,35 +49,35 @@ ensure_allocated(struct str* str, const size_t len, const char keep_old) MEM_FREE(str->allocator, str->cstr); str->cstr = buf; - return 0; + return R_OK; } /******************************************************************************* * Exported functions ******************************************************************************/ -int +res_T str_set(struct str* str, const char* cstr) { size_t cstr_len = 0; - int res = 0; + res_T res = 0; ASSERT(str && cstr); cstr_len = strlen(cstr); res = ensure_allocated(str, cstr_len + 1, 0); - if( res ) return res; + if(res != R_OK) return res; strncpy(str->cstr, cstr, cstr_len + 1); str->len = cstr_len; - return 0; + return R_OK; } -int +res_T str_insert(struct str* str, const size_t i, const char* cstr) { size_t cstr_len = 0; ASSERT(str); if(i > str->len) - return -1 ; + return R_BAD_ARG; cstr_len = strlen(cstr); ASSERT(!MEM_AREA_OVERLAP @@ -86,8 +86,8 @@ str_insert(struct str* str, const size_t i, const char* cstr) if(i == str->len) { return str_append(str, cstr); } else { - const int res = ensure_allocated(str, cstr_len + str->len + 1, 1); - if( res ) + const res_T res = ensure_allocated(str, cstr_len + str->len + 1, 1); + if(res != R_OK) return res; memmove (str->cstr + i + cstr_len, @@ -97,14 +97,14 @@ str_insert(struct str* str, const size_t i, const char* cstr) str->len = str->len + cstr_len; str->cstr[str->len] = '\0'; } - return 0; + return R_OK; } -int +res_T str_insert_char(struct str* str, const size_t i, const char ch) { if(i > str->len) - return -1; + return R_BAD_ARG; if(i == str->len) { return str_append_char(str, ch); @@ -112,8 +112,8 @@ str_insert_char(struct str* str, const size_t i, const char ch) str->cstr[i] = ch; str->len = i; } else { - const int res = ensure_allocated(str, str->len + 2, 1); - if( res ) + const res_T res = ensure_allocated(str, str->len + 2, 1); + if(res != R_OK) return res; memmove (str->cstr + i + 1, @@ -123,14 +123,14 @@ str_insert_char(struct str* str, const size_t i, const char ch) ++str->len; str->cstr[str->len] = '\0'; } - return 0; + return R_OK; } -int +res_T str_append(struct str* str, const char* cstr) { size_t cstr_len = 0; - int res = 0; + res_T res = R_OK; ASSERT(str && cstr); cstr_len = strlen(cstr); @@ -138,34 +138,34 @@ str_append(struct str* str, const char* cstr) (str->cstr, str->allocated, cstr, (cstr_len + 1) * sizeof(char))); res = ensure_allocated(str, cstr_len + str->len + 1, 1); - if(res) + if(res != R_OK) return res; memcpy(str->cstr + str->len, cstr, cstr_len * sizeof(char)); str->len += cstr_len; str->cstr[str->len] = '\0'; - return 0; + return R_OK; } -int +res_T str_append_char(struct str* str, const char ch) { - int res = 0; + res_T res = R_OK; ASSERT( str ); if(ch == '\0') - return 0; + return R_OK; res = ensure_allocated(str, str->len + 2, 1); - if(res) return res; + if(res != R_OK) return res; str->cstr[str->len] = ch; ++str->len; str->cstr[str->len] = '\0'; - return 0; + return R_OK; } -int +res_T str_reserve(struct str* str, const size_t capacity) { return ensure_allocated(str, capacity / sizeof(char), 1); diff --git a/src/str.h b/src/str.h @@ -77,57 +77,32 @@ str_cget(const struct str* str) BEGIN_DECLS -RSYS_API int /* return 0 on success != 0 otherwise */ -str_set - (struct str* str, - const char* cstr); - -RSYS_API int /* return 0 on success != 0 otherwise */ -str_insert - (struct str* str, - const size_t i, - const char* cstr); - -RSYS_API int /* return 0 on success != 0 otherwise */ -str_insert_char - (struct str* str, - const size_t i, - const char ch); - -RSYS_API int /* return 0 on success != 0 otherwise */ -str_append - (struct str* str, - const char* cstr); - -RSYS_API int /* return 0 on success != 0 otherwise */ -str_append_char - (struct str* str, - const char ch); - -RSYS_API int /* return 0 on success != 0 otherwise */ -str_reserve - (struct str* str, - const size_t capacity); +RSYS_API res_T str_set(struct str* str, const char* cstr); +RSYS_API res_T str_insert(struct str* str, const size_t i, const char* cstr); +RSYS_API res_T str_insert_char(struct str* str, const size_t i, const char ch); +RSYS_API res_T str_append(struct str* str, const char* cstr); +RSYS_API res_T str_append_char(struct str* str, const char ch); +RSYS_API res_T str_reserve(struct str* str, const size_t capacity); END_DECLS -static INLINE int /* return 0 on success != 0 otherwise */ +static INLINE res_T str_copy(struct str* dst, const struct str* src) { ASSERT(dst && src); if(dst == src) - return 0; + return R_OK; return str_set(dst, str_cget(src)); } -static INLINE int /* return 0 on success != 0 otherwise */ +static INLINE res_T str_copy_and_clear( struct str* dst, struct str* src ) { - int res = 0; + res_T res = R_OK; ASSERT(dst && src); if(dst == src) { str_clear(dst); - return 0; + return R_OK; } if(src->cstr != src->buffer && src->allocator == dst->allocator) { @@ -141,22 +116,22 @@ str_copy_and_clear( struct str* dst, struct str* src ) str_init(src->allocator, src); } else { res = str_copy(dst, src); - if(!res) + if(res == R_OK) str_clear(src); } return res; } -static INLINE int /* return 0 on success != 0 otherwise */ +static INLINE res_T str_copy_and_release(struct str* dst, struct str* src) { - int res = 0; + res_T res = R_OK; ASSERT( dst && src ); if(dst == src) { str_release(dst); } else { res = str_copy_and_clear(dst, src); - if( !res ) + if(res == R_OK) str_release(src); } return res; diff --git a/src/test_binary_heap.c b/src/test_binary_heap.c @@ -38,14 +38,14 @@ test_primitive_type(void) CHECK(bheap_u64_is_empty(&heap), 1); CHECK(bheap_u64_top(&heap, &i), 0); CHECK(bheap_u64_pop(&heap, &i), 0); - i = 48; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 51; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 1; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 7; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 78; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 4; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 61; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 72; CHECK(bheap_u64_insert(&heap, &i), 0); + i = 48; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 51; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 1; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 7; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 78; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 4; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 61; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 72; CHECK(bheap_u64_insert(&heap, &i), R_OK); CHECK(bheap_u64_is_empty(&heap), 0); NCHECK(bheap_u64_top(&heap, &i), 0); @@ -61,9 +61,9 @@ test_primitive_type(void) NCHECK(bheap_u64_pop(&heap, &i), 0); CHECK(i, 48); - i = 5; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 50; CHECK(bheap_u64_insert(&heap, &i), 0); - i = 51; CHECK(bheap_u64_insert(&heap, &i), 0); + i = 5; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 50; CHECK(bheap_u64_insert(&heap, &i), R_OK); + i = 51; CHECK(bheap_u64_insert(&heap, &i), R_OK); NCHECK(bheap_u64_top(&heap, &i), 0); CHECK(i, 5); NCHECK(bheap_u64_pop(&heap, &i), 0); @@ -86,7 +86,7 @@ test_primitive_type(void) FOR_EACH(i, 0, 17689) { const uint64_t j = (uint64_t)rand(); - CHECK(bheap_u64_insert(&heap, &j), 0); + CHECK(bheap_u64_insert(&heap, &j), R_OK); } CHECK(bheap_u64_is_empty(&heap), 0); NCHECK(bheap_u64_pop(&heap, &i), 0); diff --git a/src/test_hash_table.c b/src/test_hash_table.c @@ -39,11 +39,11 @@ test_htbl_int_float(void) mem_init_proxy_allocator(&allocator_proxy, &mem_default_allocator); htable_int_float_init(&allocator_proxy, &htbl); - htable_int_float_reserve(&htbl, 30); + CHECK(htable_int_float_reserve(&htbl, 30), R_OK); FOR_EACH(i, 0, n) { float f = (float)i; - CHECK(htable_int_float_set(&htbl, &i, &f), 0); + CHECK(htable_int_float_set(&htbl, &i, &f), R_OK); } FOR_EACH(i, 0, n) { @@ -159,13 +159,13 @@ test_htbl_str_int(void) CHECK(htable_str_int_size_get(&htbl), 0); i = 0; str_set(&tmp, str[i]); - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); CHECK(htable_str_int_is_empty(&htbl), 0); CHECK(htable_str_int_size_get(&htbl), 1); FOR_EACH(i, 1, str_size) { str_set(&tmp, str[i]); - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); } CHECK(htable_str_int_size_get(&htbl), (size_t)str_size); @@ -205,7 +205,7 @@ test_htbl_str_int(void) } FOR_EACH(i, 0, array_size) { str_set(&tmp, str[array[i]]); - CHECK(htable_str_int_set(&htbl, &tmp, array + i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, array + i), R_OK); } CHECK(htable_str_int_size_get(&htbl), (size_t)str_size); CHECK(htable_str_int_is_empty(&htbl), 0); @@ -241,18 +241,18 @@ test_htbl_str_int(void) htable_str_int_init(&allocator_proxy, &htbl); htable_str_int_reserve(&htbl, 3); str_set(&tmp, "Zero"), i = 0; - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); str_set(&tmp, "One"), i = 1; - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); str_set(&tmp, "Two"), i = 2; - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); str_set(&tmp, "Three"), i = 3; - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); str_set(&tmp, "Four"), i = 4; - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); CHECK(htable_str_int_size_get(&htbl), 5); str_set(&tmp, "Zero"), i = 'a'; - CHECK(htable_str_int_set(&htbl, &tmp, &i), 0); + CHECK(htable_str_int_set(&htbl, &tmp, &i), R_OK); CHECK(htable_str_int_size_get(&htbl), 5); data = htable_str_int_find(&htbl, &tmp); @@ -361,10 +361,10 @@ ANGVBA BS QRZBAF EHA NZBX VA BHE PVGVRF.", FOR_EACH(i, 0, nstrs) { darray_char_clear(&darray); FOR_EACH(j, 0, strlen(str[i]) + 1) { - CHECK(darray_char_push_back(&darray, str[i] + j), 0); + CHECK(darray_char_push_back(&darray, str[i] + j), R_OK); } str_set(&tmp, str[i]); - CHECK(htable_str_darray_set(&htbl, &tmp, &darray), 0); + CHECK(htable_str_darray_set(&htbl, &tmp, &darray), R_OK); } FOR_EACH(j, 0, nerase) { @@ -395,7 +395,7 @@ ANGVBA BS QRZBAF EHA NZBX VA BHE PVGVRF.", htable_str_darray_iterator_next(&it0); } - CHECK(htable_str_darray_reserve(&htbl, 2891), 0); + CHECK(htable_str_darray_reserve(&htbl, 2891), R_OK); FOR_EACH(j, 0, 5) { for(;;) { diff --git a/src/test_library.c b/src/test_library.c @@ -44,8 +44,8 @@ main(int argc, char** argv) CHECK(library_get_symbol(lib, "exported_func_BAD"), NULL); NCHECK(library_get_symbol(lib, "exported_func"), NULL); - NCHECK(library_close(NULL), 0); - CHECK(library_close(lib), 0); + CHECK(library_close(NULL), R_BAD_ARG); + CHECK(library_close(lib), R_OK); CHECK(MEM_ALLOCATED_SIZE(&mem_default_allocator), 0); diff --git a/src/test_str.c b/src/test_str.c @@ -30,7 +30,7 @@ main(int argc, char** argv) CHECK(strcmp(str_get(&str), ""), 0); CHECK(str_len(&str), 0); - CHECK(str_set(&str, "Foo"), 0); + CHECK(str_set(&str, "Foo"), R_OK); CHECK(strcmp(str_get(&str), "Foo"), 0); CHECK(str_len(&str), strlen("Foo")); @@ -38,7 +38,7 @@ main(int argc, char** argv) CHECK(strcmp(str_get(&str), ""), 0); CHECK(str_len(&str), 0); - CHECK(str_set(&str, __FILE__), 0); + CHECK(str_set(&str, __FILE__), R_OK); CHECK(strcmp(str_get(&str), __FILE__), 0); str_release(&str); @@ -46,48 +46,48 @@ main(int argc, char** argv) CHECK(strcmp(str_get(&str), ""), 0); CHECK(str_len(&str), 0); - CHECK(str_set(&str, "Hello world!"), 0); + CHECK(str_set(&str, "Hello world!"), R_OK); CHECK(strcmp(str_get(&str), "Hello world!"), 0); - NCHECK(str_insert(&str, 13, " insert"), 0); - CHECK(str_insert(&str, 12, " insert"), 0); + NCHECK(str_insert(&str, 13, " insert"), R_OK); + CHECK(str_insert(&str, 12, " insert"), R_OK); CHECK(strcmp(str_get(&str), "Hello world! insert"), 0); - CHECK(str_insert(&str, 6, "abcdefgh "), 0); + CHECK(str_insert(&str, 6, "abcdefgh "), R_OK); CHECK(strcmp(str_get(&str), "Hello abcdefgh world! insert"), 0); - CHECK(str_insert(&str, 0, "ABC "), 0); + CHECK(str_insert(&str, 0, "ABC "), R_OK); CHECK(strcmp(str_get(&str), "ABC Hello abcdefgh world! insert"), 0); - CHECK(str_insert(&str, 11, "0123456789ABCDEF"), 0); + CHECK(str_insert(&str, 11, "0123456789ABCDEF"), R_OK); CHECK(strcmp(str_get(&str), "ABC Hello a0123456789ABCDEFbcdefgh world! insert"), 0); CHECK(str_len(&str), strlen("ABC Hello a0123456789ABCDEFbcdefgh world! insert")); - CHECK(str_set(&str, "Hello world!"), 0); - CHECK(str_append(&str, " Append"), 0); + CHECK(str_set(&str, "Hello world!"), R_OK); + CHECK(str_append(&str, " Append"), R_OK); CHECK(strcmp(str_get(&str), "Hello world! Append"), 0); - CHECK(str_append(&str, "!"), 0); + CHECK(str_append(&str, "!"), R_OK); CHECK(strcmp(str_get(&str), "Hello world! Append!"), 0); - NCHECK(str_insert_char(&str, 21, 'a'), 0); + NCHECK(str_insert_char(&str, 21, 'a'), R_OK); CHECK(strcmp(str_get(&str), "Hello world! Append!"), 0); - CHECK(str_insert_char(&str, 20, 'a'), 0); + CHECK(str_insert_char(&str, 20, 'a'), R_OK); CHECK(strcmp(str_get(&str), "Hello world! Append!a"), 0); - CHECK(str_insert_char(&str, 0, '0'), 0); + CHECK(str_insert_char(&str, 0, '0'), R_OK); CHECK(strcmp(str_get(&str), "0Hello world! Append!a"), 0); - CHECK(str_insert_char(&str, 13, 'A'), 0); + CHECK(str_insert_char(&str, 13, 'A'), R_OK); CHECK(strcmp(str_get(&str), "0Hello world!A Append!a"), 0); - CHECK(str_append_char(&str, 'z'), 0); + CHECK(str_append_char(&str, 'z'), R_OK); CHECK(strcmp(str_get(&str), "0Hello world!A Append!az"), 0); - CHECK(str_reserve(&str, 128), 0); - CHECK(str_reserve(&str, 0), 0); + CHECK(str_reserve(&str, 128), R_OK); + CHECK(str_reserve(&str, 0), R_OK); CHECK(strcmp(str_get(&str), "0Hello world!A Append!az"), 0); - CHECK(str_insert_char(&str, 13, '\0'), 0); + CHECK(str_insert_char(&str, 13, '\0'), R_OK); CHECK(strcmp(str_get(&str), "0Hello world!"), 0); CHECK(str_len(&str), strlen("0Hello world!")); - CHECK(str_append_char(&str, '\0'), 0); + CHECK(str_append_char(&str, '\0'), R_OK); CHECK(strcmp(str_get(&str), "0Hello world!"), 0); CHECK(str_len(&str), strlen("0Hello world!"));