initial load of upstream version 1.06.32
[xmlrpc-c] / lib / libutil / memblock.c
1 /* Copyright information is at end of file */
2 #include "xmlrpc_config.h"
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8
9 #include "xmlrpc-c/util_int.h"
10 #include "xmlrpc-c/util.h"
11
12 #ifdef EFENCE
13         /* when looking for corruption don't allocate extra slop */
14 #define BLOCK_ALLOC_MIN (1)
15 #else
16 #define BLOCK_ALLOC_MIN (16)
17 #endif
18 #define BLOCK_ALLOC_MAX (128 * 1024 * 1024)
19
20
21 xmlrpc_mem_block * 
22 xmlrpc_mem_block_new(xmlrpc_env * const env, 
23                      size_t       const size) {
24
25     xmlrpc_mem_block* block;
26
27     XMLRPC_ASSERT_ENV_OK(env);
28
29     block = (xmlrpc_mem_block*) malloc(sizeof(xmlrpc_mem_block));
30     XMLRPC_FAIL_IF_NULL(block, env, XMLRPC_INTERNAL_ERROR,
31                         "Can't allocate memory block");
32
33     xmlrpc_mem_block_init(env, block, size);
34     XMLRPC_FAIL_IF_FAULT(env);
35
36                      cleanup:
37     if (env->fault_occurred) {
38         if (block)
39             free(block);
40         return NULL;
41     } else {
42         return block;
43     }
44 }
45
46 /* Destroy an existing xmlrpc_mem_block, and everything it contains. */
47 void
48 xmlrpc_mem_block_free(xmlrpc_mem_block * const blockP) {
49
50     XMLRPC_ASSERT(blockP != NULL);
51     XMLRPC_ASSERT(blockP->_block != NULL);
52
53     xmlrpc_mem_block_clean(blockP);
54     free(blockP);
55 }
56
57
58
59 /* Initialize the contents of the provided xmlrpc_mem_block. */
60 void
61 xmlrpc_mem_block_init(xmlrpc_env *       const envP,
62                       xmlrpc_mem_block * const blockP,
63                       size_t             const size) {
64
65     XMLRPC_ASSERT_ENV_OK(envP);
66     XMLRPC_ASSERT(blockP != NULL);
67
68     blockP->_size = size;
69     if (size < BLOCK_ALLOC_MIN)
70         blockP->_allocated = BLOCK_ALLOC_MIN;
71     else
72         blockP->_allocated = size;
73
74     blockP->_block = (void*) malloc(blockP->_allocated);
75     if (!blockP->_block)
76         xmlrpc_faultf(envP, "Can't allocate %u-byte memory block",
77                       blockP->_allocated);
78 }
79
80
81
82 /* Deallocate the contents of the provided xmlrpc_mem_block, but not
83    the block itself.
84 */
85 void
86 xmlrpc_mem_block_clean(xmlrpc_mem_block * const blockP) {
87
88     XMLRPC_ASSERT(blockP != NULL);
89     XMLRPC_ASSERT(blockP->_block != NULL);
90
91     free(blockP->_block);
92     blockP->_block = XMLRPC_BAD_POINTER;
93 }
94
95
96
97 /* Get the size of the xmlrpc_mem_block. */
98 size_t 
99 xmlrpc_mem_block_size(const xmlrpc_mem_block * const blockP) {
100
101     XMLRPC_ASSERT(blockP != NULL);
102     return blockP->_size;
103 }
104
105
106
107 /* Get the contents of the xmlrpc_mem_block. */
108 void * 
109 xmlrpc_mem_block_contents(const xmlrpc_mem_block * const blockP) {
110
111     XMLRPC_ASSERT(blockP != NULL);
112     return blockP->_block;
113 }
114
115
116
117 /* Resize an xmlrpc_mem_block, preserving as much of the contents as
118    possible.
119 */
120 void 
121 xmlrpc_mem_block_resize (xmlrpc_env *       const envP,
122                          xmlrpc_mem_block * const blockP,
123                          size_t             const size) {
124
125     size_t proposed_alloc;
126     void* new_block;
127
128     XMLRPC_ASSERT_ENV_OK(envP);
129     XMLRPC_ASSERT(blockP != NULL);
130
131     /* Check to see if we already have enough space. Maybe we'll get lucky. */
132     if (size <= blockP->_allocated) {
133         blockP->_size = size;
134         return;
135     }
136
137     /* Calculate a new allocation size. */
138 #ifdef EFENCE
139     proposed_alloc = size;
140 #else
141     proposed_alloc = blockP->_allocated;
142     while (proposed_alloc < size && proposed_alloc <= BLOCK_ALLOC_MAX)
143         proposed_alloc *= 2;
144 #endif /* DEBUG_MEM_ERRORS */
145
146     if (proposed_alloc > BLOCK_ALLOC_MAX)
147         XMLRPC_FAIL(envP, XMLRPC_INTERNAL_ERROR, "Memory block too large");
148
149     /* Allocate our new memory block. */
150     new_block = (void*) malloc(proposed_alloc);
151     XMLRPC_FAIL_IF_NULL(new_block, envP, XMLRPC_INTERNAL_ERROR,
152                         "Can't resize memory block");
153
154     /* Copy over our data and update the xmlrpc_mem_block struct. */
155     memcpy(new_block, blockP->_block, blockP->_size);
156     free(blockP->_block);
157     blockP->_block     = new_block;
158     blockP->_size      = size;
159     blockP->_allocated = proposed_alloc;
160
161  cleanup:
162     return;
163 }
164
165
166
167 void 
168 xmlrpc_mem_block_append(xmlrpc_env *       const envP,
169                         xmlrpc_mem_block * const blockP,
170                         const void *       const data, 
171                         size_t             const len) {
172
173     int size;
174
175     XMLRPC_ASSERT_ENV_OK(envP);
176     XMLRPC_ASSERT(blockP != NULL);
177
178     size = blockP->_size;
179     xmlrpc_mem_block_resize(envP, blockP, size + len);
180     XMLRPC_FAIL_IF_FAULT(envP);
181
182     memcpy(((unsigned char*) blockP->_block) + size, data, len);
183
184  cleanup:
185     return;
186 }
187
188
189
190 /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
191 **
192 ** Redistribution and use in source and binary forms, with or without
193 ** modification, are permitted provided that the following conditions
194 ** are met:
195 ** 1. Redistributions of source code must retain the above copyright
196 **    notice, this list of conditions and the following disclaimer.
197 ** 2. Redistributions in binary form must reproduce the above copyright
198 **    notice, this list of conditions and the following disclaimer in the
199 **    documentation and/or other materials provided with the distribution.
200 ** 3. The name of the author may not be used to endorse or promote products
201 **    derived from this software without specific prior written permission. 
202 **  
203 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
204 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
207 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
209 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
211 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
212 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
213 ** SUCH DAMAGE.
214 */