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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// NOTE: requires the client to define min and max on ints
typedef struct
{
char *data;
int size;
int avail;
int iptr;
int optr;
} Buffer;
Buffer *Buffer_new(int size)
{
Buffer *b= (Buffer *)malloc(sizeof(Buffer));
if (!b)
return 0;
if (!(b->data= (char *)malloc(size)))
{
free(b);
return 0;
}
b->size= size;
b->avail= 0;
b->iptr= 0;
b->optr= 0;
return b;
}
void Buffer_delete(Buffer *b)
{
assert(b && b->data);
free(b->data);
free(b);
}
inline int Buffer_avail(Buffer *b)
{
assert(!(b->avail & 3));
return b->avail;
}
inline int Buffer_free(Buffer *b)
{
return b->size - Buffer_avail(b);
}
inline void Buffer_getOutputPointers(Buffer *b, char **p1, int *n1, char **p2, int *n2)
{
int optr= b->optr;
int avail= Buffer_avail(b);
int headroom= b->size - optr;
if (avail == 0)
{
*p1= *p2= 0;
*n1= *n2= 0;
}
else if (avail <= headroom)
{
*p1= b->data + optr; *p2= 0;
*n1= avail; *n2= 0;
}
else
{
*p1= b->data + optr; *p2= b->data;
*n1= headroom; *n2= avail - headroom;
}
assert(!(*n1 & 3));
assert(!(*n2 & 3));
}
inline int Buffer_getOutputPointer(Buffer *b, char **ptr)
{
int optr= b->optr;
int avail= Buffer_avail(b);
int headroom= b->size - optr;
if (headroom < avail) avail= headroom;
assert((optr + avail) <= b->size);
*ptr= b->data + optr;
return avail;
}
inline int Buffer_getInputPointer(Buffer *b, char **ptr)
{
int iptr= b->iptr;
int free= Buffer_free(b);
int headroom= b->size - iptr;
if (headroom < free) free= headroom;
assert((iptr + free) <= b->size);
*ptr= b->data + iptr;
return free;
}
inline void Buffer_advanceOutputPointer(Buffer *b, int size)
{
int optr= b->optr;
int avail= b->avail;
assert(!(size & 3));
optr+= size;
avail-= size;
assert(optr <= b->size);
assert(avail >= 0);
if (optr == b->size) optr= 0;
b->optr= optr;
b->avail= avail;
}
inline void Buffer_advanceInputPointer(Buffer *b, int size)
{
int iptr= b->iptr;
assert(!(size & 3));
{
int free= Buffer_free(b);
free-= size;
assert(free >= 0);
}
iptr += size;
assert(iptr <= b->size);
if (iptr == b->size) iptr= 0;
b->iptr= iptr;
b->avail += size;
}
inline void Buffer_prefill(Buffer *b, int bytes)
{
char *ptr;
int size= Buffer_getInputPointer(b, &ptr);
assert(!(bytes & 3));
assert(bytes <= size);
memset(ptr, 0, size);
Buffer_advanceInputPointer(b, bytes);
}
inline int Buffer_write(Buffer *b, char *buf, int nbytes)
{
int iptr= b->iptr;
int bytesToCopy= min(nbytes, Buffer_free(b));
int headroom= b->size - iptr;
int bytesCopied= 0;
assert(!(nbytes & 3));
if (bytesToCopy >= headroom)
{
memcpy(b->data + iptr, buf, headroom);
iptr= 0;
bytesToCopy -= headroom;
bytesCopied += headroom;
if (bytesToCopy)
{
memcpy(b->data, buf + bytesCopied, bytesToCopy);
iptr= bytesToCopy;
bytesCopied += bytesToCopy;
}
}
else
{
memcpy(b->data + iptr, buf, bytesToCopy);
iptr += bytesToCopy;
bytesCopied= bytesToCopy;
}
b->iptr= iptr;
b->avail += bytesCopied;
return bytesCopied;
}
inline int Buffer_read(Buffer *b, char *buf, int nbytes)
{
int optr= b->optr;
int bytesToCopy= min(nbytes, Buffer_avail(b));
int headroom= b->size - optr;
int bytesCopied= 0;
assert(!(nbytes & 3));
if (bytesToCopy >= headroom)
{
memcpy(buf, b->data + optr, headroom);
optr= 0;
bytesToCopy -= headroom;
bytesCopied += headroom;
if (bytesToCopy)
{
memcpy(buf + bytesCopied, b->data, bytesToCopy);
optr= bytesToCopy;
bytesCopied += bytesToCopy;
}
}
else
{
memcpy(buf, b->data + optr, bytesToCopy);
optr += bytesToCopy;
bytesCopied= bytesToCopy;
}
b->optr= optr;
b->avail -= bytesCopied;
return bytesCopied;
}
|