summaryrefslogtreecommitdiff
path: root/src/ivyfifo.c
blob: a13b276290a9a6b9b577dbc4a536059f8b1e14f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/socket.h>
#endif
#include <stdio.h> // DEBUG, pour printf
#include "ivyfifo.h"
#include "param.h"



#define 	MIN(a, b)   ((a) > (b) ? (b) : (a))


static void IvyFifoRealloc(IvyFifoBuffer *f, unsigned int neededSize);

static unsigned int IvyFifoGenericRead(IvyFifoBuffer *f, const unsigned int buf_size, 
				       void (*func)(void*, void*, int), void* dest);

static void IvyFifoDrain(IvyFifoBuffer *f, int size);

/* static unsigned char IvyFifoPeek(IvyFifoBuffer *f, int offs); */



int IvyFifoInit(IvyFifoBuffer *f)
{
  f->wptr = f->rptr =
    f->buffer = malloc(IVY_FIFO_ALLOC_SIZE);
  f->end = f->buffer + IVY_FIFO_ALLOC_SIZE;
  f->full = 0;

  if (!f->buffer)
    return -1;
  return 0;
}

void IvyFifoFree (IvyFifoBuffer *f)
{
  free(f->buffer);
  f->wptr = f->rptr = f->buffer = NULL;
}


IvyFifoBuffer* IvyFifoNew (void)
{
  IvyFifoBuffer* ifb = malloc (sizeof (IvyFifoBuffer));
  IvyFifoInit (ifb);
  return (ifb);
}

void IvyFifoDelete (IvyFifoBuffer *f)
{
  IvyFifoFree (f);
  free (f);
}


unsigned int IvyFifoSize (const IvyFifoBuffer  *f)
{
  return (f->end - f->buffer);
}

unsigned int IvyFifoLength (const IvyFifoBuffer  *f)
{
  long size = f->wptr - f->rptr;
  if (size < 0)
    size += f->end - f->buffer;
  return size;
}

unsigned int IvyFifoAvail(const IvyFifoBuffer  *f)
{
  return (IvyFifoSize (f)- IvyFifoLength (f));
}

unsigned int IvyFifoRead (IvyFifoBuffer *f, char *buf, unsigned int buf_size)
{
  return IvyFifoGenericRead(f, buf_size, NULL, buf);
}

void IvyFifoRealloc (IvyFifoBuffer *f, unsigned int new_size) 
{
  unsigned int old_size= IvyFifoSize (f);
  unsigned int alignedNewSize = ((new_size/IVY_FIFO_ALLOC_SIZE) +1) * IVY_FIFO_ALLOC_SIZE;
  if (alignedNewSize > IVY_FIFO_MAX_ALLOC_SIZE) {
    f->full = 1;
  } else if (old_size < alignedNewSize) {
    int len= IvyFifoLength(f);
    IvyFifoBuffer f2;
    
    f2.wptr = f2.rptr =
      f2.buffer = malloc(alignedNewSize);
    f2.end = f2.buffer + alignedNewSize;
    f2.full = 0;
    
    IvyFifoRead(f, f2.buffer, len);
    f2.wptr += len;
    free(f->buffer);
    *f= f2;
  }
}

void IvyFifoWrite (IvyFifoBuffer *f, const char *buf, unsigned int size)
{
  if (size >= IvyFifoAvail (f)) {
    IvyFifoRealloc(f, size + IvyFifoLength (f));
  }
  if (f->full) {
    return ;
  }
  do {
    unsigned int len = MIN((unsigned int)(f->end - f->wptr), size);
    memcpy(f->wptr, buf, len);
    f->wptr += len;
    if (f->wptr >= f->end)
      f->wptr = f->buffer;
    buf += len;
    size -= len;
  } while (size > 0);
}


unsigned int IvyFifoGenericRead (IvyFifoBuffer *f, const unsigned int buf_size, void (*func)(void*, void*, int), void* dest)
{
  unsigned int bytesToRead, retV;
  retV = bytesToRead = MIN(buf_size, IvyFifoLength(f));
  
  do {
    unsigned int len = MIN((unsigned int)(f->end - f->rptr), bytesToRead);
    if (func) {
      func (dest, f->rptr, len);
    } else {
      memcpy(dest, f->rptr, len);
      dest = (unsigned char*)dest + len;
    }
    IvyFifoDrain(f, len);
    bytesToRead -= len;
  } while (bytesToRead > 0);
  return (retV);
}


