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