ucx/buffer.c

changeset 5
88625853ae74
parent 1
1bcaac272cdf
child 17
11dffb40cd91
equal deleted inserted replaced
4:ae5a98f0545c 5:88625853ae74
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
1 #include "buffer.h" 29 #include "buffer.h"
2 #include <stdarg.h> 30 #include <stdarg.h>
3 #include <stdlib.h> 31 #include <stdlib.h>
4 #include <string.h> 32 #include <string.h>
5 33
6 UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags) { 34 UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags) {
7 UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); 35 UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer));
8 if (buffer) { 36 if (buffer) {
9 buffer->flags = flags; 37 buffer->flags = flags;
10 if (!space) { 38 if (!space) {
11 buffer->space = malloc(size); 39 buffer->space = (char*)malloc(size);
12 if (!buffer->space) { 40 if (!buffer->space) {
13 free(buffer); 41 free(buffer);
14 return NULL; 42 return NULL;
15 } 43 }
16 memset(buffer->space, 0, size); 44 memset(buffer->space, 0, size);
17 buffer->flags |= UCX_BUFFER_AUTOFREE; 45 buffer->flags |= UCX_BUFFER_AUTOFREE;
18 } else { 46 } else {
19 buffer->space = space; 47 buffer->space = (char*)space;
20 } 48 }
21 buffer->capacity = size; 49 buffer->capacity = size;
22 buffer->size = 0; 50 buffer->size = 0;
23 51
24 buffer->pos = 0; 52 buffer->pos = 0;
46 return NULL; 74 return NULL;
47 } 75 }
48 76
49 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); 77 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer));
50 if (dst) { 78 if (dst) {
51 dst->space = malloc(length); 79 dst->space = (char*)malloc(length);
52 if (!dst->space) { 80 if (!dst->space) {
53 free(dst); 81 free(dst);
54 return NULL; 82 return NULL;
55 } 83 }
56 dst->capacity = length; 84 dst->capacity = length;
61 } 89 }
62 return dst; 90 return dst;
63 } 91 }
64 92
65 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { 93 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) {
66 off_t npos; 94 size_t npos = 0;
67 switch (whence) { 95 switch (whence) {
68 case SEEK_SET: 96 case SEEK_SET:
69 npos = 0; 97 npos = 0;
70 break; 98 break;
71 case SEEK_CUR: 99 case SEEK_CUR:
76 break; 104 break;
77 } 105 }
78 106
79 npos += offset; 107 npos += offset;
80 108
81 if (npos < 0 || npos > buffer->size) { 109 if (npos > buffer->size) {
82 return -1; 110 return -1;
83 } else { 111 } else {
84 buffer->pos = npos; 112 buffer->pos = npos;
85 return 0; 113 return 0;
86 } 114 }
93 121
94 int ucx_buffer_extend(UcxBuffer *buffer, size_t len) { 122 int ucx_buffer_extend(UcxBuffer *buffer, size_t len) {
95 size_t newcap = buffer->capacity; 123 size_t newcap = buffer->capacity;
96 while (buffer->pos + len > newcap) newcap <<= 1; 124 while (buffer->pos + len > newcap) newcap <<= 1;
97 125
98 char *newspace = realloc(buffer->space, newcap); 126 char *newspace = (char*)realloc(buffer->space, newcap);
99 if (newspace) { 127 if (newspace) {
100 memset(newspace+buffer->size, 0, newcap-buffer->size); 128 memset(newspace+buffer->size, 0, newcap-buffer->size);
101 buffer->space = newspace; 129 buffer->space = newspace;
102 buffer->capacity = newcap; 130 buffer->capacity = newcap;
103 } else { 131 } else {
183 } 211 }
184 212
185 size_t ucx_buffer_generic_copy(void *s1, void *s2, 213 size_t ucx_buffer_generic_copy(void *s1, void *s2,
186 read_func readfnc, write_func writefnc, size_t bufsize) { 214 read_func readfnc, write_func writefnc, size_t bufsize) {
187 size_t ncp = 0; 215 size_t ncp = 0;
188 char *buf = malloc(bufsize); 216 char *buf = (char*)malloc(bufsize);
189 if(buf == NULL) { 217 if(buf == NULL) {
190 return 0; 218 return 0;
191 } 219 }
192 220
193 size_t r; 221 size_t r;
200 } 228 }
201 229
202 free(buf); 230 free(buf);
203 return ncp; 231 return ncp;
204 } 232 }
233
234 size_t ucx_buffer_generic_ncopy(void *s1, void *s2,
235 read_func readfnc, write_func writefnc, size_t bufsize, size_t n) {
236 if(n == 0) {
237 return 0;
238 }
239
240 size_t ncp = 0;
241 char *buf = (char*)malloc(bufsize);
242 if(buf == NULL) {
243 return 0;
244 }
245
246 size_t r;
247 size_t rn = bufsize > n ? n : bufsize;
248 while((r = readfnc(buf, 1, rn, s1)) != 0) {
249 r = writefnc(buf, 1, r, s2);
250 ncp += r;
251 n -= r;
252 rn = bufsize > n ? n : bufsize;
253 if(r == 0 || n == 0) {
254 break;
255 }
256 }
257
258 free(buf);
259 return ncp;
260 }

mercurial