unsigned int IvyFifoSendSocket (IvyFifoBuffer *f, const int fd)
{
  unsigned int  maxLen, realLen;
  
  do {
    maxLen = MIN ((unsigned int)(f->end - f->rptr), IvyFifoLength(f));
#ifdef WIN32
    realLen = send (fd, f->rptr, maxLen, 0);
#else
	realLen = send (fd, f->rptr, maxLen, MSG_DONTWAIT);
#endif
    IvyFifoDrain(f, realLen);
    //    printf ("DBG> maxLen=%d realLen=%d IvyFifoLength=%d\n",
    //    maxLen, realLen, IvyFifoLength(f));
  } while (IvyFifoLength(f) && (maxLen == realLen));

  return (IvyFifoLength(f));
}



void IvyFifoDrain (IvyFifoBuffer *f, int size)
{
  f->rptr += size;
  if (f->rptr >= f->end)
    f->rptr -= f->end - f->buffer;
  if (size > 0) {
    f->full = 0;
  }
}

int IvyFifoIsFull (const IvyFifoBuffer  *f) 
{
  return (f->full);
}

/* unsigned char IvyFifoPeek(IvyFifoBuffer *f, int offs) */
/* { */
/*     unsigned char *ptr = f->rptr + offs; */
/*     if (ptr >= f->end) */
/*         ptr -= f->end - f->buffer; */
/*     return *ptr; */
/* } */


//#define TEST_UNITAIRE 1
#ifdef TEST_UNITAIRE
int main (int argc, char**argv)
{
  if (1 == 2)  {
    IvyFifoBuffer ifb;
    unsigned char toRead [4096];
    unsigned char toWrite [8192];
    int (i);
    
    for (i=0; i< sizeof (toRead); i++) {
      toRead[i] = (char) (i%10)+48;
    }
    
    
    IvyFifoInit (&ifb);
    IvyFifoWrite(&ifb, toRead, 4096);
    printf ("DBG> fifo size=%u, length=%u\n", IvyFifoSize(&ifb), IvyFifoLength(&ifb));
    IvyFifoWrite(&ifb, toRead, 1024);
    printf ("DBG> fifo size=%u, length=%u\n", IvyFifoSize(&ifb), IvyFifoLength(&ifb));
    unsigned int nbRead=IvyFifoRead(&ifb, toWrite, sizeof (toWrite));
    //  int nbRead=IvyFifoRead(&ifb, toWrite, 1024);
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u\n", nbRead, IvyFifoSize(&ifb), IvyFifoLength(&ifb));
  }


  {
    IvyFifoBuffer ifb;
    unsigned char toRead [8];
    unsigned char toWrite [16];
    unsigned int nbRead;
    int (i);
    
    for (i=0; i< sizeof (toRead); i++) {
      toRead[i] = (char) (i%10)+48;
    }
    
    
    IvyFifoInit (&ifb);


    IvyFifoWrite(&ifb, toRead, 8);
    printf ("DBG> fifo size=%u, length=%u\n", IvyFifoSize(&ifb), IvyFifoLength(&ifb));

    nbRead=IvyFifoRead(&ifb, toWrite, 4);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

    IvyFifoWrite(&ifb, toRead, 8);
    printf ("DBG> fifo size=%u, length=%u\n", IvyFifoSize(&ifb), IvyFifoLength(&ifb));

    nbRead=IvyFifoRead(&ifb, toWrite, 8);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

   nbRead=IvyFifoRead(&ifb, toWrite, 8);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

   nbRead=IvyFifoRead(&ifb, toWrite, 8);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

   nbRead=IvyFifoRead(&ifb, toWrite, 4);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

    IvyFifoWrite(&ifb, toRead, 8);
    printf ("DBG> fifo size=%u, length=%u\n", IvyFifoSize(&ifb), IvyFifoLength(&ifb));

    nbRead=IvyFifoRead(&ifb, toWrite, 8);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

   nbRead=IvyFifoRead(&ifb, toWrite, 8);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

   nbRead=IvyFifoRead(&ifb, toWrite, 8);
    toWrite[nbRead] = 0;
    printf ("DBG> ndRead=%u, fifo size=%u, length=%u, buffer=%s\n", nbRead, IvyFifoSize(&ifb), 
	    IvyFifoLength(&ifb), toWrite);

  }


  return (0);
}
#endif // TEST_UNITAIRE