2 Copyright (c) 2009 Genome Research Ltd.
3 Author: Rob Davies <rmd@sanger.ac.uk>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
16 Institute nor the names of its contributors may be used to endorse or promote
17 products derived from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "cram/pooled_alloc.h"
38 #include "cram/misc.h"
42 #define PSIZE 1024*1024
44 // credit to http://graphics.stanford.edu/~seander/bithacks.html
45 static int next_power_2(unsigned int v
) {
59 * Pool allocations are approx minimum of 1024*dsize or PSIZE.
60 * (Assumes we're not trying to use pools for >= 2Gb or more)
62 pool_alloc_t
*pool_create(size_t dsize
) {
65 if (NULL
== (p
= (pool_alloc_t
*)malloc(sizeof(*p
))))
68 /* Minimum size is a pointer, for free list */
69 dsize
= (dsize
+ sizeof(void *) - 1) & ~(sizeof(void *)-1);
70 if (dsize
< sizeof(void *))
71 dsize
= sizeof(void *);
73 p
->psize
= MIN(PSIZE
, next_power_2(p
->dsize
*1024));
82 static pool_t
*new_pool(pool_alloc_t
*p
) {
83 size_t n
= p
->psize
/ p
->dsize
;
86 pool
= realloc(p
->pools
, (p
->npools
+ 1) * sizeof(*p
->pools
));
87 if (NULL
== pool
) return NULL
;
89 pool
= &p
->pools
[p
->npools
];
91 pool
->pool
= malloc(n
* p
->dsize
);
92 if (NULL
== pool
->pool
) return NULL
;
101 void pool_destroy(pool_alloc_t
*p
) {
104 for (i
= 0; i
< p
->npools
; i
++) {
105 free(p
->pools
[i
].pool
);
111 void *pool_alloc(pool_alloc_t
*p
) {
115 /* Look on free list */
116 if (NULL
!= p
->free
) {
118 p
->free
= *((void **)p
->free
);
122 /* Look for space in the last pool */
124 pool
= &p
->pools
[p
->npools
- 1];
125 if (pool
->used
+ p
->dsize
< p
->psize
) {
126 ret
= ((char *) pool
->pool
) + pool
->used
;
127 pool
->used
+= p
->dsize
;
132 /* Need a new pool */
134 if (NULL
== pool
) return NULL
;
136 pool
->used
= p
->dsize
;
140 void pool_free(pool_alloc_t
*p
, void *ptr
) {
141 *(void **)ptr
= p
->free
;
155 pool_alloc_t
*p
= pool_create(sizeof(xyz
));
157 items
= (xyz
**)malloc(NP
* sizeof(*items
));
159 for (i
= 0; i
< NP
; i
++) {
160 item
= pool_alloc(p
);
167 for (i
= 0; i
< NP
; i
++) {
173 for (i
= 0; i
< NP
; i
++) {
174 item
= pool_alloc(p
);
176 item
->y
= 1000000+i
+1;
177 item
->z
= 1000000+i
+2;
180 for (i
= 0; i
< NP
; i
++) {
182 printf("%d\t%d\t%d\t%d\n", i
, item
->x
, item
->y
, item
->z
);