suitcase

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

utringbuffer.h (5701B)


      1 /*
      2 Copyright (c) 2015-2022, Troy D. Hanson  https://troydhanson.github.io/uthash/
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are met:
      7 
      8     * Redistributions of source code must retain the above copyright
      9       notice, this list of conditions and the following disclaimer.
     10 
     11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     12 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     13 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     14 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     15 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     16 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     17 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     18 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     19 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     22 */
     23 
     24 /* a ring-buffer implementation using macros
     25  */
     26 #ifndef UTRINGBUFFER_H
     27 #define UTRINGBUFFER_H
     28 
     29 #define UTRINGBUFFER_VERSION 2.3.0
     30 
     31 #include <stdlib.h>
     32 #include <string.h>
     33 #include "utarray.h"  // for "UT_icd"
     34 
     35 typedef struct {
     36     unsigned i;       /* index of next available slot; wraps at n */
     37     unsigned n;       /* capacity */
     38     unsigned char f;  /* full */
     39     UT_icd icd;       /* initializer, copy and destructor functions */
     40     char *d;          /* n slots of size icd->sz */
     41 } UT_ringbuffer;
     42 
     43 #define utringbuffer_init(a, _n, _icd) do {                               \
     44   memset(a, 0, sizeof(UT_ringbuffer));                                    \
     45   (a)->icd = *(_icd);                                                     \
     46   (a)->n = (_n);                                                          \
     47   if ((a)->n) { (a)->d = (char*)malloc((a)->n * (_icd)->sz); }            \
     48 } while(0)
     49 
     50 #define utringbuffer_clear(a) do {                                        \
     51   if ((a)->icd.dtor) {                                                    \
     52     if ((a)->f) {                                                         \
     53       unsigned _ut_i;                                                     \
     54       for (_ut_i = 0; _ut_i < (a)->n; ++_ut_i) {                          \
     55         (a)->icd.dtor(utringbuffer_eltptr(a, _ut_i));                     \
     56       }                                                                   \
     57     } else {                                                              \
     58       unsigned _ut_i;                                                     \
     59       for (_ut_i = 0; _ut_i < (a)->i; ++_ut_i) {                          \
     60         (a)->icd.dtor(utringbuffer_eltptr(a, _ut_i));                     \
     61       }                                                                   \
     62     }                                                                     \
     63   }                                                                       \
     64   (a)->i = 0;                                                             \
     65   (a)->f = 0;                                                             \
     66 } while(0)
     67 
     68 #define utringbuffer_done(a) do {                                         \
     69   utringbuffer_clear(a);                                                  \
     70   free((a)->d); (a)->d = NULL;                                            \
     71   (a)->n = 0;                                                             \
     72 } while(0)
     73 
     74 #define utringbuffer_new(a,n,_icd) do {                                   \
     75   a = (UT_ringbuffer*)malloc(sizeof(UT_ringbuffer));                      \
     76   utringbuffer_init(a, n, _icd);                                          \
     77 } while(0)
     78 
     79 #define utringbuffer_free(a) do {                                         \
     80   utringbuffer_done(a);                                                   \
     81   free(a);                                                                \
     82 } while(0)
     83 
     84 #define utringbuffer_push_back(a,p) do {                                                \
     85   if ((a)->icd.dtor && (a)->f) { (a)->icd.dtor(_utringbuffer_internalptr(a,(a)->i)); }  \
     86   if ((a)->icd.copy) { (a)->icd.copy( _utringbuffer_internalptr(a,(a)->i), p); }        \
     87   else { memcpy(_utringbuffer_internalptr(a,(a)->i), p, (a)->icd.sz); };                \
     88   if (++(a)->i == (a)->n) { (a)->i = 0; (a)->f = 1; }                                   \
     89 } while(0)
     90 
     91 #define utringbuffer_len(a) ((a)->f ? (a)->n : (a)->i)
     92 #define utringbuffer_empty(a) ((a)->i == 0 && !(a)->f)
     93 #define utringbuffer_full(a) ((a)->f != 0)
     94 
     95 #define _utringbuffer_real_idx(a,j) ((a)->f ? ((j) + (a)->i) % (a)->n : (j))
     96 #define _utringbuffer_internalptr(a,j) ((void*)((a)->d + ((a)->icd.sz * (j))))
     97 #define utringbuffer_eltptr(a,j) ((0 <= (j) && (j) < utringbuffer_len(a)) ? _utringbuffer_internalptr(a,_utringbuffer_real_idx(a,j)) : NULL)
     98 
     99 #define _utringbuffer_fake_idx(a,j) ((a)->f ? ((j) + (a)->n - (a)->i) % (a)->n : (j))
    100 #define _utringbuffer_internalidx(a,e) (((char*)(e) >= (a)->d) ? (((char*)(e) - (a)->d)/(a)->icd.sz) : -1)
    101 #define utringbuffer_eltidx(a,e) _utringbuffer_fake_idx(a, _utringbuffer_internalidx(a,e))
    102 
    103 #define utringbuffer_front(a) utringbuffer_eltptr(a,0)
    104 #define utringbuffer_next(a,e) ((e)==NULL ? utringbuffer_front(a) : utringbuffer_eltptr(a, utringbuffer_eltidx(a,e)+1))
    105 #define utringbuffer_prev(a,e) ((e)==NULL ? utringbuffer_back(a) : utringbuffer_eltptr(a, utringbuffer_eltidx(a,e)-1))
    106 #define utringbuffer_back(a) (utringbuffer_empty(a) ? NULL : utringbuffer_eltptr(a, utringbuffer_len(a) - 1))
    107 
    108 #endif /* UTRINGBUFFER_H */