Mercurial > repos > dawe > srf2fastq
comparison srf2fastq/io_lib-1.12.2/io_lib/pooled_alloc.c @ 0:d901c9f41a6a default tip
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author | dawe |
---|---|
date | Tue, 07 Jun 2011 17:48:05 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d901c9f41a6a |
---|---|
1 #include <stdlib.h> | |
2 #include <stdio.h> | |
3 #include <stdint.h> | |
4 | |
5 #include "io_lib/pooled_alloc.h" | |
6 | |
7 //#define TEST_MAIN | |
8 | |
9 #define PSIZE 1024*1024 | |
10 | |
11 pool_alloc_t *pool_create(size_t dsize) { | |
12 pool_alloc_t *p; | |
13 | |
14 if (NULL == (p = (pool_alloc_t *)malloc(sizeof(*p)))) | |
15 return NULL; | |
16 | |
17 /* Minimum size is a pointer, for free list */ | |
18 dsize = (dsize + sizeof(void *) - 1) & ~(sizeof(void *)-1); | |
19 if (dsize < sizeof(void *)) | |
20 dsize = sizeof(void *); | |
21 p->dsize = dsize; | |
22 | |
23 p->npools = 0; | |
24 p->pools = NULL; | |
25 p->free = NULL; | |
26 | |
27 return p; | |
28 } | |
29 | |
30 static pool_t *new_pool(pool_alloc_t *p) { | |
31 size_t i, n = PSIZE / p->dsize; | |
32 pool_t *pool; | |
33 char *cp; | |
34 | |
35 pool = realloc(p->pools, (p->npools + 1) * sizeof(*p->pools)); | |
36 if (NULL == pool) return NULL; | |
37 p->pools = pool; | |
38 pool = &p->pools[p->npools]; | |
39 | |
40 pool->pool = malloc(n * p->dsize); | |
41 if (NULL == pool->pool) return NULL; | |
42 | |
43 pool->used = 0; | |
44 | |
45 p->npools++; | |
46 | |
47 return pool; | |
48 } | |
49 | |
50 void pool_destroy(pool_alloc_t *p) { | |
51 size_t i; | |
52 | |
53 for (i = 0; i < p->npools; i++) { | |
54 free(p->pools[i].pool); | |
55 } | |
56 free(p->pools); | |
57 free(p); | |
58 } | |
59 | |
60 void *pool_alloc(pool_alloc_t *p) { | |
61 size_t i; | |
62 pool_t *pool; | |
63 void *ret; | |
64 | |
65 /* Look on free list */ | |
66 if (NULL != p->free) { | |
67 ret = p->free; | |
68 p->free = *((void **)p->free); | |
69 return ret; | |
70 } | |
71 | |
72 /* Look for space in the last pool */ | |
73 if (p->npools) { | |
74 pool = &p->pools[p->npools - 1]; | |
75 if (pool->used + p->dsize < PSIZE) { | |
76 ret = ((char *) pool->pool) + pool->used; | |
77 pool->used += p->dsize; | |
78 return ret; | |
79 } | |
80 } | |
81 | |
82 /* Need a new pool */ | |
83 pool = new_pool(p); | |
84 if (NULL == pool) return NULL; | |
85 | |
86 pool->used = p->dsize; | |
87 return pool->pool; | |
88 } | |
89 | |
90 void pool_free(pool_alloc_t *p, void *ptr) { | |
91 size_t psize, i; | |
92 | |
93 *(void **)ptr = p->free; | |
94 p->free = ptr; | |
95 } | |
96 | |
97 #ifdef TEST_MAIN | |
98 typedef struct { | |
99 int x, y, z; | |
100 } xyz; | |
101 | |
102 #define NP 10000 | |
103 int main(void) { | |
104 int i; | |
105 xyz *item; | |
106 xyz **items; | |
107 pool_alloc_t *p = pool_create(sizeof(xyz)); | |
108 | |
109 items = (xyz **)malloc(NP * sizeof(*items)); | |
110 | |
111 for (i = 0; i < NP; i++) { | |
112 item = pool_alloc(p); | |
113 item->x = i; | |
114 item->y = i+1; | |
115 item->z = i+2; | |
116 items[i] = item; | |
117 } | |
118 | |
119 for (i = 0; i < NP; i++) { | |
120 item = items[i]; | |
121 if (i % 3) | |
122 pool_free(p, item); | |
123 } | |
124 | |
125 for (i = 0; i < NP; i++) { | |
126 item = pool_alloc(p); | |
127 item->x = 1000000+i; | |
128 item->y = 1000000+i+1; | |
129 item->z = 1000000+i+2; | |
130 } | |
131 | |
132 for (i = 0; i < NP; i++) { | |
133 item = items[i]; | |
134 printf("%d\t%d\t%d\t%d\n", i, item->x, item->y, item->z); | |
135 pool_free(p, item); | |
136 } | |
137 | |
138 return 0; | |
139 } | |
140 #endif |