ZEDA  1.6.18
zeda_ring.h
Go to the documentation of this file.
1 /* ZEDA - Elementary Data and Algorithms
2  * Copyright (C) 1998 Tomomichi Sugihara (Zhidao)
3  */
9 #ifndef __ZEDA_RING_H__
10 #define __ZEDA_RING_H__
11 
12 #include <zeda/zeda_misc.h>
13 
15 
16 /* ********************************************************** *//* ************************************************** */
19 
47 #ifdef __cplusplus
48 #define zRingClass(ring_t,cell_t) \
49 struct ring_t{\
50  int size;\
51  int head;\
52  cell_t *buf;\
53  ring_t(){ size=head=0; buf=NULL; };\
54  ~ring_t(){ if( buf ) delete [] buf; };\
55 }
56 #else
57 #define zRingClass(ring_t,cell_t) \
58 typedef struct{\
59  int size;\
60  int head;\
61  cell_t *buf;\
62 } ring_t
63 #endif /* __cplusplus */
64 
65 #define zRingSize(y) (y)->size
66 #define zRingBuf(y) ( (y)->buf )
67 
68 /* NOTE: do not use the following macro before allocating buffer */
69 #define zRingElemSize(y) sizeof(*zRingBuf(y))
70 
71 #define zRingHead(y) ( &zRingBuf(y)[(y)->head] )
72 #define zRingElem(y,i) ( &zRingBuf(y)[((y)->head+(i)) % (y)->size] )
73 #define zRingSetElem(a,i,d) \
74  memcpy( zRingElem(a,i), (d), zRingElemSize(a) )
75 
76 #define zRingReset(y) ( (y)->head = 0 );
77 #define zRingInit(y) do{\
78  zRingSize(y) = (y)->head = 0;\
79  zRingBuf(y) = NULL;\
80 } while(0)
81 
87 #define zRingAlloc(y,type,size) do{\
88  zRingInit( y );\
89  if( !( zRingBuf(y) = zAlloc(type,size) ) )\
90  ZALLOCERROR();\
91  else\
92  zRingSize(y) = (size);\
93 } while(0)
94 
95 #define zRingFree(y) do{\
96  zFree( zRingBuf(y) );\
97  zRingInit( y );\
98 } while(0)
99 
100 #define zRingIncHead(y) do{\
101  if( ++(y)->head >= zRingSize(y) ) (y)->head = 0;\
102 } while(0)
103 #define zRingDecHead(y) do{\
104  if( --(y)->head < 0 ) (y)->head = zRingSize(y) - 1;\
105 } while(0)
106 
110 
111 #endif /* __ZEDA_RING_H__ */
#define __END_DECLS
Definition: zeda_defs.h:30
miscellanies.
#define __BEGIN_DECLS
Definition: zeda_defs.h:26