diff --git a/src/commons/collections/libhl/include/atomic_defs.h b/src/commons/collections/libhl/include/atomic_defs.h index b50a76266..497187444 100644 --- a/src/commons/collections/libhl/include/atomic_defs.h +++ b/src/commons/collections/libhl/include/atomic_defs.h @@ -1,27 +1,28 @@ #ifndef HL_ATOMIC_DEFS_H #define HL_ATOMIC_DEFS_H -#define ATOMIC_READ(_v) __sync_fetch_and_add(&(_v), 0) -#define ATOMIC_INCREMENT(_v) (void)__sync_fetch_and_add(&(_v), 1) -#define ATOMIC_DECREMENT(_v) (void)__sync_fetch_and_sub(&(_v), 1) -#define ATOMIC_INCREASE(_v, _n) __sync_add_and_fetch(&(_v), (_n)) -#define ATOMIC_DECREASE(_v, _n) __sync_sub_and_fetch(&(_v), (_n)) -#define ATOMIC_CAS(_v, _o, _n) __sync_bool_compare_and_swap(&(_v), (_o), (_n)) +#define ATOMIC_READ(_v) __sync_fetch_and_add(&(_v), 0) +#define ATOMIC_INCREMENT(_v) (void)__sync_fetch_and_add(&(_v), 1) +#define ATOMIC_DECREMENT(_v) (void)__sync_fetch_and_sub(&(_v), 1) +#define ATOMIC_INCREASE(_v, _n) __sync_add_and_fetch(&(_v), (_n)) +#define ATOMIC_DECREASE(_v, _n) __sync_sub_and_fetch(&(_v), (_n)) +#define ATOMIC_CAS(_v, _o, _n) __sync_bool_compare_and_swap(&(_v), (_o), (_n)) #define ATOMIC_CAS_RETURN(_v, _o, _n) __sync_val_compare_and_swap(&(_v), (_o), (_n)) -#define ATOMIC_SET(_v, _n) {\ - int _b = 0;\ - do {\ - _b = ATOMIC_CAS(_v, ATOMIC_READ(_v), _n);\ - } while (__builtin_expect(!_b, 0));\ -} - -#define ATOMIC_SET_IF(_v, _c, _n, _t) {\ - _t _o = ATOMIC_READ(_v);\ - while (__builtin_expect((_o _c (_n)) && !ATOMIC_CAS(_v, _o, _n), 0)) \ - _o = ATOMIC_READ(_v);\ -} +#define ATOMIC_SET(_v, _n) \ + { \ + int _b = 0; \ + do { \ + _b = ATOMIC_CAS(_v, ATOMIC_READ(_v), _n); \ + } while (__builtin_expect(!_b, 0)); \ + } +#define ATOMIC_SET_IF(_v, _c, _n, _t) \ + { \ + _t _o = ATOMIC_READ(_v); \ + while (__builtin_expect((_o _c(_n)) && !ATOMIC_CAS(_v, _o, _n), 0)) \ + _o = ATOMIC_READ(_v); \ + } #ifdef THREAD_SAFE @@ -32,20 +33,35 @@ #include #endif -#define MUTEX_INIT(_mutex) if (__builtin_expect(pthread_mutex_init(&(_mutex), 0) != 0, 0)) { abort(); } +#define MUTEX_INIT(_mutex) \ + if (__builtin_expect(pthread_mutex_init(&(_mutex), 0) != 0, 0)) { \ + abort(); \ + } #define MUTEX_DESTROY(_mutex) pthread_mutex_destroy(&(_mutex)) -#define MUTEX_LOCK(_mutex) if (__builtin_expect(pthread_mutex_lock(&(_mutex)) != 0, 0)) { abort(); } -#define MUTEX_UNLOCK(_mutex) if (__builtin_expect(pthread_mutex_unlock(&(_mutex)) != 0, 0)) { abort(); } +#define MUTEX_LOCK(_mutex) \ + if (__builtin_expect(pthread_mutex_lock(&(_mutex)) != 0, 0)) { \ + abort(); \ + } +#define MUTEX_UNLOCK(_mutex) \ + if (__builtin_expect(pthread_mutex_unlock(&(_mutex)) != 0, 0)) { \ + abort(); \ + } #ifdef __MACH__ #define SPIN_INIT(_mutex) ((_mutex) = 0) #define SPIN_DESTROY(_mutex) -#define SPIN_LOCK(_mutex) OSSpinLockLock(&(_mutex)) +#define SPIN_LOCK(_mutex) OSSpinLockLock(&(_mutex)) #define SPIN_UNLOCK(_mutex) OSSpinLockUnlock(&(_mutex)) #else -#define SPIN_INIT(_mutex) pthread_spin_init(&(_mutex), 0) +#define SPIN_INIT(_mutex) pthread_spin_init(&(_mutex), 0) #define SPIN_DESTROY(_mutex) pthread_spin_destroy(&(_mutex)) -#define SPIN_LOCK(_mutex) if (__builtin_expect(pthread_spin_lock(&(_mutex)) != 0, 0)) { abort(); } -#define SPIN_UNLOCK(_mutex) if (__builtin_expect(pthread_spin_unlock(&(_mutex)) != 0, 0)) { abort(); } +#define SPIN_LOCK(_mutex) \ + if (__builtin_expect(pthread_spin_lock(&(_mutex)) != 0, 0)) { \ + abort(); \ + } +#define SPIN_UNLOCK(_mutex) \ + if (__builtin_expect(pthread_spin_unlock(&(_mutex)) != 0, 0)) { \ + abort(); \ + } #endif #else #define MUTEX_INIT(_mutex) @@ -58,7 +74,7 @@ #define SPIN_UNLOCK(_mutex) #endif -#endif //ATOMIC_DEFS_H +#endif // ATOMIC_DEFS_H // vim: tabstop=4 shiftwidth=4 expandtab: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ diff --git a/src/commons/collections/libhl/include/linklist.h b/src/commons/collections/libhl/include/linklist.h index bb2c80755..beb1c37ff 100644 --- a/src/commons/collections/libhl/include/linklist.h +++ b/src/commons/collections/libhl/include/linklist.h @@ -1,4 +1,4 @@ -/** +/** * @file linklist.h * @author Andrea Guzzo * @date 22/09/2013 @@ -37,9 +37,8 @@ typedef int (*list_comparator_callback_t)(void *v1, void *v2); */ typedef struct _linked_list_s linked_list_t; - /******************************************************************** - * Common API + * Common API ********************************************************************/ /* List creation and destruction routines */ @@ -97,10 +96,9 @@ void list_lock(linked_list_t *list); void list_unlock(linked_list_t *list); /******************************************************************** - * Value-based API + * Value-based API ********************************************************************/ - /* List access routines */ /** @@ -146,7 +144,6 @@ void *list_shift_value(linked_list_t *list); */ int list_insert_value(linked_list_t *list, void *val, size_t pos); - /** * @brief Set the value at a specific position * @param list : A valid pointer to a linked_list_t structure @@ -166,7 +163,6 @@ void *list_set_value(linked_list_t *list, size_t pos, void *val); */ void *list_subst_value(linked_list_t *list, size_t pos, void *val); - /** * @brief Pick the value at a specific position * @param list : A valid pointer to a linked_list_t structure @@ -194,7 +190,7 @@ void *list_fetch_value(linked_list_t *list, size_t pos); * @param srcPos : The actual position of the value we want to move * @param dstPos : The new position where to move the value to * @return : 0 if success, -1 otherwise - */ + */ int list_move_value(linked_list_t *list, size_t srcPos, size_t dstPos); /** @@ -206,7 +202,6 @@ int list_move_value(linked_list_t *list, size_t srcPos, size_t dstPos); */ int list_swap_values(linked_list_t *list, size_t pos1, size_t pos2); - /** * @brief Callback for the value iterator * @return 1 to go ahead with the iteration, @@ -223,32 +218,31 @@ typedef int (*item_handler_t)(void *item, size_t idx, void *user); int list_foreach_value(linked_list_t *list, item_handler_t item_handler, void *user); /******************************************************************** - * Tag-based API + * Tag-based API ********************************************************************/ /** * @brief Tagged Value * - * This structure represent a tagged_value_t and is the main datatype - * you will have to handle when working with the tagged-based api. + * This structure represent a tagged_value_t and is the main datatype + * you will have to handle when working with the tagged-based api. * If user extract such structure from the list (removing it from the list) * then he MUST release its resources trough a call to destroy_tagged_value * when finished using it. - * If a new tagged_value must be created and inserted in a list, then - * list_create_tagged_value() should be used to allocate resources and obtain + * If a new tagged_value must be created and inserted in a list, then + * list_create_tagged_value() should be used to allocate resources and obtain * a pointer to a tagged_value_t structure. - */ + */ typedef struct _tagged_value_s { - char *tag; - void *value; + char * tag; + void * value; size_t vlen; - char type; + char type; #define TV_TYPE_STRING 0 #define TV_TYPE_BINARY 1 #define TV_TYPE_LIST 2 } tagged_value_t; - /* List creation and destruction routines */ /* Tagged List access routines (same of previous but with tag support */ @@ -269,8 +263,8 @@ tagged_value_t *list_create_tagged_value(char *tag, void *val, size_t len); * @param val : The value * @return A newly created tagged value with the provided tag and value * - * Only the tag will be copied, the value will just point - * to the provided value without it being copied + * Only the tag will be copied, the value will just point + * to the provided value without it being copied */ tagged_value_t *list_create_tagged_value_nocopy(char *tag, void *val); @@ -346,7 +340,7 @@ tagged_value_t *list_pick_tagged_value(linked_list_t *list, size_t pos); * @param pos : The position (offset) of the requested tagged value * @return : The tagged value stored at pos if any, NULL otherwise * - * Note this is a read-write access and the tagged value will be removed from + * Note this is a read-write access and the tagged value will be removed from * the list before returning it. * The tagged value will not be released */ @@ -372,7 +366,7 @@ tagged_value_t *list_get_tagged_value(linked_list_t *list, char *tag); * @param len: the length of the value * @param copy: leave a copy or not. * @return The previous tagged_value_t matching the given tag if any; NULL otherwise - * @note If a tagged value with the same tag is already contained in the list, + * @note If a tagged value with the same tag is already contained in the list, * this function will replace the old tagged_value_t structure with the * new one preserving the position in the list.\n * If no matching tagged_value_t structure is found, then the new one @@ -380,12 +374,11 @@ tagged_value_t *list_get_tagged_value(linked_list_t *list, char *tag); */ tagged_value_t *list_set_tagged_value(linked_list_t *list, char *tag, void *value, size_t len, int copy); - /** * @brief Get all value pointers for all tagged values matching a specific tag * @param list : A valid pointer to a linked_list_t structure holding tagged values * @param tag : The tag of the values we are looking for - * @param values : a valid pointer to a linked_list_t structure where to put the + * @param values : a valid pointer to a linked_list_t structure where to put the * value pointers held by the tagged_value_t items matching the provided tag * @return The number of tagged values matching the tag and added to the values linked list * @@ -403,14 +396,13 @@ size_t list_get_tagged_values(linked_list_t *list, char *tag, linked_list_t *val */ void list_sort(linked_list_t *list, list_comparator_callback_t comparator); - // size_t get_mem_usage_by_all_linkedlist(); /******************************************************************** - * Slice API + * Slice API ********************************************************************/ -typedef struct _slice_s slice_t; +typedef struct _slice_s slice_t; slice_t *slice_create(linked_list_t *list, size_t offset, size_t length); diff --git a/src/tools/fs/fs_ops.c b/src/tools/fs/fs_ops.c index 24dd2c1a8..ddee2f420 100644 --- a/src/tools/fs/fs_ops.c +++ b/src/tools/fs/fs_ops.c @@ -1,61 +1,70 @@ #include "fs_ops.h" -int desc_cmp(int v, int end){ +int +desc_cmp(int v, int end) +{ return v >= end; } -int asc_cmp(int v, int end) { +int +asc_cmp(int v, int end) +{ return v <= end; } -int incr(int a){ - return a+1; +int +incr(int a) +{ + return a + 1; } -int decr(int a){ - return a-1; +int +decr(int a) +{ + return a - 1; } -void collect_dir(const char *dir_path, int (*filter) (const struct dirent *), - int (*cmp) (const struct dirent **, const struct dirent **), - sorting_direction_t sd, const int topk, - int (*on_file)(struct dirent *f_entry, const char *parent_path, void *args), - int (*on_dir)(struct dirent *d_entry, const char *parent_path, void *args), - void *coll_args, - int (*pre_op)(void *coll_args), - int (*post_op)(void *coll_args)){ +void +collect_dir(const char *dir_path, int (*filter)(const struct dirent *), + int (*cmp)(const struct dirent **, const struct dirent **), sorting_direction_t sd, + const int topk, int (*on_file)(struct dirent *f_entry, const char *parent_path, void *args), + int (*on_dir)(struct dirent *d_entry, const char *parent_path, void *args), void *coll_args, + int (*pre_op)(void *coll_args), int (*post_op)(void *coll_args)) +{ if (dir_path == NULL) { // if the given start_dir is not a valid struct. return; } struct dirent **namelist; - int n; + int n; n = scandir(dir_path, &namelist, filter, cmp); if (n < 0) { perror("error occurred at scandir"); - } else { + } + else { int v, end; int (*cmp_nl)(int, int); int (*v_act)(int); - int count = 0; + int count = 0; int pre_op_rst = 0, post_op_rst = 0; if (sd == DESC) { - v = n - 1; - end = 0; + v = n - 1; + end = 0; cmp_nl = desc_cmp; - v_act = decr; - } else { - v = 0; - end = n - 1; + v_act = decr; + } + else { + v = 0; + end = n - 1; cmp_nl = asc_cmp; - v_act = incr; + v_act = incr; } while (cmp_nl(v, end) && (topk > 0 ? count < topk : 1)) { struct dirent *entry = namelist[v]; - char *path = (char *)calloc(1024, sizeof(char)); - char *name = (char *)calloc(1024, sizeof(char)); - snprintf(name,1023, "%s", entry->d_name); + char * path = (char *)calloc(1024, sizeof(char)); + char * name = (char *)calloc(1024, sizeof(char)); + snprintf(name, 1023, "%s", entry->d_name); snprintf(path, 1023, "%s/%s", dir_path, entry->d_name); if (pre_op) { pre_op_rst = pre_op(coll_args); @@ -65,7 +74,8 @@ void collect_dir(const char *dir_path, int (*filter) (const struct dirent *), on_dir(entry, dir_path, coll_args); } collect_dir(path, filter, cmp, sd, topk, on_file, on_dir, coll_args, pre_op, post_op); - } else { + } + else { if (on_file) { on_file(entry, dir_path, coll_args); } @@ -83,36 +93,38 @@ void collect_dir(const char *dir_path, int (*filter) (const struct dirent *), } } -int is_regular_file(const char *path){ +int +is_regular_file(const char *path) +{ struct stat path_stat; stat(path, &path_stat); return S_ISREG(path_stat.st_mode); } -size_t get_file_size(const char *filename) { +size_t +get_file_size(const char *filename) +{ struct stat st; - if(stat(filename, &st) != 0) { + if (stat(filename, &st) != 0) { return 0; } - return st.st_size; + return st.st_size; } - -int dir_exists(char *dirname){ - DIR* dir = opendir(dirname); - if (dir) - { +int +dir_exists(char *dirname) +{ + DIR *dir = opendir(dirname); + if (dir) { /* Directory exists. */ closedir(dir); return 1; } - else if (ENOENT == errno) - { + else if (ENOENT == errno) { /* Directory does not exist. */ return 0; } - else - { + else { /* opendir() failed for some other reason. */ return 0; } @@ -120,38 +132,39 @@ int dir_exists(char *dirname){ /* Function with behaviour like `mkdir -p' */ int -mkpath(const char *s, mode_t mode){ - char *q, *r = NULL, *path = NULL, *up = NULL; - int rv; +mkpath(const char *s, mode_t mode) +{ + char *q, *r = NULL, *path = NULL, *up = NULL; + int rv; - rv = -1; - if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0) - return (0); + rv = -1; + if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0) + return (0); - if ((path = strdup(s)) == NULL) - exit(1); - - if ((q = strdup(s)) == NULL) - exit(1); + if ((path = strdup(s)) == NULL) + exit(1); - if ((r = dirname(q)) == NULL) - goto out; - - if ((up = strdup(r)) == NULL) - exit(1); + if ((q = strdup(s)) == NULL) + exit(1); - if ((mkpath(up, mode) == -1) && (errno != EEXIST)) - goto out; + if ((r = dirname(q)) == NULL) + goto out; - if ((mkdir(path, mode) == -1) && (errno != EEXIST)) - rv = -1; - else - rv = 0; + if ((up = strdup(r)) == NULL) + exit(1); + + if ((mkpath(up, mode) == -1) && (errno != EEXIST)) + goto out; + + if ((mkdir(path, mode) == -1) && (errno != EEXIST)) + rv = -1; + else + rv = 0; out: - if (up != NULL) - free(up); - free(q); - free(path); - return (rv); + if (up != NULL) + free(up); + free(q); + free(path); + return (rv); }