1 /* 2 * region-allocator.h -- region based memory allocator. 3 * 4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved. 5 * 6 * See LICENSE for the license. 7 * 8 */ 9 10 #ifndef REGION_ALLOCATOR_H 11 #define REGION_ALLOCATOR_H 12 13 #include <stdio.h> 14 15 typedef struct region region_type; 16 17 #define DEFAULT_CHUNK_SIZE 4096 18 #define DEFAULT_LARGE_OBJECT_SIZE (DEFAULT_CHUNK_SIZE / 8) 19 #define DEFAULT_INITIAL_CLEANUP_SIZE 16 20 21 22 /* 23 * mmap allocator constants 24 * 25 */ 26 #ifdef USE_MMAP_ALLOC 27 28 /* header starts with size_t containing allocated size info and has at least 16 bytes to align the returned memory */ 29 #define MMAP_ALLOC_HEADER_SIZE (sizeof(size_t) >= 16 ? (sizeof(size_t)) : 16) 30 31 /* mmap allocator uses chunks of 32 4kB pages */ 32 #define MMAP_ALLOC_CHUNK_SIZE ((32 * 4096) - MMAP_ALLOC_HEADER_SIZE) 33 #define MMAP_ALLOC_LARGE_OBJECT_SIZE (MMAP_ALLOC_CHUNK_SIZE / 8) 34 #define MMAP_ALLOC_INITIAL_CLEANUP_SIZE 16 35 36 #endif /* USE_MMAP_ALLOC */ 37 38 /* 39 * Create a new region. 40 */ 41 region_type *region_create(void *(*allocator)(size_t), 42 void (*deallocator)(void *)); 43 44 45 /* 46 * Create a new region, with chunk size and large object size. 47 * Note that large_object_size must be <= chunk_size. 48 * Anything larger than the large object size is individually alloced. 49 * large_object_size = chunk_size/8 is reasonable; 50 * initial_cleanup_size is the number of preallocated ptrs for cleanups. 51 * The cleanups are in a growing array, and it must start larger than zero. 52 * If recycle is true, environmentally friendly memory recycling is be enabled. 53 */ 54 region_type *region_create_custom(void *(*allocator)(size_t), 55 void (*deallocator)(void *), 56 size_t chunk_size, 57 size_t large_object_size, 58 size_t initial_cleanup_size, 59 int recycle); 60 61 62 /* 63 * Destroy REGION. All memory associated with REGION is freed as if 64 * region_free_all was called. 65 */ 66 void region_destroy(region_type *region); 67 68 69 /* 70 * Add a cleanup to REGION. ACTION will be called with DATA as 71 * parameter when the region is freed or destroyed. 72 * 73 * Returns 0 on failure. 74 */ 75 size_t region_add_cleanup(region_type *region, 76 void (*action)(void *), 77 void *data); 78 79 /* 80 * Remove cleanup, both action and data must match exactly. 81 */ 82 void region_remove_cleanup(region_type *region, 83 void (*action)(void *), void *data); 84 85 /* 86 * Allocate SIZE bytes of memory inside REGION. The memory is 87 * deallocated when region_free_all is called for this region. 88 */ 89 void *region_alloc(region_type *region, size_t size); 90 91 /** Allocate array with integer overflow checks, in region */ 92 void *region_alloc_array(region_type *region, size_t num, size_t size); 93 94 /* 95 * Allocate SIZE bytes of memory inside REGION and copy INIT into it. 96 * The memory is deallocated when region_free_all is called for this 97 * region. 98 */ 99 void *region_alloc_init(region_type *region, const void *init, size_t size); 100 101 /** 102 * Allocate array (with integer overflow check on sizes), and init with 103 * the given array copied into it. Allocated in the region 104 */ 105 void *region_alloc_array_init(region_type *region, const void *init, 106 size_t num, size_t size); 107 108 /* 109 * Allocate SIZE bytes of memory inside REGION that are initialized to 110 * 0. The memory is deallocated when region_free_all is called for 111 * this region. 112 */ 113 void *region_alloc_zero(region_type *region, size_t size); 114 115 /** 116 * Allocate array (with integer overflow check on sizes), and zero it. 117 * Allocated in the region. 118 */ 119 void *region_alloc_array_zero(region_type *region, size_t num, size_t size); 120 121 /* 122 * Run the cleanup actions and free all memory associated with REGION. 123 */ 124 void region_free_all(region_type *region); 125 126 127 /* 128 * Duplicate STRING and allocate the result in REGION. 129 */ 130 char *region_strdup(region_type *region, const char *string); 131 132 /* 133 * Recycle an allocated memory block. Pass size used to alloc it. 134 * Does nothing if recycling is not enabled for the region. 135 */ 136 void region_recycle(region_type *region, void *block, size_t size); 137 138 /* 139 * Print some REGION statistics to OUT. 140 */ 141 void region_dump_stats(region_type *region, FILE *out); 142 143 /* get size of recyclebin */ 144 size_t region_get_recycle_size(region_type* region); 145 /* get size of region memory in use */ 146 size_t region_get_mem(region_type* region); 147 /* get size of region memory unused */ 148 size_t region_get_mem_unused(region_type* region); 149 150 /* Debug print REGION statistics to LOG. */ 151 void region_log_stats(region_type *region); 152 153 #endif /* REGION_ALLOCATOR_H */ 154