modified: myjupyterlab.sh
[GalaxyCodeBases.git] / c_cpp / lib / htslib / cram / pooled_alloc.c
blobb15f88edf710e4ea2ee7f56c11a7e1e60c36b9aa
1 /*
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.
31 #include <config.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <stdint.h>
37 #include "cram/pooled_alloc.h"
38 #include "cram/misc.h"
40 //#define TEST_MAIN
42 #define PSIZE 1024*1024
44 // credit to http://graphics.stanford.edu/~seander/bithacks.html
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++;
54 return v;
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)
62 pool_alloc_t *pool_create(size_t dsize) {
63 pool_alloc_t *p;
65 if (NULL == (p = (pool_alloc_t *)malloc(sizeof(*p))))
66 return NULL;
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));
75 p->npools = 0;
76 p->pools = NULL;
77 p->free = NULL;
79 return p;
82 static pool_t *new_pool(pool_alloc_t *p) {
83 size_t n = p->psize / p->dsize;
84 pool_t *pool;
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];
91 pool->pool = malloc(n * p->dsize);
92 if (NULL == pool->pool) return NULL;
94 pool->used = 0;
96 p->npools++;
98 return pool;
101 void pool_destroy(pool_alloc_t *p) {
102 size_t i;
104 for (i = 0; i < p->npools; i++) {
105 free(p->pools[i].pool);
107 free(p->pools);
108 free(p);
111 void *pool_alloc(pool_alloc_t *p) {
112 pool_t *pool;
113 void *ret;
115 /* Look on free list */
116 if (NULL != p->free) {
117 ret = p->free;
118 p->free = *((void **)p->free);
119 return ret;
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;
132 /* Need a new pool */
133 pool = new_pool(p);
134 if (NULL == pool) return NULL;
136 pool->used = p->dsize;
137 return pool->pool;
140 void pool_free(pool_alloc_t *p, void *ptr) {
141 *(void **)ptr = p->free;
142 p->free = ptr;
145 #ifdef TEST_MAIN
146 typedef struct {
147 int x, y, z;
148 } xyz;
150 #define NP 10000
151 int main(void) {
152 int i;
153 xyz *item;
154 xyz **items;
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);
161 item->x = i;
162 item->y = i+1;
163 item->z = i+2;
164 items[i] = item;
167 for (i = 0; i < NP; i++) {
168 item = items[i];
169 if (i % 3)
170 pool_free(p, item);
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;
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);
186 return 0;
188 #endif