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, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 The GNU General Public License is contained in the file COPYING.
29 #include "pub_core_basics.h"
30 #include "pub_core_libcbase.h"
31 #include "pub_core_libcassert.h"
32 #include "pub_core_xarray.h"
33 #include "pub_core_poolalloc.h" /* self */
36 UWord nrRef
; /* nr reference to this pool allocator */
37 UWord elemSzB
; /* element size */
38 UWord nPerPool
; /* # elems per pool */
39 Alloc_Fn_t alloc_fn
; /* pool allocator */
40 const HChar
* cc
; /* pool allocator's cost centre */
41 Free_Fn_t free_fn
; /* pool allocator's free-er */
42 /* XArray of void* (pointers to pools). The pools themselves.
43 Each element is a pointer to a block of size (elemSzB *
46 /* next free element. Is a pointer to an element in one of the
47 pools pointed to by .pools */
51 PoolAlloc
* VG_(newPA
) ( UWord elemSzB
,
58 vg_assert(0 == (elemSzB
% sizeof(UWord
)));
59 vg_assert(elemSzB
>= sizeof(UWord
));
60 vg_assert(nPerPool
>= 100); /* let's say */
64 pa
= alloc_fn(cc
, sizeof(*pa
));
65 VG_(memset
)(pa
, 0, sizeof(*pa
));
67 pa
->elemSzB
= elemSzB
;
68 pa
->nPerPool
= nPerPool
;
70 pa
->alloc_fn
= alloc_fn
;
72 pa
->free_fn
= free_fn
;
73 pa
->pools
= VG_(newXA
)( alloc_fn
, cc
, free_fn
, sizeof(void*) );
79 void VG_(deletePA
) ( PoolAlloc
* pa
)
82 vg_assert(pa
->nrRef
== 0);
83 for (i
= 0; i
< VG_(sizeXA
) (pa
->pools
); i
++)
84 pa
->free_fn (*(UWord
**)VG_(indexXA
) ( pa
->pools
, i
));
85 VG_(deleteXA
) (pa
->pools
);
89 /* The freelist is empty. Allocate a new pool and put all the new
90 elements in it onto the freelist. */
91 __attribute__((noinline
))
92 static void pal_add_new_pool ( PoolAlloc
* pa
)
97 vg_assert(pa
->nextFree
== NULL
);
98 pool
= pa
->alloc_fn( pa
->cc
, pa
->elemSzB
* pa
->nPerPool
);
99 /* 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
;
109 /* and add to our collection of pools */
110 VG_(addToXA
)( pa
->pools
, &pool
);
113 UWord
VG_(sizePA
) ( PoolAlloc
* pa
)
116 return pa
->nPerPool
* VG_(sizeXA
) (pa
->pools
);
119 void* VG_(allocEltPA
) ( PoolAlloc
* pa
)
122 if (UNLIKELY(pa
->nextFree
== NULL
)) {
123 pal_add_new_pool(pa
);
126 pa
->nextFree
= (void*)*elem
;
127 *elem
= 0; /* unnecessary, but just to be on the safe side */
131 void VG_(freeEltPA
) ( PoolAlloc
* pa
, void* p
)
133 UWord
* elem
= (UWord
*)p
;
134 *elem
= (UWord
)pa
->nextFree
;
139 void VG_(addRefPA
) ( PoolAlloc
* pa
)
144 UWord
VG_(releasePA
)(PoolAlloc
* pa
)
148 vg_assert(pa
->nrRef
> 0);