1 /*
2 Copyright (c) 2009 Genome Research Ltd.
3 Author: Rob Davies <rmd@sanger.ac.uk>
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
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.
14
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.
18
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.
29 */
30
31 #include <config.h>
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <stdint.h>
36
37 #include "cram/pooled_alloc.h"
38 #include "cram/misc.h"
39
40 //#define TEST_MAIN
41
42 #define PSIZE 1024*1024
43
44 // credit to http://graphics.stanford.edu/~seander/bithacks.html
next_power_2(unsigned int v)45 static int next_power_2(unsigned int v) {
46 v--;
47 v |= v >> 1;
48 v |= v >> 2;
49 v |= v >> 4;
50 v |= v >> 8;
51 v |= v >> 16;
52 v++;
53
54 return v;
55 }
56
57 /*
58 * Creates a pool.
59 * Pool allocations are approx minimum of 1024*dsize or PSIZE.
60 * (Assumes we're not trying to use pools for >= 2Gb or more)
61 */
pool_create(size_t dsize)62 pool_alloc_t *pool_create(size_t dsize) {
63 pool_alloc_t *p;
64
65 if (NULL == (p = (pool_alloc_t *)malloc(sizeof(*p))))
66 return NULL;
67
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 *);
72 p->dsize = dsize;
73 p->psize = MIN(PSIZE, next_power_2(p->dsize*1024));
74
75 p->npools = 0;
76 p->pools = NULL;
77 p->free = NULL;
78
79 return p;
80 }
81
new_pool(pool_alloc_t * p)82 static pool_t *new_pool(pool_alloc_t *p) {
83 size_t n = p->psize / p->dsize;
84 pool_t *pool;
85
86 pool = realloc(p->pools, (p->npools + 1) * sizeof(*p->pools));
87 if (NULL == pool) return NULL;
88 p->pools = pool;
89 pool = &p->pools[p->npools];
90
91 pool->pool = malloc(n * p->dsize);
92 if (NULL == pool->pool) return NULL;
93
94 pool->used = 0;
95
96 p->npools++;
97
98 return pool;
99 }
100
pool_destroy(pool_alloc_t * p)101 void pool_destroy(pool_alloc_t *p) {
102 size_t i;
103
104 for (i = 0; i < p->npools; i++) {
105 free(p->pools[i].pool);
106 }
107 free(p->pools);
108 free(p);
109 }
110
pool_alloc(pool_alloc_t * p)111 void *pool_alloc(pool_alloc_t *p) {
112 pool_t *pool;
113 void *ret;
114
115 /* Look on free list */
116 if (NULL != p->free) {
117 ret = p->free;
118 p->free = *((void **)p->free);
119 return ret;
120 }
121
122 /* Look for space in the last pool */
123 if (p->npools) {
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;
128 return ret;
129 }
130 }
131
132 /* Need a new pool */
133 pool = new_pool(p);
134 if (NULL == pool) return NULL;
135
136 pool->used = p->dsize;
137 return pool->pool;
138 }
139
pool_free(pool_alloc_t * p,void * ptr)140 void pool_free(pool_alloc_t *p, void *ptr) {
141 *(void **)ptr = p->free;
142 p->free = ptr;
143 }
144
145 #ifdef TEST_MAIN
146 typedef struct {
147 int x, y, z;
148 } xyz;
149
150 #define NP 10000
main(void)151 int main(void) {
152 int i;
153 xyz *item;
154 xyz **items;
155 pool_alloc_t *p = pool_create(sizeof(xyz));
156
157 items = (xyz **)malloc(NP * sizeof(*items));
158
159 for (i = 0; i < NP; i++) {
160 item = pool_alloc(p);
161 item->x = i;
162 item->y = i+1;
163 item->z = i+2;
164 items[i] = item;
165 }
166
167 for (i = 0; i < NP; i++) {
168 item = items[i];
169 if (i % 3)
170 pool_free(p, item);
171 }
172
173 for (i = 0; i < NP; i++) {
174 item = pool_alloc(p);
175 item->x = 1000000+i;
176 item->y = 1000000+i+1;
177 item->z = 1000000+i+2;
178 }
179
180 for (i = 0; i < NP; i++) {
181 item = items[i];
182 printf("%d\t%d\t%d\t%d\n", i, item->x, item->y, item->z);
183 pool_free(p, item);
184 }
185
186 return 0;
187 }
188 #endif
189