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

Tehreer / SheenBidi / 16097548599

06 Jul 2025 09:24AM UTC coverage: 96.979% (+0.4%) from 96.598%
16097548599

push

github

mta452
[lib] Update status stack implementation

36 of 36 new or added lines in 1 file covered. (100.0%)

6 existing lines in 1 file now uncovered.

2247 of 2317 relevant lines covered (96.98%)

785057.81 hits per line

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

85.0
/Source/Memory.c
1
/*
2
 * Copyright (C) 2025 Muhammad Tayyab Akram
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
#include <stddef.h>
18

19
#include <SheenBidi/SBBase.h>
20
#include <SheenBidi/SBConfig.h>
21

22
#include "SBAllocator.h"
23
#include "SBAssert.h"
24
#include "Memory.h"
25

26
/**
27
 * Computes the total size required for a set of memory chunks.
28
 */
29
static SBUInteger CalculateTotalSize(const SBUInteger *chunkSizes, SBUInteger chunkCount)
4,309,836✔
30
{
31
    SBUInteger totalSize = 0;
4,309,836✔
32
    SBUInteger index;
33

34
    for (index = 0; index < chunkCount; index++) {
12,929,494✔
35
        totalSize += chunkSizes[index];
8,619,658✔
36
    }
37

38
    return totalSize;
4,309,836✔
39
}
40

41
/**
42
 * Splits a contiguous memory block into multiple logical chunks.
43
 */
44
static void SplitMemoryBlock(void *pointer,
4,309,836✔
45
    const SBUInteger *chunkSizes, SBUInteger chunkCount, void **outPointers)
46
{
47
    SBUInt8 *chunkPointer = pointer;
4,309,836✔
48
    SBUInteger offset = 0;
4,309,836✔
49
    SBUInteger index;
50

51
    for (index = 0; index < chunkCount; index++) {
12,929,494✔
52
        outPointers[index] = chunkPointer + offset;
8,619,658✔
53
        offset += chunkSizes[index];
8,619,658✔
54
    }
55
}
4,309,836✔
56

57
SB_INTERNAL void MemoryInitialize(MemoryRef memory)
4,309,760✔
58
{
59
    memory->_list = NULL;
4,309,760✔
60
}
4,309,760✔
61

62
SB_INTERNAL void *MemoryAllocateBlock(MemoryRef memory, MemoryType type, SBUInteger size)
4,309,860✔
63
{
64
    void *pointer = NULL;
4,309,860✔
65

66
    /* Size MUST be greater than zero. */
67
    SBAssert(size > 0);
4,309,860✔
68

69
    if (type == MemoryTypeScratch) {
4,309,860✔
70
        pointer = SBAllocatorAllocateScratch(NULL, size);
1,723,999✔
71
    }
72

73
    if (!pointer) {
4,309,860✔
74
        MemoryListRef memoryList = memory->_list;
2,585,861✔
75

76
        if (memoryList) {
2,585,861✔
UNCOV
77
            const SBUInteger headerSize = sizeof(MemoryBlock);
×
78

UNCOV
79
            pointer = SBAllocatorAllocateBlock(NULL, headerSize + size);
×
80

UNCOV
81
            if (pointer) {
×
82
                SBUInt8 *base = pointer;
×
83
                MemoryBlockRef block;
84

UNCOV
85
                block = pointer;
×
86
                block->next = NULL;
×
87

UNCOV
88
                memoryList->last->next = block;
×
89
                memoryList->last = block;
×
90

UNCOV
91
                pointer = base + headerSize;
×
92
            }
93
        } else {
94
            const SBUInteger headerSize = sizeof(MemoryList);
2,585,861✔
95

96
            pointer = SBAllocatorAllocateBlock(NULL, headerSize + size);
2,585,861✔
97

98
            if (pointer) {
2,585,861✔
99
                SBUInt8 *base = pointer;
2,585,861✔
100

101
                memoryList = pointer;
2,585,861✔
102
                memoryList->first.next = NULL;
2,585,861✔
103
                memoryList->last = &memoryList->first;
2,585,861✔
104

105
                memory->_list = memoryList;
2,585,861✔
106

107
                pointer = base + headerSize;
2,585,861✔
108
            }
109
        }
110
    }
111

112
    return pointer;
4,309,860✔
113
}
114

115
SB_INTERNAL SBBoolean MemoryAllocateChunks(MemoryRef memory, MemoryType type,
4,309,836✔
116
    const SBUInteger *chunkSizes, SBUInteger chunkCount, void **outPointers)
117
{
118
    SBUInteger totalSize = CalculateTotalSize(chunkSizes, chunkCount);
4,309,836✔
119
    SBBoolean succeeded = SBFalse;
4,309,836✔
120
    void *pointer;
121

122
    /* Total size MUST be greater than zero. */
123
    SBAssert(totalSize > 0);
4,309,836✔
124

125
    pointer = MemoryAllocateBlock(memory, type, totalSize);
4,309,836✔
126

127
    if (pointer) {
4,309,836✔
128
        SplitMemoryBlock(pointer, chunkSizes, chunkCount, outPointers);
4,309,836✔
129
        succeeded = SBTrue;
4,309,836✔
130
    }
131

132
    return succeeded;
4,309,836✔
133
}
134

135
SB_INTERNAL void MemoryFinalize(MemoryRef memory)
4,309,760✔
136
{
137
    MemoryListRef memoryList = memory->_list;
4,309,760✔
138

139
    if (memoryList) {
4,309,760✔
140
        MemoryBlockRef block = &memoryList->first;
2,585,861✔
141

142
        while (block) {
5,171,722✔
143
            MemoryBlockRef next = block->next;
2,585,861✔
144
            /* Deallocate the block along with its data as they were allocated together. */
145
            SBAllocatorDeallocateBlock(NULL, block);
2,585,861✔
146

147
            block = next;
2,585,861✔
148
        }
149
    }
150
}
4,309,760✔
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