FreeWRL/FreeX3D  3.0.0
ringbuf.c
1 /*
2 
3 
4 X3D Networking Component
5 
6 */
7 
8 
9 /****************************************************************************
10  This file is part of the FreeWRL/FreeX3D Distribution.
11 
12  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
13 
14  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Lesser Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
26 ****************************************************************************/
27 
28 
29 
30 #include <config.h>
31 #include <system.h>
32 #include <display.h>
33 #include <internal.h>
34 
35 #include "../vrml_parser/Structs.h"
36 #include "../vrml_parser/CRoutes.h"
37 #include "../main/headers.h"
38 
39 #include "../input/EAIHeaders.h"
40 #include "../input/EAIHelpers.h"
41 #include "../opengl/Frustum.h"
42 #include "../opengl/Textures.h"
43 
44 #include "Component_Networking.h"
45 #include "Children.h"
46 
47 #include <libFreeWRL.h>
48 #include <list.h>
49 #include <io_http.h>
50 #include "../scenegraph/ringbuf.h"
51 
52 RingBuffer * NewRingBuffer (int elCount) {
53 
54  RingBuffer * buffer ;
55 
56  buffer = MALLOC(RingBuffer *,sizeof(RingBuffer)) ;
57 
58  //rbItem * data = malloc(sizeof(rbItem)*(FORCE_GUARD_ELEMENT+elCount));
59  rbItem * data = MALLOC(rbItem *, sizeof(rbItem)*(FORCE_GUARD_ELEMENT+elCount));
60 
61  buffer -> head = 0 ;
62  buffer -> tail = 0 ;
63  buffer -> noOfElements = FORCE_GUARD_ELEMENT+elCount ;
64  buffer -> data = data ;
65 
66  #ifdef TRACK_RINGBUFFER_MSG
67  printf("NewRingBuffer at %p , data at %p , %d elements\n",buffer, buffer->data , buffer->noOfElements);
68  #endif
69 
70  return buffer ;
71 }
72 
73 int RingBuffer_qLen(RingBuffer * buffer) {
74 
75  if(buffer->data == NULL) return 0;
76 
77  if (buffer->tail >= buffer->head) {
78  return buffer->tail - buffer->head;
79  } else {
80  return (buffer->tail + buffer->noOfElements) - buffer->head;
81  }
82 }
83 
84 int RingBuffer_freeLen(RingBuffer * buffer) {
85 
86  if(buffer->data == NULL) return 0;
87  int used = RingBuffer_qLen(buffer) ;
88  return (buffer->noOfElements-FORCE_GUARD_ELEMENT) - used ;
89 }
90 
91 int RingBuffer_testEmpty(RingBuffer * buffer) {
92  if(buffer->data == NULL) return 1;
93  return (buffer->tail == buffer->head)? 1:0;
94 }
95 
96 int RingBuffer_testFull(RingBuffer * buffer) {
97  if(buffer->data == NULL) return 1;
98  int qlen = RingBuffer_qLen(buffer) ;
99  return (qlen < (buffer->noOfElements-FORCE_GUARD_ELEMENT))? 0:1;
100 }
101 
102 int RingBuffer_pushInt(RingBuffer * buffer, int newInt) {
103 
104  if(buffer->data == NULL) return -1;
105 
106  if(!RingBuffer_testFull(buffer)) {
107  rbItem * data = buffer->data ;
108  (data+buffer->tail)->i = newInt;
109  buffer->tail++;
110  buffer->tail = buffer->tail % buffer->noOfElements;
111  return 0;
112  }
113  return -1;
114 }
115 
116 int RingBuffer_pushFloat(RingBuffer * buffer, float newFloat) {
117 
118  if(buffer->data == NULL) return -1;
119 
120  if(!RingBuffer_testFull(buffer)) {
121  rbItem * data = buffer->data ;
122  (data+buffer->tail)->f = newFloat;
123  buffer->tail++;
124  buffer->tail = buffer->tail % buffer->noOfElements;
125  return 0;
126  }
127  return -1;
128 }
129 
130 int RingBuffer_pushPointer(RingBuffer * buffer, void *newPointer) {
131 
132  if(buffer->data == NULL) return -1;
133 
134  if(!RingBuffer_testFull(buffer)) {
135  rbItem * data = buffer->data ;
136  (data+buffer->tail)->p = newPointer;
137  buffer->tail++;
138  buffer->tail = buffer->tail % buffer->noOfElements;
139  return 0;
140  }
141  return -1;
142 }
143 
144 rbItem * RingBuffer_pullUnion(RingBuffer * buffer) {
145 
146  if(buffer->data == NULL) return NULL;
147 
148  if(!RingBuffer_testEmpty(buffer)) {
149  rbItem * xyz ;
150  rbItem * data = buffer->data ;
151  xyz=(data+buffer->head);
152  buffer->head++;
153  buffer->head = buffer->head % buffer->noOfElements;
154 
155  return xyz;
156  } else {
157  return NULL ;
158  }
159 }
160 
161 rbItem * RingBuffer_peekUnion(RingBuffer * buffer) {
162 
163  if(buffer->data == NULL) return NULL;
164 
165  if(!RingBuffer_testEmpty(buffer)) {
166  rbItem * xyz ;
167  rbItem * data = buffer->data ;
168  xyz=(data+buffer->head);
169  return xyz;
170  } else {
171  return NULL ;
172  }
173 }
174 
175 void RingBuffer_makeEmpty(RingBuffer * buffer) {
176  buffer->tail = 0 ;
177  buffer->head = 0 ;
178 }
179 
180 void RingBuffer_freeDataArea(RingBuffer * buffer) {
181  if(buffer->data == NULL) return ;
182  FREE(buffer->data) ;
183  buffer->data = NULL;
184 }
185 
Definition: ringbuf.h:2
Definition: ringbuf.h:8