GCC Code Coverage Report


Directory: src/
File: src/standalone/shm_toc.c
Date: 2026-11-18 16:12:57
Exec Total Coverage
Lines: 44 53 83.0%
Functions: 6 6 100.0%
Branches: 14 20 70.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2026 Tiger Data, Inc.
3 * Licensed under the PostgreSQL License. See LICENSE for details.
4 *
5 * shm_toc_standalone.c - Keyed shared-region table over a heap arena
6 *
7 * Standalone implementation of the shm_toc API the parallel build uses. See
8 * vs_shm_toc.h. PG builds use PostgreSQL's shm_toc (backed by a DSM segment)
9 * instead, so this file is compiled only for standalone.
10 */
11
12 #ifdef VS_STANDALONE
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include "standalone/shm_toc.h"
18
19 struct shm_toc
20 {
21 uint64_t magic;
22 char *arena; /* caller-provided chunk arena */
23 size_t nbytes; /* arena capacity */
24 size_t allocated; /* bump offset into the arena */
25
26 /* Key -> region map (grown on insert; separate from the arena). */
27 uint64_t *keys;
28 void **ptrs;
29 size_t nkeys;
30 size_t cap;
31 };
32
33 size_t
34 110 shm_toc_estimate(shm_toc_estimator *e)
35 {
36 110 return e->space_for_chunks;
37 }
38
39 shm_toc *
40 116 shm_toc_create(uint64_t magic, void *address, size_t nbytes)
41 {
42 116 shm_toc *toc = malloc(sizeof(shm_toc));
43
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (toc == NULL)
45 {
46 ✗ fprintf(stderr, "shm_toc_create: out of memory\n");
47 ✗ abort();
48 }
49
50 116 toc->magic = magic;
51 116 toc->arena = (char *)address;
52 116 toc->nbytes = nbytes;
53 116 toc->allocated = 0;
54 116 toc->keys = NULL;
55 116 toc->ptrs = NULL;
56 116 toc->nkeys = 0;
57 116 toc->cap = 0;
58 116 return toc;
59 }
60
61 void *
62 1176 shm_toc_allocate(shm_toc *toc, size_t nbytes)
63 {
64 1176 size_t offset = VS_TOC_ALIGN(toc->allocated);
65 1176 size_t end = offset + VS_TOC_ALIGN(nbytes);
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1176 times.
1176 if (end > toc->nbytes)
68 {
69 ✗ fprintf(stderr,
70 "shm_toc_allocate: arena exhausted (need %zu, have %zu)\n",
71 end,
72 toc->nbytes);
73 ✗ abort();
74 }
75
76 1176 toc->allocated = end;
77 1176 return toc->arena + offset;
78 }
79
80 void
81 958 shm_toc_insert(shm_toc *toc, uint64_t key, void *address)
82 {
83
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 838 times.
958 if (toc->nkeys == toc->cap)
84 {
85
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 114 times.
120 size_t newcap = toc->cap == 0 ? 8 : toc->cap * 2;
86 120 uint64_t *nk = realloc(toc->keys, newcap * sizeof(uint64_t));
87 120 void **np = realloc(toc->ptrs, newcap * sizeof(void *));
88
89
2/4
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 120 times.
120 if (nk == NULL || np == NULL)
90 {
91 ✗ fprintf(stderr, "shm_toc_insert: out of memory\n");
92 ✗ abort();
93 }
94 120 toc->keys = nk;
95 120 toc->ptrs = np;
96 120 toc->cap = newcap;
97 }
98
99 958 toc->keys[toc->nkeys] = key;
100 958 toc->ptrs[toc->nkeys] = address;
101 958 toc->nkeys++;
102 958 }
103
104 void *
105 1474 shm_toc_lookup(shm_toc *toc, uint64_t key, bool noError)
106 {
107
2/2
✓ Branch 0 taken 9290 times.
✓ Branch 1 taken 2 times.
9292 for (size_t i = 0; i < toc->nkeys; i++)
108
2/2
✓ Branch 0 taken 1472 times.
✓ Branch 1 taken 7818 times.
9290 if (toc->keys[i] == key)
109 1472 return toc->ptrs[i];
110
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!noError)
112 {
113 ✗ fprintf(stderr,
114 "shm_toc_lookup: key %llu not found\n",
115 (unsigned long long)key);
116 ✗ abort();
117 }
118 2 return NULL;
119 }
120
121 void
122 116 vs_shm_toc_free(shm_toc *toc)
123 {
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (toc == NULL)
125 ✗ return;
126 116 free(toc->keys);
127 116 free(toc->ptrs);
128 116 free(toc);
129 }
130
131 #endif /* VS_STANDALONE */
132