Mercurial > repos > youngkim > ezbamqc
comparison ezBAMQC/src/htslib/cram/pooled_alloc.c @ 0:dfa3745e5fd8
Uploaded
author | youngkim |
---|---|
date | Thu, 24 Mar 2016 17:12:52 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:dfa3745e5fd8 |
---|---|
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 #ifdef HAVE_CONFIG_H | |
32 #include "io_lib_config.h" | |
33 #endif | |
34 | |
35 #include <stdlib.h> | |
36 #include <stdio.h> | |
37 #include <stdint.h> | |
38 | |
39 #include "cram/pooled_alloc.h" | |
40 | |
41 //#define TEST_MAIN | |
42 | |
43 #define PSIZE 1024*1024 | |
44 | |
45 pool_alloc_t *pool_create(size_t dsize) { | |
46 pool_alloc_t *p; | |
47 | |
48 if (NULL == (p = (pool_alloc_t *)malloc(sizeof(*p)))) | |
49 return NULL; | |
50 | |
51 /* Minimum size is a pointer, for free list */ | |
52 dsize = (dsize + sizeof(void *) - 1) & ~(sizeof(void *)-1); | |
53 if (dsize < sizeof(void *)) | |
54 dsize = sizeof(void *); | |
55 p->dsize = dsize; | |
56 | |
57 p->npools = 0; | |
58 p->pools = NULL; | |
59 p->free = NULL; | |
60 | |
61 return p; | |
62 } | |
63 | |
64 static pool_t *new_pool(pool_alloc_t *p) { | |
65 size_t n = PSIZE / p->dsize; | |
66 pool_t *pool; | |
67 | |
68 pool = realloc(p->pools, (p->npools + 1) * sizeof(*p->pools)); | |
69 if (NULL == pool) return NULL; | |
70 p->pools = pool; | |
71 pool = &p->pools[p->npools]; | |
72 | |
73 pool->pool = malloc(n * p->dsize); | |
74 if (NULL == pool->pool) return NULL; | |
75 | |
76 pool->used = 0; | |
77 | |
78 p->npools++; | |
79 | |
80 return pool; | |
81 } | |
82 | |
83 void pool_destroy(pool_alloc_t *p) { | |
84 size_t i; | |
85 | |
86 for (i = 0; i < p->npools; i++) { | |
87 free(p->pools[i].pool); | |
88 } | |
89 free(p->pools); | |
90 free(p); | |
91 } | |
92 | |
93 void *pool_alloc(pool_alloc_t *p) { | |
94 pool_t *pool; | |
95 void *ret; | |
96 | |
97 /* Look on free list */ | |
98 if (NULL != p->free) { | |
99 ret = p->free; | |
100 p->free = *((void **)p->free); | |
101 return ret; | |
102 } | |
103 | |
104 /* Look for space in the last pool */ | |
105 if (p->npools) { | |
106 pool = &p->pools[p->npools - 1]; | |
107 if (pool->used + p->dsize < PSIZE) { | |
108 ret = ((char *) pool->pool) + pool->used; | |
109 pool->used += p->dsize; | |
110 return ret; | |
111 } | |
112 } | |
113 | |
114 /* Need a new pool */ | |
115 pool = new_pool(p); | |
116 if (NULL == pool) return NULL; | |
117 | |
118 pool->used = p->dsize; | |
119 return pool->pool; | |
120 } | |
121 | |
122 void pool_free(pool_alloc_t *p, void *ptr) { | |
123 *(void **)ptr = p->free; | |
124 p->free = ptr; | |
125 } | |
126 | |
127 #ifdef TEST_MAIN | |
128 typedef struct { | |
129 int x, y, z; | |
130 } xyz; | |
131 | |
132 #define NP 10000 | |
133 int main(void) { | |
134 int i; | |
135 xyz *item; | |
136 xyz **items; | |
137 pool_alloc_t *p = pool_create(sizeof(xyz)); | |
138 | |
139 items = (xyz **)malloc(NP * sizeof(*items)); | |
140 | |
141 for (i = 0; i < NP; i++) { | |
142 item = pool_alloc(p); | |
143 item->x = i; | |
144 item->y = i+1; | |
145 item->z = i+2; | |
146 items[i] = item; | |
147 } | |
148 | |
149 for (i = 0; i < NP; i++) { | |
150 item = items[i]; | |
151 if (i % 3) | |
152 pool_free(p, item); | |
153 } | |
154 | |
155 for (i = 0; i < NP; i++) { | |
156 item = pool_alloc(p); | |
157 item->x = 1000000+i; | |
158 item->y = 1000000+i+1; | |
159 item->z = 1000000+i+2; | |
160 } | |
161 | |
162 for (i = 0; i < NP; i++) { | |
163 item = items[i]; | |
164 printf("%d\t%d\t%d\t%d\n", i, item->x, item->y, item->z); | |
165 pool_free(p, item); | |
166 } | |
167 | |
168 return 0; | |
169 } | |
170 #endif |