rsys

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

commit d6567505c614c362b6d34f164855effadc447709
parent b4283b9f2ac974455143b6e5ac82f94d2b243798
Author: vaplv <vaplv@free.fr>
Date:   Sat, 23 Nov 2013 09:33:20 +0100

Merge remote-tracking branch 'origin/master' into branch-c89

Diffstat:
Msrc/CMakeLists.txt | 9++++++++-
Msrc/clock_time.c | 9+++------
Msrc/clock_time.h | 51++++++++++++++++++++++++++++++++++++---------------
Msrc/image.c | 6+++---
Asrc/library.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/library.h | 32++++++++++++++++++++++++++++++++
Msrc/list.h | 16++++++++++------
Msrc/mem_allocator.c | 96+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/mem_allocator.h | 22+++++++++++-----------
Msrc/ref_count.h | 3+--
Msrc/rsys.h | 23++++++++++++++---------
Msrc/signal.h | 1-
Msrc/test_atomic.c | 4++--
Msrc/test_condition.c | 25++++++++-----------------
Asrc/test_library.c | 24++++++++++++++++++++++++
Msrc/test_list.c | 12++++--------
Msrc/test_mem_allocator.c | 4++--
Msrc/test_mutex.c | 10++++------
Msrc/test_ref.c | 2+-
Msrc/test_signal.c | 21+++++++++------------
20 files changed, 270 insertions(+), 148 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt @@ -13,7 +13,7 @@ find_package(OpenMP) # Setup compile flags/parameters ################################################################################ set(CMAKE_DEBUG_POSTFIX "-dbg") -set(CMAKE_C_FLAGS "-pedantic -ansi -Wall -Wextra -Wcast-align -Wmissing-declarations -Wmissing-prototypes -fvisibility=hidden -fstrict-aliasing -fPIC -Wl,-z,defs -Wconversion") +set(CMAKE_C_FLAGS "-pedantic -std=c99 -Wall -Wextra -Wcast-align -Wmissing-declarations -Wmissing-prototypes -fvisibility=hidden -fstrict-aliasing -fPIC -Wl,-z,defs -Wconversion") set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG") set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") @@ -28,12 +28,14 @@ add_definitions(-D_POSIX_C_SOURCE=200112L) set(RSYS_FILES_SRC clock_time.c image.c + library.c mem_allocator.c) set(RSYS_FILES_INC atomic.h clock_time.h image.h + library.h list.h mem_allocator.h ref_count.h @@ -41,11 +43,16 @@ set(RSYS_FILES_INC rsys.h) add_library(rsys SHARED ${RSYS_FILES_SRC} ${RSYS_FILES_INC}) +target_link_libraries(rsys dl) set_target_properties(rsys PROPERTIES DEFINE_SYMBOL RSYS_SHARED_BUILD) ################################################################################ # Add tests ################################################################################ +add_executable(test_library test_library.c) +target_link_libraries(test_library rsys) +add_test(test_library test_library) + add_executable(test_list test_list.c) target_link_libraries(test_list rsys) add_test(test_list test_list) diff --git a/src/clock_time.c b/src/clock_time.c @@ -9,13 +9,10 @@ #define NSEC_PER_HOUR (60L * NSEC_PER_MIN) #define NSEC_PER_DAY (24L * NSEC_PER_HOUR) -#define TIME_TO_NSEC(Time) \ - (((Time)->tv_usec + (Time)->tv_sec * 1000000L) * 1000L) - -long int +int64_t time_val(const time_T* time, enum time_unit unit) { - long val = TIME_TO_NSEC(time); + int64_t val = TIME_TO_NSEC__(time); switch(unit) { case TIME_NSEC: /* Do nothing. */ @@ -75,7 +72,7 @@ time_dump } \ } (void) 0 - time_nsec = TIME_TO_NSEC(time); + time_nsec = TIME_TO_NSEC__(time); if(flag & TIME_DAY) { const int64_t nb_days = time_nsec / NSEC_PER_DAY; DUMP(nb_days, "day"); diff --git a/src/clock_time.h b/src/clock_time.h @@ -7,10 +7,31 @@ #error "Unsupported platform" #endif -#include <stddef.h> -#include <sys/time.h> +#if _POSIX_C_SOURCE < 200112L + #include <sys/time.h> + + #define CURRENT_TIME__(Time) gettimeofday((Time), NULL) + #define GREATER_TIME_UNIT__(Time) (Time)->tv_sec + #define SMALLER_TIME_UNIT__(Time) (Time)->tv_usec + #define GREATER_TO_SMALLER_TIME_UNIT__ 1000000L + #define TIME_TO_NSEC__(Time) \ + (((Time)->tv_usec + (Time)->tv_sec * 1000000L) * 1000L) + + typedef struct timeval time_T; +#else + #include <time.h> + + #define CURRENT_TIME__(Time) clock_gettime(CLOCK_REALTIME, (Time)) + #define GREATER_TIME_UNIT__(Time) (Time)->tv_sec + #define SMALLER_TIME_UNIT__(Time) (Time)->tv_nsec + #define GREATER_TO_SMALLER_TIME_UNIT__ 1000000000L + #define TIME_TO_NSEC__(Time) \ + ((time)->tv_nsec + (Time)->tv_sec * 1000000000L) + + typedef struct timespec time_T; +#endif -typedef struct timeval time_T; +#include <stddef.h> enum time_unit { TIME_NSEC = BIT(0), @@ -27,7 +48,7 @@ time_current(time_T* time) { int err = 0; (void) err; ASSERT(time); - err = gettimeofday(time, NULL); + err = CURRENT_TIME__(time); ASSERT(err == 0); } @@ -36,11 +57,11 @@ static FINLINE void time_sub(time_T* res, const time_T* a, const time_T* b) { ASSERT(res && a && b); - res->tv_sec = a->tv_sec - b->tv_sec; - res->tv_usec = a->tv_usec - b->tv_usec; - if(res->tv_usec < 0) { - --res->tv_sec; - res->tv_usec += 1000000L; + GREATER_TIME_UNIT__(res) = GREATER_TIME_UNIT__(a) - GREATER_TIME_UNIT__(b); + SMALLER_TIME_UNIT__(res) = SMALLER_TIME_UNIT__(a) - SMALLER_TIME_UNIT__(b); + if(SMALLER_TIME_UNIT__(res) < 0) { + --GREATER_TIME_UNIT__(res); + SMALLER_TIME_UNIT__(res) += GREATER_TO_SMALLER_TIME_UNIT__; } } @@ -49,11 +70,11 @@ time_add(time_T* res, const time_T* a, const time_T* b) { ASSERT(res && a && b); - res->tv_sec = a->tv_sec + b->tv_sec; - res->tv_usec = a->tv_usec + b->tv_usec; - if(res->tv_usec >= 1000000L) { - ++res->tv_sec; - res->tv_usec -= 1000000L; + GREATER_TIME_UNIT__(res) = GREATER_TIME_UNIT__(a) + GREATER_TIME_UNIT__(b); + SMALLER_TIME_UNIT__(res) = SMALLER_TIME_UNIT__(a) + SMALLER_TIME_UNIT__(b); + if(SMALLER_TIME_UNIT__(res) >= GREATER_TO_SMALLER_TIME_UNIT__) { + ++GREATER_TIME_UNIT__(res); + SMALLER_TIME_UNIT__(res) -= GREATER_TO_SMALLER_TIME_UNIT__; } } @@ -61,7 +82,7 @@ time_add(time_T* res, const time_T* a, const time_T* b) extern "C" { #endif -RSYS_API long int +RSYS_API int64_t time_val (const time_T* time, enum time_unit unit); diff --git a/src/image.c b/src/image.c @@ -29,15 +29,15 @@ image_ppm_write goto error; \ } \ } (void)0 - #define SNPRINTF(Buf, Sz, Fmt, Arg0, Arg1, Arg2) \ + #define SNPRINTF(b, sz, ...) \ { \ - const int i = snprintf(Buf, Sz, Fmt, Arg0, Arg1, Arg2); \ + const int i = snprintf(b, sz, __VA_ARGS__); \ if( i >= BUFSIZ ) { \ goto error; \ } \ } (void)0 - SNPRINTF(buf, BUFSIZ, "P3\n\n%i %i\n%i\n", width, height, 255); + SNPRINTF(buf, BUFSIZ, "%s\n%i %i\n%i\n", "P3\n", width, height, 255); FWRITE(fp, buf); if(Bpp) { diff --git a/src/library.c b/src/library.c @@ -0,0 +1,48 @@ +#include "library.h" +#include <dlfcn.h> +#include <stdio.h> + +void* +library_open(const char* filename) +{ + if(!filename) + return NULL; + + void* handle = dlopen(filename, RTLD_NOW|RTLD_GLOBAL); + if(!handle) { + fprintf(stderr, "%s\n", dlerror()); + } + return handle; +} + +void* +library_get_symbol(void* lib, const char* sym) +{ + if(!lib || !sym) + return NULL; + + void* tmp_sym = dlsym(lib, sym); + char* err = dlerror(); + if(err == NULL) { + return tmp_sym; + } else { + fprintf(stderr, "%s\n", err); + return NULL; + } +} + +int +library_close(void* handle) +{ + if(!handle) + return -1; + + const int err = dlclose(handle); + if(err) { + fprintf(stderr, "%s\n", dlerror()); + return -1; + } + + return 0; +} + diff --git a/src/library.h b/src/library.h @@ -0,0 +1,32 @@ +#ifndef LIBRARY_H +#define LIBRARY_H + +#include "rsys.h" + +#ifdef PLATFORM_UNIX + #define SHARED_LIBRARY_NAME(Lib) "lib"Lib".so" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +RSYS_API void* /* Library handle */ +library_open + (const char* filename); + +RSYS_API void* +library_get_symbol + (void* lib, + const char* symbol); + +RSYS_API int +library_close + (void* handle); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LIBRARY_H */ + diff --git a/src/list.h b/src/list.h @@ -36,18 +36,22 @@ del_node__(struct list_node* prev, struct list_node* next) * Helper macros ******************************************************************************/ #define LIST_FOR_EACH(Pos, List) \ - for(Pos = (List)->next; Pos != (List); Pos = Pos->next) + for(struct list_node* Pos = (List)->next; Pos != (List); Pos = Pos->next) #define LIST_FOR_EACH_REVERSE(Pos, List) \ - for(Pos = (List)->prev; Pos != (List); Pos = Pos->prev) + for(struct list_node* Pos = (List)->prev; Pos != (List); Pos = Pos->prev) /* Safe against removal of list entry. */ -#define LIST_FOR_EACH_SAFE(Pos, Tmp, List) \ - for(Pos=(List)->next, Tmp=Pos->next; Pos!=(List); Pos=Tmp, Tmp=Pos->next) +#define LIST_FOR_EACH_SAFE(Pos, List) \ + for(struct list_node* Pos = (List)->next,* tmp ## COUNTER ## __ = Pos->next; \ + Pos != (List); \ + Pos = tmp ## COUNTER ## __ , tmp ## COUNTER ## __ = Pos->next) /* Safe against removal of list entry. */ -#define LIST_FOR_EACH_REVERSE_SAFE(Pos, Tmp, List) \ - for(Pos=(List)->prev, Tmp=Pos->prev; Pos!=(List); Pos=Tmp, Tmp=Pos->prev) +#define LIST_FOR_EACH_REVERSE_SAFE(Pos, List) \ + for(struct list_node* Pos = (List)->prev,* tmp ## COUNTER ## __ = Pos->prev; \ + Pos != (List); \ + Pos = tmp ## COUNTER ## __, tmp ## COUNTER ## __ = Pos->prev) /****************************************************************************** * Node list functions diff --git a/src/mem_allocator.c b/src/mem_allocator.c @@ -9,6 +9,8 @@ /******************************************************************************* * Default allocator functions ******************************************************************************/ +#define TRACK_DEFAULT_ALLOC /* Enable the tracking of default allocations */ + struct alloc_counter { atomic_size_T nb_allocs; atomic_size_T allocated_size; @@ -17,9 +19,9 @@ struct alloc_counter { static void* default_alloc (void* data, - size_t size, + const size_t size, const char* filename, - unsigned int fileline) + const unsigned int fileline) { void* mem = NULL; @@ -28,7 +30,7 @@ default_alloc if(size) { mem = malloc(size); - #ifdef NDEBUG + #ifndef TRACK_DEFAULT_ALLOC (void)data; #else ASSERT(data); @@ -38,7 +40,7 @@ default_alloc ATOMIC_ADD(&counter->allocated_size, size_mem); ATOMIC_INCR(&counter->nb_allocs); } - #endif + #endif /* TRACK_DEFAULT_ALLOC */ } return mem; } @@ -47,7 +49,7 @@ static void default_free(void* data, void* mem) { if(mem) { - #ifdef NDEBUG + #ifndef TRACK_DEFAULT_ALLOC (void)data; #else struct alloc_counter* counter = data; @@ -59,7 +61,7 @@ default_free(void* data, void* mem) ATOMIC_SUB(&counter->allocated_size, size_mem); ATOMIC_DECR(&counter->nb_allocs); - #endif + #endif /* TRACK_DEFAULT_ALLOC */ free(mem); } } @@ -67,10 +69,10 @@ default_free(void* data, void* mem) static void* default_calloc (void* data, - size_t nbelmts, - size_t size, + const size_t nbelmts, + const size_t size, const char* filename, - unsigned int fileline) + const unsigned int fileline) { void* mem = NULL; const size_t alloc_size = nbelmts * size; @@ -86,13 +88,13 @@ static void* default_realloc (void* data, void* mem, - size_t size, + const size_t size, const char* filename, - unsigned int fileline) + const unsigned int fileline) { void* new_mem = NULL; - #ifdef NDEBUG + #ifndef TRACK_DEFAULT_ALLOC (void)data; (void)filename; (void)fileline; @@ -116,17 +118,17 @@ default_realloc ATOMIC_ADD(&counter->allocated_size, size_new); } } - #endif + #endif /* TRACK_DEFAULT_ALLOC */ return new_mem; } static void* default_aligned_alloc (void* data, - size_t size, - size_t alignment, + const size_t size, + const size_t alignment, const char* filename, - unsigned int fileline) + const unsigned int fileline) { void* mem = NULL; @@ -135,7 +137,7 @@ default_aligned_alloc if(size && IS_POWER_OF_2(alignment)) { mem = memalign(alignment, size); - #ifdef NDEBUG + #ifndef TRACK_DEFAULT_ALLOC (void)data; #else ASSERT(data); @@ -145,7 +147,7 @@ default_aligned_alloc ATOMIC_ADD(&counter->allocated_size, size_mem); ATOMIC_INCR(&counter->nb_allocs); } - #endif + #endif /* TRACK_DEFAULT_ALLOC */ } return mem; } @@ -153,23 +155,23 @@ default_aligned_alloc static size_t default_allocated_size(const void* data) { - #ifdef NDEBUG + #ifndef TRACK_DEFAULT_ALLOC (void)data; return 0; #else const struct alloc_counter* counter = data; ASSERT(counter != NULL); return counter->allocated_size; - #endif + #endif /* TRACK_DEFAULT_ALLOC */ } static size_t default_dump (const void* data, char* dump, - size_t max_dump_len) + const size_t max_dump_len) { - #ifdef NDEBUG + #ifndef TRACK_DEFAULT_ALLOC (void)data; if(dump && max_dump_len) dump[0] = '\0'; @@ -220,15 +222,16 @@ struct mem_node { static void* proxy_aligned_alloc (void* data, - size_t size, - size_t align, + const size_t size, + const size_t align, const char* filename, - unsigned int fileline) + const unsigned int fileline) { struct proxy_data* proxy_data = NULL; char* mem = NULL; size_t node_header_size = 0; size_t node_size = 0; + size_t align_adjusted = 0; struct mem_node* node = NULL; ASSERT(data); @@ -236,17 +239,18 @@ proxy_aligned_alloc if((IS_POWER_OF_2(align) == 0) || align > 32768) return NULL; - align = align < PROXY_DEFAULT_ALIGNMENT ? PROXY_DEFAULT_ALIGNMENT : align; + align_adjusted = align < PROXY_DEFAULT_ALIGNMENT + ? PROXY_DEFAULT_ALIGNMENT : align; - node_header_size = ALIGN_SIZE(sizeof(struct mem_node), align); + node_header_size = ALIGN_SIZE(sizeof(struct mem_node), align_adjusted); node_size = node_header_size + size; - node = MEM_ALIGNED_ALLOC(proxy_data->allocator, node_size, align); + node = MEM_ALIGNED_ALLOC(proxy_data->allocator, node_size, align_adjusted); if(!node) return NULL; - mem = (char*)((char*)node + node_header_size); - mem[-1] = (char)(align & 0xFF); - mem[-2] = (char)((align >> 8) & 0xFF); + mem = (char*)((uintptr_t)node + (uintptr_t)node_header_size); + mem[-1] = (char)(align_adjusted & 0xFF); + mem[-2] = (char)((align_adjusted >> 8) & 0xFF); node->next = proxy_data->node_list; node->prev = NULL; node->filename = filename; @@ -261,9 +265,9 @@ proxy_aligned_alloc static void* proxy_alloc (void* data, - size_t size, + const size_t size, const char* filename, - unsigned int fileline) + const unsigned int fileline) { return proxy_aligned_alloc (data, size, PROXY_DEFAULT_ALIGNMENT, filename, fileline); @@ -272,10 +276,10 @@ proxy_alloc static void* proxy_calloc (void* data, - size_t nbelmts, - size_t size, + const size_t nbelmts, + const size_t size, const char* filename, - unsigned int fileline) + const unsigned int fileline) { size_t allocation_size = nbelmts * size; void* mem = proxy_aligned_alloc @@ -291,14 +295,14 @@ proxy_free(void* data, void* mem) if(mem) { struct proxy_data* proxy_data = NULL; struct mem_node* node = NULL; - size_t alignment = 0; + uintptr_t alignment = 0; ASSERT(data); proxy_data = data; - alignment = (size_t)(((char*)mem)[-1] | (((char*)mem)[-2] << 8)); + alignment = (uintptr_t)(((char*)mem)[-1] | (((char*)mem)[-2] << 8)); node = - (void*)((char*)mem - ALIGN_SIZE(sizeof(struct mem_node), alignment)); + (void*)((uintptr_t)mem - ALIGN_SIZE(sizeof(struct mem_node), alignment)); if(node->prev) { node->prev->next = node->next; @@ -317,9 +321,9 @@ static void* proxy_realloc (void* data, void* mem, - size_t size, + const size_t size, const char* filename, - unsigned int fileline) + const unsigned int fileline) { if(size == 0) { proxy_free(data, mem); @@ -329,12 +333,12 @@ proxy_realloc (data, size, PROXY_DEFAULT_ALIGNMENT, filename, fileline); } else { struct mem_node* node = NULL; - size_t node_header_size = 0; - size_t alignment = 0; + uintptr_t node_header_size = 0; + uintptr_t alignment = 0; - alignment = (size_t)(((char*)mem)[-1] | (((char*)mem)[-2] << 8)); + alignment = (uintptr_t)(((char*)mem)[-1] | (((char*)mem)[-2] << 8)); node_header_size = ALIGN_SIZE(sizeof(struct mem_node), alignment); - node = (void*)((char*)mem - node_header_size); + node = (void*)((uintptr_t)mem - node_header_size); if(node->size == size) { return mem; @@ -372,7 +376,7 @@ static size_t proxy_dump (const void* data, char* dump, - size_t max_dump_len) + const size_t max_dump_len) { const struct proxy_data* proxy_data = NULL; struct mem_node* node = NULL; diff --git a/src/mem_allocator.h b/src/mem_allocator.h @@ -10,30 +10,30 @@ struct mem_allocator { void* (*alloc) (void* data, - size_t size, + const size_t size, const char* filename, - unsigned int fileline); + const unsigned int fileline); void* (*calloc) (void* data, - size_t nbelmts, - size_t size, + const size_t nbelmts, + const size_t size, const char* filename, - unsigned int fileline); + const unsigned int fileline); void* (*realloc) (void* data, void* mem, - size_t size, + const size_t size, const char* filename, - unsigned int fileline); + const unsigned int fileline); void* (*aligned_alloc) (void* data, - size_t size, - size_t alignment, + const size_t size, + const size_t alignment, const char* filename, - unsigned int fileline); + const unsigned int fileline); void (*free) (void* data, @@ -45,7 +45,7 @@ struct mem_allocator { size_t (*dump) /* Return the real dump len (without the null char) */ (const void* data, char* dump, - size_t max_dump_len); /* Include the null char */ + const size_t max_dump_len); /* Include the null char */ void* data; }; diff --git a/src/ref_count.h b/src/ref_count.h @@ -23,11 +23,10 @@ ref_get(ref_T* ref) static FINLINE int ref_put(ref_T* ref, void (*release)(ref_T*)) { - int curr = 0; ASSERT(NULL != ref); ASSERT(NULL != release); - curr = ATOMIC_DECR(ref); + const int curr = ATOMIC_DECR(ref); ASSERT(curr >= 0); if(0 == curr) { diff --git a/src/rsys.h b/src/rsys.h @@ -5,6 +5,7 @@ #error "Unsupported compiler" #endif +#include <stdint.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> @@ -43,8 +44,8 @@ /******************************************************************************* * Code inlining ******************************************************************************/ -#define INLINE __inline__ -#define FINLINE INLINE __attribute__((always_inline)) +#define FINLINE inline __attribute__((always_inline)) +#define INLINE inline #define NOINLINE __attribute__((noinline)) /******************************************************************************* @@ -53,7 +54,7 @@ #define ALIGN(Size) __attribute__((aligned(Size))) #define ALIGNOF(Type) __alignof__(Type) #define ALIGN_SIZE(Size, Algnt) (((Size) + ((Algnt) - 1)) & ~((Algnt) - 1)) -#define IS_ALIGNED(Addr, Algnt) (((unsigned long)(Addr) & ((Algnt)-1)) == 0) +#define IS_ALIGNED(Addr, Algnt) (((uintptr_t)(Addr) & ((Algnt)-1)) == 0) /******************************************************************************* * Code checking @@ -118,15 +119,19 @@ #define CONCAT__(A, B) A ## B #define CONCAT(A, B) CONCAT__(A, B) -#define CONTAINER_OF(Ptr, Type, Member) \ - ((Type*)((char*)Ptr - offsetof(Type, Member))) +#define CONTAINER_OF(Ptr, Type, Member) \ + ((Type*)((uintptr_t)Ptr - offsetof(Type, Member))) #define COUNTER __COUNTER__ -#define FOR_EACH(Id, Start, End) \ - for((Id) = (Start); (Id) < (End); ++(Id)) +#define FOR_EACH(Type, Id, Start, End) \ + for(Type (Id) = (Start), CONCAT(end__, __LINE__) = (End); \ + (Id) < CONCAT(end__, __LINE__); \ + ++(Id)) #define FOR_EACH_REVERSE(Type, Id, Start, End) \ - for((Id) = (Start); (Id) > (End); --(Id)) + for(Type (Id) = (Start), CONCAT(end__, __LINE__) = (End); \ + (Id) > CONCAT(end__, __LINE__); \ + --(Id)) #define SWAP(Type, A, B) \ { \ @@ -144,7 +149,7 @@ #define STR__(X) #X #define STR(X) STR__(X) -#define OFFSET_PTR(Ptr, Offset) (void*)((char*)(Ptr) + (Offset)) +#define OFFSET_PTR(Ptr, Offset) (void*)((uintptr_t)(Ptr) + (Offset)) #endif /* SNLSYS_H */ diff --git a/src/signal.h b/src/signal.h @@ -57,7 +57,6 @@ signal_connect_callback(signal_T* signal, struct callback* clbk) static FINLINE void signal_invoke(signal_T* signal, void* args) { - struct list_node* pos = NULL; LIST_FOR_EACH(pos, signal) { struct callback* clbk = CONTAINER_OF(pos, struct callback, node); clbk->func(args, clbk->data); diff --git a/src/test_atomic.c b/src/test_atomic.c @@ -4,11 +4,11 @@ int main(int argc, char** argv) { + (void)argc, (void)argv; + atomic_int_T atom = 0; int tmp; - (void)argc, (void)argv; - tmp = ATOMIC_INCR(&atom); CHECK(atom, 1); CHECK(tmp, 1); diff --git a/src/test_condition.c b/src/test_condition.c @@ -78,21 +78,17 @@ struct buff static void read(struct stream* stream) { - size_t i = 0; ASSERT(stream); - FOR_EACH(i, 0, sizeof(src_str)/sizeof(const char*)) { - struct list_node* buff_node = NULL; - struct buff* buff = NULL; - + for(size_t i = 0; i < sizeof(src_str)/sizeof(const char*); ++i) { mutex_lock(&stream->mutex); if(is_list_empty(&stream->list_flush)) { cond_wait(&stream->cond_flush, &stream->mutex); } mutex_unlock(&stream->mutex); - buff_node = list_head(&stream->list_flush); - buff = CONTAINER_OF(buff_node, struct buff, node); + struct list_node* buff_node = list_head(&stream->list_flush); + struct buff* buff = CONTAINER_OF(buff_node, struct buff, node); CHECK(strcmp(buff->scratch, src_str[i]), 0); printf("\n%s\n", buff->scratch); @@ -107,21 +103,17 @@ read(struct stream* stream) static void write(struct stream* stream) { - size_t i = 0; ASSERT(stream); - FOR_EACH(i, 0, sizeof(src_str)/sizeof(const char*)) { - struct list_node* buff_node = NULL; - struct buff* buff = NULL; - + for(size_t i = 0; i < sizeof(src_str)/sizeof(const char*); ++i) { mutex_lock(&stream->mutex); if(is_list_empty(&stream->list_fill)) { cond_wait(&stream->cond_fill, &stream->mutex); } mutex_unlock(&stream->mutex); - buff_node = list_head(&stream->list_fill); - buff = CONTAINER_OF(buff_node, struct buff, node); + struct list_node* buff_node = list_head(&stream->list_fill); + struct buff* buff = CONTAINER_OF(buff_node, struct buff, node); ASSERT(sizeof(buff->scratch)/sizeof(char) > strlen(src_str[i])); strcpy(buff->scratch, src_str[i]); @@ -137,17 +129,16 @@ write(struct stream* stream) int main(int argc, char** argv) { - struct stream stream; - struct buff buff[2]; - (void)argc, (void)argv; + struct stream stream; list_init(&stream.list_fill); list_init(&stream.list_flush); mutex_init(&stream.mutex); cond_init(&stream.cond_flush); cond_init(&stream.cond_fill); + struct buff buff[2]; list_init(&buff[0].node); list_init(&buff[1].node); list_add(&stream.list_fill, &buff[0].node); diff --git a/src/test_library.c b/src/test_library.c @@ -0,0 +1,24 @@ +#include "library.h" +#include "mem_allocator.h" + +int +main(int argc, char** argv) +{ + void* lib = NULL; + (void)argc, (void)argv; + + CHECK(library_open(NULL), NULL); + CHECK(library_open("none"), NULL); + lib = library_open("./" SHARED_LIBRARY_NAME("rsys")); + NCHECK(lib, NULL); + + CHECK(library_get_symbol(lib, "library_get_SYMBOL"), NULL); + NCHECK(library_get_symbol(lib, "library_get_symbol"), NULL); + + NCHECK(library_close(NULL), 0); + CHECK(library_close(lib), 0); + + CHECK(MEM_ALLOCATED_SIZE(&mem_default_allocator), 0); + + return 0; +} diff --git a/src/test_list.c b/src/test_list.c @@ -10,8 +10,6 @@ main(int argc, char** argv) char c; } elmt0, elmt1, elmt2; struct list_node list, list1; - struct list_node* n = NULL; - struct list_node* tmp = NULL; int i = 0; (void)argc; @@ -139,10 +137,9 @@ main(int argc, char** argv) CHECK(i, 0); i = 0; - LIST_FOR_EACH_SAFE(n, tmp, &list1) { - struct elmt* e = NULL; + LIST_FOR_EACH_SAFE(n, &list1) { list_move_tail(n, &list); - e = CONTAINER_OF(n, struct elmt, node); + struct elmt* e = CONTAINER_OF(n, struct elmt, node); CHECK(e->c, 'a' + i); ++i; } @@ -151,10 +148,9 @@ main(int argc, char** argv) CHECK(is_list_empty(&list), 0); i = 3; - LIST_FOR_EACH_REVERSE_SAFE(n, tmp, &list) { - struct elmt* e = NULL; + LIST_FOR_EACH_REVERSE_SAFE(n, &list) { list_move(n, &list1); - e = CONTAINER_OF(n, struct elmt, node); + struct elmt* e = CONTAINER_OF(n, struct elmt, node); --i; CHECK(e->c, 'a' + i); } diff --git a/src/test_mem_allocator.c b/src/test_mem_allocator.c @@ -15,7 +15,7 @@ regular_test(struct mem_allocator* allocator) p = MEM_ALIGNED_ALLOC(allocator, 1024, ALIGNOF(char)); NCHECK(p, NULL); - CHECK(IS_ALIGNED(p, ALIGNOF(char)), 1); + CHECK(IS_ALIGNED((uintptr_t)p, ALIGNOF(char)), 1); MEM_FREE(allocator, p); q[0] = MEM_ALIGNED_ALLOC(allocator, 10, 8); @@ -24,7 +24,7 @@ regular_test(struct mem_allocator* allocator) NCHECK(q[0], NULL); NCHECK(q[1], NULL); NCHECK(q[2], NULL); - CHECK(IS_ALIGNED(q[0], 8), 1); + CHECK(IS_ALIGNED((uintptr_t)q[0], 8), 1); p = MEM_CALLOC(allocator, 2, 2); NCHECK(p, NULL); diff --git a/src/test_mutex.c b/src/test_mutex.c @@ -136,8 +136,8 @@ string_write(struct string* string, const enum mutex_type type) static void string_read(struct string* string) { - int i = 0; ASSERT(string); + int i = 0; do { mutex_rw_rlock(&string->mutex_rw); i = string->i; @@ -154,11 +154,7 @@ string_read(struct string* string) static void test_mutex(const enum mutex_type type) { - time_T time_start, time_end, time_res; - char dump[32]; - struct string string; - string.str[0] = '\0'; - string.i = 0; + struct string string = { .str = { [0] = '\0' }, .i = 0 }; switch(type) { case MUTEX_COMMON: mutex_init(&string.mutex); break; case MUTEX_SPIN: mutex_spin_init(&string.mutex_spin); break; @@ -166,6 +162,7 @@ test_mutex(const enum mutex_type type) default: ASSERT(0); break; } + time_T time_start, time_end, time_res; time_current(&time_start); #pragma omp parallel @@ -189,6 +186,7 @@ test_mutex(const enum mutex_type type) time_current(&time_end); time_sub(&time_res, &time_end, &time_start); + char dump[32]; time_dump (&time_res, TIME_MSEC|TIME_USEC, diff --git a/src/test_ref.c b/src/test_ref.c @@ -15,9 +15,9 @@ release(ref_T* ref) int main(int argc, char** argv) { - struct test test; (void)argc, (void)argv; + struct test test; ref_init(&test.ref); test.val = (int)0xDEADBEEF; diff --git a/src/test_signal.c b/src/test_signal.c @@ -37,30 +37,27 @@ sig1_func(void* arg, void* data) int main(int argc, char** argv) { - signal_T signals[SIGNALS_COUNT]; + (void)argc, (void)argv; struct ctxt ctxt; + + signal_T signals[SIGNALS_COUNT]; + FOR_EACH(int, i, 0, SIGNALS_COUNT) signal_init(&signals[i]); + struct callback clbk0_a; struct callback clbk0_b; struct callback clbk0_c; struct callback clbk1_a; struct callback clbk1_b; - int i; - - (void)argc, (void)argv; - - FOR_EACH(i, 0, SIGNALS_COUNT) - signal_init(&signals[i]); - callback_init(&clbk0_a); callback_init(&clbk0_b); callback_init(&clbk0_c); callback_init(&clbk1_a); callback_init(&clbk1_b); callback_setup(&clbk0_a, sig0_func1, NULL); - callback_setup(&clbk0_b, sig0_func2, &(int){12}); - callback_setup(&clbk0_c, sig0_func2, &(int){-1}); - callback_setup(&clbk1_a, sig1_func, &(int){2}); - callback_setup(&clbk1_b, sig1_func, &(int){3}); + callback_setup(&clbk0_b, sig0_func2, (int[]){12}); + callback_setup(&clbk0_c, sig0_func2, (int[]){-1}); + callback_setup(&clbk1_a, sig1_func, (int[]){2}); + callback_setup(&clbk1_b, sig1_func, (int[]){1}); ctxt.sig0_func1_invoked = 0; ctxt.sig0_func2_sum = 0;