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

Tehreer / SheenBidi / 15942379337

28 Jun 2025 08:39AM UTC coverage: 95.533% (-1.5%) from 97.027%
15942379337

push

github

mta452
[lib] Use default allocator for memory management

26 of 35 new or added lines in 7 files covered. (74.29%)

14 existing lines in 1 file now uncovered.

2203 of 2306 relevant lines covered (95.53%)

778648.62 hits per line

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

85.25
/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
#include <stdlib.h>
19

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

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

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

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

39
    return totalSize;
4,309,753✔
40
}
41

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

52
    for (index = 0; index < chunkCount; index++) {
12,929,250✔
53
        outPointers[index] = chunkPointer + offset;
8,619,497✔
54
        offset += chunkSizes[index];
8,619,497✔
55
    }
56
}
4,309,753✔
57

58
SB_INTERNAL void MemoryInitialize(MemoryRef memory)
6,895,602✔
59
{
60
    memory->_list = NULL;
6,895,602✔
61
}
6,895,602✔
62

63
SB_INTERNAL void *MemoryAllocateBlock(MemoryRef memory, MemoryType type, SBUInteger size)
4,309,882✔
64
{
65
    SBAllocatorRef allocator = SBAllocatorGetCurrent();
4,309,882✔
66
    void *pointer = NULL;
4,309,882✔
67

68
    /* Size MUST be greater than zero. */
69
    SBAssert(size > 0);
4,309,882✔
70

71
    if (type == MemoryTypeScratch) {
4,309,882✔
72
        pointer = SBAllocatorAllocateScratch(allocator, size);
1,724,026✔
73
    }
74

75
    if (!pointer) {
4,309,882✔
76
        MemoryListRef memoryList = memory->_list;
2,585,856✔
77

78
        if (memoryList) {
2,585,856✔
NEW
79
            const SBUInteger headerSize = sizeof(MemoryBlock);
×
80

NEW
81
            pointer = SBAllocatorAllocateBlock(allocator, headerSize + size);
×
82

NEW
83
            if (pointer) {
×
NEW
84
                SBUInt8 *base = pointer;
×
85
                MemoryBlockRef block;
86

NEW
87
                block = pointer;
×
NEW
88
                block->next = NULL;
×
89

NEW
90
                memoryList->last->next = block;
×
NEW
91
                memoryList->last = block;
×
92

NEW
93
                pointer = base + headerSize;
×
94
            }
95
        } else {
96
            const SBUInteger headerSize = sizeof(MemoryList);
2,585,856✔
97

98
            pointer = SBAllocatorAllocateBlock(allocator, headerSize + size);
2,585,856✔
99

100
            if (pointer) {
2,585,856✔
101
                SBUInt8 *base = pointer;
2,585,856✔
102

103
                memoryList = pointer;
2,585,856✔
104
                memoryList->first.next = NULL;
2,585,856✔
105
                memoryList->last = &memoryList->first;
2,585,856✔
106

107
                memory->_list = memoryList;
2,585,856✔
108

109
                pointer = base + headerSize;
2,585,856✔
110
            }
111
        }
112
    }
113

114
    return pointer;
4,309,882✔
115
}
116

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

124
    /* Total size MUST be greater than zero. */
125
    SBAssert(totalSize > 0);
4,309,753✔
126

127
    pointer = MemoryAllocateBlock(memory, type, totalSize);
4,309,753✔
128

129
    if (pointer) {
4,309,753✔
130
        SplitMemoryBlock(pointer, chunkSizes, chunkCount, outPointers);
4,309,753✔
131
        succeeded = SBTrue;
4,309,753✔
132
    }
133

134
    return succeeded;
4,309,753✔
135
}
136

137
SB_INTERNAL void MemoryFinalize(MemoryRef memory)
6,895,602✔
138
{
139
    MemoryListRef memoryList = memory->_list;
6,895,602✔
140

141
    if (memoryList) {
6,895,602✔
142
        MemoryBlockRef block = &memoryList->first;
2,585,856✔
143

144
        while (block) {
5,171,712✔
145
            MemoryBlockRef next = block->next;
2,585,856✔
146
            /* Free the block along with its data as they were allocated together. */
147
            free(block);
2,585,856✔
148

149
            block = next;
2,585,856✔
150
        }
151
    }
152
}
6,895,602✔
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