1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007, 2009 Daniel Pittman and Christian Grothoff 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /** 21 * @file memorypool.h 22 * @brief memory pool; mostly used for efficient (de)allocation 23 * for each connection and bounding memory use for each 24 * request 25 * @author Christian Grothoff 26 */ 27 28 #ifndef MEMORYPOOL_H 29 #define MEMORYPOOL_H 30 31 #include "internal.h" 32 33 /** 34 * Opaque handle for a memory pool. 35 * Pools are not reentrant and must not be used 36 * by multiple threads. 37 */ 38 struct MemoryPool; 39 40 41 /** 42 * Create a memory pool. 43 * 44 * @param max maximum size of the pool 45 * @return NULL on error 46 */ 47 struct MemoryPool * 48 MHD_pool_create (size_t max); 49 50 51 /** 52 * Destroy a memory pool. 53 * 54 * @param pool memory pool to destroy 55 */ 56 void 57 MHD_pool_destroy (struct MemoryPool *pool); 58 59 60 /** 61 * Allocate size bytes from the pool. 62 * 63 * @param pool memory pool to use for the operation 64 * @param size number of bytes to allocate 65 * @param from_end allocate from end of pool (set to MHD_YES); 66 * use this for small, persistent allocations that 67 * will never be reallocated 68 * @return NULL if the pool cannot support size more 69 * bytes 70 */ 71 void * 72 MHD_pool_allocate (struct MemoryPool *pool, 73 size_t size, int from_end); 74 75 76 /** 77 * Reallocate a block of memory obtained from the pool. 78 * This is particularly efficient when growing or 79 * shrinking the block that was last (re)allocated. 80 * If the given block is not the most recenlty 81 * (re)allocated block, the memory of the previous 82 * allocation may be leaked until the pool is 83 * destroyed (and copying the data maybe required). 84 * 85 * @param pool memory pool to use for the operation 86 * @param old the existing block 87 * @param old_size the size of the existing block 88 * @param new_size the new size of the block 89 * @return new address of the block, or 90 * NULL if the pool cannot support new_size 91 * bytes (old continues to be valid for old_size) 92 */ 93 void * 94 MHD_pool_reallocate (struct MemoryPool *pool, 95 void *old, 96 size_t old_size, 97 size_t new_size); 98 99 100 /** 101 * Clear all entries from the memory pool except 102 * for "keep" of the given "size". 103 * 104 * @param pool memory pool to use for the operation 105 * @param keep pointer to the entry to keep (maybe NULL) 106 * @param size how many bytes need to be kept at this address 107 * @return addr new address of "keep" (if it had to change) 108 */ 109 void * 110 MHD_pool_reset (struct MemoryPool *pool, 111 void *keep, 112 size_t size); 113 114 #endif 115