| 1 |
|
| /*################################################################################### |
| 2 |
|
| # |
| 3 |
|
| # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de |
| 4 |
|
| # Embperl - Copyright (c) 2008-2015 Gerald Richter |
| 5 |
|
| # Embperl - Copyright (c) 2015-2023 actevy.io |
| 6 |
|
| # |
| 7 |
|
| # You may distribute under the terms of either the GNU General Public |
| 8 |
|
| # License or the Artistic License, as specified in the Perl README file. |
| 9 |
|
| # For use with Apache httpd and mod_perl, see also Apache copyright. |
| 10 |
|
| # |
| 11 |
|
| # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 12 |
|
| # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 13 |
|
| # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
|
| # |
| 15 |
|
| ###################################################################################*/ |
| 16 |
|
| |
| 17 |
|
| /* parts of this file are taken from the Apache sources, so we need another copyright ... */ |
| 18 |
|
| |
| 19 |
|
| /* ==================================================================== |
| 20 |
|
| * The Apache Software License, Version 1.1 |
| 21 |
|
| * |
| 22 |
|
| * Copyright (c) 2000 The Apache Software Foundation. All rights |
| 23 |
|
| * reserved. |
| 24 |
|
| * |
| 25 |
|
| * Redistribution and use in source and binary forms, with or without |
| 26 |
|
| * modification, are permitted provided that the following conditions |
| 27 |
|
| * are met: |
| 28 |
|
| * |
| 29 |
|
| * 1. Redistributions of source code must retain the above copyright |
| 30 |
|
| * notice, this list of conditions and the following disclaimer. |
| 31 |
|
| * |
| 32 |
|
| * 2. Redistributions in binary form must reproduce the above copyright |
| 33 |
|
| * notice, this list of conditions and the following disclaimer in |
| 34 |
|
| * the documentation and/or other materials provided with the |
| 35 |
|
| * distribution. |
| 36 |
|
| * |
| 37 |
|
| * 3. The end-user documentation included with the redistribution, |
| 38 |
|
| * if any, must include the following acknowledgment: |
| 39 |
|
| * "This product includes software developed by the |
| 40 |
|
| * Apache Software Foundation (http:/ /www.apache.org/)." |
| 41 |
|
| * Alternately, this acknowledgment may appear in the software itself, |
| 42 |
|
| * if and wherever such third-party acknowledgments normally appear. |
| 43 |
|
| * |
| 44 |
|
| * 4. The names "Apache" and "Apache Software Foundation" must |
| 45 |
|
| * not be used to endorse or promote products derived from this |
| 46 |
|
| * software without prior written permission. For written |
| 47 |
|
| * permission, please contact apache@apache.org. |
| 48 |
|
| * |
| 49 |
|
| * 5. Products derived from this software may not be called "Apache", |
| 50 |
|
| * nor may "Apache" appear in their name, without prior written |
| 51 |
|
| * permission of the Apache Software Foundation. |
| 52 |
|
| * |
| 53 |
|
| * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 54 |
|
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 55 |
|
| * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 56 |
|
| * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 57 |
|
| * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 58 |
|
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 59 |
|
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 60 |
|
| * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 61 |
|
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 62 |
|
| * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 63 |
|
| * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 64 |
|
| * SUCH DAMAGE. |
| 65 |
|
| * ==================================================================== |
| 66 |
|
| * |
| 67 |
|
| * This software consists of voluntary contributions made by many |
| 68 |
|
| * individuals on behalf of the Apache Software Foundation. For more |
| 69 |
|
| * information on the Apache Software Foundation, please see |
| 70 |
|
| * <http:/ /www.apache.org/>. |
| 71 |
|
| * |
| 72 |
|
| * Portions of this software are based upon public domain software |
| 73 |
|
| * originally written at the National Center for Supercomputing Applications, |
| 74 |
|
| * University of Illinois, Urbana-Champaign. |
| 75 |
|
| */ |
| 76 |
|
| |
| 77 |
|
| /* |
| 78 |
|
| * Resource allocation code... the code here is responsible for making |
| 79 |
|
| * sure that nothing leaks. |
| 80 |
|
| * |
| 81 |
|
| * rst --- 4/95 --- 6/95 |
| 82 |
|
| */ |
| 83 |
|
| |
| 84 |
|
| #include "ep.h" |
| 85 |
|
| |
| 86 |
|
| |
| 87 |
|
| #ifdef OS2 |
| 88 |
|
| #define INCL_DOS |
| 89 |
|
| #include <os2.h> |
| 90 |
|
| #endif |
| 91 |
|
| |
| 92 |
|
| #ifndef EP_API_EXPORT |
| 93 |
|
| #define EP_API_EXPORT(x) x |
| 94 |
|
| #endif |
| 95 |
|
| #ifndef EP_API_EXPORT_NONSTD |
| 96 |
|
| #define EP_API_EXPORT_NONSTD(x) x |
| 97 |
|
| #endif |
| 98 |
|
| |
| 99 |
|
| #ifndef ep_inline |
| 100 |
|
| #define ep_inline |
| 101 |
|
| #endif |
| 102 |
|
| |
| 103 |
|
| #define ep_block_alarms() |
| 104 |
|
| #define ep_unblock_alarms() |
| 105 |
|
| |
| 106 |
|
| |
| 107 |
|
| #ifndef BLOCK_MINFREE |
| 108 |
|
| #define BLOCK_MINFREE 4096 |
| 109 |
|
| #endif |
| 110 |
|
| #ifndef BLOCK_MINALLOC |
| 111 |
|
| #define BLOCK_MINALLOC 8192 |
| 112 |
|
| #endif |
| 113 |
|
| |
| 114 |
|
| |
| 115 |
|
| /* --- don't use Perl's memory management and io layer here --- */ |
| 116 |
|
| |
| 117 |
|
| #ifndef DMALLOC |
| 118 |
|
| #undef malloc |
| 119 |
|
| #undef free |
| 120 |
|
| #undef fprintf |
| 121 |
|
| #undef exit |
| 122 |
|
| #endif |
| 123 |
|
| |
| 124 |
|
| /* debugging support, define this to enable code which helps detect re-use |
| 125 |
|
| * of freed memory and other such nonsense. |
| 126 |
|
| * |
| 127 |
|
| * The theory is simple. The FILL_BYTE (0xa5) is written over all malloc'd |
| 128 |
|
| * memory as we receive it, and is written over everything that we free up |
| 129 |
|
| * during a clear_pool. We check that blocks on the free list always |
| 130 |
|
| * have the FILL_BYTE in them, and we check during palloc() that the bytes |
| 131 |
|
| * still have FILL_BYTE in them. If you ever see garbage URLs or whatnot |
| 132 |
|
| * containing lots of 0xa5s then you know something used data that's been |
| 133 |
|
| * freed or uninitialized. |
| 134 |
|
| */ |
| 135 |
|
| /* #define ALLOC_DEBUG */ |
| 136 |
|
| |
| 137 |
|
| /* debugging support, if defined all allocations will be done with |
| 138 |
|
| * malloc and free()d appropriately at the end. This is intended to be |
| 139 |
|
| * used with something like Electric Fence or Purify to help detect |
| 140 |
|
| * memory problems. Note that if you're using efence then you should also |
| 141 |
|
| * add in ALLOC_DEBUG. But don't add in ALLOC_DEBUG if you're using Purify |
| 142 |
|
| * because ALLOC_DEBUG would hide all the uninitialized read errors that |
| 143 |
|
| * Purify can diagnose. |
| 144 |
|
| */ |
| 145 |
|
| /* #define ALLOC_USE_MALLOC */ |
| 146 |
|
| |
| 147 |
|
| /* tMemPool debugging support. This is intended to detect cases where the |
| 148 |
|
| * wrong tMemPool is used when assigning data to an object in another pool. |
| 149 |
|
| * In particular, it causes the table_{set,add,merge}n routines to check |
| 150 |
|
| * that their arguments are safe for the table they're being placed in. |
| 151 |
|
| * It currently only works with the unix multiprocess model, but could |
| 152 |
|
| * be extended to others. |
| 153 |
|
| */ |
| 154 |
|
| /* #define POOL_DEBUG */ |
| 155 |
|
| |
| 156 |
|
| /* Provide diagnostic information about make_table() calls which are |
| 157 |
|
| * possibly too small. This requires a recent gcc which supports |
| 158 |
|
| * __builtin_return_address(). The error_log output will be a |
| 159 |
|
| * message such as: |
| 160 |
|
| * table_push: table created by 0x804d874 hit limit of 10 |
| 161 |
|
| * Use "l *0x804d874" to find the source that corresponds to. It |
| 162 |
|
| * indicates that a table allocated by a call at that address has |
| 163 |
|
| * possibly too small an initial table size guess. |
| 164 |
|
| */ |
| 165 |
|
| /* #define MAKE_TABLE_PROFILE */ |
| 166 |
|
| |
| 167 |
|
| /* Provide some statistics on the cost of allocations. It requires a |
| 168 |
|
| * bit of an understanding of how alloc.c works. |
| 169 |
|
| */ |
| 170 |
|
| /* #define ALLOC_STATS */ |
| 171 |
|
| |
| 172 |
|
| #ifdef POOL_DEBUG |
| 173 |
|
| #ifdef ALLOC_USE_MALLOC |
| 174 |
|
| # error "sorry, no support for ALLOC_USE_MALLOC and POOL_DEBUG at the same time" |
| 175 |
|
| #endif |
| 176 |
|
| #ifdef MULTITHREAD |
| 177 |
|
| # error "sorry, no support for MULTITHREAD and POOL_DEBUG at the same time" |
| 178 |
|
| #endif |
| 179 |
|
| #endif |
| 180 |
|
| |
| 181 |
|
| #ifdef ALLOC_USE_MALLOC |
| 182 |
|
| #undef BLOCK_MINFREE |
| 183 |
|
| #undef BLOCK_MINALLOC |
| 184 |
|
| #define BLOCK_MINFREE 0 |
| 185 |
|
| #define BLOCK_MINALLOC 0 |
| 186 |
|
| #endif |
| 187 |
|
| |
| 188 |
|
| /***************************************************************** |
| 189 |
|
| * |
| 190 |
|
| * Managing free storage blocks... |
| 191 |
|
| */ |
| 192 |
|
| |
| 193 |
|
| union align { |
| 194 |
|
| /* Types which are likely to have the longest RELEVANT alignment |
| 195 |
|
| * restrictions... |
| 196 |
|
| */ |
| 197 |
|
| |
| 198 |
|
| char *cp; |
| 199 |
|
| void (*f) (void); |
| 200 |
|
| long l; |
| 201 |
|
| FILE *fp; |
| 202 |
|
| double d; |
| 203 |
|
| }; |
| 204 |
|
| |
| 205 |
|
| #define CLICK_SZ (sizeof(union align)) |
| 206 |
|
| |
| 207 |
|
| union block_hdr { |
| 208 |
|
| union align a; |
| 209 |
|
| |
| 210 |
|
| /* Actual header... */ |
| 211 |
|
| |
| 212 |
|
| struct { |
| 213 |
|
| char *endp; |
| 214 |
|
| union block_hdr *next; |
| 215 |
|
| char *first_avail; |
| 216 |
|
| #ifdef POOL_DEBUG |
| 217 |
|
| union block_hdr *global_next; |
| 218 |
|
| struct tMemPool *owning_pool; |
| 219 |
|
| #endif |
| 220 |
|
| } h; |
| 221 |
|
| }; |
| 222 |
|
| |
| 223 |
|
| static union block_hdr *block_freelist = NULL; |
| 224 |
|
| |
| 225 |
|
| static perl_mutex alloc_mutex ; |
| 226 |
|
| static perl_mutex spawn_mutex ; |
| 227 |
|
| #ifdef POOL_DEBUG |
| 228 |
|
| static char *known_stack_point; |
| 229 |
|
| static int stack_direction; |
| 230 |
|
| static union block_hdr *global_block_list; |
| 231 |
|
| #define FREE_POOL ((struct tMemPool *)(-1)) |
| 232 |
|
| #endif |
| 233 |
|
| #ifdef ALLOC_STATS |
| 234 |
|
| static unsigned long long num_free_blocks_calls; |
| 235 |
|
| static unsigned long long num_blocks_freed; |
| 236 |
|
| static unsigned max_blocks_in_one_free; |
| 237 |
|
| static unsigned num_malloc_calls; |
| 238 |
|
| static unsigned num_malloc_bytes; |
| 239 |
|
| #endif |
| 240 |
|
| |
| 241 |
|
| #ifdef ALLOC_DEBUG |
| 242 |
|
| #define FILL_BYTE ((char)(0xa5)) |
| 243 |
|
| |
| 244 |
|
| #define debug_fill(ptr,size) ((void)memset((ptr), FILL_BYTE, (size))) |
| 245 |
|
| |
| 246 |
|
| static ep_inline void debug_verify_filled(const char *ptr, |
| 247 |
|
| const char *endp, const char *error_msg) |
| 248 |
|
| { |
| 249 |
|
| for (; ptr < endp; ++ptr) { |
| 250 |
|
| if (*ptr != FILL_BYTE) { |
| 251 |
|
| fputs(error_msg, stderr); |
| 252 |
|
| abort(); |
| 253 |
|
| exit(1); |
| 254 |
|
| } |
| 255 |
|
| } |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
|
| #else |
| 259 |
|
| #define debug_fill(a,b) |
| 260 |
|
| #define debug_verify_filled(a,b,c) |
| 261 |
|
| #endif |
| 262 |
|
| |
| 263 |
|
| |
| 264 |
|
| /* Get a completely new block from the system pool. Note that we rely on |
| 265 |
|
| malloc() to provide aligned memory. */ |
| 266 |
|
| |
| 267 |
0 |
| static union block_hdr *malloc_block(int size) |
| 268 |
|
| { |
| 269 |
|
| union block_hdr *blok; |
| 270 |
|
| |
| 271 |
|
| #ifdef ALLOC_DEBUG |
| 272 |
|
| /* make some room at the end which we'll fill and expect to be |
| 273 |
|
| * always filled |
| 274 |
|
| */ |
| 275 |
|
| size += CLICK_SZ; |
| 276 |
|
| #endif |
| 277 |
|
| #ifdef ALLOC_STATS |
| 278 |
|
| ++num_malloc_calls; |
| 279 |
|
| num_malloc_bytes += size + sizeof(union block_hdr); |
| 280 |
|
| #endif |
| 281 |
0 |
| blok = (union block_hdr *) malloc(size + sizeof(union block_hdr)); |
| 282 |
0 |
▶ | if (blok == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (blok == NULL) { |
0 |
0 |
0 |
|
| 283 |
|
| /*fprintf(stderr, "Ouch! malloc failed in malloc_block()\n");*/ |
| 284 |
|
| /* mmmh, Perl overrides stderr, so it won't work here!!! bad... */ |
| 285 |
0 |
| printf("Ouch! malloc failed in malloc_block()\n"); |
| 286 |
0 |
| exit(1); |
| 287 |
|
| } |
| 288 |
|
| debug_fill(blok, size + sizeof(union block_hdr)); |
| 289 |
0 |
| blok->h.next = NULL; |
| 290 |
0 |
| blok->h.first_avail = (char *) (blok + 1); |
| 291 |
0 |
| blok->h.endp = size + blok->h.first_avail; |
| 292 |
|
| #ifdef ALLOC_DEBUG |
| 293 |
|
| blok->h.endp -= CLICK_SZ; |
| 294 |
|
| #endif |
| 295 |
|
| #ifdef POOL_DEBUG |
| 296 |
|
| blok->h.global_next = global_block_list; |
| 297 |
|
| global_block_list = blok; |
| 298 |
|
| blok->h.owning_pool = NULL; |
| 299 |
|
| #endif |
| 300 |
|
| |
| 301 |
0 |
| return blok; |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
|
| |
| 305 |
|
| |
| 306 |
|
| #if defined(ALLOC_DEBUG) && !defined(ALLOC_USE_MALLOC) |
| 307 |
|
| static void chk_on_blk_list(union block_hdr *blok, union block_hdr *free_blk) |
| 308 |
|
| { |
| 309 |
|
| debug_verify_filled(blok->h.endp, blok->h.endp + CLICK_SZ, |
| 310 |
|
| "Ouch! Someone trounced the padding at the end of a block!\n"); |
| 311 |
|
| while (free_blk) { |
| 312 |
|
| if (free_blk == blok) { |
| 313 |
|
| fprintf(stderr, "Ouch! Freeing free block\n"); |
| 314 |
|
| abort(); |
| 315 |
|
| exit(1); |
| 316 |
|
| } |
| 317 |
|
| free_blk = free_blk->h.next; |
| 318 |
|
| } |
| 319 |
|
| } |
| 320 |
|
| #else |
| 321 |
|
| #define chk_on_blk_list(_x, _y) |
| 322 |
|
| #endif |
| 323 |
|
| |
| 324 |
|
| /* Free a chain of blocks --- must be called with alarms blocked. */ |
| 325 |
|
| |
| 326 |
0 |
| static void free_blocks(union block_hdr *blok) |
| 327 |
|
| { |
| 328 |
|
| #ifdef ALLOC_USE_MALLOC |
| 329 |
|
| union block_hdr *next; |
| 330 |
|
| |
| 331 |
|
| for (; blok; blok = next) { |
| 332 |
|
| next = blok->h.next; |
| 333 |
|
| free(blok); |
| 334 |
|
| } |
| 335 |
|
| #else |
| 336 |
|
| #ifdef ALLOC_STATS |
| 337 |
|
| unsigned num_blocks; |
| 338 |
|
| #endif |
| 339 |
|
| /* First, put new blocks at the head of the free list --- |
| 340 |
|
| * we'll eventually bash the 'next' pointer of the last block |
| 341 |
|
| * in the chain to point to the free blocks we already had. |
| 342 |
|
| */ |
| 343 |
|
| |
| 344 |
|
| union block_hdr *old_free_list; |
| 345 |
|
| |
| 346 |
0 |
▶ | if (blok == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (blok == NULL) |
0 |
0 |
0 |
|
| 347 |
0 |
| return; /* Sanity check --- freeing empty pool? */ |
| 348 |
|
| |
| 349 |
|
| ep_acquire_mutex(alloc_mutex); |
| 350 |
0 |
| old_free_list = block_freelist; |
| 351 |
0 |
| block_freelist = blok; |
| 352 |
|
| |
| 353 |
|
| /* |
| 354 |
|
| * Next, adjust first_avail pointers of each block --- have to do it |
| 355 |
|
| * sooner or later, and it simplifies the search in new_block to do it |
| 356 |
|
| * now. |
| 357 |
|
| */ |
| 358 |
|
| |
| 359 |
|
| #ifdef ALLOC_STATS |
| 360 |
|
| num_blocks = 1; |
| 361 |
|
| #endif |
| 362 |
0 |
▶ | while (blok->h.next != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (blok->h.next != NULL) { |
0 |
0 |
0 |
|
| 363 |
|
| #ifdef ALLOC_STATS |
| 364 |
|
| ++num_blocks; |
| 365 |
|
| #endif |
| 366 |
|
| chk_on_blk_list(blok, old_free_list); |
| 367 |
0 |
| blok->h.first_avail = (char *) (blok + 1); |
| 368 |
|
| debug_fill(blok->h.first_avail, blok->h.endp - blok->h.first_avail); |
| 369 |
|
| #ifdef POOL_DEBUG |
| 370 |
|
| blok->h.owning_pool = FREE_POOL; |
| 371 |
|
| #endif |
| 372 |
0 |
| blok = blok->h.next; |
| 373 |
|
| } |
| 374 |
|
| |
| 375 |
|
| chk_on_blk_list(blok, old_free_list); |
| 376 |
0 |
| blok->h.first_avail = (char *) (blok + 1); |
| 377 |
|
| debug_fill(blok->h.first_avail, blok->h.endp - blok->h.first_avail); |
| 378 |
|
| #ifdef POOL_DEBUG |
| 379 |
|
| blok->h.owning_pool = FREE_POOL; |
| 380 |
|
| #endif |
| 381 |
|
| |
| 382 |
|
| /* Finally, reset next pointer to get the old free blocks back */ |
| 383 |
|
| |
| 384 |
0 |
| blok->h.next = old_free_list; |
| 385 |
|
| |
| 386 |
|
| #ifdef ALLOC_STATS |
| 387 |
|
| if (num_blocks > max_blocks_in_one_free) { |
| 388 |
|
| max_blocks_in_one_free = num_blocks; |
| 389 |
|
| } |
| 390 |
|
| ++num_free_blocks_calls; |
| 391 |
|
| num_blocks_freed += num_blocks; |
| 392 |
|
| #endif |
| 393 |
|
| |
| 394 |
|
| ep_release_mutex(alloc_mutex); |
| 395 |
|
| #endif |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
|
| |
| 399 |
|
| /* Get a new block, from our own free list if possible, from the system |
| 400 |
|
| * if necessary. Must be called with alarms blocked. |
| 401 |
|
| */ |
| 402 |
|
| |
| 403 |
0 |
| static union block_hdr *new_block(int min_size) |
| 404 |
|
| { |
| 405 |
0 |
| union block_hdr **lastptr = &block_freelist; |
| 406 |
0 |
| union block_hdr *blok = block_freelist; |
| 407 |
|
| |
| 408 |
|
| /* First, see if we have anything of the required size |
| 409 |
|
| * on the free list... |
| 410 |
|
| */ |
| 411 |
|
| |
| 412 |
0 |
▶ | while (blok != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (blok != NULL) { |
0 |
0 |
0 |
|
| 413 |
0 |
▶ | if (min_size + BLOCK_MINFREE <= blok->h.endp - blok->h.first_avail) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (min_size + BLOCK_MINFREE <= blok->h.endp - blok->h.first_avail) { |
0 |
0 |
0 |
|
| 414 |
0 |
| *lastptr = blok->h.next; |
| 415 |
0 |
| blok->h.next = NULL; |
| 416 |
|
| debug_verify_filled(blok->h.first_avail, blok->h.endp, |
| 417 |
|
| "Ouch! Someone trounced a block on the free list!\n"); |
| 418 |
0 |
| return blok; |
| 419 |
|
| } |
| 420 |
|
| else { |
| 421 |
0 |
| lastptr = &blok->h.next; |
| 422 |
0 |
| blok = blok->h.next; |
| 423 |
|
| } |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
|
| /* Nope. */ |
| 427 |
|
| |
| 428 |
0 |
| min_size += BLOCK_MINFREE; |
| 429 |
0 |
| blok = malloc_block((min_size > BLOCK_MINALLOC) ? min_size : BLOCK_MINALLOC); |
| 430 |
0 |
| return blok; |
| 431 |
|
| } |
| 432 |
|
| |
| 433 |
|
| |
| 434 |
|
| /* Accounting */ |
| 435 |
|
| |
| 436 |
0 |
| static long bytes_in_block_list(union block_hdr *blok) |
| 437 |
|
| { |
| 438 |
0 |
| long size = 0; |
| 439 |
|
| |
| 440 |
0 |
▶ | while (blok) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (blok) { |
0 |
0 |
0 |
|
| 441 |
0 |
| size += blok->h.endp - (char *) (blok + 1); |
| 442 |
0 |
| blok = blok->h.next; |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
0 |
| return size; |
| 446 |
|
| } |
| 447 |
|
| |
| 448 |
|
| |
| 449 |
|
| /***************************************************************** |
| 450 |
|
| * |
| 451 |
|
| * tMemPool internals and management... |
| 452 |
|
| * NB that subprocesses are not handled by the generic cleanup code, |
| 453 |
|
| * basically because we don't want cleanups for multiple subprocesses |
| 454 |
|
| * to result in multiple three-second pauses. |
| 455 |
|
| */ |
| 456 |
|
| |
| 457 |
|
| struct process_chain; |
| 458 |
|
| struct cleanup; |
| 459 |
|
| |
| 460 |
|
| /* static void run_cleanups(struct cleanup *); */ |
| 461 |
|
| /* static void free_proc_chain(struct process_chain *); */ |
| 462 |
|
| #define run_cleanups(x) |
| 463 |
|
| #define free_proc_chain(x) |
| 464 |
|
| |
| 465 |
|
| struct tMemPool { |
| 466 |
|
| union block_hdr *first; |
| 467 |
|
| union block_hdr *last; |
| 468 |
|
| struct cleanup *cleanups; |
| 469 |
|
| struct process_chain *subprocesses; |
| 470 |
|
| struct tMemPool *sub_pools; |
| 471 |
|
| struct tMemPool *sub_next; |
| 472 |
|
| struct tMemPool *sub_prev; |
| 473 |
|
| struct tMemPool *parent; |
| 474 |
|
| char *free_first_avail; |
| 475 |
|
| #ifdef ALLOC_USE_MALLOC |
| 476 |
|
| void *allocation_list; |
| 477 |
|
| #endif |
| 478 |
|
| #ifdef POOL_DEBUG |
| 479 |
|
| struct tMemPool *joined; |
| 480 |
|
| #endif |
| 481 |
|
| }; |
| 482 |
|
| |
| 483 |
|
| static tMemPool *permanent_pool; |
| 484 |
|
| |
| 485 |
|
| /* Each tMemPool structure is allocated in the start of its own first block, |
| 486 |
|
| * so we need to know how many bytes that is (once properly aligned...). |
| 487 |
|
| * This also means that when a pool's sub-pool is destroyed, the storage |
| 488 |
|
| * associated with it is *completely* gone, so we have to make sure it |
| 489 |
|
| * gets taken off the parent's sub-pool list... |
| 490 |
|
| */ |
| 491 |
|
| |
| 492 |
|
| #define POOL_HDR_CLICKS (1 + ((sizeof(struct tMemPool) - 1) / CLICK_SZ)) |
| 493 |
|
| #define POOL_HDR_BYTES (POOL_HDR_CLICKS * CLICK_SZ) |
| 494 |
|
| |
| 495 |
0 |
| EP_API_EXPORT(struct tMemPool *) ep_make_sub_pool(struct tMemPool *p) |
| 496 |
|
| { |
| 497 |
|
| union block_hdr *blok; |
| 498 |
|
| tMemPool *new_pool; |
| 499 |
|
| |
| 500 |
|
| ep_block_alarms(); |
| 501 |
|
| |
| 502 |
|
| ep_acquire_mutex(alloc_mutex); |
| 503 |
|
| |
| 504 |
0 |
| blok = new_block(POOL_HDR_BYTES); |
| 505 |
0 |
| new_pool = (tMemPool *) blok->h.first_avail; |
| 506 |
0 |
| blok->h.first_avail += POOL_HDR_BYTES; |
| 507 |
|
| #ifdef POOL_DEBUG |
| 508 |
|
| blok->h.owning_pool = new_pool; |
| 509 |
|
| #endif |
| 510 |
|
| |
| 511 |
0 |
| memset((char *) new_pool, '\0', sizeof(struct tMemPool)); |
| 512 |
0 |
| new_pool->free_first_avail = blok->h.first_avail; |
| 513 |
0 |
| new_pool->first = new_pool->last = blok; |
| 514 |
|
| |
| 515 |
0 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
0 |
0 |
0 |
|
| 516 |
0 |
| new_pool->parent = p; |
| 517 |
0 |
| new_pool->sub_next = p->sub_pools; |
| 518 |
0 |
▶ | if (new_pool->sub_next) |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pool->sub_next) |
0 |
0 |
0 |
|
| 519 |
0 |
| new_pool->sub_next->sub_prev = new_pool; |
| 520 |
0 |
| p->sub_pools = new_pool; |
| 521 |
|
| } |
| 522 |
|
| |
| 523 |
|
| ep_release_mutex(alloc_mutex); |
| 524 |
|
| ep_unblock_alarms(); |
| 525 |
|
| |
| 526 |
0 |
| return new_pool; |
| 527 |
|
| } |
| 528 |
|
| |
| 529 |
|
| #ifdef POOL_DEBUG |
| 530 |
|
| static void stack_var_init(char *s) |
| 531 |
|
| { |
| 532 |
|
| char t; |
| 533 |
|
| |
| 534 |
|
| if (s < &t) { |
| 535 |
|
| stack_direction = 1; /* stack grows up */ |
| 536 |
|
| } |
| 537 |
|
| else { |
| 538 |
|
| stack_direction = -1; /* stack grows down */ |
| 539 |
|
| } |
| 540 |
|
| } |
| 541 |
|
| #endif |
| 542 |
|
| |
| 543 |
|
| #ifdef ALLOC_STATS |
| 544 |
|
| static void dump_stats(void) |
| 545 |
|
| { |
| 546 |
|
| fprintf(stderr, |
| 547 |
|
| "alloc_stats: [%d] #free_blocks %llu #blocks %llu max %u #malloc %u #bytes %u\n", |
| 548 |
|
| (int)getpid(), |
| 549 |
|
| num_free_blocks_calls, |
| 550 |
|
| num_blocks_freed, |
| 551 |
|
| max_blocks_in_one_free, |
| 552 |
|
| num_malloc_calls, |
| 553 |
|
| num_malloc_bytes); |
| 554 |
|
| } |
| 555 |
|
| #endif |
| 556 |
|
| |
| 557 |
0 |
| tMemPool *ep_init_alloc(void) |
| 558 |
|
| { |
| 559 |
|
| #ifdef POOL_DEBUG |
| 560 |
|
| char s; |
| 561 |
|
| |
| 562 |
|
| known_stack_point = &s; |
| 563 |
|
| stack_var_init(&s); |
| 564 |
|
| #endif |
| 565 |
|
| ep_create_mutex(alloc_mutex); |
| 566 |
|
| ep_create_mutex(spawn_mutex ); |
| 567 |
0 |
| permanent_pool = ep_make_sub_pool(NULL); |
| 568 |
|
| #ifdef ALLOC_STATS |
| 569 |
|
| atexit(dump_stats); |
| 570 |
|
| #endif |
| 571 |
|
| |
| 572 |
0 |
| return permanent_pool; |
| 573 |
|
| } |
| 574 |
|
| |
| 575 |
0 |
| void ep_cleanup_alloc(void) |
| 576 |
|
| { |
| 577 |
|
| ep_destroy_mutex(alloc_mutex); |
| 578 |
|
| ep_destroy_mutex(spawn_mutex); |
| 579 |
0 |
| } |
| 580 |
|
| |
| 581 |
0 |
| EP_API_EXPORT(void) ep_clear_pool(struct tMemPool *a) |
| 582 |
|
| { |
| 583 |
|
| ep_block_alarms(); |
| 584 |
|
| |
| 585 |
|
| ep_acquire_mutex(alloc_mutex); |
| 586 |
0 |
▶ | while (a->sub_pools) |
Branch: 1 branch
| Branch | True | False | Total |
| while (a->sub_pools) |
0 |
0 |
0 |
|
| 587 |
0 |
| ep_destroy_pool(a->sub_pools); |
| 588 |
|
| ep_release_mutex(alloc_mutex); |
| 589 |
|
| /* Don't hold the mutex during cleanups. */ |
| 590 |
|
| run_cleanups(a->cleanups); |
| 591 |
0 |
| a->cleanups = NULL; |
| 592 |
|
| free_proc_chain(a->subprocesses); |
| 593 |
0 |
| a->subprocesses = NULL; |
| 594 |
0 |
| free_blocks(a->first->h.next); |
| 595 |
0 |
| a->first->h.next = NULL; |
| 596 |
|
| |
| 597 |
0 |
| a->last = a->first; |
| 598 |
0 |
| a->first->h.first_avail = a->free_first_avail; |
| 599 |
|
| debug_fill(a->first->h.first_avail, |
| 600 |
|
| a->first->h.endp - a->first->h.first_avail); |
| 601 |
|
| |
| 602 |
|
| #ifdef ALLOC_USE_MALLOC |
| 603 |
|
| { |
| 604 |
|
| void *c, *n; |
| 605 |
|
| |
| 606 |
|
| for (c = a->allocation_list; c; c = n) { |
| 607 |
|
| n = *(void **)c; |
| 608 |
|
| free(c); |
| 609 |
|
| } |
| 610 |
|
| a->allocation_list = NULL; |
| 611 |
|
| } |
| 612 |
|
| #endif |
| 613 |
|
| |
| 614 |
|
| ep_unblock_alarms(); |
| 615 |
0 |
| } |
| 616 |
|
| |
| 617 |
0 |
| EP_API_EXPORT(void) ep_destroy_pool(tMemPool *a) |
| 618 |
|
| { |
| 619 |
|
| ep_block_alarms(); |
| 620 |
0 |
| ep_clear_pool(a); |
| 621 |
|
| |
| 622 |
|
| ep_acquire_mutex(alloc_mutex); |
| 623 |
0 |
▶ | if (a->parent) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->parent) { |
0 |
0 |
0 |
|
| 624 |
0 |
▶ | if (a->parent->sub_pools == a) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->parent->sub_pools == a) |
0 |
0 |
0 |
|
| 625 |
0 |
| a->parent->sub_pools = a->sub_next; |
| 626 |
0 |
▶ | if (a->sub_prev) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->sub_prev) |
0 |
0 |
0 |
|
| 627 |
0 |
| a->sub_prev->sub_next = a->sub_next; |
| 628 |
0 |
▶ | if (a->sub_next) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->sub_next) |
0 |
0 |
0 |
|
| 629 |
0 |
| a->sub_next->sub_prev = a->sub_prev; |
| 630 |
|
| } |
| 631 |
|
| ep_release_mutex(alloc_mutex); |
| 632 |
|
| |
| 633 |
0 |
| free_blocks(a->first); |
| 634 |
|
| ep_unblock_alarms(); |
| 635 |
0 |
| } |
| 636 |
|
| |
| 637 |
0 |
| EP_API_EXPORT(long) ep_bytes_in_pool(tMemPool *p) |
| 638 |
|
| { |
| 639 |
0 |
| return bytes_in_block_list(p->first); |
| 640 |
|
| } |
| 641 |
0 |
| EP_API_EXPORT(long) ep_bytes_in_free_blocks(void) |
| 642 |
|
| { |
| 643 |
0 |
| return bytes_in_block_list(block_freelist); |
| 644 |
|
| } |
| 645 |
|
| |
| 646 |
|
| /***************************************************************** |
| 647 |
|
| * POOL_DEBUG support |
| 648 |
|
| */ |
| 649 |
|
| #ifdef POOL_DEBUG |
| 650 |
|
| |
| 651 |
|
| /* the unix linker defines this symbol as the last byte + 1 of |
| 652 |
|
| * the executable... so it includes TEXT, BSS, and DATA |
| 653 |
|
| */ |
| 654 |
|
| extern char _end; |
| 655 |
|
| |
| 656 |
|
| /* is ptr in the range [lo,hi) */ |
| 657 |
|
| #define is_ptr_in_range(ptr, lo, hi) \ |
| 658 |
|
| (((unsigned long)(ptr) - (unsigned long)(lo)) \ |
| 659 |
|
| < \ |
| 660 |
|
| (unsigned long)(hi) - (unsigned long)(lo)) |
| 661 |
|
| |
| 662 |
|
| /* Find the tMemPool that ts belongs to, return NULL if it doesn't |
| 663 |
|
| * belong to any pool. |
| 664 |
|
| */ |
| 665 |
|
| EP_API_EXPORT(tMemPool *) ep_find_pool(const void *ts) |
| 666 |
|
| { |
| 667 |
|
| const char *s = ts; |
| 668 |
|
| union block_hdr **pb; |
| 669 |
|
| union block_hdr *b; |
| 670 |
|
| |
| 671 |
|
| /* short-circuit stuff which is in TEXT, BSS, or DATA */ |
| 672 |
|
| if (is_ptr_in_range(s, 0, &_end)) { |
| 673 |
|
| return NULL; |
| 674 |
|
| } |
| 675 |
|
| /* consider stuff on the stack to also be in the NULL pool... |
| 676 |
|
| * XXX: there's cases where we don't want to assume this |
| 677 |
|
| */ |
| 678 |
|
| if ((stack_direction == -1 && is_ptr_in_range(s, &ts, known_stack_point)) |
| 679 |
|
| || (stack_direction == 1 && is_ptr_in_range(s, known_stack_point, &ts))) { |
| 680 |
|
| abort(); |
| 681 |
|
| return NULL; |
| 682 |
|
| } |
| 683 |
|
| ep_block_alarms(); |
| 684 |
|
| /* search the global_block_list */ |
| 685 |
|
| for (pb = &global_block_list; *pb; pb = &b->h.global_next) { |
| 686 |
|
| b = *pb; |
| 687 |
|
| if (is_ptr_in_range(s, b, b->h.endp)) { |
| 688 |
|
| if (b->h.owning_pool == FREE_POOL) { |
| 689 |
|
| fprintf(stderr, |
| 690 |
|
| "Ouch! find_pool() called on pointer in a free block\n"); |
| 691 |
|
| abort(); |
| 692 |
|
| exit(1); |
| 693 |
|
| } |
| 694 |
|
| if (b != global_block_list) { |
| 695 |
|
| /* promote b to front of list, this is a hack to speed |
| 696 |
|
| * up the lookup */ |
| 697 |
|
| *pb = b->h.global_next; |
| 698 |
|
| b->h.global_next = global_block_list; |
| 699 |
|
| global_block_list = b; |
| 700 |
|
| } |
| 701 |
|
| ep_unblock_alarms(); |
| 702 |
|
| return b->h.owning_pool; |
| 703 |
|
| } |
| 704 |
|
| } |
| 705 |
|
| ep_unblock_alarms(); |
| 706 |
|
| return NULL; |
| 707 |
|
| } |
| 708 |
|
| |
| 709 |
|
| /* return TRUE iff a is an ancestor of b |
| 710 |
|
| * NULL is considered an ancestor of all pools |
| 711 |
|
| */ |
| 712 |
|
| EP_API_EXPORT(int) ep_pool_is_ancestor(tMemPool *a, tMemPool *b) |
| 713 |
|
| { |
| 714 |
|
| if (a == NULL) { |
| 715 |
|
| return 1; |
| 716 |
|
| } |
| 717 |
|
| while (a->joined) { |
| 718 |
|
| a = a->joined; |
| 719 |
|
| } |
| 720 |
|
| while (b) { |
| 721 |
|
| if (a == b) { |
| 722 |
|
| return 1; |
| 723 |
|
| } |
| 724 |
|
| b = b->parent; |
| 725 |
|
| } |
| 726 |
|
| return 0; |
| 727 |
|
| } |
| 728 |
|
| |
| 729 |
|
| /* All blocks belonging to sub will be changed to point to p |
| 730 |
|
| * instead. This is a guarantee by the caller that sub will not |
| 731 |
|
| * be destroyed before p is. |
| 732 |
|
| */ |
| 733 |
|
| EP_API_EXPORT(void) ep_pool_join(tMemPool *p, tMemPool *sub) |
| 734 |
|
| { |
| 735 |
|
| union block_hdr *b; |
| 736 |
|
| |
| 737 |
|
| /* We could handle more general cases... but this is it for now. */ |
| 738 |
|
| if (sub->parent != p) { |
| 739 |
|
| fprintf(stderr, "pool_join: p is not parent of sub\n"); |
| 740 |
|
| abort(); |
| 741 |
|
| } |
| 742 |
|
| ep_block_alarms(); |
| 743 |
|
| while (p->joined) { |
| 744 |
|
| p = p->joined; |
| 745 |
|
| } |
| 746 |
|
| sub->joined = p; |
| 747 |
|
| for (b = global_block_list; b; b = b->h.global_next) { |
| 748 |
|
| if (b->h.owning_pool == sub) { |
| 749 |
|
| b->h.owning_pool = p; |
| 750 |
|
| } |
| 751 |
|
| } |
| 752 |
|
| ep_unblock_alarms(); |
| 753 |
|
| } |
| 754 |
|
| #endif |
| 755 |
|
| |
| 756 |
|
| /***************************************************************** |
| 757 |
|
| * |
| 758 |
|
| * Allocating stuff... |
| 759 |
|
| */ |
| 760 |
|
| |
| 761 |
|
| |
| 762 |
0 |
| EP_API_EXPORT(void *) ep_palloc(struct tMemPool *a, int reqsize) |
| 763 |
|
| { |
| 764 |
|
| #ifdef ALLOC_USE_MALLOC |
| 765 |
|
| int size = reqsize + CLICK_SZ; |
| 766 |
|
| void *ptr; |
| 767 |
|
| |
| 768 |
|
| ep_block_alarms(); |
| 769 |
|
| ptr = malloc(size); |
| 770 |
|
| if (ptr == NULL) { |
| 771 |
|
| fputs("Ouch! Out of memory!\n", stderr); |
| 772 |
|
| exit(1); |
| 773 |
|
| } |
| 774 |
|
| debug_fill(ptr, size); /* might as well get uninitialized protection */ |
| 775 |
|
| *(void **)ptr = a->allocation_list; |
| 776 |
|
| a->allocation_list = ptr; |
| 777 |
|
| ep_unblock_alarms(); |
| 778 |
|
| return (char *)ptr + CLICK_SZ; |
| 779 |
|
| #else |
| 780 |
|
| |
| 781 |
|
| /* Round up requested size to an even number of alignment units (core clicks) |
| 782 |
|
| */ |
| 783 |
|
| |
| 784 |
0 |
| int nclicks = 1 + ((reqsize - 1) / CLICK_SZ); |
| 785 |
0 |
| int size = nclicks * CLICK_SZ; |
| 786 |
|
| |
| 787 |
|
| /* First, see if we have space in the block most recently |
| 788 |
|
| * allocated to this pool |
| 789 |
|
| */ |
| 790 |
|
| |
| 791 |
0 |
| union block_hdr *blok = a->last; |
| 792 |
0 |
| char *first_avail = blok->h.first_avail; |
| 793 |
|
| char *new_first_avail; |
| 794 |
|
| |
| 795 |
0 |
▶ | if (reqsize <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (reqsize <= 0) |
0 |
0 |
0 |
|
| 796 |
0 |
| return NULL; |
| 797 |
|
| |
| 798 |
0 |
| new_first_avail = first_avail + size; |
| 799 |
|
| |
| 800 |
0 |
▶ | if (new_first_avail <= blok->h.endp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_first_avail <= blok->h.endp) { |
0 |
0 |
0 |
|
| 801 |
|
| debug_verify_filled(first_avail, blok->h.endp, |
| 802 |
|
| "Ouch! Someone trounced past the end of their allocation!\n"); |
| 803 |
0 |
| blok->h.first_avail = new_first_avail; |
| 804 |
0 |
| return (void *) first_avail; |
| 805 |
|
| } |
| 806 |
|
| |
| 807 |
|
| /* Nope --- get a new one that's guaranteed to be big enough */ |
| 808 |
|
| |
| 809 |
|
| ep_block_alarms(); |
| 810 |
|
| |
| 811 |
|
| ep_acquire_mutex(alloc_mutex); |
| 812 |
|
| |
| 813 |
0 |
| blok = new_block(size); |
| 814 |
0 |
| a->last->h.next = blok; |
| 815 |
0 |
| a->last = blok; |
| 816 |
|
| #ifdef POOL_DEBUG |
| 817 |
|
| blok->h.owning_pool = a; |
| 818 |
|
| #endif |
| 819 |
|
| |
| 820 |
|
| ep_release_mutex(alloc_mutex); |
| 821 |
|
| |
| 822 |
|
| ep_unblock_alarms(); |
| 823 |
|
| |
| 824 |
0 |
| first_avail = blok->h.first_avail; |
| 825 |
0 |
| blok->h.first_avail += size; |
| 826 |
|
| |
| 827 |
0 |
| return (void *) first_avail; |
| 828 |
|
| #endif |
| 829 |
|
| } |
| 830 |
|
| |
| 831 |
0 |
| EP_API_EXPORT(void *) ep_pcalloc(struct tMemPool *a, int size) |
| 832 |
|
| { |
| 833 |
0 |
| void *res = ep_palloc(a, size); |
| 834 |
0 |
| memset(res, '\0', size); |
| 835 |
0 |
| return res; |
| 836 |
|
| } |
| 837 |
|
| |
| 838 |
0 |
| EP_API_EXPORT(char *) ep_pstrdup(struct tMemPool *a, const char *s) |
| 839 |
|
| { |
| 840 |
|
| char *res; |
| 841 |
|
| size_t len; |
| 842 |
|
| |
| 843 |
0 |
▶ | if (s == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s == NULL) |
0 |
0 |
0 |
|
| 844 |
0 |
| return NULL; |
| 845 |
0 |
| len = strlen(s) + 1; |
| 846 |
0 |
| res = ep_palloc(a, len); |
| 847 |
0 |
| memcpy(res, s, len); |
| 848 |
0 |
| return res; |
| 849 |
|
| } |
| 850 |
|
| |
| 851 |
0 |
| EP_API_EXPORT(char *) ep_pstrndup(struct tMemPool *a, const char *s, int n) |
| 852 |
|
| { |
| 853 |
|
| char *res; |
| 854 |
|
| |
| 855 |
0 |
▶ | if (s == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s == NULL) |
0 |
0 |
0 |
|
| 856 |
0 |
| return NULL; |
| 857 |
0 |
| res = ep_palloc(a, n + 1); |
| 858 |
0 |
| memcpy(res, s, n); |
| 859 |
0 |
| res[n] = '\0'; |
| 860 |
0 |
| return res; |
| 861 |
|
| } |
| 862 |
|
| |
| 863 |
0 |
| EP_API_EXPORT_NONSTD(char *) ep_pstrcat(tMemPool *a,...) |
| 864 |
|
| { |
| 865 |
|
| char *cp, *argp, *res; |
| 866 |
|
| |
| 867 |
|
| /* Pass one --- find length of required string */ |
| 868 |
|
| |
| 869 |
0 |
| int len = 0; |
| 870 |
|
| va_list adummy; |
| 871 |
|
| |
| 872 |
0 |
| va_start(adummy, a); |
| 873 |
|
| |
| 874 |
0 |
▶ | while ((cp = va_arg(adummy, char *)) != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((cp = va_arg(adummy, char *)) != NULL) |
0 |
0 |
0 |
|
| 875 |
0 |
| len += strlen(cp); |
| 876 |
|
| |
| 877 |
0 |
| va_end(adummy); |
| 878 |
|
| |
| 879 |
|
| /* Allocate the required string */ |
| 880 |
|
| |
| 881 |
0 |
| res = (char *) ep_palloc(a, len + 1); |
| 882 |
0 |
| cp = res; |
| 883 |
0 |
| *cp = '\0'; |
| 884 |
|
| |
| 885 |
|
| /* Pass two --- copy the argument strings into the result space */ |
| 886 |
|
| |
| 887 |
0 |
| va_start(adummy, a); |
| 888 |
|
| |
| 889 |
0 |
▶ | while ((argp = va_arg(adummy, char *)) != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((argp = va_arg(adummy, char *)) != NULL) { |
0 |
0 |
0 |
|
| 890 |
0 |
| strcpy(cp, argp); |
| 891 |
0 |
| cp += strlen(argp); |
| 892 |
|
| } |
| 893 |
|
| |
| 894 |
0 |
| va_end(adummy); |
| 895 |
|
| |
| 896 |
|
| /* Return the result string */ |
| 897 |
|
| |
| 898 |
0 |
| return res; |
| 899 |
|
| } |
| 900 |
|
| |
| 901 |
|
| #ifdef EPSPRINTF |
| 902 |
|
| |
| 903 |
|
| /* ep_psprintf is implemented by writing directly into the current |
| 904 |
|
| * block of the pool, starting right at first_avail. If there's |
| 905 |
|
| * insufficient room, then a new block is allocated and the earlier |
| 906 |
|
| * output is copied over. The new block isn't linked into the pool |
| 907 |
|
| * until all the output is done. |
| 908 |
|
| * |
| 909 |
|
| * Note that this is completely safe because nothing else can |
| 910 |
|
| * allocate in this tMemPool while ep_psprintf is running. alarms are |
| 911 |
|
| * blocked, and the only thing outside of alloc.c that's invoked |
| 912 |
|
| * is ep_vformatter -- which was purposefully written to be |
| 913 |
|
| * self-contained with no callouts. |
| 914 |
|
| */ |
| 915 |
|
| |
| 916 |
|
| struct psprintf_data { |
| 917 |
|
| ep_vformatter_buff vbuff; |
| 918 |
|
| #ifdef ALLOC_USE_MALLOC |
| 919 |
|
| char *base; |
| 920 |
|
| #else |
| 921 |
|
| union block_hdr *blok; |
| 922 |
|
| int got_a_new_block; |
| 923 |
|
| #endif |
| 924 |
|
| }; |
| 925 |
|
| |
| 926 |
|
| static int psprintf_flush(ep_vformatter_buff *vbuff) |
| 927 |
|
| { |
| 928 |
|
| struct psprintf_data *ps = (struct psprintf_data *)vbuff; |
| 929 |
|
| #ifdef ALLOC_USE_MALLOC |
| 930 |
|
| int size; |
| 931 |
|
| char *ptr; |
| 932 |
|
| |
| 933 |
|
| size = (char *)ps->vbuff.curpos - ps->base; |
| 934 |
|
| ptr = realloc(ps->base, 2*size); |
| 935 |
|
| if (ptr == NULL) { |
| 936 |
|
| fputs("Ouch! Out of memory!\n", stderr); |
| 937 |
|
| exit(1); |
| 938 |
|
| } |
| 939 |
|
| ps->base = ptr; |
| 940 |
|
| ps->vbuff.curpos = ptr + size; |
| 941 |
|
| ps->vbuff.endpos = ptr + 2*size - 1; |
| 942 |
|
| return 0; |
| 943 |
|
| #else |
| 944 |
|
| union block_hdr *blok; |
| 945 |
|
| union block_hdr *nblok; |
| 946 |
|
| size_t cur_len; |
| 947 |
|
| char *strp; |
| 948 |
|
| |
| 949 |
|
| blok = ps->blok; |
| 950 |
|
| strp = ps->vbuff.curpos; |
| 951 |
|
| cur_len = strp - blok->h.first_avail; |
| 952 |
|
| |
| 953 |
|
| /* must try another blok */ |
| 954 |
|
| (void) ep_acquire_mutex(alloc_mutex); |
| 955 |
|
| nblok = new_block(2 * cur_len); |
| 956 |
|
| (void) ep_release_mutex(alloc_mutex); |
| 957 |
|
| memcpy(nblok->h.first_avail, blok->h.first_avail, cur_len); |
| 958 |
|
| ps->vbuff.curpos = nblok->h.first_avail + cur_len; |
| 959 |
|
| /* save a byte for the NUL terminator */ |
| 960 |
|
| ps->vbuff.endpos = nblok->h.endp - 1; |
| 961 |
|
| |
| 962 |
|
| /* did we allocate the current blok? if so free it up */ |
| 963 |
|
| if (ps->got_a_new_block) { |
| 964 |
|
| debug_fill(blok->h.first_avail, blok->h.endp - blok->h.first_avail); |
| 965 |
|
| (void) ep_acquire_mutex(alloc_mutex); |
| 966 |
|
| blok->h.next = block_freelist; |
| 967 |
|
| block_freelist = blok; |
| 968 |
|
| (void) ep_release_mutex(alloc_mutex); |
| 969 |
|
| } |
| 970 |
|
| ps->blok = nblok; |
| 971 |
|
| ps->got_a_new_block = 1; |
| 972 |
|
| /* note that we've deliberately not linked the new block onto |
| 973 |
|
| * the tMemPool yet... because we may need to flush again later, and |
| 974 |
|
| * we'd have to spend more effort trying to unlink the block. |
| 975 |
|
| */ |
| 976 |
|
| return 0; |
| 977 |
|
| #endif |
| 978 |
|
| } |
| 979 |
|
| |
| 980 |
|
| EP_API_EXPORT(char *) ep_pvsprintf(tMemPool *p, const char *fmt, va_list ap) |
| 981 |
|
| { |
| 982 |
|
| #ifdef ALLOC_USE_MALLOC |
| 983 |
|
| struct psprintf_data ps; |
| 984 |
|
| void *ptr; |
| 985 |
|
| |
| 986 |
|
| ep_block_alarms(); |
| 987 |
|
| ps.base = malloc(512); |
| 988 |
|
| if (ps.base == NULL) { |
| 989 |
|
| fputs("Ouch! Out of memory!\n", stderr); |
| 990 |
|
| exit(1); |
| 991 |
|
| } |
| 992 |
|
| /* need room at beginning for allocation_list */ |
| 993 |
|
| ps.vbuff.curpos = ps.base + CLICK_SZ; |
| 994 |
|
| ps.vbuff.endpos = ps.base + 511; |
| 995 |
|
| ep_vformatter(psprintf_flush, &ps.vbuff, fmt, ap); |
| 996 |
|
| *ps.vbuff.curpos++ = '\0'; |
| 997 |
|
| ptr = ps.base; |
| 998 |
|
| /* shrink */ |
| 999 |
|
| ptr = realloc(ptr, (char *)ps.vbuff.curpos - (char *)ptr); |
| 1000 |
|
| if (ptr == NULL) { |
| 1001 |
|
| fputs("Ouch! Out of memory!\n", stderr); |
| 1002 |
|
| exit(1); |
| 1003 |
|
| } |
| 1004 |
|
| *(void **)ptr = p->allocation_list; |
| 1005 |
|
| p->allocation_list = ptr; |
| 1006 |
|
| ep_unblock_alarms(); |
| 1007 |
|
| return (char *)ptr + CLICK_SZ; |
| 1008 |
|
| #else |
| 1009 |
|
| struct psprintf_data ps; |
| 1010 |
|
| char *strp; |
| 1011 |
|
| int size; |
| 1012 |
|
| |
| 1013 |
|
| ep_block_alarms(); |
| 1014 |
|
| ps.blok = p->last; |
| 1015 |
|
| ps.vbuff.curpos = ps.blok->h.first_avail; |
| 1016 |
|
| ps.vbuff.endpos = ps.blok->h.endp - 1; /* save one for NUL */ |
| 1017 |
|
| ps.got_a_new_block = 0; |
| 1018 |
|
| |
| 1019 |
|
| ep_vformatter(psprintf_flush, &ps.vbuff, fmt, ap); |
| 1020 |
|
| |
| 1021 |
|
| strp = ps.vbuff.curpos; |
| 1022 |
|
| *strp++ = '\0'; |
| 1023 |
|
| |
| 1024 |
|
| size = strp - ps.blok->h.first_avail; |
| 1025 |
|
| size = (1 + ((size - 1) / CLICK_SZ)) * CLICK_SZ; |
| 1026 |
|
| strp = ps.blok->h.first_avail; /* save away result pointer */ |
| 1027 |
|
| ps.blok->h.first_avail += size; |
| 1028 |
|
| |
| 1029 |
|
| /* have to link the block in if it's a new one */ |
| 1030 |
|
| if (ps.got_a_new_block) { |
| 1031 |
|
| p->last->h.next = ps.blok; |
| 1032 |
|
| p->last = ps.blok; |
| 1033 |
|
| #ifdef POOL_DEBUG |
| 1034 |
|
| ps.blok->h.owning_pool = p; |
| 1035 |
|
| #endif |
| 1036 |
|
| } |
| 1037 |
|
| ep_unblock_alarms(); |
| 1038 |
|
| |
| 1039 |
|
| return strp; |
| 1040 |
|
| #endif |
| 1041 |
|
| } |
| 1042 |
|
| |
| 1043 |
|
| EP_API_EXPORT_NONSTD(char *) ep_psprintf(tMemPool *p, const char *fmt, ...) |
| 1044 |
|
| { |
| 1045 |
|
| va_list ap; |
| 1046 |
|
| char *res; |
| 1047 |
|
| |
| 1048 |
|
| va_start(ap, fmt); |
| 1049 |
|
| res = ep_pvsprintf(p, fmt, ap); |
| 1050 |
|
| va_end(ap); |
| 1051 |
|
| return res; |
| 1052 |
|
| } |
| 1053 |
|
| |
| 1054 |
|
| #endif |