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

thoni56 / c-xrefactory / 1617

19 Nov 2025 11:56PM UTC coverage: 81.092% (+54.7%) from 26.404%
1617

push

travis-ci

thoni56
[tests] Trying to get C11 test more platform independent

The output is the references but they vary depending on the standard include design.

12836 of 15829 relevant lines covered (81.09%)

1769773.98 hits per line

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

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

3
#include <string.h>
4

5
#include "commons.h"
6
#include "constants.h"
7
#include "head.h"
8
#include "log.h"
9
#include "memory.h"
10
#include "proto.h"
11

12

13

14
CodeBlock *currentBlock;
15

16

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

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

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

29

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

37

38
void addToFrame(void (*action)(void*), void *argument) {
663,713✔
39
    FrameAllocation *f;
40

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

50
void removeFromFrameUntil(FrameAllocation *untilP) {
1,743,591✔
51
    FrameAllocation *f;
52
    for (f=currentBlock->frameAllocations; untilP<f; f=f->next) {
2,273,307✔
53
        assert(f!=NULL);
529,716✔
54
        (*(f->action))(f->argument);
55
    }
1,743,591✔
56
    if (f!=untilP) {
×
57
        error(ERR_INTERNAL, "block structure mismatch?");
58
    }
1,743,591✔
59
    currentBlock->frameAllocations = f;
1,743,591✔
60
    if (memoryTrace)
×
61
        frameDump();
1,743,591✔
62
}
63

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

1,743,933✔
72
static void fillCodeBlock(CodeBlock *block, int firstFreeIndex, FrameAllocation *allocation, CodeBlock *outerBlock) {
1,743,933✔
73
    block->firstFreeIndex = firstFreeIndex;
1,743,933✔
74
    block->frameAllocations = allocation;
1,743,933✔
75
    block->outerBlock = outerBlock;
1,743,933✔
76
}
77

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

84
static int stackMemoryMax = 0;
85

30,072,700✔
86
void *stackMemoryAlloc(int size) {
87
    int i;
88

30,072,700✔
89
    assert(currentBlock);
30,072,700✔
90
    mem_trace("stackMemoryAlloc: allocating %d bytes", size);
30,072,699✔
91
    i = currentBlock->firstFreeIndex;
30,072,699✔
92
    if (i+size < SIZE_stackMemory) {
22,045,538✔
93
        currentBlock->firstFreeIndex = i+size;
30,072,699✔
94
        if (currentBlock->firstFreeIndex > stackMemoryMax)
95
            stackMemoryMax = currentBlock->firstFreeIndex;
1✔
96
        return &stackMemory[i];
97
    } else {
1✔
98
        fatalError(ERR_ST,"i+size > SIZE_stackMemory,\n\tstack memory overflowed,\n\tread TROUBLES section of README file\n", XREF_EXIT_ERR, __FILE__, __LINE__);
99
        /* Should not return, but for testing and compilers sake return something */
100
        return NULL;
101
    }
2,772,104✔
102
}
2,772,104✔
103

2,772,104✔
104
static void *stackMemoryPush(void *pointer, int size) {
2,772,104✔
105
    void *new = stackMemoryAlloc(size);
106
    memcpy(new, pointer, size);
107
    return new;
1,028,371✔
108
}
1,028,371✔
109

1,028,371✔
110
char *stackMemoryPushString(char *string) {
111
    log_debug("Pushing string '%s'", string);
112
    return (char*)stackMemoryPush(string, strlen(string)+1);
1,743,733✔
113
}
114

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

125
void endBlock(void) {
1,743,591✔
126
    log_debug("End block");
1,743,591✔
127
    //&removeFromFrameUntil(NULL);
1,743,591✔
128
    assert(currentBlock && currentBlock->outerBlock);
129
    removeFromFrameUntil(currentBlock->outerBlock->frameAllocations);
797,758✔
130
    *currentBlock =  *currentBlock->outerBlock;
797,758✔
131
    assert(currentBlock != NULL);
797,758✔
132
}
14,608,758✔
133

13,811,000✔
134
int nestingLevel(void) {
13,811,000✔
135
    int level = 0;
136
    CodeBlock *block = currentBlock;
797,758✔
137
    while (block->outerBlock != NULL) {
138
        block = block->outerBlock;
139
        level++;
16,380✔
140
    }
9,928✔
141
    return level;
26,308✔
142
}
9,928✔
143

144
bool isMemoryFromPreviousBlock(void *address) {
145
    return currentBlock->outerBlock != NULL &&
×
146
        (char*)address > stackMemory &&
×
147
        (char*)address < stackMemory + currentBlock->outerBlock->firstFreeIndex;
×
148
}
149

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

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