Changeset ead9478


Ignore:
Timestamp:
04/10/14 01:24:05 (7 years ago)
Author:
Richard Sanger <rsangerarj@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
df668eb
Parents:
fb1fd42
Message:

Use size_t rather than int for the data structures. Export vector, deque and ring_buffer for testing and use in programs.
Remove some inlines for now since these result in lots of compile warnings when also exported, the compiler is probably smart enough anyway.

Location:
lib/data-struct
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • lib/data-struct/deque.c

    rd6a56b6 read9478  
    1919};
    2020
    21 void libtrace_deque_init(libtrace_queue_t * q, int element_size)
     21DLLEXPORT void libtrace_deque_init(libtrace_queue_t * q, size_t element_size)
    2222{
    2323        q->head = NULL;
     
    2525        q->size = 0;
    2626        q->element_size = element_size;
    27 //      q->max_size;
    2827        assert(pthread_mutex_init(&q->lock, NULL) == 0);
    2928}
    3029
    31 inline void libtrace_deque_push_back(libtrace_queue_t *q, void *d)
     30DLLEXPORT void libtrace_deque_push_back(libtrace_queue_t *q, void *d)
    3231{
    3332        // Do as much work as possible outside the lock
     
    5251}
    5352
    54 inline void libtrace_deque_push_front(libtrace_queue_t *q, void *d)
     53DLLEXPORT void libtrace_deque_push_front(libtrace_queue_t *q, void *d)
    5554{
    5655        // Do as much work as possible outside the lock
     
    7675}
    7776
    78 inline int libtrace_deque_peek_front(libtrace_queue_t *q, void *d)
     77DLLEXPORT int libtrace_deque_peek_front(libtrace_queue_t *q, void *d)
    7978{
    8079        int ret = 1;
     
    8887}
    8988
    90 inline int libtrace_deque_peek_tail(libtrace_queue_t *q, void *d)
     89DLLEXPORT int libtrace_deque_peek_tail(libtrace_queue_t *q, void *d)
    9190{
    9291        int ret = 1;
     
    10099}
    101100
    102 inline int libtrace_deque_pop_front(libtrace_queue_t *q, void *d)
     101DLLEXPORT int libtrace_deque_pop_front(libtrace_queue_t *q, void *d)
    103102{
    104103        int ret = 0;
     
    124123}
    125124
    126 inline int libtrace_deque_pop_tail(libtrace_queue_t *q, void *d)
     125DLLEXPORT int libtrace_deque_pop_tail(libtrace_queue_t *q, void *d)
    127126{
    128127        int ret = 0;
     
    133132                ret = 1;
    134133                q->tail = n->prev;
    135                 if(q->tail)
     134                if (q->tail)
    136135                        q->tail->next = NULL;
    137136                q->size--;
     
    140139        }
    141140        assert(pthread_mutex_unlock(&q->lock) == 0);
    142         memcpy(d, &q->tail->data, q->element_size);
    143         free(n);
     141        if (ret) {
     142                memcpy(d, &n->data, q->element_size);
     143                free(n);
     144        }
    144145        return ret;
    145146}
    146147
    147 inline int libtrace_deque_get_size(libtrace_queue_t *q)
     148DLLEXPORT size_t libtrace_deque_get_size(libtrace_queue_t *q)
    148149{
    149150#if RACE_SAFE
    150         int ret;
     151        size_t ret;
    151152        assert(pthread_mutex_lock(&q->lock) == 0);
    152153        ret = q->size;
     
    158159}
    159160
    160 inline void libtrace_zero_deque(libtrace_queue_t *q)
     161DLLEXPORT void libtrace_zero_deque(libtrace_queue_t *q)
    161162{
    162163        q->head = q->tail = NULL;
  • lib/data-struct/deque.h

    rd6a56b6 read9478  
    11#include <pthread.h>
     2#include "../libtrace.h"
    23
    34#ifndef LIBTRACE_DEQUE_H
     
    910        list_node_t * tail;
    1011        pthread_mutex_t lock;
    11         int size;
    12         int element_size;
     12        size_t size;
     13        size_t element_size;
    1314} libtrace_queue_t;
    1415
    15 void libtrace_deque_init(libtrace_queue_t * q, int element_size);
    16 inline void libtrace_deque_push_back(libtrace_queue_t *q, void *d);
    17 inline void libtrace_deque_push_front(libtrace_queue_t *q, void *d);
    18 inline int libtrace_deque_get_size(libtrace_queue_t *q);
     16DLLEXPORT void libtrace_deque_init(libtrace_queue_t * q, size_t element_size);
     17DLLEXPORT void libtrace_deque_push_back(libtrace_queue_t *q, void *d);
     18DLLEXPORT void libtrace_deque_push_front(libtrace_queue_t *q, void *d);
     19DLLEXPORT size_t libtrace_deque_get_size(libtrace_queue_t *q);
    1920
    20 inline int libtrace_deque_peek_front(libtrace_queue_t *q, void *d);
    21 inline int libtrace_deque_peek_tail(libtrace_queue_t *q, void *d);
    22 inline int libtrace_deque_pop_front(libtrace_queue_t *q, void *d);
    23 inline int libtrace_deque_pop_tail(libtrace_queue_t *q, void *d);
    24 inline void libtrace_zero_deque(libtrace_queue_t *q);
     21DLLEXPORT int libtrace_deque_peek_front(libtrace_queue_t *q, void *d);
     22DLLEXPORT int libtrace_deque_peek_tail(libtrace_queue_t *q, void *d);
     23DLLEXPORT int libtrace_deque_pop_front(libtrace_queue_t *q, void *d);
     24DLLEXPORT int libtrace_deque_pop_tail(libtrace_queue_t *q, void *d);
     25DLLEXPORT void libtrace_zero_deque(libtrace_queue_t *q);
    2526
    2627#endif
  • lib/data-struct/message_queue.c

    rfb1fd42 read9478  
    7272        pthread_spin_unlock(&mq->spin);
    7373        assert(read(mq->pipefd[0], message, mq->message_len) == (int) mq->message_len);
    74         return ret; 
     74        return ret;
    7575}
    7676
  • lib/data-struct/ring_buffer.c

    rd6a56b6 read9478  
    5353 *                              NOTE: this mainly applies to the blocking functions
    5454 */
    55 inline void libtrace_ringbuffer_init(libtrace_ringbuffer_t * rb, int size, int mode) {
     55DLLEXPORT void libtrace_ringbuffer_init(libtrace_ringbuffer_t * rb, size_t size, int mode) {
    5656        size = size + 1;
    5757        assert (size > 1);
     
    8888 * @param rb The ringbuffer to destroy
    8989 */
    90 inline void libtrace_ringbuffer_destroy(libtrace_ringbuffer_t * rb) {
     90DLLEXPORT void libtrace_ringbuffer_destroy(libtrace_ringbuffer_t * rb) {
    9191#if USE_LOCK_TYPE == LOCK_TYPE_SPIN
    9292        assert(pthread_spin_destroy(&rb->swlock) == 0);
     
    116116 * write/read try instead.
    117117 */
    118 inline int libtrace_ringbuffer_is_empty(const libtrace_ringbuffer_t * rb) {
     118DLLEXPORT int libtrace_ringbuffer_is_empty(const libtrace_ringbuffer_t * rb) {
    119119        return rb->start == rb->end;
    120120}
     
    125125 * write/read try instead.
    126126 */
    127 inline int libtrace_ringbuffer_is_full(const libtrace_ringbuffer_t * rb) {
     127DLLEXPORT int libtrace_ringbuffer_is_full(const libtrace_ringbuffer_t * rb) {
    128128#if USE_MODULUS
    129129        return rb->start == ((rb->end + 1) % rb->size);
     
    143143 * @param value the value to store
    144144 */
    145 inline void libtrace_ringbuffer_write(libtrace_ringbuffer_t * rb, void* value) {
     145DLLEXPORT void libtrace_ringbuffer_write(libtrace_ringbuffer_t * rb, void* value) {
    146146        /* Need an empty to start with */
    147147        if (rb->mode == LIBTRACE_RINGBUFFER_BLOCKING)
     
    180180 * @return 1 if a object was written otherwise 0.
    181181 */
    182 inline int libtrace_ringbuffer_try_write(libtrace_ringbuffer_t * rb, void* value) {
     182DLLEXPORT int libtrace_ringbuffer_try_write(libtrace_ringbuffer_t * rb, void* value) {
    183183        if (libtrace_ringbuffer_is_full(rb))
    184184                return 0;
     
    194194 * @return The object that was read
    195195 */
    196 inline void* libtrace_ringbuffer_read(libtrace_ringbuffer_t *rb) {
     196DLLEXPORT void* libtrace_ringbuffer_read(libtrace_ringbuffer_t *rb) {
    197197        void* value;
    198198       
     
    228228 * @return 1 if a object was received otherwise 0, in this case out remains unchanged
    229229 */
    230 inline int libtrace_ringbuffer_try_read(libtrace_ringbuffer_t *rb, void ** value) {
     230DLLEXPORT int libtrace_ringbuffer_try_read(libtrace_ringbuffer_t *rb, void ** value) {
    231231        if (libtrace_ringbuffer_is_empty(rb))
    232232                return 0;
     
    238238 * A thread safe version of libtrace_ringbuffer_write
    239239 */
    240 inline void libtrace_ringbuffer_swrite(libtrace_ringbuffer_t * rb, void* value) {
     240DLLEXPORT void libtrace_ringbuffer_swrite(libtrace_ringbuffer_t * rb, void* value) {
    241241        LOCK(w);
    242242        libtrace_ringbuffer_write(rb, value);
     
    247247 * A thread safe version of libtrace_ringbuffer_try_write
    248248 */
    249 inline int libtrace_ringbuffer_try_swrite(libtrace_ringbuffer_t * rb, void* value) {
     249DLLEXPORT int libtrace_ringbuffer_try_swrite(libtrace_ringbuffer_t * rb, void* value) {
    250250        int ret;
    251251#if USE_CHECK_EARLY
     
    266266 * and libtrace_ringbuffer_try_swrite are being used.
    267267 */
    268 inline int libtrace_ringbuffer_try_swrite_bl(libtrace_ringbuffer_t * rb, void* value) {
     268DLLEXPORT int libtrace_ringbuffer_try_swrite_bl(libtrace_ringbuffer_t * rb, void* value) {
    269269        int ret;
    270270#if USE_CHECK_EARLY
     
    281281 * A thread safe version of libtrace_ringbuffer_read
    282282 */
    283 inline void * libtrace_ringbuffer_sread(libtrace_ringbuffer_t *rb) {
     283DLLEXPORT void * libtrace_ringbuffer_sread(libtrace_ringbuffer_t *rb) {
    284284        void* value;
    285285        LOCK(r);
     
    292292 * A thread safe version of libtrace_ringbuffer_try_write
    293293 */
    294 inline int libtrace_ringbuffer_try_sread(libtrace_ringbuffer_t *rb, void ** value) {
     294DLLEXPORT int libtrace_ringbuffer_try_sread(libtrace_ringbuffer_t *rb, void ** value) {
    295295        int ret;
    296296#if USE_CHECK_EARLY
     
    311311 * and libtrace_ringbuffer_try_sread are being used.
    312312 */
    313 inline int libtrace_ringbuffer_try_sread_bl(libtrace_ringbuffer_t *rb, void ** value) {
     313DLLEXPORT int libtrace_ringbuffer_try_sread_bl(libtrace_ringbuffer_t *rb, void ** value) {
    314314        int ret;
    315315#if USE_CHECK_EARLY
     
    323323}
    324324
    325 inline void libtrace_zero_ringbuffer(libtrace_ringbuffer_t * rb)
     325DLLEXPORT void libtrace_zero_ringbuffer(libtrace_ringbuffer_t * rb)
    326326{
    327327        rb->start = 0;
  • lib/data-struct/ring_buffer.h

    rd6a56b6 read9478  
    11#include <pthread.h>
    22#include <semaphore.h>
     3#include "../libtrace.h"
    34
    45#ifndef LIBTRACE_RINGBUFFER_H
     
    1112// if LIBTRACE_RINGBUFFER_POLLING is to work.
    1213typedef struct libtrace_ringbuffer {
    13         volatile int start;
    14         int size;
     14        volatile size_t start;
     15        size_t size;
    1516        int mode;
    1617        void *volatile*elements;
     
    2425        sem_t fulls;
    2526        // Aim to get this on a separate cache line to start - important if spinning
    26         volatile int end;
    27        
     27        volatile size_t end;
    2828} libtrace_ringbuffer_t;
    2929
    30 void libtrace_ringbuffer_init(libtrace_ringbuffer_t * rb, int size, int mode);
    31 inline void libtrace_zero_ringbuffer(libtrace_ringbuffer_t * rb);
    32 void libtrace_ringbuffer_destroy(libtrace_ringbuffer_t * rb);
    33 int libtrace_ringbuffer_is_empty(const libtrace_ringbuffer_t * rb);
    34 int libtrace_ringbuffer_is_full(const libtrace_ringbuffer_t * rb);
     30DLLEXPORT void libtrace_ringbuffer_init(libtrace_ringbuffer_t * rb, size_t size, int mode);
     31DLLEXPORT void libtrace_zero_ringbuffer(libtrace_ringbuffer_t * rb);
     32DLLEXPORT void libtrace_ringbuffer_destroy(libtrace_ringbuffer_t * rb);
     33DLLEXPORT int libtrace_ringbuffer_is_empty(const libtrace_ringbuffer_t * rb);
     34DLLEXPORT int libtrace_ringbuffer_is_full(const libtrace_ringbuffer_t * rb);
    3535
    36 void libtrace_ringbuffer_write(libtrace_ringbuffer_t * rb, void* value);
    37 int libtrace_ringbuffer_try_write(libtrace_ringbuffer_t * rb, void* value);
    38 void libtrace_ringbuffer_swrite(libtrace_ringbuffer_t * rb, void* value);
    39 int libtrace_ringbuffer_try_swrite(libtrace_ringbuffer_t * rb, void* value);
    40 int libtrace_ringbuffer_try_swrite_bl(libtrace_ringbuffer_t * rb, void* value);
     36DLLEXPORT void libtrace_ringbuffer_write(libtrace_ringbuffer_t * rb, void* value);
     37DLLEXPORT int libtrace_ringbuffer_try_write(libtrace_ringbuffer_t * rb, void* value);
     38DLLEXPORT void libtrace_ringbuffer_swrite(libtrace_ringbuffer_t * rb, void* value);
     39DLLEXPORT int libtrace_ringbuffer_try_swrite(libtrace_ringbuffer_t * rb, void* value);
     40DLLEXPORT int libtrace_ringbuffer_try_swrite_bl(libtrace_ringbuffer_t * rb, void* value);
    4141
    42 void* libtrace_ringbuffer_read(libtrace_ringbuffer_t *rb) ;
    43 int libtrace_ringbuffer_try_read(libtrace_ringbuffer_t *rb, void ** value);
    44 void * libtrace_ringbuffer_sread(libtrace_ringbuffer_t *rb);
    45 int libtrace_ringbuffer_try_sread(libtrace_ringbuffer_t *rb, void ** value);
    46 int libtrace_ringbuffer_try_sread_bl(libtrace_ringbuffer_t *rb, void ** value);
     42DLLEXPORT void* libtrace_ringbuffer_read(libtrace_ringbuffer_t *rb) ;
     43DLLEXPORT int libtrace_ringbuffer_try_read(libtrace_ringbuffer_t *rb, void ** value);
     44DLLEXPORT void * libtrace_ringbuffer_sread(libtrace_ringbuffer_t *rb);
     45DLLEXPORT int libtrace_ringbuffer_try_sread(libtrace_ringbuffer_t *rb, void ** value);
     46DLLEXPORT int libtrace_ringbuffer_try_sread_bl(libtrace_ringbuffer_t *rb, void ** value);
    4747
    4848#endif
  • lib/data-struct/sliding_window.c

    rd6a56b6 read9478  
    1414 *                              NOTE: this mainly applies to the blocking functions
    1515 */
    16 inline void libtrace_slidingwindow_init(libtrace_slidingwindow_t *sw, int size, uint64_t start_number) {
     16inline void libtrace_slidingwindow_init(libtrace_slidingwindow_t *sw, size_t size, uint64_t start_number) {
    1717        sw->size = size; // All of this size can be used
    1818        sw->start = 0;
    1919        sw->elements = calloc(sw->size, sizeof(void*));
    2020        assert(sw->elements);
    21         memset(sw->elements, 0, sizeof(void*) * sw->size);
     21        memset((void *) sw->elements, 0, sizeof(void*) * sw->size);
    2222        sw->start_number = start_number;
    2323}
  • lib/data-struct/sliding_window.h

    rd6a56b6 read9478  
    11#include <stdint.h>
     2#include <stddef.h>
    23
    34#ifndef LIBTRACE_SLIDING_WINDOW_H
     
    1011// if LIBTRACE_RINGBUFFER_SPINNING is to work.
    1112typedef struct libtrace_slidingwindow {
    12         volatile int start;
    13         int size;
     13        volatile size_t start;
     14        size_t size;
    1415        volatile uint64_t start_number;
    1516        void *volatile*elements;
    1617} libtrace_slidingwindow_t;
    1718
    18 void libtrace_slidingwindow_init(libtrace_slidingwindow_t * sw, int size, uint64_t start_number);
     19void libtrace_slidingwindow_init(libtrace_slidingwindow_t * sw, size_t size, uint64_t start_number);
    1920inline void libtrace_zero_slidingwindow(libtrace_slidingwindow_t * sw);
    2021void libtrace_slidingwindow_destroy(libtrace_slidingwindow_t * sw);
  • lib/data-struct/vector.c

    rd6a56b6 read9478  
    55#include <assert.h>
    66
    7 DLLEXPORT inline void libtrace_vector_init(libtrace_vector_t *v, int element_size) {
     7DLLEXPORT void libtrace_vector_init(libtrace_vector_t *v, size_t element_size) {
    88        v->element_size = element_size;
    99        v->size = 0; // Starts empty
     
    1313}
    1414
    15 DLLEXPORT inline void libtrace_vector_destroy(libtrace_vector_t *v) {
     15DLLEXPORT void libtrace_vector_destroy(libtrace_vector_t *v) {
    1616        assert(pthread_mutex_destroy(&v->lock) == 0);
    1717        free(v->elements);
     
    2323}
    2424
    25 DLLEXPORT inline void libtrace_vector_push_back(libtrace_vector_t *v, void *d) {
     25DLLEXPORT void libtrace_vector_push_back(libtrace_vector_t *v, void *d) {
    2626        assert(pthread_mutex_lock(&v->lock) == 0);
    2727        if (v->size >= v->max_size) {
     
    3636}
    3737
    38 DLLEXPORT inline int libtrace_vector_get_size(libtrace_vector_t *v) {
     38DLLEXPORT size_t libtrace_vector_get_size(libtrace_vector_t *v) {
    3939        return v->size;
    4040}
    4141
    42 DLLEXPORT inline int libtrace_vector_get(libtrace_vector_t *v, int location, void *d) {
     42DLLEXPORT int libtrace_vector_get(libtrace_vector_t *v, size_t location, void *d) {
    4343        assert(pthread_mutex_lock(&v->lock) == 0);
    4444        if (location >= v->size) {
     
    5151}
    5252
    53 DLLEXPORT inline int libtrace_vector_remove_front(libtrace_vector_t *v) {
    54         int i;
     53DLLEXPORT int libtrace_vector_remove_front(libtrace_vector_t *v) {
     54        size_t i;
    5555        assert(pthread_mutex_lock(&v->lock) == 0);
    5656        if (!v->size) {
     
    6666}
    6767
    68 static inline void memswap(void *a, void *b, int size) {
     68static inline void memswap(void *a, void *b, size_t size) {
    6969        char c;
    70         int i;
     70        size_t i;
    7171        for (i=0; i<size; i++) {
    7272                c = ((char *)a)[i];
     
    7676}
    7777// Note elements must be the same size
    78 DLLEXPORT inline void libtrace_vector_append(libtrace_vector_t *dest, libtrace_vector_t *src)
     78// This also empties the second source array
     79DLLEXPORT void libtrace_vector_append(libtrace_vector_t *dest, libtrace_vector_t *src)
    7980{
    8081        assert(dest->element_size == src->element_size);
     
    9192                memswap(&dest->elements, &src->elements, sizeof(src->elements));
    9293        } else {
    93                 int oldmax = dest->max_size;
     94                size_t oldmax = dest->max_size;
    9495                while (dest->max_size - dest->size < src->size) dest->max_size *= 2;
    9596                if (oldmax != dest->max_size)
     
    107108}
    108109
    109 DLLEXPORT inline void libtrace_zero_vector(libtrace_vector_t *v)
     110DLLEXPORT void libtrace_zero_vector(libtrace_vector_t *v)
    110111{
    111112        v->max_size = 0;
     
    115116}
    116117
    117 DLLEXPORT inline void libtrace_vector_empty(libtrace_vector_t *v) {
     118DLLEXPORT void libtrace_vector_empty(libtrace_vector_t *v) {
    118119        assert(pthread_mutex_lock(&v->lock) == 0);
    119120        v->size = 0;
  • lib/data-struct/vector.h

    rd6a56b6 read9478  
    77
    88typedef struct libtrace_vector {
    9         int max_size;
    10         int size;
    11         int element_size;
     9        size_t max_size;
     10        size_t size;
     11        size_t element_size;
    1212        char *elements; // Means we can use array indexing
    1313        pthread_mutex_t lock;
    1414} libtrace_vector_t;
    1515
    16 DLLEXPORT inline void libtrace_vector_init(libtrace_vector_t *v, int element_size);
     16DLLEXPORT inline void libtrace_vector_init(libtrace_vector_t *v, size_t element_size);
    1717DLLEXPORT inline void libtrace_vector_push_back(libtrace_vector_t *v, void *d);
    18 DLLEXPORT inline int libtrace_vector_get_size(libtrace_vector_t *v);
    19 DLLEXPORT inline int libtrace_vector_get(libtrace_vector_t *v, int location, void *d);
     18DLLEXPORT inline size_t libtrace_vector_get_size(libtrace_vector_t *v);
     19DLLEXPORT inline int libtrace_vector_get(libtrace_vector_t *v, size_t location, void *d);
    2020DLLEXPORT inline void libtrace_vector_append(libtrace_vector_t *dest, libtrace_vector_t *src);
    2121DLLEXPORT inline void libtrace_vector_destroy(libtrace_vector_t *v);
Note: See TracChangeset for help on using the changeset viewer.