JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
RingBuffer.cpp
Go to the documentation of this file.
1
7#include "RingBuffer.h"
8
9
10using namespace jbxl;
11
12
14{
15 buf = NULL;
16 bufsz = 0;
17 datasz = 0;
18 spoint = 0;
19 epoint = 0;
20 datano = 0;
21 state = 0;
22 enable = FALSE;
23}
24
25
26BOOL CRingBuffer::init(int ring_size, int data_size)
27{
28 init_data();
29
30 buf = (void**)malloc(ring_size*sizeof(void*));
31 if (buf==NULL) return FALSE;
32 memset(buf, 0, ring_size*sizeof(void*));
33 bufsz = ring_size;
34 datasz = data_size;
35
36 BOOL ret = TRUE;
37 for (int i=0; i<bufsz; i++) {
38 buf[i] = (void*)malloc(datasz);
39 if (buf[i]==NULL) {
40 this->free();
41 ret = FALSE;
42 state = -1;
43 break;
44 }
45 memset(buf[i], 0, datasz);
46 }
47
48 if (ret) enable = TRUE;
49 return ret;
50}
51
52
54{
55 if (!isNull(buf)) {
56 for (int i=0; i<bufsz; i++) {
57 if (!isNull(buf[i])) {
58 ::free(buf[i]);
59 }
60 }
61 ::free(buf);
62 }
63 init_data();
64
65 return;
66}
67
68
70{
71 for (int i=0; i<bufsz; i++) {
72 memset(buf[i], 0, datasz);
73 }
74 spoint = 0;
75 epoint = 0;
76 datano = 0;
77 state = 0;
78
79 return;
80}
81
82
84{
85 void* ptr = NULL;
86
87 if (datano>0) {
88 ptr = buf[spoint];
89 spoint++;
90 if (spoint==bufsz) spoint = 0;
91 datano--;
92 }
93
94 return ptr;
95}
96
97
98void* CRingBuffer::get(int pos)
99{
100 pos = epoint + pos;
101 if (pos<0) pos = bufsz + pos % bufsz;
102 else pos = pos % bufsz;
103
104 return buf[pos];
105}
106
107
108void CRingBuffer::put(void* ptr)
109{
110 memcpy(buf[epoint], ptr, datasz);
111 epoint++;
112 if (epoint==bufsz) epoint = 0;
113 if (datano<bufsz) datano++;
114
115 return;
116}
117
118
119void CRingBuffer::put(void* ptr, int pos)
120{
121 pos = epoint + pos;
122 if (pos<0) pos = bufsz + pos % bufsz;
123 else pos = pos % bufsz;
124
125 memcpy(buf[pos], ptr, datasz);
126
127 return;
128}
129
汎用リングバッファライブラリ ヘッダ
BOOL init(int ring_size, int data_size)
void put(void *ptr)
void * get(void)
void init_data(void)
#define TRUE
Definition common.h:226
#define FALSE
Definition common.h:223
Definition Brep.h:29
bool isNull(void *p)
Definition common++.h:24