rsys

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

commit 584a372a37cfb2aa7e57ad6a881bcdd8d89da11c
parent e0c59ae74f0703f5421fad8a94c5a8062d9af0aa
Author: vaplv <vaplv@free.fr>
Date:   Sun, 27 Oct 2013 19:52:32 +0100

Revert "Begin the convertion from C99 files to C89 ones"

This reverts commit af2b8e7d5b4a51201e6a962cecefe918da809ad7.

Diffstat:
Msrc/CMakeLists.txt | 2+-
Msrc/clock_time.c | 9+++------
Msrc/clock_time.h | 51++++++++++++++++++++++++++++++++++++---------------
Msrc/image.c | 6+++---
Msrc/list.h | 16++++++++++------
Msrc/mem_allocator.c | 16++++++++--------
Msrc/ref_count.h | 3+--
Msrc/rsys.h | 23++++++++++++++---------
Msrc/signal.h | 1-
Msrc/test_atomic.c | 4++--
Msrc/test_condition.c | 25++++++++-----------------
Msrc/test_list.c | 12++++--------
Msrc/test_mem_allocator.c | 4++--
Msrc/test_mutex.c | 12++++--------
Msrc/test_ref.c | 2+-
Msrc/test_signal.c | 21+++++++++------------
16 files changed, 106 insertions(+), 101 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") 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/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 @@ -244,7 +244,7 @@ proxy_aligned_alloc if(!node) return NULL; - mem = (char*)((char*)node + node_header_size); + mem = (char*)((uintptr_t)node + (uintptr_t)node_header_size); mem[-1] = (char)(align & 0xFF); mem[-2] = (char)((align >> 8) & 0xFF); node->next = proxy_data->node_list; @@ -291,14 +291,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; @@ -329,12 +329,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; 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_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, @@ -204,8 +202,6 @@ test_mutex(const enum mutex_type type) #pragma omp taskwait } - printf("%zu\n", (size_t)0); - switch(type) { case MUTEX_COMMON: mutex_destroy(&string.mutex); break; case MUTEX_SPIN: mutex_spin_destroy(&string.mutex_spin); break; 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;