Skip to content

Commit 4d1f761

Browse files
committed
Refactor MallocHeader -> KMallocHeader
Makes it easier to differentiate the kernel type with the libcm type of the same name. Also it makes name consistent with names of other kernel types.
1 parent 9b95380 commit 4d1f761

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

include/memmanage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
#define SALLOC_GRANUALITY (8 * Byte)
1515
#define KMALLOC_GRANULARITY (16 * bytes)
1616

17-
typedef struct MallocHeader
17+
typedef struct KMallocHeader
1818
{
1919
size_t netNodeSize; /// Size of a region together with the header size.
2020
bool isAllocated; /// Is the region allocated or free.
2121
ListNode adjnode; /// A node in the Adjacent list.
2222
ListNode freenode; /// A node in the Free list.
2323
ListNode allocnode; /// A node in the Allocation list
24-
} MallocHeader;
24+
} KMallocHeader;
2525

2626
void ksalloc_init();
2727
void* ksalloc (UINT bytes);

src/kernel/kmalloc.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ typedef enum FindCriteria
2323
FIND_CRIT_NODE_ADDRESS
2424
} FindCriteria;
2525

26-
static MallocHeader* s_createNewNode (void* at, size_t netSize);
27-
static MallocHeader* s_findFirst (ListNode* head, FindCriteria criteria, PTR value);
28-
static MallocHeader* s_getMallocHeaderFromList (ListNode* head, ListNode* node);
29-
static void s_splitFreeNode (size_t bytes, MallocHeader* freeNodeHdr);
30-
static void s_combineAdjFreeNodes (MallocHeader* currentNode);
26+
static KMallocHeader* s_createNewNode (void* at, size_t netSize);
27+
static KMallocHeader* s_findFirst (ListNode* head, FindCriteria criteria, PTR value);
28+
static KMallocHeader* s_getMallocHeaderFromList (ListNode* head, ListNode* node);
29+
static void s_splitFreeNode (size_t bytes, KMallocHeader* freeNodeHdr);
30+
static void s_combineAdjFreeNodes (KMallocHeader* currentNode);
3131

3232
extern ListNode s_freeHead, s_allocHead, s_adjHead;
3333
static void* s_buffer;
3434

35-
#define NET_ALLOCATION_SIZE(sz_bytes) (sz_bytes + sizeof (MallocHeader))
35+
#define NET_ALLOCATION_SIZE(sz_bytes) (sz_bytes + sizeof (KMallocHeader))
3636

3737
#ifndef UNITTEST
3838
ListNode s_freeHead, s_allocHead, s_adjHead;
@@ -61,13 +61,13 @@ void kmalloc_init()
6161
FATAL_BUG(); // Should not fail.
6262
}
6363

64-
MallocHeader* newH = s_createNewNode (s_buffer, ARCH_MEM_LEN_BYTES_KMALLOC);
64+
KMallocHeader* newH = s_createNewNode (s_buffer, ARCH_MEM_LEN_BYTES_KMALLOC);
6565
list_add_before (&s_freeHead, &newH->freenode);
6666
list_add_before (&s_adjHead, &newH->adjnode);
6767

6868
KERNEL_PHASE_SET(KERNEL_PHASE_STATE_KMALLOC_READY);
6969

70-
INFO ("Size of MallocHeader: %lu bytes", sizeof (MallocHeader));
70+
INFO ("Size of KMallocHeader: %lu bytes", sizeof (KMallocHeader));
7171
INFO ("Malloc buffer is at: %px", s_buffer);
7272
}
7373

@@ -108,16 +108,16 @@ void* kmalloc (size_t bytes)
108108
INFO ("Requested net size of %lu bytes", NET_ALLOCATION_SIZE (bytes));
109109

110110
// Search for suitable node
111-
size_t searchAllocSize = NET_ALLOCATION_SIZE (bytes) + sizeof (MallocHeader);
112-
MallocHeader* node = s_findFirst (&s_freeHead, FIND_CRIT_NODE_SIZE, searchAllocSize);
111+
size_t searchAllocSize = NET_ALLOCATION_SIZE (bytes) + sizeof (KMallocHeader);
112+
KMallocHeader* node = s_findFirst (&s_freeHead, FIND_CRIT_NODE_SIZE, searchAllocSize);
113113

114114
if (node != NULL)
115115
{
116116
k_assert (node->netNodeSize >= NET_ALLOCATION_SIZE (bytes), "Found node too small");
117117

118118
// Split the free node into two.
119119
s_splitFreeNode (bytes, node);
120-
return (void*)((PTR)node + sizeof (MallocHeader));
120+
return (void*)((PTR)node + sizeof (KMallocHeader));
121121
}
122122

123123
RETURN_ERROR (ERR_OUT_OF_MEM, NULL);
@@ -136,8 +136,8 @@ bool kfree (void* addr)
136136

137137
KERNEL_PHASE_VALIDATE(KERNEL_PHASE_STATE_KMALLOC_READY);
138138

