diff --git a/src/fifo.c b/src/fifo.c index f59ef94..f9f82b7 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -1,10 +1,9 @@ -#include +#include #include #include #include -#include "utils.h" +#include #include "fifo.h" -#include "spinlock.h" #define is_power_of_2(x) (((x) != 0) && (((x) & (x - 1)) == 0)) #define min(a, b) (((a) < (b)) ? (a) : (b)) @@ -14,10 +13,15 @@ struct fifo { int size; int in; int out; - struct spinlock lock; + pthread_spinlock_t lock; }; -unsigned int roundup_pow_of_two(unsigned int x) +#define SPIN_INIT(fxo) pthread_spin_init(&fxo->lock, PTHREAD_PROCESS_PRIVATE) +#define SPIN_RELEASE(fxo) pthread_spin_destroy(&fxo->lock) +#define SPIN_LOCK(fxo) pthread_spin_lock(&fxo->lock) +#define SPIN_UNLOCK(fxo) pthread_spin_unlock(&fxo->lock) + +static unsigned int roundup_pow_of_two(unsigned int x) { int i = 0; @@ -40,17 +44,13 @@ fifo_t fifo_new_with_ext_buffer(unsigned char *buffer, unsigned int size) fifo_t fifo; if (!is_power_of_2(size)){ -#ifndef __mingw__ - log_info("size must be power of 2."); -#endif + fprintf(stderr, "size must be power of 2."); return NULL; } fifo = calloc(1, sizeof *fifo); if (!fifo) { -#ifndef __mingw__ - log_info("fifo_init; out of memory."); -#endif + fprintf(stderr, "kfifo_init; out of memory."); return NULL; } @@ -61,23 +61,49 @@ fifo_t fifo_new_with_ext_buffer(unsigned char *buffer, unsigned int size) return fifo; } +unsigned int fifo_cap(fifo_t fifo) +{ + return fifo->size; +} + +int fifo_resize(fifo_t fifo, unsigned int size) +{ + unsigned char *buffer, *op; + unsigned int xlen; + + if (!is_power_of_2(size)) { + fprintf(stderr, "size is not of power2 %d.\n", size); + size = roundup_pow_of_two(size); + } + buffer = malloc(size); + if (!buffer) { + return -1; + } + xlen = fifo_get(fifo, buffer, size); + SPIN_LOCK(fifo); + op = fifo->buffer; + fifo->buffer = buffer; + fifo->size = size; + fifo->in = xlen; + fifo->out = 0; + SPIN_UNLOCK(fifo); + free(op); + return 0; +} + fifo_t fifo_new(unsigned int size) { unsigned char *buffer; fifo_t fifo; if (!is_power_of_2(size)) { -#ifndef __mingw__ - log_info("size is not of power2."); -#endif + fprintf(stderr, "size is not of power2 %d.\n", size); size = roundup_pow_of_two(size); } buffer = calloc(1, size); if (!buffer) { -#ifndef __mingw__ - log_info("fifo_alloc: out of memory."); -#endif + fprintf(stderr, "kfifo_alloc: out of memory."); return NULL; } diff --git a/src/spinlock.h b/src/spinlock.h deleted file mode 100644 index 9460f0d..0000000 --- a/src/spinlock.h +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef __XA_SPINLOCK_H__ -#define __XA_SPINLOCK_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#define SPIN_INIT(x) spinlock_init(&(x)->lock) -#define SPIN_LOCK(x) spinlock_lock(&(x)->lock) -#define SPIN_UNLOCK(x) spinlock_unlock(&(x)->lock) -#define SPIN_RELEASE(x) spinlock_destroy(&(x)->lock) - -/* - * Use gcc's instric instructions implentate - * spinlock. - */ -#ifndef __UNUSED__ -#define __UNUSED__ __attribute__((unused)) -#endif - -struct spinlock { - int lock; -}; - -/* - * Initialize spinlock. - */ -static inline void __UNUSED__ spinlock_init(struct spinlock *lock) -{ - lock->lock = 0; -} - -/* - * Lock - */ -static inline void __UNUSED__ spinlock_lock(struct spinlock *lock) -{ - while (__sync_lock_test_and_set(&lock->lock, 1)) - ; -} - -/* - * Trylock. - * - * return 1 on locked, 0 on faile. - */ -static inline int __UNUSED__ spinlock_trylock(struct spinlock *lock) -{ - return __sync_lock_test_and_set(&lock->lock, 1) == 0; -} - -/* - * Unlock. - */ -static inline void __UNUSED__ spinlock_unlock(struct spinlock *lock) -{ - __sync_lock_release(&lock->lock); -} - -/* - * Deinitialize. - */ -static inline void __UNUSED__ spinlock_destroy(struct spinlock *lock) -{ - (void)lock; -} - -#ifdef __cplusplus -} -#endif -#endif -