|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Expanding text buffer |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 #include <malloc.h> |
|
23 #include "buffer.h" |
|
24 #include <string.h> |
|
25 |
|
26 /* efficient allocation unit: */ |
|
27 #define ALLOCSIZE 4096 |
|
28 #define INITIALBLOCKCOUNT 128 |
|
29 |
|
30 byteblock *buffer_newblock(buffer *b, unsigned int size) |
|
31 { |
|
32 byteblock *bb; |
|
33 if (!b) |
|
34 return NULL; |
|
35 |
|
36 b->lastblock++; |
|
37 |
|
38 if (b->lastblock == b->maxblocks) |
|
39 { |
|
40 byteblock **nbb = (byteblock **)realloc(b->blocks, sizeof(byteblock *) * (b->maxblocks + INITIALBLOCKCOUNT)); |
|
41 if (!nbb) |
|
42 return NULL; |
|
43 |
|
44 b->blocks = nbb; |
|
45 b->maxblocks += INITIALBLOCKCOUNT; |
|
46 } |
|
47 |
|
48 bb = malloc(sizeof(byteblock) + size-1); |
|
49 |
|
50 if (!bb) |
|
51 { |
|
52 |
|
53 return NULL; |
|
54 } |
|
55 |
|
56 b->blocks[b->lastblock] = bb; |
|
57 |
|
58 bb->fill = 0; |
|
59 bb->size = size; |
|
60 |
|
61 return bb; |
|
62 } |
|
63 |
|
64 buffer *buffer_new(void) |
|
65 { |
|
66 buffer *b = malloc(sizeof(buffer)); |
|
67 |
|
68 if (b) |
|
69 { |
|
70 b->lastblock = -1; /* no blocks as yet */ |
|
71 b->maxblocks = INITIALBLOCKCOUNT; |
|
72 b->blocks = (byteblock **)malloc(sizeof(byteblock *) * b->maxblocks); |
|
73 if (!b->blocks) |
|
74 { |
|
75 free(b); |
|
76 return NULL; |
|
77 } |
|
78 |
|
79 buffer_newblock(b, ALLOCSIZE); |
|
80 } |
|
81 |
|
82 return b; |
|
83 } |
|
84 |
|
85 |
|
86 char *buffer_append(buffer *b, char *bytes, unsigned int size) |
|
87 { |
|
88 if (!b || !bytes) |
|
89 return NULL; |
|
90 |
|
91 char *space = buffer_makespace(b, size); |
|
92 if (!space) |
|
93 return NULL; |
|
94 memcpy(space, bytes, size); |
|
95 buffer_usespace(b, size); |
|
96 |
|
97 return space; |
|
98 } |
|
99 |
|
100 char *buffer_prepend(buffer *b, char *bytes, unsigned int size) |
|
101 { |
|
102 byteblock *bb; |
|
103 |
|
104 if (!b || !bytes) |
|
105 return NULL; |
|
106 |
|
107 bb = buffer_newblock(b, size); |
|
108 /* cheat by moving the new block from the end to the start. */ |
|
109 |
|
110 bb = b->blocks[b->lastblock]; |
|
111 if (b->lastblock != 0) |
|
112 { |
|
113 memmove(b->blocks+1, b->blocks, sizeof(byteblock *) * b->lastblock ); |
|
114 b->blocks[0] = bb; |
|
115 } |
|
116 |
|
117 memcpy(&(b->blocks[0]->byte0), bytes, size); |
|
118 |
|
119 b->blocks[0]->fill = size; |
|
120 |
|
121 return &(b->blocks[0]->byte0); |
|
122 } |
|
123 |
|
124 /* Allocate memory at the end of the buffer (if there isn't |
|
125 * enough already) so that the user can append at least that |
|
126 * many bytes without overrunning the buffer. This is useful |
|
127 * where one may not know in advance how many bytes are to be |
|
128 * added (e.g. reading from a socket) but one does know the |
|
129 * upper limit. |
|
130 */ |
|
131 char *buffer_makespace(buffer *b, unsigned int size) |
|
132 { |
|
133 byteblock *bb; |
|
134 byteblock *last; |
|
135 if (!b) |
|
136 return NULL; |
|
137 |
|
138 last = b->blocks[b->lastblock]; |
|
139 |
|
140 if (last->size - last->fill > size) |
|
141 return (&last->byte0 + last->fill); |
|
142 |
|
143 if (size > ALLOCSIZE) |
|
144 { |
|
145 bb = buffer_newblock(b, size); |
|
146 } else { |
|
147 bb = buffer_newblock(b, ALLOCSIZE); |
|
148 } |
|
149 |
|
150 if (!bb) |
|
151 return NULL; |
|
152 |
|
153 return &bb->byte0; |
|
154 } |
|
155 |
|
156 void buffer_usespace(buffer *b, unsigned int nbytes) |
|
157 { |
|
158 byteblock *last; |
|
159 |
|
160 if (!b) |
|
161 return; |
|
162 |
|
163 last = b->blocks[b->lastblock]; |
|
164 |
|
165 if (last->fill + nbytes < last->size) |
|
166 last->fill += nbytes; |
|
167 else |
|
168 last->fill = last->size; /* really an error - no exceptions though. */ |
|
169 } |
|
170 |
|
171 byteblock *buffer_getbytes(buffer *b, int *iterator) |
|
172 { |
|
173 if (!b) |
|
174 return NULL; |
|
175 |
|
176 if (*iterator > b->lastblock) |
|
177 return NULL; |
|
178 |
|
179 return b->blocks[(*iterator)++]; |
|
180 } |
|
181 |
|
182 void buffer_free(buffer **b) |
|
183 { |
|
184 int i; |
|
185 buffer *bf; |
|
186 |
|
187 if (!b || !*b) |
|
188 return; |
|
189 |
|
190 bf=*b; |
|
191 |
|
192 if (bf->blocks) |
|
193 { |
|
194 for (i=0; i <= bf->lastblock; i++) |
|
195 { |
|
196 if (bf->blocks[i]) |
|
197 free(bf->blocks[i]); |
|
198 } |
|
199 free(bf->blocks); |
|
200 } |
|
201 free(bf); |
|
202 |
|
203 *b = NULL; |
|
204 } |
|
205 |