139-
void* headerAddress = (void*)((PTR)addr - sizeof (MallocHeader));
140-
MallocHeader* allocHdr = s_findFirst (&s_allocHead, FIND_CRIT_NODE_ADDRESS, (PTR)headerAddress);
139+
void* headerAddress = (void*)((PTR)addr - sizeof (KMallocHeader));
140+
KMallocHeader* allocHdr = s_findFirst (&s_allocHead, FIND_CRIT_NODE_ADDRESS, (PTR)headerAddress);
141141
if (allocHdr != NULL)
142142
{
143143
INFO ("Free at %px, Size = %lu", allocHdr, allocHdr->netNodeSize);
@@ -171,7 +171,7 @@ SIZE kmalloc_getUsedMemory()
171171

172172
list_for_each (&s_allocHead, node)
173173
{
174-
MallocHeader* header = LIST_ITEM (node, MallocHeader, allocnode);
174+
KMallocHeader* header = LIST_ITEM (node, KMallocHeader, allocnode);
175175
INFO ("Node size: %u bytes", header->netNodeSize);
176176
k_assert (header && (header->netNodeSize < ARCH_MEM_LEN_BYTES_KMALLOC),
177177
"Invalid state of kmalloc data");
@@ -181,26 +181,26 @@ SIZE kmalloc_getUsedMemory()
181181
return usedSz;
182182
}
183183

184-
static void s_combineAdjFreeNodes (MallocHeader* currentNode)
184+
static void s_combineAdjFreeNodes (KMallocHeader* currentNode)
185185
{
186-
MallocHeader* next = NULL;
187-
MallocHeader* prev = NULL;
186+
KMallocHeader* next = NULL;
187+
KMallocHeader* prev = NULL;
188188

189189
k_assert (currentNode->isAllocated == false, "Cannot combine allocated node. Invalid input.");
190190

191-
// Prev node could be the list head (which is not a MallocHeader). That would mean beginning of
191+
// Prev node could be the list head (which is not a KMallocHeader). That would mean beginning of
192192
// list and is not taken into account.
193193
if (currentNode->adjnode.prev != &s_adjHead)
194194
{
195-
prev = LIST_ITEM (currentNode->adjnode.prev, MallocHeader, adjnode);
195+
prev = LIST_ITEM (currentNode->adjnode.prev, KMallocHeader, adjnode);
196196
}
197197

198-
// Next node could be the list head (which is not a MallocHeader). That would mean end of list
198+
// Next node could be the list head (which is not a KMallocHeader). That would mean end of list
199199
// and is not taken into account (Practically this is not possible as there will always be
200200
// one Free section at the end of kmalloc buffer).
201201
if (currentNode->adjnode.next != &s_adjHead)
202202
{
203-
next = LIST_ITEM (currentNode->adjnode.next, MallocHeader, adjnode);
203+
next = LIST_ITEM (currentNode->adjnode.next, KMallocHeader, adjnode);
204204
}
205205

206206
if (next && !next->isAllocated)
@@ -220,12 +220,12 @@ static void s_combineAdjFreeNodes (MallocHeader* currentNode)
220220
}
221221
}
222222

223-
static MallocHeader* s_createNewNode (void* at, size_t netSize)
223+
static KMallocHeader* s_createNewNode (void* at, size_t netSize)
224224
{
225225
k_assert (((PTR)at + netSize - 1) < ((PTR)s_buffer + ARCH_MEM_LEN_BYTES_KMALLOC),
226226
"Node netSize too large");
227227

228-
MallocHeader* newH = at;
228+
KMallocHeader* newH = at;
229229
newH->netNodeSize = netSize;
230230
newH->isAllocated = false;
231231
list_init (&newH->freenode);
@@ -234,26 +234,26 @@ static MallocHeader* s_createNewNode (void* at, size_t netSize)
234234
return newH;
235235
}
236236

237-
static MallocHeader* s_getMallocHeaderFromList (ListNode* head, ListNode* node)
237+
static KMallocHeader* s_getMallocHeaderFromList (ListNode* head, ListNode* node)
238238
{
239-
MallocHeader* ret = NULL;
239+
KMallocHeader* ret = NULL;
240240
if (head == &s_allocHead)
241241
{
242-
ret = LIST_ITEM (node, MallocHeader, allocnode);
242+
ret = LIST_ITEM (node, KMallocHeader, allocnode);
243243
}
244244
else if (head == &s_freeHead)
245245
{
246-
ret = LIST_ITEM (node, MallocHeader, freenode);
246+
ret = LIST_ITEM (node, KMallocHeader, freenode);
247247
}
248248

249249
return ret;
250250
}
251251

