annotate ezBAMQC/src/htslib/cram/pooled_alloc.c @ 0:dfa3745e5fd8

Uploaded
author youngkim
date Thu, 24 Mar 2016 17:12:52 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2 Copyright (c) 2009 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Author: Rob Davies <rmd@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 modification, are permitted provided that the following conditions are met:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 1. Redistributions of source code must retain the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 this list of conditions and the following disclaimer.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 this list of conditions and the following disclaimer in the documentation
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 and/or other materials provided with the distribution.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 Institute nor the names of its contributors may be used to endorse or promote
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 products derived from this software without specific prior written permission.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 #ifdef HAVE_CONFIG_H
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 #include "io_lib_config.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 #include <stdlib.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37 #include <stdint.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 #include "cram/pooled_alloc.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 //#define TEST_MAIN
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43 #define PSIZE 1024*1024
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 pool_alloc_t *pool_create(size_t dsize) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 pool_alloc_t *p;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 if (NULL == (p = (pool_alloc_t *)malloc(sizeof(*p))))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 /* Minimum size is a pointer, for free list */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 dsize = (dsize + sizeof(void *) - 1) & ~(sizeof(void *)-1);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 if (dsize < sizeof(void *))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 dsize = sizeof(void *);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 p->dsize = dsize;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 p->npools = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 p->pools = NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 p->free = NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 return p;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 static pool_t *new_pool(pool_alloc_t *p) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 size_t n = PSIZE / p->dsize;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 pool_t *pool;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 pool = realloc(p->pools, (p->npools + 1) * sizeof(*p->pools));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 if (NULL == pool) return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 p->pools = pool;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 pool = &p->pools[p->npools];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 pool->pool = malloc(n * p->dsize);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 if (NULL == pool->pool) return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 pool->used = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 p->npools++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 return pool;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 void pool_destroy(pool_alloc_t *p) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 size_t i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 for (i = 0; i < p->npools; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 free(p->pools[i].pool);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 free(p->pools);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 free(p);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 void *pool_alloc(pool_alloc_t *p) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 pool_t *pool;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 void *ret;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 /* Look on free list */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98 if (NULL != p->free) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 ret = p->free;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 p->free = *((void **)p->free);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 return ret;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 /* Look for space in the last pool */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 if (p->npools) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 pool = &p->pools[p->npools - 1];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 if (pool->used + p->dsize < PSIZE) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 ret = ((char *) pool->pool) + pool->used;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 pool->used += p->dsize;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 return ret;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 /* Need a new pool */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 pool = new_pool(p);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 if (NULL == pool) return NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 pool->used = p->dsize;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 return pool->pool;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 void pool_free(pool_alloc_t *p, void *ptr) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 *(void **)ptr = p->free;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 p->free = ptr;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 #ifdef TEST_MAIN
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 typedef struct {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 int x, y, z;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 } xyz;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 #define NP 10000
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 int main(void) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 int i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 xyz *item;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 xyz **items;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 pool_alloc_t *p = pool_create(sizeof(xyz));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 items = (xyz **)malloc(NP * sizeof(*items));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 for (i = 0; i < NP; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 item = pool_alloc(p);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 item->x = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 item->y = i+1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145 item->z = i+2;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 items[i] = item;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 for (i = 0; i < NP; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 item = items[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 if (i % 3)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 pool_free(p, item);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 for (i = 0; i < NP; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 item = pool_alloc(p);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 item->x = 1000000+i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 item->y = 1000000+i+1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 item->z = 1000000+i+2;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 for (i = 0; i < NP; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 item = items[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 printf("%d\t%d\t%d\t%d\n", i, item->x, item->y, item->z);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 pool_free(p, item);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170 #endif