| 1 |
#include "rsync.h" |
| 2 |
|
| 3 |
#define POOL_DEF_EXTENT (32 * 1024) |
| 4 |
|
| 5 |
#define POOL_QALIGN_P2 (1<<16) |
| 6 |
|
| 7 |
struct alloc_pool |
| 8 |
{ |
| 9 |
size_t size; |
| 10 |
size_t quantum; |
| 11 |
struct pool_extent *extents; |
| 12 |
void (*bomb)(const char*, const char*, int); |
| 13 |
int flags; |
| 14 |
|
| 15 |
|
| 16 |
unsigned long e_created; |
| 17 |
unsigned long e_freed; |
| 18 |
int64 n_allocated; |
| 19 |
int64 n_freed; |
| 20 |
int64 b_allocated; |
| 21 |
int64 b_freed; |
| 22 |
}; |
| 23 |
|
| 24 |
struct pool_extent |
| 25 |
{ |
| 26 |
struct pool_extent *next; |
| 27 |
void *start; |
| 28 |
size_t free; |
| 29 |
size_t bound; |
| 30 |
}; |
| 31 |
|
| 32 |
struct align_test { |
| 33 |
uchar foo; |
| 34 |
union { |
| 35 |
int64 i; |
| 36 |
void *p; |
| 37 |
} bar; |
| 38 |
}; |
| 39 |
|
| 40 |
#define MINALIGN offsetof(struct align_test, bar) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#define PTR_ADD(b,o) ( (void*) ((char*)(b) + (o)) ) |
| 45 |
#define PTR_SUB(b,o) ( (void*) ((char*)(b) - (o)) ) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#if defined(__SANITIZE_ADDRESS__) |
| 56 |
# define POOL_ASAN 1 |
| 57 |
#elif defined(__has_feature) |
| 58 |
# if __has_feature(address_sanitizer) |
| 59 |
# define POOL_ASAN 1 |
| 60 |
# endif |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifdef POOL_ASAN |
| 64 |
# include <sanitizer/asan_interface.h> |
| 65 |
# define POOL_REDZONE 16 |
| 66 |
# define POOL_POISON(p,n) ASAN_POISON_MEMORY_REGION((p), (n)) |
| 67 |
# define POOL_UNPOISON(p,n) ASAN_UNPOISON_MEMORY_REGION((p), (n)) |
| 68 |
#else |
| 69 |
# define POOL_POISON(p,n) ((void)0) |
| 70 |
# define POOL_UNPOISON(p,n) ((void)0) |
| 71 |
#endif |
| 72 |
|
| 73 |
alloc_pool_t |
| 74 |
pool_create(size_t size, size_t quantum, void (*bomb)(const char*, const char*, int), int flags) |
| 75 |
{ |
| 76 |
struct alloc_pool *pool; |
| 77 |
|
| 78 |
if ((MINALIGN & (MINALIGN - 1)) != (0)) { |
| 79 |
if (bomb) |
| 80 |
(*bomb)("Compiler error: MINALIGN is not a power of 2", __FILE__, __LINE__); |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
if (!(pool = new0(struct alloc_pool))) |
| 85 |
return NULL; |
| 86 |
|
| 87 |
if (!size) |
| 88 |
size = POOL_DEF_EXTENT; |
| 89 |
if (!quantum) |
| 90 |
quantum = MINALIGN; |
| 91 |
|
| 92 |
if (flags & POOL_INTERN) { |
| 93 |
if (size <= sizeof (struct pool_extent)) |
| 94 |
size = quantum; |
| 95 |
else |
| 96 |
size -= sizeof (struct pool_extent); |
| 97 |
flags |= POOL_PREPEND; |
| 98 |
} |
| 99 |
|
| 100 |
if (quantum <= 1) |
| 101 |
flags = (flags | POOL_NO_QALIGN) & ~POOL_QALIGN_P2; |
| 102 |
else if (!(flags & POOL_NO_QALIGN)) { |
| 103 |
if (size % quantum) |
| 104 |
size += quantum - size % quantum; |
| 105 |
|
| 106 |
if (!(quantum & (quantum - 1))) |
| 107 |
flags |= POOL_QALIGN_P2; |
| 108 |
} |
| 109 |
|
| 110 |
pool->size = size; |
| 111 |
pool->quantum = quantum; |
| 112 |
pool->bomb = bomb; |
| 113 |
pool->flags = flags; |
| 114 |
|
| 115 |
return pool; |
| 116 |
} |
| 117 |
|
| 118 |
void |
| 119 |
pool_destroy(alloc_pool_t p) |
| 120 |
{ |
| 121 |
struct alloc_pool *pool = (struct alloc_pool *) p; |
| 122 |
struct pool_extent *cur, *next; |
| 123 |
|
| 124 |
if (!pool) |
| 125 |
return; |
| 126 |
|
| 127 |
for (cur = pool->extents; cur; cur = next) { |
| 128 |
next = cur->next; |
| 129 |
if (pool->flags & POOL_PREPEND) |
| 130 |
free(PTR_SUB(cur->start, sizeof (struct pool_extent))); |
| 131 |
else { |
| 132 |
free(cur->start); |
| 133 |
free(cur); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
free(pool); |
| 138 |
} |
| 139 |
|
| 140 |
void * |
| 141 |
pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg) |
| 142 |
{ |
| 143 |
struct alloc_pool *pool = (struct alloc_pool *) p; |
| 144 |
if (!pool) |
| 145 |
return NULL; |
| 146 |
|
| 147 |
if (!len) |
| 148 |
len = pool->quantum; |
| 149 |
else if (pool->flags & POOL_QALIGN_P2) { |
| 150 |
if (len & (pool->quantum - 1)) |
| 151 |
len += pool->quantum - (len & (pool->quantum - 1)); |
| 152 |
} else if (!(pool->flags & POOL_NO_QALIGN)) { |
| 153 |
if (len % pool->quantum) |
| 154 |
len += pool->quantum - len % pool->quantum; |
| 155 |
} |
| 156 |
|
| 157 |
if (len > pool->size) |
| 158 |
goto bomb_out; |
| 159 |
|
| 160 |
if (!pool->extents || len > pool->extents->free) { |
| 161 |
void *start; |
| 162 |
size_t asize; |
| 163 |
struct pool_extent *ext; |
| 164 |
|
| 165 |
asize = pool->size; |
| 166 |
if (pool->flags & POOL_PREPEND) |
| 167 |
asize += sizeof (struct pool_extent); |
| 168 |
|
| 169 |
if (!(start = new_array(char, asize))) |
| 170 |
goto bomb_out; |
| 171 |
|
| 172 |
if (pool->flags & POOL_CLEAR) |
| 173 |
memset(start, 0, asize); |
| 174 |
|
| 175 |
if (pool->flags & POOL_PREPEND) { |
| 176 |
ext = start; |
| 177 |
start = PTR_ADD(start, sizeof (struct pool_extent)); |
| 178 |
} else if (!(ext = new(struct pool_extent))) |
| 179 |
goto bomb_out; |
| 180 |
ext->start = start; |
| 181 |
ext->free = pool->size; |
| 182 |
ext->bound = 0; |
| 183 |
ext->next = pool->extents; |
| 184 |
pool->extents = ext; |
| 185 |
|
| 186 |
pool->e_created++; |
| 187 |
} |
| 188 |
|
| 189 |
pool->n_allocated++; |
| 190 |
pool->b_allocated += len; |
| 191 |
|
| 192 |
pool->extents->free -= len; |
| 193 |
|
| 194 |
{ |
| 195 |
void *ret = PTR_ADD(pool->extents->start, pool->extents->free); |
| 196 |
#ifdef POOL_ASAN |
| 197 |
size_t rz = pool->extents->free < POOL_REDZONE |
| 198 |
? pool->extents->free : POOL_REDZONE; |
| 199 |
if (rz) { |
| 200 |
pool->extents->free -= rz; |
| 201 |
POOL_POISON(PTR_ADD(pool->extents->start, pool->extents->free), rz); |
| 202 |
} |
| 203 |
#endif |
| 204 |
return ret; |
| 205 |
} |
| 206 |
|
| 207 |
bomb_out: |
| 208 |
if (pool->bomb) |
| 209 |
(*pool->bomb)(bomb_msg, __FILE__, __LINE__); |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
void |
| 217 |
pool_free(alloc_pool_t p, size_t len, void *addr) |
| 218 |
{ |
| 219 |
struct alloc_pool *pool = (struct alloc_pool *)p; |
| 220 |
struct pool_extent *cur, *prev; |
| 221 |
|
| 222 |
if (!pool) |
| 223 |
return; |
| 224 |
|
| 225 |
if (!addr) { |
| 226 |
|
| 227 |
if ((cur = pool->extents) != NULL && cur->free != pool->size) { |
| 228 |
cur->bound += cur->free; |
| 229 |
cur->free = 0; |
| 230 |
} |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
if (!len) |
| 235 |
len = pool->quantum; |
| 236 |
else if (pool->flags & POOL_QALIGN_P2) { |
| 237 |
if (len & (pool->quantum - 1)) |
| 238 |
len += pool->quantum - (len & (pool->quantum - 1)); |
| 239 |
} else if (!(pool->flags & POOL_NO_QALIGN)) { |
| 240 |
if (len % pool->quantum) |
| 241 |
len += pool->quantum - len % pool->quantum; |
| 242 |
} |
| 243 |
|
| 244 |
pool->n_freed++; |
| 245 |
pool->b_freed += len; |
| 246 |
|
| 247 |
for (prev = NULL, cur = pool->extents; cur; prev = cur, cur = cur->next) { |
| 248 |
if (addr >= cur->start |
| 249 |
&& addr < PTR_ADD(cur->start, pool->size)) |
| 250 |
break; |
| 251 |
} |
| 252 |
if (!cur) |
| 253 |
return; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
POOL_UNPOISON(cur->start, pool->size); |
| 258 |
|
| 259 |
if (!prev) { |
| 260 |
|
| 261 |
if (cur->free + cur->bound + len >= pool->size) { |
| 262 |
if (pool->flags & POOL_CLEAR) { |
| 263 |
memset(PTR_ADD(cur->start, cur->free), 0, |
| 264 |
pool->size - cur->free); |
| 265 |
} |
| 266 |
cur->free = pool->size; |
| 267 |
cur->bound = 0; |
| 268 |
} else if (addr == PTR_ADD(cur->start, cur->free)) { |
| 269 |
if (pool->flags & POOL_CLEAR) |
| 270 |
memset(addr, 0, len); |
| 271 |
cur->free += len; |
| 272 |
} else |
| 273 |
cur->bound += len; |
| 274 |
} else { |
| 275 |
cur->bound += len; |
| 276 |
|
| 277 |
if (cur->free + cur->bound >= pool->size) { |
| 278 |
prev->next = cur->next; |
| 279 |
if (pool->flags & POOL_PREPEND) |
| 280 |
free(PTR_SUB(cur->start, sizeof (struct pool_extent))); |
| 281 |
else { |
| 282 |
free(cur->start); |
| 283 |
free(cur); |
| 284 |
} |
| 285 |
pool->e_freed++; |
| 286 |
} else if (prev != pool->extents) { |
| 287 |
|
| 288 |
prev->next = cur->next; |
| 289 |
cur->next = pool->extents->next; |
| 290 |
pool->extents->next = cur; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
void |
| 300 |
pool_free_old(alloc_pool_t p, void *addr) |
| 301 |
{ |
| 302 |
struct alloc_pool *pool = (struct alloc_pool *)p; |
| 303 |
struct pool_extent *cur, *prev, *next; |
| 304 |
|
| 305 |
if (!pool || !addr) |
| 306 |
return; |
| 307 |
|
| 308 |
for (prev = NULL, cur = pool->extents; cur; prev = cur, cur = cur->next) { |
| 309 |
if (addr >= cur->start |
| 310 |
&& addr < PTR_ADD(cur->start, pool->size)) |
| 311 |
break; |
| 312 |
} |
| 313 |
if (!cur) |
| 314 |
return; |
| 315 |
|
| 316 |
POOL_UNPOISON(cur->start, pool->size); |
| 317 |
|
| 318 |
if (addr == PTR_ADD(cur->start, cur->free)) { |
| 319 |
if (prev) { |
| 320 |
prev->next = NULL; |
| 321 |
next = cur; |
| 322 |
} else { |
| 323 |
|
| 324 |
if (pool->flags & POOL_CLEAR) |
| 325 |
memset(addr, 0, pool->size - cur->free); |
| 326 |
cur->free = pool->size; |
| 327 |
cur->bound = 0; |
| 328 |
next = cur->next; |
| 329 |
cur->next = NULL; |
| 330 |
} |
| 331 |
} else { |
| 332 |
next = cur->next; |
| 333 |
cur->next = NULL; |
| 334 |
} |
| 335 |
|
| 336 |
while ((cur = next) != NULL) { |
| 337 |
next = cur->next; |
| 338 |
if (pool->flags & POOL_PREPEND) |
| 339 |
free(PTR_SUB(cur->start, sizeof (struct pool_extent))); |
| 340 |
else { |
| 341 |
free(cur->start); |
| 342 |
free(cur); |
| 343 |
} |
| 344 |
pool->e_freed++; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
void * |
| 353 |
pool_boundary(alloc_pool_t p, size_t len) |
| 354 |
{ |
| 355 |
struct alloc_pool *pool = (struct alloc_pool *)p; |
| 356 |
struct pool_extent *cur; |
| 357 |
|
| 358 |
if (!pool || !pool->extents) |
| 359 |
return NULL; |
| 360 |
|
| 361 |
cur = pool->extents; |
| 362 |
|
| 363 |
if (cur->free < len) { |
| 364 |
cur->bound += cur->free; |
| 365 |
cur->free = 0; |
| 366 |
} |
| 367 |
|
| 368 |
return PTR_ADD(cur->start, cur->free); |
| 369 |
} |
| 370 |
|
| 371 |
#define FDPRINT(label, value) \ |
| 372 |
do { \ |
| 373 |
int len = snprintf(buf, sizeof buf, label, value); \ |
| 374 |
if (write(fd, buf, len) != len) \ |
| 375 |
ret = -1; \ |
| 376 |
} while (0) |
| 377 |
|
| 378 |
#define FDEXTSTAT(ext) \ |
| 379 |
do { \ |
| 380 |
int len = snprintf(buf, sizeof buf, " %12ld %5ld\n", \ |
| 381 |
(long)ext->free, (long)ext->bound); \ |
| 382 |
if (write(fd, buf, len) != len) \ |
| 383 |
ret = -1; \ |
| 384 |
} while (0) |
| 385 |
|
| 386 |
int |
| 387 |
pool_stats(alloc_pool_t p, int fd, int summarize) |
| 388 |
{ |
| 389 |
struct alloc_pool *pool = (struct alloc_pool *) p; |
| 390 |
struct pool_extent *cur; |
| 391 |
char buf[BUFSIZ]; |
| 392 |
int ret = 0; |
| 393 |
|
| 394 |
if (!pool) |
| 395 |
return ret; |
| 396 |
|
| 397 |
FDPRINT(" Extent size: %12ld\n", (long) pool->size); |
| 398 |
FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum); |
| 399 |
FDPRINT(" Extents created: %12ld\n", pool->e_created); |
| 400 |
FDPRINT(" Extents freed: %12ld\n", pool->e_freed); |
| 401 |
FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated); |
| 402 |
FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed); |
| 403 |
FDPRINT(" Bytes allocated: %12.0f\n", (double) pool->b_allocated); |
| 404 |
FDPRINT(" Bytes freed: %12.0f\n", (double) pool->b_freed); |
| 405 |
|
| 406 |
if (summarize) |
| 407 |
return ret; |
| 408 |
|
| 409 |
if (!pool->extents) |
| 410 |
return ret; |
| 411 |
|
| 412 |
if (write(fd, "\n", 1) != 1) |
| 413 |
ret = -1; |
| 414 |
|
| 415 |
for (cur = pool->extents; cur; cur = cur->next) |
| 416 |
FDEXTSTAT(cur); |
| 417 |
|
| 418 |
return ret; |
| 419 |
} |