Twiddled some brace positions and removed a superfluous ';' in a macro.
[rsync/rsync.git] / lib / pool_alloc.c
CommitLineData
7efdcf32
S
1#include "rsync.h"
2
3#define POOL_DEF_EXTENT (32 * 1024)
4
5struct alloc_pool
6{
7 size_t size; /* extent size */
8 size_t quantum; /* allocation quantum */
9 struct pool_extent *live; /* current extent for
10 * allocations */
11 struct pool_extent *free; /* unfreed extent list */
12 void (*bomb)();
13 /* function to call if
14 * malloc fails */
15 int flags;
16
17 /* statistical data */
18 unsigned long e_created; /* extents created */
19 unsigned long e_freed; /* extents detroyed */
707415d4
WD
20 int64 n_allocated; /* calls to alloc */
21 int64 n_freed; /* calls to free */
22 int64 b_allocated; /* cum. bytes allocated */
23 int64 b_freed; /* cum. bytes freed */
7efdcf32
S
24};
25
26struct pool_extent
27{
28 void *start; /* starting address */
29 size_t free; /* free bytecount */
30 size_t bound; /* bytes bound by padding,
31 * overhead and freed */
32 struct pool_extent *next;
33};
34
4d4df3cd
WD
35struct align_test {
36 void *foo;
707415d4 37 int64 bar;
4d4df3cd
WD
38};
39
40#define MINALIGN offsetof(struct align_test, bar)
7efdcf32 41
71b291d7
WD
42/* Temporarily cast a void* var into a char* var when adding an offset (to
43 * keep some compilers from complaining about the pointer arithmetic). */
44#define PTR_ADD(b,o) ( (void*) ((char*)(b) + (o)) )
45
7efdcf32
S
46alloc_pool_t
47pool_create(size_t size, size_t quantum,
48 void (*bomb)(char *), int flags)
49{
50 struct alloc_pool *pool;
51
52 if (!(pool = (struct alloc_pool*) malloc(sizeof (struct alloc_pool))))
53 return pool;
54 memset(pool, 0, sizeof (struct alloc_pool));
55
56 pool->size = size /* round extent size to min alignment reqs */
57 ? (size + MINALIGN - 1) & ~(MINALIGN - 1)
58 : POOL_DEF_EXTENT;
8ea17b50 59 if (pool->flags & POOL_INTERN) {
7efdcf32
S
60 pool->size -= sizeof (struct pool_extent);
61 flags |= POOL_APPEND;
62 }
63 pool->quantum = quantum ? quantum : MINALIGN;
64 pool->bomb = bomb;
65 pool->flags = flags;
66
67 return pool;
68}
69
70void
71pool_destroy(alloc_pool_t p)
72{
73 struct alloc_pool *pool = (struct alloc_pool *) p;
74 struct pool_extent *cur, *next;
75
76 if (!pool)
77 return;
78
8ea17b50 79 if (pool->live) {
7efdcf32
S
80 cur = pool->live;
81 free(cur->start);
82 if (!(pool->flags & POOL_APPEND))
83 free(cur);
84 }
8ea17b50 85 for (cur = pool->free; cur; cur = next) {
7efdcf32
S
86 next = cur->next;
87 free(cur->start);
88 if (!(pool->flags & POOL_APPEND))
89 free(cur);
90 }
91 free(pool);
92}
93
be20dc34
WD
94void *
95pool_alloc(alloc_pool_t p, size_t len, char *bomb)
7efdcf32
S
96{
97 struct alloc_pool *pool = (struct alloc_pool *) p;
98 if (!pool)
15f85b1f 99 return NULL;
7efdcf32
S
100
101 if (!len)
102 len = pool->quantum;
103 else if (pool->quantum > 1 && len % pool->quantum)
104 len += pool->quantum - len % pool->quantum;
105
106 if (len > pool->size)
107 goto bomb;
108
8ea17b50 109 if (!pool->live || len > pool->live->free) {
7efdcf32
S
110 void *start;
111 size_t free;
112 size_t bound;
113 size_t sqew;
114 size_t asize;
115
8ea17b50 116 if (pool->live) {
7efdcf32
S
117 pool->live->next = pool->free;
118 pool->free = pool->live;
119 }
120
121 free = pool->size;
122 bound = 0;
123
124 asize = pool->size;
125 if (pool->flags & POOL_APPEND)
126 asize += sizeof (struct pool_extent);
127
128 if (!(start = (void *) malloc(asize)))
129 goto bomb;
130
131 if (pool->flags & POOL_CLEAR)
132 memset(start, 0, pool->size);
133
134 if (pool->flags & POOL_APPEND)
71b291d7 135 pool->live = PTR_ADD(start, free);
7efdcf32 136 else if (!(pool->live = (struct pool_extent *) malloc(sizeof (struct pool_extent))))
7efdcf32 137 goto bomb;
7efdcf32 138 if (pool->flags & POOL_QALIGN && pool->quantum > 1
8ea17b50 139 && (sqew = (size_t)PTR_ADD(start, free) % pool->quantum)) {
7efdcf32
S
140 bound += sqew;
141 free -= sqew;
142 }
143 pool->live->start = start;
144 pool->live->free = free;
145 pool->live->bound = bound;
146 pool->live->next = NULL;
147
148 pool->e_created++;
149 }
150
151 pool->n_allocated++;
152 pool->b_allocated += len;
153
154 pool->live->free -= len;
155
71b291d7 156 return PTR_ADD(pool->live->start, pool->live->free);
7efdcf32
S
157
158bomb:
159 if (pool->bomb)
160 (*pool->bomb)(bomb);
161 return NULL;
162}
163
164void
165pool_free(alloc_pool_t p, size_t len, void *addr)
166{
167 struct alloc_pool *pool = (struct alloc_pool *) p;
168 struct pool_extent *cur;
169 struct pool_extent *prev;
170
171 if (!pool)
172 return;
173
174 if (!len)
175 len = pool->quantum;
176 else if (pool->quantum > 1 && len % pool->quantum)
177 len += pool->quantum - len % pool->quantum;
178
8ea17b50 179 if (!addr && pool->live) {
7efdcf32
S
180 pool->live->next = pool->free;
181 pool->free = pool->live;
182 pool->live = NULL;
183 return;
184 }
185 pool->n_freed++;
186 pool->b_freed += len;
187
188 cur = pool->live;
8ea17b50
WD
189 if (cur && addr >= cur->start
190 && addr < PTR_ADD(cur->start, pool->size)) {
191 if (addr == PTR_ADD(cur->start, cur->free)) {
7efdcf32
S
192 if (pool->flags & POOL_CLEAR)
193 memset(addr, 0, len);
194 pool->b_freed += len;
8ea17b50 195 } else
7efdcf32 196 cur->bound += len;
8ea17b50 197 if (cur->free + cur->bound >= pool->size) {
7efdcf32
S
198 size_t sqew;
199
200 cur->free = pool->size;
201 cur->bound = 0;
202 if (pool->flags & POOL_QALIGN && pool->quantum > 1
8ea17b50 203 && (sqew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum)) {
7efdcf32
S
204 cur->bound += sqew;
205 cur->free -= sqew;
206 }
207 }
208 return;
209 }
8ea17b50 210 for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next) {
7efdcf32 211 if (addr >= cur->start
04575bca 212 && addr < PTR_ADD(cur->start, pool->size))
7efdcf32
S
213 break;
214 }
215 if (!cur)
216 return;
217
8ea17b50 218 if (prev) {
7efdcf32
S
219 prev->next = cur->next;
220 cur->next = pool->free;
221 pool->free = cur;
222 }
223 cur->bound += len;
224
8ea17b50 225 if (cur->free + cur->bound >= pool->size) {
7efdcf32
S
226 pool->free = cur->next;
227
228 free(cur->start);
229 if (!(pool->flags & POOL_APPEND))
230 free(cur);
231 pool->e_freed++;
232 }
233 return;
234}
235
236#define FDPRINT(label, value) \
8ea17b50
WD
237 snprintf(buf, sizeof buf, label, value), \
238 write(fd, buf, strlen(buf))
7efdcf32
S
239
240#define FDEXTSTAT(ext) \
8ea17b50 241 snprintf(buf, sizeof buf, " %12ld %5ld\n", \
7efdcf32
S
242 (long) ext->free, \
243 (long) ext->bound), \
244 write(fd, buf, strlen(buf))
245
246void
247pool_stats(alloc_pool_t p, int fd, int summarize)
248{
249 struct alloc_pool *pool = (struct alloc_pool *) p;
250 struct pool_extent *cur;
251 char buf[BUFSIZ];
252
253 if (!pool)
254 return;
255
256 FDPRINT(" Extent size: %12ld\n", (long) pool->size);
257 FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum);
258 FDPRINT(" Extents created: %12ld\n", pool->e_created);
259 FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
260 FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
261 FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
262 FDPRINT(" Alloc bytes: %12.0f\n", (double) pool->b_allocated);
263 FDPRINT(" Free bytes: %12.0f\n", (double) pool->b_freed);
264
265 if (summarize)
266 return;
267
268 if (!pool->live && !pool->free)
269 return;
270
271 write(fd, "\n", 1);
272
273 if (pool->live)
7efdcf32 274 FDEXTSTAT(pool->live);
7efdcf32
S
275 strcpy(buf, " FREE BOUND\n");
276 write(fd, buf, strlen(buf));
277
be20dc34 278 for (cur = pool->free; cur; cur = cur->next)
7efdcf32 279 FDEXTSTAT(cur);
7efdcf32 280}