include/hamt/hamt_pool.h

filter: Statementstmt 100% 35 / 35 Branchbran 79.1% 19 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.5% 54 / 59 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« HAMT.xs ↑ Summary include/hamt/hamt_sv.h »
1 #ifndef HAMT_POOL_H
2 #define HAMT_POOL_H
3  
4 /* Freed blocks kept by size, so the next allocation of that size is a pop
5 * and not a trip to the C library. An update frees about as many small
6 * blocks as it allocates, of the same few sizes, which is the case this is
7 * for.
8 *
9 * Sizes are rounded up to a multiple of sixteen bytes and each multiple has
10 * a list, linked through the first word of every free block. A list holds a
11 * fixed number of blocks at most, so a large structure being freed goes back
12 * to the C library and is not pinned here. A block too large for any list is
13 * allocated and freed directly.
14 *
15 * A pool is used by one thread. Nothing here locks.
16 *
17 * The caller must free a block with the size it allocated it with: that is
18 * how the block finds its list. */
19  
20 #ifndef HAMT_POOL_RAW_ALLOC
21 # define HAMT_POOL_RAW_ALLOC(bytes) malloc(bytes)
22 # define HAMT_POOL_RAW_FREE(p, bytes) free(p)
23 #endif
24  
25 #define HAMT_POOL_GRAIN 16
26 #define HAMT_POOL_CLASSES 20 /* lists for 16, 32, ... 304 bytes */
27 #ifndef HAMT_POOL_KEEP
28 # define HAMT_POOL_KEEP 128 /* blocks a list holds at most */
29 #endif
30  
31 typedef struct hamt_pool {
32 int open;
33 uint32_t n[HAMT_POOL_CLASSES];
34 void *head[HAMT_POOL_CLASSES];
35 } hamt_pool;
36  
37 #define HAMT_POOL_CLASS(bytes) (((size_t)(bytes) + HAMT_POOL_GRAIN - 1) / HAMT_POOL_GRAIN)
38  
39 34 static void hamt_pool_open(hamt_pool *pl) {
40 34 memset(pl, 0, sizeof(*pl));
41 34 pl->open = 1;
42 34 }
43  
44 /* `pl` may be NULL, for a caller that has no pool to use: the block is then
45 * allocated as a pooled one would be, so it may be freed into any pool. */
46 4105126 static void *hamt_pool_alloc(hamt_pool *pl, size_t bytes) {
47 4105126 size_t c = HAMT_POOL_CLASS(bytes);
48  
49 4105126 ▶ if (c == 0 || c >= HAMT_POOL_CLASSES) return HAMT_POOL_RAW_ALLOC(bytes); /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0 || c >= HAMT_POOL_CLASSES) return HAMT_POOL_RAW_ALLOC(bytes); /* # uncoverable condition left count:1 */ 4105126 0 4105126
if (c == 0 || c >= HAMT_POOL_CLASSES) return HAMT_POOL_RAW_ALLOC(bytes); /* # uncoverable condition left count:1 */ 8556 4096570 4105126
50 4096570 ▶ if (pl && pl->head[c]) {
Branch: 2 branches
BranchTrueFalseTotal
if (pl && pl->head[c]) { 4096570 0 4096570
if (pl && pl->head[c]) { 2352317 1744253 4096570
51 2352317 void *p = pl->head[c];
52 2352317 memcpy(&pl->head[c], p, sizeof(void *));
53 2352317 pl->n[c]--;
54 2352317 return p;
55 }
56 1744253 return HAMT_POOL_RAW_ALLOC(c * HAMT_POOL_GRAIN);
57 }
58  
59 4105126 static void hamt_pool_free(hamt_pool *pl, void *p, size_t bytes) {
60 4105126 size_t c = HAMT_POOL_CLASS(bytes);
61  
62 4105126 ▶ if (c == 0 || c >= HAMT_POOL_CLASSES) { HAMT_POOL_RAW_FREE(p, bytes); return; } /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0 || c >= HAMT_POOL_CLASSES) { HAMT_POOL_RAW_FREE(p, bytes); return; } /* # uncoverable condition left count:1 */ 4105126 0 4105126
if (c == 0 || c >= HAMT_POOL_CLASSES) { HAMT_POOL_RAW_FREE(p, bytes); return; } /* # uncoverable condition left count:1 */ 8556 4096570 4105126
63 4096570 ▶ if (pl && pl->open && pl->n[c] < HAMT_POOL_KEEP) { /* # uncoverable condition right count:2 */
Branch: 3 branches
BranchTrueFalseTotal
if (pl && pl->open && pl->n[c] < HAMT_POOL_KEEP) { /* # uncoverable condition right count:2 */ 4096570 0 4096570
if (pl && pl->open && pl->n[c] < HAMT_POOL_KEEP) { /* # uncoverable condition right count:2 */ 4096570 0 4096570
if (pl && pl->open && pl->n[c] < HAMT_POOL_KEEP) { /* # uncoverable condition right count:2 */ 2362991 1733579 4096570
64 2362991 memcpy(p, &pl->head[c], sizeof(void *));
65 2362991 pl->head[c] = p;
66 2362991 pl->n[c]++;
67 2362991 return;
68 }
69 1733579 HAMT_POOL_RAW_FREE(p, c * HAMT_POOL_GRAIN);
70 }
71  
72 /* Give everything back and keep nothing from here on. Blocks handed out
73 * before this are still freed through hamt_pool_free. */
74 34 static void hamt_pool_close(hamt_pool *pl) {
75 size_t c;
76 34 pl->open = 0;
77 714 ▶ for (c = 0; c < HAMT_POOL_CLASSES; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < HAMT_POOL_CLASSES; c++) { 680 34 714
78 11354 ▶ while (pl->head[c]) {
Branch: 1 branch
BranchTrueFalseTotal
while (pl->head[c]) { 10674 680 11354
79 10674 void *p = pl->head[c];
80 10674 memcpy(&pl->head[c], p, sizeof(void *));
81 10674 HAMT_POOL_RAW_FREE(p, c * HAMT_POOL_GRAIN);
82 }
83 680 pl->n[c] = 0;
84 }
85 34 }
86  
87 /* Blocks waiting in the lists. */
88 20005 static size_t hamt_pool_held(const hamt_pool *pl) {
89 20005 size_t c, n = 0;
90 420105 ▶ for (c = 0; c < HAMT_POOL_CLASSES; c++) n += pl->n[c];
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < HAMT_POOL_CLASSES; c++) n += pl->n[c]; 400100 20005 420105
91 20005 return n;
92 }
93  
94 #endif
« HAMT.xs ↑ Summary include/hamt/hamt_sv.h »