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

thoni56 / c-xrefactory / 1580

23 Feb 2025 08:05PM UTC coverage: 80.875% (-0.1%) from 80.976%
1580

push

travis-ci

thoni56
[memory] Scratch attempt with FlushableMemory, go for removing caching instead...

12538 of 15503 relevant lines covered (80.87%)

9568334.45 hits per line

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

76.53
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,
119✔
30
                                                    int line)) {
31
    fatalMemoryError = function;
119✔
32
}
119✔
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)) {
116✔
43
    internalCheckFailForMemory = function;
116✔
44
}
116✔
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) {
8,156,473✔
61
    ENTER();
8,156,473✔
62
    memory->name = name;
8,156,473✔
63
    memory->overflowHandler = overflowHandler;
8,156,473✔
64
    memory->name = name;
8,156,473✔
65
    if (size > memory->size) {
8,156,473✔
66
        memory->area = realloc(memory->area, size);
724✔
67
        memory->size = size;
724✔
68
    }
69
    memory->index = 0;
8,156,473✔
70
    LEAVE();
8,156,473✔
71
}
8,156,473✔
72

73
void *memoryAllocc(Memory *memory, int count, size_t size) {
65,884,303✔
74
    void *pointer = &memory->area[memory->index];
65,884,303✔
75
    assert(size > 0);
76
    assert(count >= 0);
65,884,303✔
77

3✔
78
    if (memory->index+count*size > memory->size) {
1✔
79
        if (memory->overflowHandler != NULL && memory->overflowHandler(count))
80
            memoryResized(memory);
2✔
81
        else
82
            fatalMemoryError(ERR_NO_MEMORY, memory->name, XREF_EXIT_ERR, __FILE__, __LINE__);
65,884,302✔
83
    }
65,884,302✔
84
    memory->index += count*size;
2,287,196✔
85
    if (memory->index > memory->max)
86
        memory->max = memory->index;
65,884,302✔
87

88
    return pointer;
89
}
25,622,084✔
90

25,622,084✔
91
void *memoryAlloc(Memory *memory, size_t size) {
92
    return memoryAllocc(memory, 1, size);
93
}
31,047,644✔
94

31,047,644✔
95
static bool memoryHasEnoughSpaceFor(Memory *memory, size_t bytes) {
96
    return memory->index + bytes < memory->size;
97
}
20,650,893✔
98

20,650,893✔
99
bool memoryIsBetween(Memory *memory, void *pointer, int low, int high) {
100
    return pointer >= (void *)&memory->area[low] && pointer <= (void *)&memory->area[high];
101
}
13,517,272✔
102

13,517,272✔
103
static bool isInMemory(Memory *memory, void *pointer) {
104
    return memoryIsBetween(memory, pointer, 0, memory->index);
105
}
13,517,272✔
106

13,517,272✔
107
void memoryFreeUntil(Memory *memory, void *pointer) {
13,517,271✔
108
    assert(isInMemory(memory, pointer));
13,517,271✔
109
    memory->index = (char *)pointer - (char *)memory->area;
110
}
6,767,900✔
111

6,767,900✔
112
static bool memoryPointerIsFreed(Memory *memory, void *pointer) {
113
    return memoryIsBetween(memory, pointer, memory->index, memory->size);
114
}
115

27,118,607✔
116
/* Reallocates the most recently allocated area in 'memory' to be different size */
27,118,607✔
117
void *memoryRealloc(Memory *memory, void *pointer, size_t oldSize, size_t newSize) {
27,118,606✔
118
    assert(pointer == &memory->area[memory->index-oldSize]);
27,118,606✔
119
    memory->index += newSize - oldSize;
120
    return pointer;
121
}
122

20,298,922✔
123
// Used by ppmReallocc()
20,298,922✔
124
static void *memoryReallocc(Memory *memory, void *pointer, int newCount, size_t size, int oldCount) {
125
    return memoryRealloc(memory, pointer, oldCount*size, newCount*size);
126
}
127

