Home | History | Annotate | Line # | Download | only in util
regional.h revision 1.1.1.1
      1  1.1  christos /*
      2  1.1  christos  * regional.h -- region based memory allocator.
      3  1.1  christos  *
      4  1.1  christos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
      5  1.1  christos  *
      6  1.1  christos  * This software is open source.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  *
     12  1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13  1.1  christos  * this list of conditions and the following disclaimer.
     14  1.1  christos  *
     15  1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16  1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17  1.1  christos  * and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20  1.1  christos  * be used to endorse or promote products derived from this software without
     21  1.1  christos  * specific prior written permission.
     22  1.1  christos  *
     23  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24  1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25  1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26  1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27  1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28  1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29  1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30  1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31  1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32  1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33  1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.1  christos  */
     35  1.1  christos 
     36  1.1  christos /**
     37  1.1  christos  * \file
     38  1.1  christos  * Regional allocator. Allocates small portions of of larger chunks.
     39  1.1  christos  * Based on region-allocator from NSD, but rewritten to be light.
     40  1.1  christos  *
     41  1.1  christos  * Different from (nsd) region-allocator.h
     42  1.1  christos  * 	o does not have recycle bin
     43  1.1  christos  * 	o does not collect stats; just enough to answer get_mem() in use.
     44  1.1  christos  * 	o does not keep cleanup list
     45  1.1  christos  * 	o does not have function pointers to setup
     46  1.1  christos  * 	o allocs the regional struct inside the first block.
     47  1.1  christos  * 	o can take a block to create regional from.
     48  1.1  christos  * 	o blocks and large allocations are kept on singly linked lists.
     49  1.1  christos  */
     50  1.1  christos 
     51  1.1  christos #ifndef UTIL_REGIONAL_H_
     52  1.1  christos #define UTIL_REGIONAL_H_
     53  1.1  christos 
     54  1.1  christos /**
     55  1.1  christos  * the regional* is the first block*.
     56  1.1  christos  * every block has a ptr to the next in first bytes.
     57  1.1  christos  * and so does the regional struct, which is the first block.
     58  1.1  christos  */
     59  1.1  christos struct regional
     60  1.1  christos {
     61  1.1  christos 	/**
     62  1.1  christos 	 * next chunk. NULL if first chunk is the only chunk.
     63  1.1  christos 	 * first inside that chunk is the char* next pointer.
     64  1.1  christos 	 * When regional_free_all() has been called this value is NULL.
     65  1.1  christos 	 */
     66  1.1  christos 	char* next;
     67  1.1  christos 	/** first large object, cast to char** to obtain next ptr */
     68  1.1  christos 	char* large_list;
     69  1.1  christos 	/** total large size */
     70  1.1  christos 	size_t total_large;
     71  1.1  christos 	/** initial chunk size */
     72  1.1  christos 	size_t first_size;
     73  1.1  christos 	/** number of bytes available in the current chunk. */
     74  1.1  christos 	size_t available;
     75  1.1  christos 	/** current chunk data position. */
     76  1.1  christos 	char* data;
     77  1.1  christos };
     78  1.1  christos 
     79  1.1  christos /**
     80  1.1  christos  * Create a new regional.
     81  1.1  christos  * @return: newly allocated regional.
     82  1.1  christos  */
     83  1.1  christos struct regional* regional_create(void);
     84  1.1  christos 
     85  1.1  christos /**
     86  1.1  christos  * Create a new region, with custom settings.
     87  1.1  christos  * @param size: length of first block.
     88  1.1  christos  * @return: newly allocated regional.
     89  1.1  christos  */
     90  1.1  christos struct regional* regional_create_custom(size_t size);
     91  1.1  christos 
     92  1.1  christos /**
     93  1.1  christos  * Free all memory associated with regional. Only keeps the first block with
     94  1.1  christos  * the regional inside it.
     95  1.1  christos  * @param r: the region.
     96  1.1  christos  */
     97  1.1  christos void regional_free_all(struct regional *r);
     98  1.1  christos 
     99  1.1  christos /**
    100  1.1  christos  * Destroy regional.  All memory associated with regional is freed as if
    101  1.1  christos  * regional_free_all was called, as well as destroying the regional struct.
    102  1.1  christos  * @param r: to delete.
    103  1.1  christos  */
    104  1.1  christos void regional_destroy(struct regional *r);
    105  1.1  christos 
    106  1.1  christos /**
    107  1.1  christos  * Allocate size bytes of memory inside regional.  The memory is
    108  1.1  christos  * deallocated when region_free_all is called for this region.
    109  1.1  christos  * @param r: the region.
    110  1.1  christos  * @param size: number of bytes.
    111  1.1  christos  * @return: pointer to memory allocated.
    112  1.1  christos  */
    113  1.1  christos void *regional_alloc(struct regional *r, size_t size);
    114  1.1  christos 
    115  1.1  christos /**
    116  1.1  christos  * Allocate size bytes of memory inside regional and copy INIT into it.
    117  1.1  christos  * The memory is deallocated when region_free_all is called for this
    118  1.1  christos  * region.
    119  1.1  christos  * @param r: the region.
    120  1.1  christos  * @param init: to copy.
    121  1.1  christos  * @param size: number of bytes.
    122  1.1  christos  * @return: pointer to memory allocated.
    123  1.1  christos  */
    124  1.1  christos void *regional_alloc_init(struct regional* r, const void *init, size_t size);
    125  1.1  christos 
    126  1.1  christos /**
    127  1.1  christos  * Allocate size bytes of memory inside regional that are initialized to
    128  1.1  christos  * 0.  The memory is deallocated when region_free_all is called for
    129  1.1  christos  * this region.
    130  1.1  christos  * @param r: the region.
    131  1.1  christos  * @param size: number of bytes.
    132  1.1  christos  * @return: pointer to memory allocated.
    133  1.1  christos  */
    134  1.1  christos void *regional_alloc_zero(struct regional *r, size_t size);
    135  1.1  christos 
    136  1.1  christos /**
    137  1.1  christos  * Duplicate string and allocate the result in regional.
    138  1.1  christos  * @param r: the region.
    139  1.1  christos  * @param string: null terminated string.
    140  1.1  christos  * @return: pointer to memory allocated.
    141  1.1  christos  */
    142  1.1  christos char *regional_strdup(struct regional *r, const char *string);
    143  1.1  christos 
    144  1.1  christos /** Debug print regional statistics to log */
    145  1.1  christos void regional_log_stats(struct regional *r);
    146  1.1  christos 
    147  1.1  christos /** get total memory size in use by region */
    148  1.1  christos size_t regional_get_mem(struct regional* r);
    149  1.1  christos 
    150  1.1  christos #endif /* UTIL_REGIONAL_H_ */
    151