@@ -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
3232extern ListNode s_freeHead , s_allocHead , s_adjHead ;
3333static 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
3838ListNode 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
0 commit comments