17,904,696✔
128
/* Preprocessor Macro Memory */
17,904,696✔
129
void *ppmAlloc(size_t size) {
130
    return memoryAlloc(&ppmMemory, size);
131
}
37,336,974✔
132

37,336,974✔
133
void *ppmAllocc(int count, size_t size) {
134
    return memoryAllocc(&ppmMemory, count, size);
135
}
20,298,922✔
136

20,298,922✔
137
void *ppmReallocc(void *pointer, int newCount, size_t size, int oldCount) {
138
    return memoryReallocc(&ppmMemory, pointer, newCount, size, oldCount);
139
}
6,759,283✔
140

6,759,283✔
141
void ppmFreeUntil(void *pointer) {
6,759,283✔
142
    memoryFreeUntil(&ppmMemory, pointer);
143
}
6,767,900✔
144

6,767,900✔
145
bool ppmIsFreedPointer(void *pointer) {
146
    return memoryPointerIsFreed(&ppmMemory, pointer);
147
}
148

149

×
150
/* CX */
151
static int calculateNewSize(int n, int oldsize) {
×
152
    int oldfactor, factor, newsize;
×
153
    oldfactor = oldsize / CX_MEMORY_CHUNK_SIZE;
154
    factor = ((n > 1) ? (n - 1) : 0) / CX_MEMORY_CHUNK_SIZE + 1; // 1 no patience to wait // TODO: WTF?
×
155
    //& if (options.cxMemoryFactor>=1) factor *= options.cxMemoryFactor;
×
156
    factor += oldfactor;
×
157
    if (oldfactor * 2 > factor)
×
158
        factor = oldfactor * 2;
159
    newsize = factor * CX_MEMORY_CHUNK_SIZE;
×
160

161
    return newsize;
162
}
31,047,644✔
163

31,047,644✔
164
bool cxMemoryHasEnoughSpaceFor(size_t bytes) {
165
    return memoryHasEnoughSpaceFor(&cxMemory, bytes);
166
}
×
167

×
168
bool cxMemoryOverflowHandler(int n) {
×
169
    log_trace("Handling CX memory overflow with n=%d", n);
×
170
    int oldsize = cxMemory.size;
171
    int newsize = calculateNewSize(n, oldsize);
×
172

×
173
    memoryInit(&cxMemory, "cxMemory", cxMemoryOverflowHandler, newsize);
174
    log_debug("Reallocating cxMemory: %d -> %d", oldsize, newsize);
×
175

176
    return cxMemory.area != NULL;
177
}
114✔
178

114✔
179
void initCxMemory(size_t size) {
114✔
180
    memoryInit(&cxMemory, "cxMemory", cxMemoryOverflowHandler, size);
181
}
2,924,159✔
182

2,924,159✔
183
void *cxAlloc(size_t size) {
184
    return memoryAllocc(&cxMemory, size, 1);
185
}
533✔
186

533✔
187
bool cxMemoryPointerIsBetween(void *pointer, int low, int high) {
188
    return memoryIsBetween(&cxMemory, pointer, low, high);
189
}
161✔
190

161✔
191
bool cxMemoryIsFreed(void *pointer) {
192
    return memoryIsBetween(&cxMemory, pointer, cxMemory.index, cxMemory.size);
193
}
243✔
194

243✔
195
void cxFreeUntil(void *pointer) {
243✔
196
    memoryFreeUntil(&cxMemory, pointer);
197
}
×
198

×
199
void printMemoryStatisticsFor(Memory *memory) {
200
    printf("Max memory use for %s : %d\n", memory->name, memory->max);
201
}
×
202

×
203
void printMemoryStatistics(void) {
×
204
    printMemoryStatisticsFor(&cxMemory);
205
    printMemoryStatisticsFor(&ppmMemory);
206
}
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