• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thoni56 / c-xrefactory / 1774

06 Apr 2026 08:55AM UTC coverage: 83.228% (+0.1%) from 83.117%
1774

push

travis-ci

thoni56
[tidy][unused] Remove more unused stuff after move to memory-is-truth

15676 of 18835 relevant lines covered (83.23%)

15599379.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.47
src/memory.c
1
#include "memory.h"
2

3
#include <stdlib.h>
4

5
#include "log.h"
6
#include "head.h"
7
#include "constants.h"
8
#include "proto.h"
9

10

11
jmp_buf memoryResizeJumpTarget;
12

13

14
/* Dynamic memory for cross-references and similar stuff */
15
Memory cxMemory={};
16

17
/* Static memory areas */
18
Memory ppmMemory;
19

20

21
/* This is used unless the fatalError function is set */
22
static void defaultFatalMemoryErrorHandler(int errorCode, char *message, int exitStatus, char *file, int line) {
×
23
    log_fatal("Error code: %d, Message: '%s' in file %s", errorCode, message, file);
×
24
    exit(exitStatus);
×
25
}
26

27
/* Inject the function to call when fatalErrors occur */
28
static void (*fatalMemoryError)(int errCode, char *mess, int exitStatus, char *file, int line) = defaultFatalMemoryErrorHandler;
29
void setFatalErrorHandlerForMemory(void (*function)(int errCode, char *mess, int exitStatus, char *file,
228✔
30
                                                    int line)) {
31
    fatalMemoryError = function;
228✔
32
}
228✔
33

34
/* Copy of a few defines from commons.h to avoid dependency on other stuff... */
35
#undef assert
36
#define assert(expr)                                                                                    \
37
    if (!(expr))                                                                                        \