252-
static MallocHeader* s_findFirst (ListNode* head, FindCriteria criteria, PTR value)
252+
static KMallocHeader* s_findFirst (ListNode* head, FindCriteria criteria, PTR value)
253253
{
254254
bool found = false;
255255
ListNode* node = NULL;
256-
MallocHeader* header = NULL;
256+
KMallocHeader* header = NULL;
257257

258258
INFO ("Searching for value %lu (%lx)", value, value);
259259

@@ -281,14 +281,14 @@ static MallocHeader* s_findFirst (ListNode* head, FindCriteria criteria, PTR val
281281
return (found) ? header : NULL;
282282
}
283283

284-
static void s_splitFreeNode (size_t bytes, MallocHeader* freeNodeHdr)
284+
static void s_splitFreeNode (size_t bytes, KMallocHeader* freeNodeHdr)
285285
{
286286
size_t netAllocSize = NET_ALLOCATION_SIZE (bytes);
287287
PTR splitAt = (PTR)freeNodeHdr + netAllocSize;
288288
size_t remainingSize = freeNodeHdr->netNodeSize - netAllocSize;
289-
k_assert (remainingSize >= sizeof (MallocHeader), "Not enough space for kmalloc header");
289+
k_assert (remainingSize >= sizeof (KMallocHeader), "Not enough space for kmalloc header");
290290

291-
MallocHeader* newFreeNodeHdr = s_createNewNode ((void*)splitAt, remainingSize);
291+
KMallocHeader* newFreeNodeHdr = s_createNewNode ((void*)splitAt, remainingSize);
292292
freeNodeHdr->netNodeSize = netAllocSize;
293293
freeNodeHdr->isAllocated = true;
294294

src/unittests/kernel/kmalloc_test.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ char kmalloc_buffer[UT_KMALLOC_SIZE_BYTES];
4444

4545
static inline size_t getNodeSize (size_t usableSize)
4646
{
47-
return sizeof (MallocHeader) + usableSize;
47+
return sizeof (KMallocHeader) + usableSize;
4848
}
4949

50-
static inline MallocHeader* calculateHeaderLocation (void* usableAddrStart)
50+
static inline KMallocHeader* calculateHeaderLocation (void* usableAddrStart)
5151
{
52-
return usableAddrStart - sizeof (MallocHeader);
52+
return usableAddrStart - sizeof (KMallocHeader);
5353
}
5454

55-
static inline void* calculateUsableAddressStart (MallocHeader* headerAddr)
55+
static inline void* calculateUsableAddressStart (KMallocHeader* headerAddr)
5656
{
57-
return (void*)headerAddr + sizeof (MallocHeader);
57+
return (void*)headerAddr + sizeof (KMallocHeader);
5858
}
5959

60-
static MallocHeader* getMallocHeaderFromList (MallocLists list, ListNode* node);
60+
static KMallocHeader* getMallocHeaderFromList (MallocLists list, ListNode* node);
6161
static bool isAddressFoundInList (void* addr, MallocLists list);
6262
static size_t getCapacity (MallocLists list);
6363
static void matchSectionPlacementAndAttributes (SectionAttributes* secAttrs, size_t count);
@@ -225,12 +225,12 @@ TEST (kmalloc_getUsedMemory, used_memory_test)
225225
static void matchSectionPlacementAndAttributes (SectionAttributes* secAttrs, size_t count)
226226
{
227227
ListNode* node = NULL;
228-
MallocHeader* header = NULL;
228+
KMallocHeader* header = NULL;
229229
int i = 0;
230230
list_for_each (&s_adjHead, node)
231231
{
232232
assert (i < count);
233-
header = LIST_ITEM (node, MallocHeader, adjnode);
233+
header = LIST_ITEM (node, KMallocHeader, adjnode);
234234
EQ_SCALAR (header->netNodeSize, secAttrs[i].nodeSize);
235235
EQ_SCALAR (header->isAllocated, secAttrs[i].isAllocated);
236236
i++;
@@ -249,15 +249,15 @@ static ListNode* getListHead (MallocLists list)
249249
};
250250
}
251251

252-
static MallocHeader* getMallocHeaderFromList (MallocLists list, ListNode* node)
252+
static KMallocHeader* getMallocHeaderFromList (MallocLists list, ListNode* node)
253253
{
254254
switch (list) {
255255
case FREE_LIST:
256-
return LIST_ITEM (node, MallocHeader, freenode);
256+
return LIST_ITEM (node, KMallocHeader, freenode);
257257
case ALLOC_LIST:
258-
return LIST_ITEM (node, MallocHeader, allocnode);
258+
return LIST_ITEM (node, KMallocHeader, allocnode);
259259
case ADJ_LIST:
260-
return LIST_ITEM (node, MallocHeader, adjnode);
260+
return LIST_ITEM (node, KMallocHeader, adjnode);
261261
};
262262
}
263263

@@ -268,7 +268,7 @@ static bool isAddressFoundInList (void* addr, MallocLists list)
268268

269269
list_for_each (head, node)
270270
{
271-
MallocHeader* header = getMallocHeaderFromList (list, node);
271+
KMallocHeader* header = getMallocHeaderFromList (list, node);
272272
if ((PTR)header == (PTR)calculateHeaderLocation (addr))
273273
return true;
274274
}

0 commit comments

Comments
 (0)