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

thoni56 / c-xrefactory / 1763

01 Apr 2026 03:04PM UTC coverage: 82.698% (+0.3%) from 82.407%
1763

push

travis-ci

thoni56
[tests][flaky] One test captured logging output which contained source line references

15658 of 18934 relevant lines covered (82.7%)

15517812.5 hits per line

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

83.12
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

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

23

24
static void frameDump(void) {
×
25
    log_debug("*** begin frameDump");
×
26
    for (FrameAllocation *f=currentBlock->frameAllocations; f!=NULL; f=f->next)
×
27
        log_debug("%p ", f);
×
28
    log_debug("*** end frameDump");
×
29
}
30

31

32
void addToFrame(void (*action)(void*), void *argument) {
9,906,094✔
33
    FrameAllocation *f;
34

35
    f = stackMemoryAlloc(sizeof(FrameAllocation));
9,906,094✔
36
    f->action = action;
9,906,094✔
37
    f->argument = (void **) argument;
9,906,094✔
38
    f->next = currentBlock->frameAllocations;
9,906,094✔
39
    currentBlock->frameAllocations = f;
9,906,094✔
40
    if (memoryTrace)
9,906,094✔
41
        frameDump();
×
42
}
9,906,094✔
43

44
void removeFromFrameUntil(FrameAllocation *untilP) {
2,614,724✔
45
    FrameAllocation *f;
46
    for (f=currentBlock->frameAllocations; untilP<f; f=f->next) {
3,776,368✔
47
        assert(f!=NULL);
1,161,644✔
48
        (*(f->action))(f->argument);
49
    }
2,614,724✔
50
    assert(f == untilP && "block structure mismatch");
2,614,724✔
51
    currentBlock->frameAllocations = f;
×
52
    if (memoryTrace)
2,614,724✔
53
        frameDump();
54
}
×
55

56
void recoverMemoryFromFrameAllocations(void) {
×
57
    FrameAllocation **pp;
×
58
    pp = &currentBlock->frameAllocations;
×
59
    while (isFreedStackMemory(*pp)) {
60
        *pp = (*pp)->next;
61
    }
62
}
2,619,030✔
63

2,619,030✔
64
static void fillCodeBlock(CodeBlock *block, int firstFreeIndex, FrameAllocation *allocation, CodeBlock *outerBlock) {
2,619,030✔
65
    block->firstFreeIndex = firstFreeIndex;
2,619,030✔
66
    block->frameAllocations = allocation;
2,619,030✔
67
    block->outerBlock = outerBlock;
68
}
4,245✔
69

70
void initOuterCodeBlock(void) {
4,245✔
71
    /* Any use of stack memory will require that this is run first */
4,245✔
72
    currentBlock = (CodeBlock *) stackMemory;
4,245✔
73
    fillCodeBlock(currentBlock, sizeof(CodeBlock), NULL, NULL);
74
}
75

76
static int stackMemoryMax = 0;
120,495,536✔
77

78
void *stackMemoryAlloc(int size) {
79
    int i;
120,495,536✔
80

120,495,536✔
81
    assert(currentBlock);
120,495,535✔
82
    mem_trace("stackMemoryAlloc: allocating %d bytes", size);
120,495,535✔
83
    i = currentBlock->firstFreeIndex;
20,462,063✔
84
    if (i+size < StackMemorySize) {
120,495,535✔
85
        currentBlock->firstFreeIndex = i+size;
86
        if (currentBlock->firstFreeIndex > stackMemoryMax)
1✔
87
            stackMemoryMax = currentBlock->firstFreeIndex;
88
        return &stackMemory[i];
1✔
89
    } else {
90
        fatalError(ERR_ST,"i+size > SIZE_stackMemory,\n\tstack memory overflowed,\n\tread TROUBLES section of README file\n", EXIT_FAILURE, __FILE__, __LINE__);
91
        /* Should not return, but for testing and compilers sake return something */
92
        return NULL;
22,451,897✔
93
    }
22,451,897✔
94
}
22,451,897✔
95

22,451,897✔
96
static void *stackMemoryPush(void *pointer, int size) {
97
    void *new = stackMemoryAlloc(size);
98
    memcpy(new, pointer, size);
19,837,112✔
99
    return new;
19,837,112✔
100
}
19,837,112✔
101

102
char *stackMemoryPushString(char *string) {
103
    log_debug("Pushing string '%s'", string);
2,614,785✔
104
    return (char*)stackMemoryPush(string, strlen(string)+1);
105
}
2,614,785✔
106

2,614,785✔
107
void beginBlock(void) {
2,614,785✔
108
    CodeBlock *pushed, previous;
109
    log_debug("Begin block");
110
    previous = *currentBlock;
2,614,785✔
111
    pushed = stackMemoryPush(&previous, sizeof(CodeBlock));
2,614,785✔
112
    // allocation can't be reset to NULL, because in case of syntax errors
113
    // this would avoid balancing of } at the end of class
2,614,724✔
114
    fillCodeBlock(currentBlock, currentBlock->firstFreeIndex, currentBlock->frameAllocations, pushed);
2,614,724✔
115
}
116

2,614,724✔
117
void endBlock(void) {
2,614,724✔
118
    log_debug("End block");
2,614,724✔
119
    //&removeFromFrameUntil(NULL);
120
    assert(currentBlock && currentBlock->outerBlock);
14,516,490✔
121
    removeFromFrameUntil(currentBlock->outerBlock->frameAllocations);
14,516,490✔
122
    *currentBlock =  *currentBlock->outerBlock;
14,516,490✔
123
    assert(currentBlock != NULL);
17,816,107✔
124
}
3,299,617✔
125

3,299,617✔
126
int nestingLevel(void) {
127
    int level = 0;
14,516,490✔
128
    CodeBlock *block = currentBlock;
129
    while (block->outerBlock != NULL) {
130
        block = block->outerBlock;
818,267✔
131
        level++;
33,413✔
132
    }
851,680✔
133
    return level;
33,413✔
134
}
135

136
bool isMemoryFromPreviousBlock(void *address) {
8,505,448✔
137
    return currentBlock->outerBlock != NULL &&
16,771,016✔
138
        (char*)address > stackMemory &&
8,265,568✔
139
        (char*)address < stackMemory + currentBlock->outerBlock->firstFreeIndex;
140
}
141

×
142
bool isFreedStackMemory(void *ptr) {
×
143
    return ((char*)ptr >= stackMemory + currentBlock->firstFreeIndex &&
144
            (char*)ptr < stackMemory + StackMemorySize);
145
}
146

147
void stackMemoryStatistics(void) {
148
    printf("Max memory use for stack: %d\n", stackMemoryMax);
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc