blob: 544239de06e7158d99bb9595bfd37f662ea1d408 [file] [log] [blame]
philippe6643e962012-01-17 21:16:30 +00001/*------------------------------------------------------------------------*/
2/*--- A simple pool (memory) allocator implementation. m_poolalloc.c --- */
3/*------------------------------------------------------------------------*/
4/*
5 This file is part of Valgrind, a dynamic binary instrumentation
6 framework.
7
sewardj0f157dd2013-10-18 14:27:36 +00008 Copyright (C) 2011-2013 OpenWorks LLP info@open-works.co.uk,
philippe6643e962012-01-17 21:16:30 +00009 Philippe Waroquiers philippe.waroquiers@skynet.be
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 02111-1307, USA.
25
26 The GNU General Public License is contained in the file COPYING.
27*/
28
29#include "pub_core_basics.h"
30#include "pub_core_libcbase.h"
31#include "pub_core_libcassert.h"
florianc91f5842013-09-15 10:42:26 +000032#include "pub_core_xarray.h"
33#include "pub_core_poolalloc.h" /* self */
philippe6643e962012-01-17 21:16:30 +000034
35struct _PoolAlloc {
36 UWord nrRef; /* nr reference to this pool allocator */
37 UWord elemSzB; /* element size */
38 UWord nPerPool; /* # elems per pool */
florian6a650742014-09-14 22:44:53 +000039 void* (*alloc_fn)(const HChar*, SizeT); /* pool allocator */
40 const HChar* cc; /* pool allocator's cost centre */
41 void (*free_fn)(void*); /* pool allocator's free-er */
philippe6643e962012-01-17 21:16:30 +000042 /* XArray of void* (pointers to pools). The pools themselves.
43 Each element is a pointer to a block of size (elemSzB *
44 nPerPool) bytes. */
45 XArray* pools;
46 /* next free element. Is a pointer to an element in one of the
47 pools pointed to by .pools */
48 void* nextFree;
49};
50
51PoolAlloc* VG_(newPA) ( UWord elemSzB,
52 UWord nPerPool,
florian6a650742014-09-14 22:44:53 +000053 void* (*alloc_fn)(const HChar*, SizeT),
florian54fe2022012-10-27 23:07:42 +000054 const HChar* cc,
philippe6643e962012-01-17 21:16:30 +000055 void (*free_fn)(void*) )
56{
57 PoolAlloc* pa;
58 vg_assert(0 == (elemSzB % sizeof(UWord)));
59 vg_assert(elemSzB >= sizeof(UWord));
60 vg_assert(nPerPool >= 100); /* let's say */
florian6a650742014-09-14 22:44:53 +000061 vg_assert(alloc_fn);
philippe6643e962012-01-17 21:16:30 +000062 vg_assert(cc);
63 vg_assert(free_fn);
florian6a650742014-09-14 22:44:53 +000064 pa = alloc_fn(cc, sizeof(*pa));
philippe6643e962012-01-17 21:16:30 +000065 VG_(memset)(pa, 0, sizeof(*pa));
66 pa->nrRef = 0;
67 pa->elemSzB = elemSzB;
68 pa->nPerPool = nPerPool;
69 pa->pools = NULL;
florian6a650742014-09-14 22:44:53 +000070 pa->alloc_fn = alloc_fn;
philippe6643e962012-01-17 21:16:30 +000071 pa->cc = cc;
florian6a650742014-09-14 22:44:53 +000072 pa->free_fn = free_fn;
73 pa->pools = VG_(newXA)( alloc_fn, cc, free_fn, sizeof(void*) );
philippe6643e962012-01-17 21:16:30 +000074 pa->nextFree = NULL;
75 vg_assert(pa->pools);
76 return pa;
77}
78
79void VG_(deletePA) ( PoolAlloc* pa)
80{
81 Word i;
82 vg_assert(pa->nrRef == 0);
83 for (i = 0; i < VG_(sizeXA) (pa->pools); i++)
florian6a650742014-09-14 22:44:53 +000084 pa->free_fn (*(UWord **)VG_(indexXA) ( pa->pools, i ));
philippe6643e962012-01-17 21:16:30 +000085 VG_(deleteXA) (pa->pools);
florian6a650742014-09-14 22:44:53 +000086 pa->free_fn (pa);
philippe6643e962012-01-17 21:16:30 +000087}
88
89/* The freelist is empty. Allocate a new pool and put all the new
90 elements in it onto the freelist. */
91__attribute__((noinline))
92static void pal_add_new_pool ( PoolAlloc* pa )
93{
94 Word i;
95 UWord* pool;
96 vg_assert(pa);
97 vg_assert(pa->nextFree == NULL);
florian6a650742014-09-14 22:44:53 +000098 pool = pa->alloc_fn( pa->cc, pa->elemSzB * pa->nPerPool );
philippe6643e962012-01-17 21:16:30 +000099 /* extend the freelist through the new pool. Place the freelist
100 pointer in the first word of each element. That's why the
101 element size must be at least one word. */
102 for (i = pa->nPerPool-1; i >= 0; i--) {
103 UChar* elemC = ((UChar*)pool) + i * pa->elemSzB;
104 UWord* elem = (UWord*)elemC;
105 vg_assert(0 == (((UWord)elem) % sizeof(UWord)));
106 *elem = (UWord)pa->nextFree;
107 pa->nextFree = elem;
108 }
109 /* and add to our collection of pools */
110 VG_(addToXA)( pa->pools, &pool );
111}
112
113void* VG_(allocEltPA) ( PoolAlloc* pa)
114{
115 UWord* elem;
116 if (UNLIKELY(pa->nextFree == NULL)) {
117 pal_add_new_pool(pa);
118 }
119 elem = pa->nextFree;
120 pa->nextFree = (void*)*elem;
121 *elem = 0; /* unnecessary, but just to be on the safe side */
122 return elem;
123}
124
125void VG_(freeEltPA) ( PoolAlloc* pa, void* p)
126{
127 UWord* elem = (UWord*)p;
128 *elem = (UWord)pa->nextFree;
129 pa->nextFree = elem;
130}
131
132
133void VG_(addRefPA) ( PoolAlloc* pa)
134{
135 pa->nrRef++;
136}
137
bart6e074482012-01-18 08:12:16 +0000138UWord VG_(releasePA)(PoolAlloc* pa)
philippe6643e962012-01-17 21:16:30 +0000139{
bart6e074482012-01-18 08:12:16 +0000140 UWord nrRef;
141
philippe6643e962012-01-17 21:16:30 +0000142 vg_assert(pa->nrRef > 0);
bart6e074482012-01-18 08:12:16 +0000143 nrRef = --pa->nrRef;
144 if (nrRef == 0)
145 VG_(deletePA)(pa);
146 return nrRef;
philippe6643e962012-01-17 21:16:30 +0000147}