Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:30

0001 
0002 /*--------------------------------------------------------------------*/
0003 /*--- A pool (memory) allocator that avoids duplicated copies.     ---*/
0004 /*---                                    pub_tool_deduppoolalloc.h ---*/
0005 /*--------------------------------------------------------------------*/
0006 
0007 /*
0008    This file is part of Valgrind, a dynamic binary instrumentation
0009    framework.
0010 
0011    Copyright (C) 2014-2017 Philippe Waroquiers philippe.waroquiers@skynet.be
0012 
0013    This program is free software; you can redistribute it and/or
0014    modify it under the terms of the GNU General Public License as
0015    published by the Free Software Foundation; either version 2 of the
0016    License, or (at your option) any later version.
0017 
0018    This program is distributed in the hope that it will be useful, but
0019    WITHOUT ANY WARRANTY; without even the implied warranty of
0020    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0021    General Public License for more details.
0022 
0023    You should have received a copy of the GNU General Public License
0024    along with this program; if not, see <http://www.gnu.org/licenses/>.
0025 
0026    The GNU General Public License is contained in the file COPYING.
0027 */
0028 
0029 #ifndef __PUB_TOOL_DEDUPPOOLALLOC_H
0030 #define __PUB_TOOL_DEDUPPOOLALLOC_H
0031 
0032 #include "pub_tool_basics.h"   // UWord
0033 
0034 //-----------------------------------------------------------------------------
0035 // PURPOSE: Provides a pool allocator for elements, storing only once identical
0036 // elements. In other words, this can be considered a "dictionary" of elements.
0037 //
0038 // This pool allocator manages elements allocation by allocating "pools" of
0039 // many elements from a lower level allocator (typically pub_tool_mallocfree.h).
0040 // Single elements are allocated from these pools.
0041 // Currently, elements can only be allocated, elements cannot be freed
0042 // individually.
0043 // Once allocated, an element must not be modified anymore.
0044 //
0045 // Elements can be inserted in the pool using VG_(allocEltDedupPA),
0046 // VG_(allocFixedEltDedupPA) or VG_(allocStrDedupPA).
0047 //
0048 // Use VG_(allocFixedEltDedupPA) to allocate elements that are all of
0049 // the same size and that you want to identify with a (small) number:
0050 // VG_(allocFixedEltDedupPA) will assign a sequence number to each
0051 // unique allocated element. This unique number can be translated to
0052 // an address when the element data must be used.
0053 // The idea is that such small numbers can be used as reference instead
0054 // of the element address, to spare memory.
0055 // Elements are numbered starting from 1. The nr 0 can thus be used
0056 // as 'null element'. The address identified by a nr can change
0057 // if new elements are inserted in the pool. Once the pool is frozen,
0058 // an element address does not change.
0059 //
0060 // Use VG_(allocEltDedupPA) for variable size elements or when the
0061 // memory needed to store the element reference is not critical or
0062 // when performance to access elements is critical.
0063 // The address of an element allocated with VG_(allocEltDedupPA) does
0064 // not change, even if new elements are inserted in the pool.
0065 //
0066 // Use VG_(allocStrDedupPA) to create a pool of strings (in other words, a
0067 //  dictionnary of strings). Similarly to VG_(allocFixedEltDedupPA), strings
0068 // inserted in a dedup pool can be identified by an element number.
0069 //
0070 // In the same pool, you can only use one of the allocate element functions.
0071 // 
0072 // A dedup pool allocator has significantly less memory overhead than
0073 // calling directly pub_tool_mallocfree.h if the deduplication factor
0074 // is big. However, allocating an element incurs a cost for searching
0075 // if an identical element is already in the pool.
0076 //
0077 // Note: the elements of the pool cannot be freed (at least currently).
0078 // The only way to free the elements is to delete the dedup pool allocator.
0079 //--------------------------------------------------------------------
0080 
0081 
0082 typedef  struct _DedupPoolAlloc  DedupPoolAlloc;
0083 
0084 /* Create new DedupPoolAlloc, using given allocation and free function.
0085    alloc_fn must not return NULL (that is, if it returns it must have
0086    succeeded.)
0087    poolSzB is the (minimum) size in bytes of the pool of elements allocated
0088    with alloc. 
0089    eltAlign is the minimum required alignement for the elements allocated
0090    from the DedupPoolAlloc.
0091    This function never returns NULL. */
0092 extern DedupPoolAlloc* VG_(newDedupPA) ( SizeT  poolSzB,
0093                                          SizeT  eltAlign,
0094                                          Alloc_Fn_t alloc_fn,
0095                                          const  HChar* cc,
0096                                          Free_Fn_t free_fn );
0097 
0098 /* Allocates or retrieve element from ddpa with eltSzB bytes to store elt.
0099    This function never returns NULL.
0100    If ddpa already contains an element equal to elt, then the address of
0101    the already existing element is returned.
0102    Equality between elements is done by comparing all bytes.
0103    So, if void *elt points to a struct, be sure to initialise all components
0104    and the holes between components. */
0105 extern const void* VG_(allocEltDedupPA) (DedupPoolAlloc* ddpa,
0106                                          SizeT eltSzB, const void* elt);
0107 
0108 /* Allocates or retrieve a (fixed size) element from ddpa. Returns the
0109    unique number identifying this element.
0110    Similarly to VG_(allocEltDedupPA), this will return the unique number
0111    of an already existing identical element to elt. */
0112 extern UInt VG_(allocFixedEltDedupPA) (DedupPoolAlloc* ddpa,
0113                                        SizeT eltSzB, const void* elt);
0114 
0115 /* Translate an element number to its address. Note that the address
0116    corresponding to eltNr can change if new elements are inserted
0117    in the pool. */
0118 extern void* VG_(indexEltNumber) (DedupPoolAlloc* ddpa,
0119                                   UInt eltNr);
0120 
0121 /* Allocates or retrieve a string element from ddpa. Returns the
0122    unique number identifying this string.
0123    newStr is set to True if the str is a newly inserted string, False
0124    if the str was already present in the pool.
0125    Similarly to VG_(allocEltDedupPA), this will return the unique number
0126    of an already existing identical string. */
0127 extern UInt VG_(allocStrDedupPA) (DedupPoolAlloc *ddpa,
0128                                   const HChar* str,
0129                                   Bool* newStr);
0130 /* Note: Implementing a function to return the string value from its strNr
0131    implies some overhead, so will be done only if/when needed. */
0132 
0133 
0134 /* The Dedup Pool Allocator must maintain a data structure to avoid
0135    duplicates as long as new elements can be allocated from the pool.
0136    Once no new elements will be allocated, this dedup data structure
0137    can be released using VG_(freezeDedupPA). Once ddpa has been frozen,
0138    it is an error to call VG_(allocEltDedupPA) or VG_(allocFixedEltDedupPA).
0139    If shrink_block is not NULL, the last pool will be shrunk using
0140    shrink_block. */
0141 extern void VG_(freezeDedupPA) (DedupPoolAlloc* ddpa,
0142                                 void (*shrink_block)(void*, SizeT));
0143 
0144 /* How many (unique) elements are there in this ddpa now? */
0145 extern UInt VG_(sizeDedupPA) (DedupPoolAlloc* ddpa);
0146 
0147 /* Free all memory associated with a DedupPoolAlloc. */
0148 extern void VG_(deleteDedupPA) ( DedupPoolAlloc* ddpa);
0149 
0150 #endif   // __PUB_TOOL_DEDUPPOOLALLOC_
0151 
0152 /*--------------------------------------------------------------------*/
0153 /*--- end                               pub_tool_deduppoolalloc.h  ---*/
0154 /*--------------------------------------------------------------------*/