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

thoni56 / c-xrefactory / 1576

20 Feb 2025 03:29PM UTC coverage: 80.983% (+0.4%) from 80.572%
1576

push

travis-ci

thoni56
[util] Fix server_driver to check for `c-xref` in path else assume it's a test

12648 of 15618 relevant lines covered (80.98%)

9497458.86 hits per line

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

84.42
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_trace("*** begin frameDump");
×
32
    for (FrameAllocation *f=currentBlock->frameAllocations; f!=NULL; f=f->next)
×
33
        log_trace("%p ", f);
×
34
    log_trace("*** end frameDump");
×
35
}
36

37

38
void addToFrame(void (*action)(void*), void *argument) {
4,995,576✔
39
    FrameAllocation *f;
40

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

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

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

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

76
static int stackMemoryMax = 0;
77

58,811,797✔
78
void *stackMemoryAlloc(int size) {
79
    int i;
80

58,811,797✔
81
    assert(currentBlock);
58,811,797✔
82
    mem_trace("stackMemoryAlloc: allocating %d bytes", size);
58,811,796✔
83
    i = currentBlock->firstFreeIndex;
58,811,796✔
84
    if (i+size < SIZE_stackMemory) {
6,356,297✔
85
        currentBlock->firstFreeIndex = i+size;
58,811,796✔
86
        if (currentBlock->firstFreeIndex > stackMemoryMax)
87
            stackMemoryMax = currentBlock->firstFreeIndex;
1✔
88
        return &stackMemory[i];
89
    } else {
1✔
90
        fatalError(ERR_ST,"i+size > SIZE_stackMemory,\n\tstack memory overflowed,\n\tread TROUBLES section of README file\n", XREF_EXIT_ERR, __FILE__, __LINE__);
91
        /* Should not return, but for testing and compilers sake return something */
92
        return NULL;
93
    }
12,343,954✔
94
}
12,343,954✔
95

12,343,954✔
96
static void *stackMemoryPush(void *pointer, int size) {
12,343,954✔
97
    void *new = stackMemoryAlloc(size);
98
    memcpy(new, pointer, size);
99
    return new;
10,331,031✔
100
}
10,331,031✔
101

10,331,031✔
102
char *stackMemoryPushString(char *string) {
103
    log_trace("Pushing string '%s'", string);
104
    return (char*)stackMemoryPush(string, strlen(string)+1);
2,012,923✔
105
}
106

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

117
void endBlock(void) {
2,012,644✔
118
    log_trace("End block");
2,012,644✔
119
    //&removeFromFrameUntil(NULL);
2,012,644✔
120
    assert(currentBlock && currentBlock->outerBlock);
121
    removeFromFrameUntil(currentBlock->outerBlock->frameAllocations);
6,835,329✔
122
    *currentBlock =  *currentBlock->outerBlock;
6,835,329✔
123
    assert(currentBlock != NULL);
6,835,329✔
124
}
9,244,912✔
125

2,409,583✔
126
int nestingLevel(void) {
2,409,583✔
127
    int level = 0;
128
    CodeBlock *block = currentBlock;
6,835,329✔
129
    while (block->outerBlock != NULL) {
130
        block = block->outerBlock;
131
        level++;
413,141✔
132
    }
6,316✔
133
    return level;
419,457✔
134
}
6,316✔
135

136
bool isMemoryFromPreviousBlock(void *address) {
137
    return currentBlock->outerBlock != NULL &&
3,979,228✔
138
        (char*)address > stackMemory &&
7,724,465✔
139
        (char*)address < stackMemory + currentBlock->outerBlock->firstFreeIndex;
3,745,237✔
140
}
141

142
bool isFreedStackMemory(void *ptr) {
×
143
    return ((char*)ptr >= stackMemory + currentBlock->firstFreeIndex &&
×
144
            (char*)ptr < stackMemory + SIZE_stackMemory);
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 · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc