|
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; |
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 { |
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 } |