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

thoni56 / c-xrefactory / 1717

29 Jan 2026 12:54PM UTC coverage: 81.319% (+0.02%) from 81.304%
1717

push

travis-ci

thoni56
[navigation][fix] A preloaded file only updated references from the current file

Fix by also loading cross-file references from the disk DB

14661 of 18029 relevant lines covered (81.32%)

16180780.29 hits per line

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

80.25
src/stackmemory.c
1
#include "stackmemory.h"
2

3
#include <string.h>
4
#include <stdlib.h>
5

6
#include "commons.h"
7
#include "constants.h"
8
#include "log.h"
9
#include "proto.h"
10

11

12

13
CodeBlock *currentBlock;
14

15

16
static bool memoryTrace = false;
17
#define mem_trace(...)  { if (memoryTrace) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__); }
18

19
/* Inject the function to call for error() */
20
static void (*error)(int code, char *message);
21
void setErrorHandlerForStackMemory(void (*function)(int code, char *message)) {
×
22
    error = function;
×
23
}
24

25
/* Stack memory - in this we allocate blocks which have separate free indices */
26
char stackMemory[StackMemorySize];   /* Allocation using stackMemoryAlloc() et.al */
27

28

29
static void frameDump(void) {
×
30
    log_debug("*** begin frameDump");
×
31
    for (FrameAllocation *f=currentBlock->frameAllocations; f!=NULL; f=f->next)
×
32
        log_debug("%p ", f);
×
33
    log_debug("*** end frameDump");
×
34
}
35

36

37
void addToFrame(void (*action)(void*), void *argument) {
9,850,918✔
38
    FrameAllocation *f;
39

40
    f = stackMemoryAlloc(sizeof(FrameAllocation));
9,850,918✔
41
    f->action = action;
9,850,918✔
42
    f->argument = (void **) argument;
9,850,918✔
43
    f->next = currentBlock->frameAllocations;
9,850,918✔
44
    currentBlock->frameAllocations = f;
9,850,918✔
45
    if (memoryTrace)
9,850,918✔
46
        frameDump();
×
47
}
9,850,918✔
48

49
void removeFromFrameUntil(FrameAllocation *untilP) {
2,602,248✔
50
    FrameAllocation *f;
51
    for (f=currentBlock->frameAllocations; untilP<f; f=f->next) {
3,740,677✔
52
        assert(f!=NULL);
1,138,429✔
53
        (*(f->action))(f->argument);
54
    }
2,602,248✔
55
    if (f!=untilP) {
×
56
        error(ERR_INTERNAL, "block structure mismatch?");
57
    }
2,602,248✔
58
    currentBlock->frameAllocations = f;
2,602,248✔
59
    if (memoryTrace)
×
60
        frameDump();
2,602,248✔
61
}
62

×
63
void recoverMemoryFromFrameAllocations(void) {
64
    FrameAllocation **pp;
×
65
    pp = &currentBlock->frameAllocations;
×
66
    while (isFreedStackMemory(*pp)) {
×
67
        *pp = (*pp)->next;
68
    }
69
}
70

2,606,246✔
71
static void fillCodeBlock(CodeBlock *block, int firstFreeIndex, FrameAllocation *allocation, CodeBlock *outerBlock) {
2,606,246✔
72
    block->firstFreeIndex = firstFreeIndex;
2,606,246✔
73
    block->frameAllocations = allocation;
2,606,246✔
74
    block->outerBlock = outerBlock;
2,606,246✔
75
}
76

3,942✔
77
void initOuterCodeBlock(void) {
78
    /* Any use of stack memory will require that this is run first */
3,942✔
79
    currentBlock = (CodeBlock *) stackMemory;
3,942✔
80
    fillCodeBlock(currentBlock, sizeof(CodeBlock), NULL, NULL);
3,942✔
81
}
82

83
static int stackMemoryMax = 0;
84

119,969,211✔
85
void *stackMemoryAlloc(int size) {
86
    int i;
87

119,969,211✔
88
    assert(currentBlock);
119,969,211✔
89
    mem_trace("stackMemoryAlloc: allocating %d bytes", size);
119,969,210✔
90
    i = currentBlock->firstFreeIndex;
119,969,210✔
91
    if (i+size < StackMemorySize) {
20,409,232✔
92
        currentBlock->firstFreeIndex = i+size;
119,969,210✔
93
        if (currentBlock->firstFreeIndex > stackMemoryMax)
94
            stackMemoryMax = currentBlock->firstFreeIndex;
1✔
95
        return &stackMemory[i];
96
    } else {
1✔
97
        fatalError(ERR_ST,"i+size > SIZE_stackMemory,\n\tstack memory overflowed,\n\tread TROUBLES section of README file\n", EXIT_FAILURE, __FILE__, __LINE__);
98
        /* Should not return, but for testing and compilers sake return something */
99
        return NULL;
100
    }
22,337,631✔
101
}
22,337,631✔
102

22,337,631✔
103
static void *stackMemoryPush(void *pointer, int size) {
22,337,631✔
104
    void *new = stackMemoryAlloc(size);
105
    memcpy(new, pointer, size);
106
    return new;
19,735,327✔
107
}
19,735,327✔
108

19,735,327✔
109
char *stackMemoryPushString(char *string) {
110
    log_debug("Pushing string '%s'", string);
111
    return (char*)stackMemoryPush(string, strlen(string)+1);
2,602,304✔
112
}
113

2,602,304✔
114
void beginBlock(void) {
2,602,304✔
115
    CodeBlock *pushed, previous;
2,602,304✔
116
    log_debug("Begin block");
117
    previous = *currentBlock;
118
    pushed = stackMemoryPush(&previous, sizeof(CodeBlock));
2,602,304✔
119
    // allocation can't be reset to NULL, because in case of syntax errors
2,602,304✔
120
    // this would avoid balancing of } at the end of class
121
    fillCodeBlock(currentBlock, currentBlock->firstFreeIndex, currentBlock->frameAllocations, pushed);
2,602,248✔
122
}
2,602,248✔
123

124
void endBlock(void) {
2,602,248✔
125
    log_debug("End block");
2,602,248✔
126
    //&removeFromFrameUntil(NULL);
2,602,248✔
127
    assert(currentBlock && currentBlock->outerBlock);
128
    removeFromFrameUntil(currentBlock->outerBlock->frameAllocations);
14,445,505✔
129
    *currentBlock =  *currentBlock->outerBlock;
14,445,505✔
130
    assert(currentBlock != NULL);
14,445,505✔
131
}
17,674,153✔
132

3,228,648✔
133
int nestingLevel(void) {
3,228,648✔
134
    int level = 0;
135
    CodeBlock *block = currentBlock;
14,445,505✔
136
    while (block->outerBlock != NULL) {
137
        block = block->outerBlock;
138
        level++;
816,600✔
139
    }
33,399✔
140
    return level;
849,999✔
141
}
33,399✔
142

143
bool isMemoryFromPreviousBlock(void *address) {
144
    return currentBlock->outerBlock != NULL &&
8,457,772✔
145
        (char*)address > stackMemory &&
16,695,764✔
146
        (char*)address < stackMemory + currentBlock->outerBlock->firstFreeIndex;
8,237,992✔
147
}
148

149
bool isFreedStackMemory(void *ptr) {
×
150
    return ((char*)ptr >= stackMemory + currentBlock->firstFreeIndex &&
×
151
            (char*)ptr < stackMemory + StackMemorySize);
152
}
153

154
void stackMemoryStatistics(void) {
155
    printf("Max memory use for stack: %d\n", stackMemoryMax);
156
}
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