1 /*------------------------------------------------------------------------*/
2 /*--- A simple pool (memory) allocator implementation. m_poolalloc.c --- */
3 /*------------------------------------------------------------------------*/
5 This file is part of Valgrind, a dynamic binary instrumentation
8 Copyright (C) 2011-2017 OpenWorks LLP info@open-works.co.uk,
9 Philippe Waroquiers philippe.waroquiers@skynet.be
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.
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.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 The GNU General Public License is contained in the file COPYING.
27 #include "pub_core_basics.h"
28 #include "pub_core_libcbase.h"
29 #include "pub_core_libcassert.h"
30 #include "pub_core_xarray.h"
31 #include "pub_core_poolalloc.h" /* self */
34 UWord nrRef
; /* nr reference to this pool allocator */
35 UWord elemSzB
; /* element size */
36 UWord nPerPool
; /* # elems per pool */
37 Alloc_Fn_t alloc_fn
; /* pool allocator */
38 const HChar
* cc
; /* pool allocator's cost centre */
39 Free_Fn_t free_fn
; /* pool allocator's free-er */
40 /* XArray of void* (pointers to pools). The pools themselves.
41 Each element is a pointer to a block of size (elemSzB *
44 /* next free element. Is a pointer to an element in one of the
45 pools pointed to by .pools */
49 PoolAlloc
* VG_(newPA
) ( UWord elemSzB
,
56 vg_assert(0 == (elemSzB
% sizeof(UWord
)));
57 vg_assert(elemSzB
>= sizeof(UWord
));
58 vg_assert(nPerPool
>= 100); /* let's say */
62 pa
= alloc_fn(cc
, sizeof(*pa
));
63 VG_(memset
)(pa
, 0, sizeof(*pa
));
65 pa
->elemSzB
= elemSzB
;
66 pa
->nPerPool
= nPerPool
;
68 pa
->alloc_fn
= alloc_fn
;
70 pa
->free_fn
= free_fn
;
71 pa
->pools
= VG_(newXA
)( alloc_fn
, cc
, free_fn
, sizeof(void*) );
77 void VG_(deletePA
) ( PoolAlloc
* pa
)
80 vg_assert(pa
->nrRef
== 0);
81 for (i
= 0; i
< VG_(sizeXA
) (pa
->pools
); i
++)
82 pa
->free_fn (*(UWord
**)VG_(indexXA
) ( pa
->pools
, i
));
83 VG_(deleteXA
) (pa
->pools
);
87 /* The freelist is empty. Allocate a new pool and put all the new
88 elements in it onto the freelist. */
89 __attribute__((noinline
))
90 static void pal_add_new_pool ( PoolAlloc
* pa
)
95 vg_assert(pa
->nextFree
== NULL
);
96 pool
= pa
->alloc_fn( pa
->cc
, pa
->elemSzB
* pa
->nPerPool
);
97 /* extend the freelist through the new pool. Place the freelist
98 pointer in the first word of each element. That's why the
99 element size must be at least one word. */
100 for (i
= pa
->nPerPool
-1; i
>= 0; i
--) {
101 UChar
* elemC
= ((UChar
*)pool
) + i
* pa
->elemSzB
;
102 UWord
* elem
= (UWord
*)elemC
;
103 vg_assert(0 == (((UWord
)elem
) % sizeof(UWord
)));
104 *elem
= (UWord
)pa
->nextFree
;
107 /* and add to our collection of pools */
108 VG_(addToXA
)( pa
->pools
, &pool
);
111 UWord
VG_(sizePA
) ( PoolAlloc
* pa
)
114 return pa
->nPerPool
* VG_(sizeXA
) (pa
->pools
);
117 void* VG_(allocEltPA
) ( PoolAlloc
* pa
)
120 if (UNLIKELY(pa
->nextFree
== NULL
)) {
121 pal_add_new_pool(pa
);
124 pa
->nextFree
= (void*)*elem
;
125 *elem
= 0; /* unnecessary, but just to be on the safe side */
129 void VG_(freeEltPA
) ( PoolAlloc
* pa
, void* p
)
131 UWord
* elem
= (UWord
*)p
;
132 *elem
= (UWord
)pa
->nextFree
;
137 void VG_(addRefPA
) ( PoolAlloc
* pa
)
142 UWord
VG_(releasePA
)(PoolAlloc
* pa
)
146 vg_assert(pa
->nrRef
> 0);