38
        internalCheckFailForMemory(#expr, __FILE__, __LINE__)
39

40
/* Inject the function to call when assert() fails, a.k.a internalCheckFail() */
41
static void (*internalCheckFailForMemory)(char *expr, char *file, int line);
42
void setInternalCheckFailHandlerForMemory(void (*function)(char *expr, char *file, int line)) {
225✔
43
    internalCheckFailForMemory = function;
225✔
44
}
225✔
45

46
/* With this as a separate function it is possible to catch memory resize longjmps */
47
void memoryResized(Memory *memory) {
1✔
48
    log_info("Memory '%s' has been resized too %d", memory->name, memory->size);
1✔
49
    longjmp(memoryResizeJumpTarget,1);
1✔
50
}
51

52

53
/* *****************************************************************
54

55
   Memory - this memory type has a dynamic area allocated separately from the Memory
56
            struct in which allocation takes place by moving the index.
57

58
 */
59

60
void memoryInit(Memory *memory, char *name, bool (*overflowHandler)(int n), int size) {
15,197,311✔
61
    ENTER();
15,197,311✔
62
    log_debug("Init %s with new name '%s' and size %d", memory->name, name, size);
15,197,311✔
63
    memory->name = name;
15,197,311✔
64
    memory->overflowHandler = overflowHandler;
15,197,311✔
65
    memory->name = name;
15,197,311✔
66
    if (size > memory->size) {
15,197,311✔
67
        memory->area = realloc(memory->area, size);
1,493✔
68
        memory->size = size;
1,493✔
69
    }
70
    memory->index = 0;
15,197,311✔
71
    LEAVE();
15,197,311✔
72
}
15,197,311✔
73

74
void *memoryAllocc(Memory *memory, int count, size_t size) {
224,979,554✔
75
    void *pointer = &memory->area[memory->index];
224,979,554✔
76
    assert(size > 0);
77
    assert(count >= 0);
224,979,554✔
78

3✔
79
    if (memory->index+count*size > memory->size) {
1✔
80
        if (memory->overflowHandler != NULL && memory->overflowHandler(count))
81
            memoryResized(memory);
2✔
82
        else
83
            fatalMemoryError(ERR_NO_MEMORY, memory->name, EXIT_FAILURE, __FILE__, __LINE__);
224,979,553✔
84
    }
224,979,553✔
85
    memory->index += count*size;
3,300,536✔
86
    if (memory->index > memory->max)
87
        memory->max = memory->index;
224,979,553✔
88

89
    return pointer;
90
}
60,881,090✔
91

60,881,090✔
92
void *memoryAlloc(Memory *memory, size_t size) {
93
    return memoryAllocc(memory, 1, size);
94
}
85,030,086✔
95

85,030,086✔
96
static bool memoryHasEnoughSpaceFor(Memory *memory, size_t bytes) {
97
    return memory->index + bytes < memory->size;
98
}
83,019,140✔
99

83,019,140✔
100
bool memoryIsBetween(Memory *memory, void *pointer, int low, int high) {
101
    return pointer >= (void *)&memory->area[low] && pointer <= (void *)&memory->area[high];
102
}
71,454,482✔
103

71,454,482✔
104
static bool isInMemory(Memory *memory, void *pointer) {
105
    return memoryIsBetween(memory, pointer, 0, memory->index);
106
}
71,454,482✔
107

71,454,482✔
108
size_t memoryFreeUntil(Memory *memory, void *pointer) {
109
    assert(isInMemory(memory, pointer));
71,454,481✔
110
    /* Ensure we're not freeing beyond current allocations (marker must be <= current index) */
71,454,481✔
111
    int markerOffset = (char *)pointer - (char *)memory->area;
×
112
    if (markerOffset > memory->index) {
113
        log_fatal("Attempting to free '%s' arena until offset %d, but current index is only %d.",
×
114
                  memory->name, markerOffset, memory->index);
×
115
        log_fatal("This means the marker is beyond allocated memory - likely a marker from a different arena or corrupted.");
116
        assert(markerOffset <= memory->index);
71,454,481✔
117
    }
71,454,481✔
118
    int oldIndex = memory->index;
71,454,481✔
119
    memory->index = markerOffset;
120
    return oldIndex - memory->index;  // Amount freed (rolled back)
121
}
11,219,122✔
122

11,219,122✔
123
static bool memoryPointerIsFreed(Memory *memory, void *pointer) {
124
    return memoryIsBetween(memory, pointer, memory->index, memory->size);
125
}
126

109,450,030✔
127
/* Reallocates the most recently allocated area in 'memory' to be different size */
128
void *memoryRealloc(Memory *memory, void *pointer, size_t oldSize, size_t newSize) {
129
    /* Arena allocators can only resize the most recent allocation (top-of-stack).
130
     * If this fails, check if ppmFreeUntil() was called too late, freeing allocations
109,450,030✔
131
     * made AFTER the buffer being resized. The buffer must be at top-of-stack to grow. */
1✔
132
    if (pointer != &memory->area[memory->index-oldSize]) {
133
        log_fatal("Attempting to resize buffer %p (size=%zu) in '%s' arena, but it is not the most recent allocation.",
1✔
134
                  pointer, oldSize, memory->name);
135
        log_fatal("Expected buffer at %p (index=%d - oldSize=%zu = %d), but current top-of-stack is at %p (index=%d).",
136
                  &memory->area[memory->index-oldSize], memory->index, oldSize, memory->index - (int)oldSize,
1✔
137
                  &memory->area[memory->index], memory->index);
1✔
138
        log_fatal("This usually means allocations made after the buffer need to be freed first (e.g., move ppmFreeUntil() earlier).");
139
        assert(pointer == &memory->area[memory->index-oldSize]);
109,450,029✔
140
    }
109,450,029✔
141
    memory->index += newSize - oldSize;
142
    return pointer;
143
}
144

85,534,296✔
145
// Used by ppmReallocc()
85,534,296✔
146
static void *memoryReallocc(Memory *memory, void *pointer, int newCount, size_t size, int oldCount) {
147
    return memoryRealloc(memory, pointer, oldCount*size, newCount*size);
148
}
149

34,926,446✔
150
/* Preprocessor Macro Memory */
34,926,446✔
151
void *ppmAlloc(size_t size) {
152
    return memoryAlloc(&ppmMemory, size);
153
}
160,263,988✔
154

160,263,988✔
155
void *ppmAllocc(int count, size_t size) {
156
    return memoryAllocc(&ppmMemory, count, size);
157
}
85,534,296✔
158

85,534,296✔
159
void *ppmReallocc(void *pointer, int newCount, size_t size, int oldCount) {
160
    return memoryReallocc(&ppmMemory, pointer, newCount, size, oldCount);
161
}
47,641,021✔
162

47,641,021✔
163
int ppmFreeUntil(void *pointer) {
164
    return memoryFreeUntil(&ppmMemory, pointer);
165
}
11,219,122✔
166

11,219,122✔
167
bool ppmIsFreedPointer(void *pointer) {
168
    return memoryPointerIsFreed(&ppmMemory, pointer);
169
}
170

171

×
172
/* CX */
173
static int calculateNewSize(int n, int oldsize) {
×
174
    int oldfactor, factor, newsize;
×
175
    oldfactor = oldsize / CX_MEMORY_CHUNK_SIZE;
176
    factor = ((n > 1) ? (n - 1) : 0) / CX_MEMORY_CHUNK_SIZE + 1; // 1 no patience to wait // TODO: WTF?
×
177
    //& if (options.cxMemoryFactor>=1) factor *= options.cxMemoryFactor;
×
178
    factor += oldfactor;
×
179
    if (oldfactor * 2 > factor)
×
180
        factor = oldfactor * 2;
181
    newsize = factor * CX_MEMORY_CHUNK_SIZE;
×
182

183
    return newsize;
184
}
85,030,086✔
185

85,030,086✔
186
bool cxMemoryHasEnoughSpaceFor(size_t bytes) {
187
    return memoryHasEnoughSpaceFor(&cxMemory, bytes);
188
}
×
189

×
190
bool cxMemoryOverflowHandler(int n) {
×
191
    log_debug("Handling CX memory overflow with n=%d", n);
×
192
    int oldsize = cxMemory.size;
193
    int newsize = calculateNewSize(n, oldsize);
×
194

×
195
    memoryInit(&cxMemory, "cxMemory", cxMemoryOverflowHandler, newsize);
196
    log_debug("Reallocating cxMemory: %d -> %d", oldsize, newsize);
×
197

198
    return cxMemory.area != NULL;
199
}
229✔
200

229✔
201
void initCxMemory(size_t size) {
229✔
202
    memoryInit(&cxMemory, "cxMemory", cxMemoryOverflowHandler, size);
203
}
3,826,757✔
204

3,826,757✔
205
void *cxAlloc(size_t size) {
206
    return memoryAllocc(&cxMemory, size, 1);
207
}
28,653✔
208

28,653✔
209
bool cxMemoryPointerIsBetween(void *pointer, int low, int high) {
210
    return memoryIsBetween(&cxMemory, pointer, low, high);
211
}
105✔
212

105✔
213
void cxFreeUntil(void *pointer) {
105✔
214
    (void)memoryFreeUntil(&cxMemory, pointer);
215
}
×
216

×
217
void printMemoryStatisticsFor(Memory *memory) {
218
    printf("Max memory use for %s : %d\n", memory->name, memory->max);
219
}
×
220

×
221
void printMemoryStatistics(void) {
×
222
    printMemoryStatisticsFor(&cxMemory);
223
    printMemoryStatisticsFor(&ppmMemory);
224
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc