1 /* 2 * region-allocator.h -- region based memory allocator. 3 * 4 * Copyright (c) 2001-2011, 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 prealloced 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 92 /* 93 * Allocate SIZE bytes of memory inside REGION and copy INIT into it. 94 * The memory is deallocated when region_free_all is called for this 95 * region. 96 */ 97 void *region_alloc_init(region_type *region, const void *init, size_t size); 98 99 100 /* 101 * Allocate SIZE bytes of memory inside REGION that are initialized to 102 * 0. The memory is deallocated when region_free_all is called for 103 * this region. 104 */ 105 void *region_alloc_zero(region_type *region, size_t size); 106 107 108 /* 109 * Run the cleanup actions and free all memory associated with REGION. 110 */ 111 void region_free_all(region_type *region); 112 113 114 /* 115 * Duplicate STRING and allocate the result in REGION. 116 */ 117 char *region_strdup(region_type *region, const char *string); 118 119 /* 120 * Recycle an allocated memory block. Pass size used to alloc it. 121 * Does nothing if recycling is not enabled for the region. 122 */ 123 void region_recycle(region_type *region, void *block, size_t size); 124 125 /* 126 * Print some REGION statistics to OUT. 127 */ 128 void region_dump_stats(region_type *region, FILE *out); 129 130 /* get size of recyclebin */ 131 size_t region_get_recycle_size(region_type* region); 132 133 /* Debug print REGION statistics to LOG. */ 134 void region_log_stats(region_type *region); 135 136 #endif /* _REGION_ALLOCATOR_H_ */ 137