rsys

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

commit 5a9464bd4f90a87608aedbfd6da55b7facb453ed
parent a68343f8673a400f00ee2fde12bacf7fa864f627
Author: vaplv <vaplv@free.fr>
Date:   Sat, 11 Jan 2014 15:48:06 +0100

Begin the code convertion from the c99 to the C90 norm

Diffstat:
Msrc/CMakeLists.txt | 113+++++++++++++++++++++++++++++++++++--------------------------------------------
Msrc/clock_time.c | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msrc/clock_time.h | 73+++++++++++++++++++++++++++----------------------------------------------
Msrc/free_list.h | 3++-
Msrc/image.c | 15++++++++-------
Msrc/library.c | 13+++++++++----
Msrc/list.h | 20++++++++++----------
Msrc/mem_allocator.c | 7+++++--
Msrc/mutex.h | 87++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/pthread/mutex.h | 7++++---
Msrc/ref_count.h | 3++-
Msrc/rsys.h | 32+++++++++++++-------------------
Msrc/signal.h | 1+
Msrc/test_atomic.c | 5+++--
Msrc/test_free_list.c | 11++++++-----
Msrc/test_list.c | 12++++++++----
Msrc/test_ref.c | 2+-
Msrc/test_signal.c | 22+++++++++++++---------
Msrc/test_time.c | 16+++++++++++-----
19 files changed, 300 insertions(+), 211 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt @@ -12,17 +12,19 @@ find_package(OpenMP) ################################################################################ # Setup compile flags/parameters ################################################################################ -set(CMAKE_DEBUG_POSTFIX "-dbg") -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(C_FLAGS "-pedantic -std=c89 -fvisibility=hidden -fstrict-aliasing -fPIC") +set(C_FLAGS_WARN "-Wall -Wextra -Wcast-align -Wmissing-declarations -Wmissing-prototypes -Wconversion") +set(C_FLAGS_LINK "-Wl,--no-undefined") +set(CMAKE_C_FLAGS "${C_FLAGS} ${C_FLAGS_WARN} ${C_FLAGS_LINK}") set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG") -set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") +set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") if(CMAKE_USE_PTHREADS_INIT) add_definitions(-DRSYS_USE_PTHREADS) endif() ################################################################################ -# Configure some generated files +# Define targets ################################################################################ set(VERSION_MAJOR 0) set(VERSION_MINOR 0) @@ -34,31 +36,29 @@ set(LIB_SUFFIX ${CMAKE_${BUILD_TYPE}_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX}) set(LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX}) configure_file(platform.h.in ${CMAKE_CURRENT_SOURCE_DIR}/platform.h) -################################################################################ -# Define targets -################################################################################ set(RSYS_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) message(STATUS "Current library version: ${RSYS_VERSION}") set(RSYS_FILES_SRC - clock_time.c - image.c - library.c - mem_allocator.c) - -set(RSYS_FILES_INC - atomic.h - clock_time.h - free_list.h - image.h - library.h - list.h - mem_allocator.h - platform.h - ref_count.h - rsys.h - rsys_version.h - signal.h) + clock_time.c + image.c + library.c + mem_allocator.c) +set(RSYS_FILES_INC_COMMON + atomic.h + clock_time.h + free_list.h + image.h + library.h + list.h + mem_allocator.h + mutex.h + ref_count.h + rsys.h + signal.h) + +set(RSYS_FILES_INC_INSTALL ${RSYS_FILES_INC_COMMON} platform.h rsys_version.h) +set(RSYS_FILES_INC_EDIT ${RSYS_FILES_INC_COMMON} platform.h.in rsys_version.h.in) add_library(rsys SHARED ${RSYS_FILES_SRC} ${RSYS_FILES_INC}) target_link_libraries(rsys dl rt) @@ -67,53 +67,40 @@ set_target_properties(rsys PROPERTIES VERSION ${RSYS_VERSION} SOVERSION ${VERSION_MAJOR}) +source_group(src FILES ${RSYS_FILES_SRC} ${RSYS_FILES_INC_EDIT}) + ################################################################################ # 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) - -add_executable(test_free_list test_free_list.c) -target_link_libraries(test_free_list rsys) -add_test(test_free_list test_free_list) - -add_executable(test_mem_allocator test_mem_allocator.c) -target_link_libraries(test_mem_allocator rsys) -add_test(test_mem_allocator test_mem_allocator) - -add_executable(test_time test_time.c) -target_link_libraries(test_time rsys) -add_test(test_time test_time) - -add_executable(test_atomic test_atomic.c) -add_test(test_atomic test_atomic) - -add_executable(test_ref test_ref.c) -add_test(test_ref test_ref) +macro(new_test _name) + add_executable(${_name} ${_name}.c) + + set(_libraries ${ARGN}) + foreach(_lib ${_libraries}) + target_link_libraries(${_name} ${_lib}) + endforeach(_lib) + + add_test(${_name} ${_name}) +endmacro(new_test) + +new_test(test_atomic) +new_test(test_free_list rsys) +new_test(test_library rsys) +new_test(test_list rsys) +new_test(test_mem_allocator rsys) +new_test(test_ref) +new_test(test_signal rsys) +new_test(test_time rsys) if(NOT OPENMP_FOUND) message(STATUS "No OpenMP support: multi-threaded tests cannot be generated") else() - add_executable(test_mutex test_mutex.c) - target_link_libraries(test_mutex rsys) - add_test(test_mutex test_mutex) - - add_executable(test_condition test_condition.c) - add_test(test_condition test_condition) - - set_target_properties(test_mutex test_condition PROPERTIES COMPILE_FLAGS ${OpenMP_C_FLAGS}) - set_target_properties(test_mutex test_condition PROPERTIES LINK_FLAGS ${OpenMP_C_FLAGS}) +# new_test(test_condition) +# new_test(test_mutex rsys) +# set_target_properties(test_mutex test_condition PROPERTIES COMPILE_FLAGS ${OpenMP_C_FLAGS}) +# set_target_properties(test_mutex test_condition PROPERTIES LINK_FLAGS ${OpenMP_C_FLAGS}) endif() -add_executable(test_signal test_signal.c) -target_link_libraries(test_signal rsys) -add_test(test_signal test_signal) - ################################################################################ # Define output & install directories ################################################################################ diff --git a/src/clock_time.c b/src/clock_time.c @@ -1,10 +1,10 @@ +#define _POSIX_C_SOURCE 200112L + #include "clock_time.h" -#include <inttypes.h> +#include <time.h> #include <string.h> -#define TIME_TO_NSEC(Time) \ - ((Time)->tv_nsec + (Time)->tv_sec * 1000000000L) - +#define TIME_TO_NSEC(Time) ((Time)->nsec + (Time)->sec * 1000000000L) #define NSEC_PER_USEC 1000L #define NSEC_PER_MSEC (1000L * NSEC_PER_USEC) #define NSEC_PER_SEC (1000L * NSEC_PER_MSEC) @@ -13,7 +13,54 @@ #define NSEC_PER_DAY (24L * NSEC_PER_HOUR) void -time_val_set(time_T* time, const int64_t val, const enum time_unit unit) +time_current(struct time* t) +{ + struct timespec time; + int err = 0; (void) err; + ASSERT(t); + + err = clock_gettime(CLOCK_REALTIME, &time); + ASSERT(err == 0); + t->sec = (int64_t)time.tv_sec; + t->nsec = (int64_t)time.tv_nsec; +} + +void +time_sleep(const struct time* t) +{ + struct timespec time; + time.tv_sec = (time_t)t->sec; + time.tv_nsec = (long)t->nsec; + clock_nanosleep(CLOCK_REALTIME, 0, &time, NULL); +} + +void +time_sub(struct time* res, const struct time* a, const struct time* b) +{ + ASSERT(res && a && b); + res->sec = a->sec - b->sec; + res->nsec = a->nsec - b->nsec; + if(res->nsec < 0) { + --res->sec; + res->nsec += 1000000000L; + } +} + +void +time_add(struct time* res, const struct time* a, const struct time* b) +{ + ASSERT(res && a && b); + + res->sec = a->sec + b->sec; + res->nsec = a->nsec + b->nsec; + if(res->nsec >= 1000000000L) { + ++res->sec; + res->nsec -= 1000000000L; + } +} + +void +time_val_set(struct time* time, const int64_t val, const enum time_unit unit) { int64_t ns = 0; int64_t s = 0; @@ -48,12 +95,12 @@ time_val_set(time_T* time, const int64_t val, const enum time_unit unit) s = val * factor; ns = 0; } - time->tv_sec = s; - time->tv_nsec = ns; + time->sec = s; + time->nsec = ns; } int64_t -time_val(const time_T* time, enum time_unit unit) +time_val(const struct time* time, enum time_unit unit) { int64_t val = TIME_TO_NSEC(time); switch(unit) { @@ -85,7 +132,7 @@ time_val(const time_T* time, enum time_unit unit) void time_dump - (const time_T* time, + (const struct time* time, int flag, size_t* real_dump_len, char* dump, @@ -96,11 +143,11 @@ time_dump ASSERT(time && (!max_dump_len || dump)); - #define DUMP(time, suffix) \ + #define DUMP(Time, Suffix) \ { \ const int len = snprintf \ (dump, available_dump_space, \ - "%" PRIi64 " %s",time, time > 1 ? suffix "s ": suffix " "); \ + "%li %s", Time, Time > 1 ? Suffix "s ": Suffix " "); \ ASSERT(len >= 0); \ if(real_dump_len) { \ real_dump_len += len; \ diff --git a/src/clock_time.h b/src/clock_time.h @@ -2,10 +2,12 @@ #define TIME_H #include "rsys.h" -#include <time.h> -#include <stddef.h> -typedef struct timespec time_T; +struct time { + /* Internal data */ + int64_t sec; + int64_t nsec; +}; enum time_unit { TIME_NSEC = BIT(0), @@ -17,65 +19,44 @@ enum time_unit { TIME_DAY = BIT(6) }; -static FINLINE void -time_current(time_T* time) -{ - int err = 0; (void) err; - ASSERT(time); - err = clock_gettime(CLOCK_REALTIME, time); - ASSERT(err == 0); - -} - -static FINLINE void -time_sleep(const time_T* time) -{ - clock_nanosleep(CLOCK_REALTIME, 0, time, NULL); -} +#ifdef __cplusplus +extern "C" { +#endif -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_nsec = a->tv_nsec - b->tv_nsec; - if(res->tv_nsec < 0) { - --res->tv_sec; - res->tv_nsec += 1000000000L; - } -} +RSYS_API void +time_current + (struct time* time); -static FINLINE void -time_add(time_T* res, const time_T* a, const time_T* b) -{ - ASSERT(res && a && b); +RSYS_API void +time_sleep + (const struct time* time); - res->tv_sec = a->tv_sec + b->tv_sec; - res->tv_nsec = a->tv_nsec + b->tv_nsec; - if(res->tv_nsec >= 1000000000L) { - ++res->tv_sec; - res->tv_nsec -= 1000000000L; - } -} +RSYS_API void +time_sub + (struct time* res, + const struct time* a, + const struct time* b); -#ifdef __cplusplus -extern "C" { -#endif +RSYS_API void +time_add + (struct time* res, + const struct time* a, + const struct time* b); RSYS_API void time_val_set - (time_T* time, + (struct time* time, const int64_t val, const enum time_unit unit); RSYS_API int64_t time_val - (const time_T* time, + (const struct time* time, enum time_unit unit); RSYS_API void time_dump - (const time_T* time, + (const struct time* time, int flag, size_t* real_dump_len, /* May be NULL. */ char* dump, /* May be NULL. */ diff --git a/src/free_list.h b/src/free_list.h @@ -90,6 +90,7 @@ FLIST_FUNC__(add)(struct FLIST_TYPE__* list) list->head = list->items[list->head].fitem__.next; } else { const uint32_t nitems_new = list->nitems ? list->nitems * 2 : 16; + uint32_t iitem = 0; struct FITEM_TYPE item; memset(&item, 0, sizeof(struct FITEM_TYPE)); @@ -98,7 +99,7 @@ FLIST_FUNC__(add)(struct FLIST_TYPE__* list) (list->allocator, list->items, nitems_new * sizeof(struct FITEM_TYPE)); - FOR_EACH(uint32_t, iitem, list->nitems, nitems_new - 1) { + FOR_EACH(iitem, list->nitems, nitems_new - 1) { list->items[iitem].fitem__.next = iitem + 1; } list->items[id.index].fitem__.id = id; diff --git a/src/image.c b/src/image.c @@ -1,3 +1,4 @@ +#define _POSIX_C_SOURCE 200112L /* snprintf support */ #include "image.h" #include <stdio.h> #include <string.h> @@ -22,22 +23,23 @@ image_ppm_write goto error; } - #define FWRITE(fp, string) \ + #define FWRITE(Fp, String) \ { \ - const size_t i = fwrite(string, sizeof(char), strlen(string), fp); \ - if( i != strlen(string) * sizeof(char) ) { \ + const size_t i = fwrite(String, sizeof(char), strlen(String), Fp); \ + if( i != strlen(String) * sizeof(char) ) { \ goto error; \ } \ } (void)0 - #define SNPRINTF(b, sz, ...) \ + + #define SNPRINTF(Buf, Sz, Str, Arg0, Arg1, Arg2) \ { \ - const int i = snprintf(b, sz, __VA_ARGS__); \ + const int i = snprintf(Buf, Sz, Str, Arg0, Arg1, Arg2); \ if( i >= BUFSIZ ) { \ goto error; \ } \ } (void)0 - SNPRINTF(buf, BUFSIZ, "%s\n%i %i\n%i\n", "P3\n", width, height, 255); + SNPRINTF(buf, BUFSIZ, "P3\n\n%i %i\n%i\n", width, height, 255); FWRITE(fp, buf); if(Bpp) { @@ -68,4 +70,3 @@ error: goto exit; } - diff --git a/src/library.c b/src/library.c @@ -5,10 +5,11 @@ void* library_open(const char* filename) { + void* handle = NULL; if(!filename) return NULL; - void* handle = dlopen(filename, RTLD_NOW|RTLD_GLOBAL); + handle = dlopen(filename, RTLD_NOW|RTLD_GLOBAL); if(!handle) { fprintf(stderr, "%s\n", dlerror()); } @@ -18,11 +19,14 @@ library_open(const char* filename) void* library_get_symbol(void* lib, const char* sym) { + void* tmp_sym = NULL; + char* err = NULL; + if(!lib || !sym) return NULL; - void* tmp_sym = dlsym(lib, sym); - char* err = dlerror(); + tmp_sym = dlsym(lib, sym); + err = dlerror(); if(err == NULL) { return tmp_sym; } else { @@ -34,10 +38,11 @@ library_get_symbol(void* lib, const char* sym) int library_close(void* handle) { + int err = 0; if(!handle) return -1; - const int err = dlclose(handle); + err = dlclose(handle); if(err) { fprintf(stderr, "%s\n", dlerror()); return -1; diff --git a/src/list.h b/src/list.h @@ -36,22 +36,22 @@ del_node__(struct list_node* prev, struct list_node* next) * Helper macros ******************************************************************************/ #define LIST_FOR_EACH(Pos, List) \ - for(struct list_node* Pos = (List)->next; Pos != (List); Pos = Pos->next) + for(Pos = (List)->next; Pos != (List); Pos = Pos->next) #define LIST_FOR_EACH_REVERSE(Pos, List) \ - for(struct list_node* Pos = (List)->prev; Pos != (List); Pos = Pos->prev) + for(Pos = (List)->prev; Pos != (List); Pos = Pos->prev) /* Safe against removal of list entry. */ -#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) +#define LIST_FOR_EACH_SAFE(Pos, Tmp, List) \ + for((Pos) = (List)->next, (Tmp) = (Pos)->next; \ + (Pos) != (List); \ + (Pos) = Tmp, Tmp = (Pos)->next) /* Safe against removal of list entry. */ -#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) +#define LIST_FOR_EACH_REVERSE_SAFE(Pos, Tmp, List) \ + for((Pos) = (List)->prev, (Tmp) = (Pos)->prev; \ + (Pos) != (List); \ + (Pos) = Tmp, Tmp = (Pos)->prev) /****************************************************************************** * Node list functions diff --git a/src/mem_allocator.c b/src/mem_allocator.c @@ -1,3 +1,4 @@ +#define _POSIX_C_SOURCE 200112L /* snprintf support */ #include "atomic.h" #include "mem_allocator.h" #include "math.h" @@ -109,12 +110,13 @@ default_realloc } else { struct alloc_counter* counter = data; const size_t size_old = malloc_usable_size(mem); + size_t size_new = 0; ASSERT(counter->allocated_size >= size_old); ATOMIC_SUB(&counter->allocated_size, size_old); new_mem = realloc(mem, size); - const size_t size_new = malloc_usable_size(new_mem); + size_new = malloc_usable_size(new_mem); ATOMIC_ADD(&counter->allocated_size, size_new); } } @@ -176,6 +178,7 @@ default_dump if(dump && max_dump_len) dump[0] = '\0'; return 0; + #else const struct alloc_counter* counter = data; size_t dump_len = 0; @@ -186,7 +189,7 @@ default_dump len = snprintf (dump, max_dump_len, - "%zu bytes allocated in %zu allocations.", + "%lu bytes allocated in %lu allocations.", counter->allocated_size, counter->nb_allocs); ASSERT(len >= 0); diff --git a/src/mutex.h b/src/mutex.h @@ -3,30 +3,81 @@ #include "rsys.h" +#ifdef __cplusplus +extern "C" { +#endif + +/******************************************************************************* + * Common mutex + ******************************************************************************/ struct mutex; -static FINLINE void mutex_init(struct mutex* mutex); -static FINLINE void mutex_destroy(struct mutex* mutex); -static FINLINE void mutex_lock(struct mutex* mutex); -static FINLINE void mutex_unlock(struct mutex* mutex); +RSYS_API struct mutex* +mutex_create /* Return NULL if an error occur */ + (void); + +RSYS_API void +mutex_destroy + (struct mutex* mutex); + +RSYS_API void +mutex_lock + (struct mutex* mutex); + +RSYS_API void +mutex_unlock + (struct mutex* mutex); + +/******************************************************************************* + * Spin lock mutex + ******************************************************************************/ struct mutex_spin; -static FINLINE void mutex_spin_init(struct mutex_spin* mutex); -static FINLINE void mutex_spin_destroy(struct mutex_spin* mutex); -static FINLINE void mutex_spin_lock(struct mutex_spin* mutex); -static FINLINE void mutex_spin_unlock(struct mutex_spin* mutex); +RSYS_API struct mutex_spin* +mutex_spin_create + (void); + +RSYS_API void +mutex_spin_destroy + (struct mutex_spin* mutex); + +RSYS_API void +mutex_spin_lock + (struct mutex_spin* mutex); + +RSYS_API void +mutex_spin_unlock + (struct mutex_spin* mutex); + +/******************************************************************************* + * Read write mutex + ******************************************************************************/ struct mutex_rw; -static FINLINE void mutex_rw_init(struct mutex_rw* mutex); -static FINLINE void mutex_rw_destroy(struct mutex_rw* mutex); -static FINLINE void mutex_rw_rlock(struct mutex_rw* mutex); -static FINLINE void mutex_rw_wlock(struct mutex_rw* mutex); -static FINLINE void mutex_rw_unlock(struct mutex_rw* mutex); - -#ifdef RSYS_USE_PTHREADS - #include "pthread/mutex.h" -#else - #error "No supported thread library is defined" + +RSYS_API void +mutex_rw_init + (struct mutex_rw* mutex); + +RSYS_API void +mutex_rw_destroy + (struct mutex_rw* mutex); + +RSYS_API void +mutex_rw_rlock + (struct mutex_rw* mutex); + +RSYS_API void +mutex_rw_wlock + (struct mutex_rw* mutex); + +RSYS_API void +mutex_rw_unlock + (struct mutex_rw* mutex); + +#ifdef __cplusplus +} /* extern "C" */ #endif #endif /* MUTEX_H */ + diff --git a/src/pthread/mutex.h b/src/pthread/mutex.h @@ -1,3 +1,4 @@ +#include "mem_allocator.h" #include <pthread.h> #ifdef NDEBUG @@ -11,10 +12,9 @@ ******************************************************************************/ struct mutex { pthread_mutex_t mutex__; }; -void -mutex_init(struct mutex* mutex) +struct mutex* +mutex_create(void) { - ASSERT(mutex); PTHREAD__(mutex_init(&mutex->mutex__, NULL)); } @@ -48,6 +48,7 @@ void mutex_spin_init(struct mutex_spin* mutex) { ASSERT(mutex); + pthread_spin_init PTHREAD__(spin_init(&mutex->mutex__, PTHREAD_PROCESS_PRIVATE)); } diff --git a/src/ref_count.h b/src/ref_count.h @@ -23,10 +23,11 @@ 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); - const int curr = ATOMIC_DECR(ref); + curr = ATOMIC_DECR(ref); ASSERT(curr >= 0); if(0 == curr) { diff --git a/src/rsys.h b/src/rsys.h @@ -9,8 +9,6 @@ #error "Unsupported compiler" #endif -#define _POSIX_C_SOURCE 200112L - #include "platform.h" #include <stdint.h> #include <stddef.h> @@ -57,8 +55,8 @@ /******************************************************************************* * Code inlining ******************************************************************************/ -#define FINLINE inline __attribute__((always_inline)) -#define INLINE inline +#define FINLINE __inline__ __attribute__((always_inline)) +#define INLINE __inline__ #define NOINLINE __attribute__((noinline)) /******************************************************************************* @@ -108,6 +106,17 @@ #define UNLIKELY(X) __builtin_expect((X), 0) /******************************************************************************* + * Iteration + ******************************************************************************/ +/* Iterate over [Start, End) */ +#define FOR_EACH(Id, Start, End) \ + for((Id) = (Start); (Id) < (End); ++(Id)) + +/* Reverse iterrate over [Start, End) */ +#define FOR_EACH_REVERSE(Type, Id, Start, End) \ + for((Id) = (Start); (Id) > (End); --(Id)) + +/******************************************************************************* * SIMD instruction sets ******************************************************************************/ #ifdef __SSE__ @@ -137,15 +146,6 @@ ((Type*)((uintptr_t)Ptr - offsetof(Type, Member))) #define COUNTER __COUNTER__ -#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(Type (Id) = (Start), CONCAT(end__, __LINE__) = (End); \ - (Id) > CONCAT(end__, __LINE__); \ - --(Id)) #define SWAP(Type, A, B) \ { \ @@ -154,12 +154,6 @@ (B) = tmp__; \ } (void)0 -#define IS_MEMORY_OVERLAPPED(D0, Sz0, D1, Sz1) \ - (((intptr_t)(D0) >= (intptr_t)(D1) && \ - (intptr_t)(D0) < ((intptr_t)(D1) + (intptr_t)(Sz1))) || \ - (((intptr_t)(D0) + (intptr_t)(Sz0)) >= (intptr_t)(D1) && \ - ((intptr_t)(D0) + (intptr_t)(Sz0)) < ((intptr_t)(D1) + (intptr_t)(Sz1)))) - #define STR__(X) #X #define STR(X) STR__(X) diff --git a/src/signal.h b/src/signal.h @@ -57,6 +57,7 @@ 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,12 @@ 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_free_list.c b/src/test_free_list.c @@ -16,9 +16,10 @@ main(int argc, char** argv) struct flist_object list; struct object* obj = NULL; struct fid id[NB_OBJ]; + int i = 0; (void)argc, (void)argv; - FOR_EACH(int, i, 0, NB_OBJ) { + FOR_EACH(i, 0, NB_OBJ) { id[i] = FID_NULL; } @@ -26,7 +27,7 @@ main(int argc, char** argv) CHECK(flist_object_hold(&list, id[0]), false); CHECK(flist_object_get(&list, id[0]), NULL); - FOR_EACH(int, i, 0, NB_OBJ / 2) { + FOR_EACH(i, 0, NB_OBJ / 2) { struct fid tmp_id; id[i] = flist_object_add(&list); CHECK(flist_object_hold(&list, id[i]), true); @@ -37,14 +38,14 @@ main(int argc, char** argv) obj->i = 0xDECAF000 + (unsigned)i; } - FOR_EACH(int, i, 0, NB_OBJ * 2 / 3) { + FOR_EACH(i, 0, NB_OBJ * 2 / 3) { const float rand_f /* in [0, 1] */ = (float)rand() / (float)RAND_MAX; const int i = (int)(rand_f * (NB_OBJ - 1)); flist_object_del(&list, id[i]); id[i] = FID_NULL; } - FOR_EACH(int, i, NB_OBJ / 2, NB_OBJ) { + FOR_EACH(i, NB_OBJ / 2, NB_OBJ) { id[i] = flist_object_add(&list); CHECK(flist_object_hold(&list, id[i]), true); obj = flist_object_get(&list, id[i]); @@ -52,7 +53,7 @@ main(int argc, char** argv) obj->i = 0xDECAF000 + (unsigned)i; } - FOR_EACH(int, i, 0, NB_OBJ) { + FOR_EACH(i, 0, NB_OBJ) { if(IS_FID_NULL(id[i])) { CHECK(flist_object_hold(&list, id[i]), false); CHECK(flist_object_get(&list, id[i]), NULL); diff --git a/src/test_list.c b/src/test_list.c @@ -10,6 +10,8 @@ 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; @@ -137,9 +139,10 @@ main(int argc, char** argv) CHECK(i, 0); i = 0; - LIST_FOR_EACH_SAFE(n, &list1) { + LIST_FOR_EACH_SAFE(n, tmp, &list1) { + struct elmt* e = NULL; list_move_tail(n, &list); - struct elmt* e = CONTAINER_OF(n, struct elmt, node); + e = CONTAINER_OF(n, struct elmt, node); CHECK(e->c, 'a' + i); ++i; } @@ -148,9 +151,10 @@ main(int argc, char** argv) CHECK(is_list_empty(&list), 0); i = 3; - LIST_FOR_EACH_REVERSE_SAFE(n, &list) { + LIST_FOR_EACH_REVERSE_SAFE(n, tmp, &list) { + struct elmt* e = NULL; list_move(n, &list1); - struct elmt* e = CONTAINER_OF(n, struct elmt, node); + e = CONTAINER_OF(n, struct elmt, node); --i; CHECK(e->c, 'a' + i); } 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,27 +37,31 @@ sig1_func(void* arg, void* data) int main(int argc, char** argv) { - (void)argc, (void)argv; - struct ctxt ctxt; - signal_T signals[SIGNALS_COUNT]; - FOR_EACH(int, i, 0, SIGNALS_COUNT) signal_init(&signals[i]); - + struct ctxt ctxt; struct callback clbk0_a; struct callback clbk0_b; struct callback clbk0_c; struct callback clbk1_a; struct callback clbk1_b; + int i = 0; + int array[] = { 12, -1, 2, 1 }; + + (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[]){1}); + callback_setup(&clbk0_b, sig0_func2, array + 0); + callback_setup(&clbk0_c, sig0_func2, array + 1); + callback_setup(&clbk1_a, sig1_func, array + 2); + callback_setup(&clbk1_b, sig1_func, array + 3); ctxt.sig0_func1_invoked = 0; ctxt.sig0_func2_sum = 0; diff --git a/src/test_time.c b/src/test_time.c @@ -3,10 +3,16 @@ #include <stdlib.h> #include <unistd.h> +static FINLINE int64_t +absi64(const int64_t i) +{ + return i < 0 ? -i : i; +} + int main(int argc, char** argv) { - time_T start, end, res; + struct time start, end, res; int64_t time = 0; (void)argc, (void)argv; @@ -17,11 +23,11 @@ main(int argc, char** argv) time_sub(&res, &end, &start); time = time_val(&res, TIME_NSEC); - CHECK(llabs((long long)(time - 1250000000)) <= 1000000 , 1); + CHECK(absi64(time - 1250000000) <= 1000000 , 1); time = time_val(&res, TIME_USEC); - CHECK(llabs((long long)(time - 1250000)) <= 1000 , 1); + CHECK(absi64(time - 1250000) <= 1000 , 1); time = time_val(&res, TIME_MSEC); - CHECK(llabs((long long)(time - 1250)) <= 1 , 1); + CHECK(absi64(time - 1250) <= 1 , 1); time = time_val(&res, TIME_SEC); CHECK(time, 1); CHECK(time_val(&res, TIME_NSEC) >= time_val(&res, TIME_USEC) * 1000, 1); @@ -34,6 +40,6 @@ main(int argc, char** argv) time_current(&end); time_sub(&res, &end, &start); time = time_val(&res, TIME_NSEC); - CHECK(llabs((long long)(time - 1234000)) <= 1000000 , 1); + CHECK(absi64(time - 1234000) <= 1000000 , 1); return 0; }