Modestus Moon OS  R4
CS 450 project
mcb.c File Reference
#include <core/mcb.h>
Include dependency graph for mcb.c:

Go to the source code of this file.

Functions

void initHeap (size_t initialHeapSize)
 initHeap initializes the system heap by calling kmalloc with a valid byte size. Creates a CMCB at the beginning and an LMCB at the end. Note the total size is the given size + sizeof(CMCB) + sizeof(LMCB). More...
 
void * allocateMemFromHeap (size_t requestedSize)
 allocateMemFromHeap takes the requested size and finds the first block that fits. Implements first fit. Size_t is unsigned. More...
 
void printAlloc ()
 printAlloc prints the allocated memory blocks. Traverses through the allocated mcb list. More...
 
void printFree ()
 printFree prints the free memory blocks. Traverses through the free mcb list. More...
 
int mcbCompareFunc (void *mcb, void *mcbToCompare)
 mcbCompareFunc compares the start addresses of two mcbs. More...
 
int mcbSearchCompFunc (void *mcb, void *blockStartAddress)
 mcbSearchCompFunc searches for the mcb that you want to compare with. More...
 
int freeHeapMem (void *blockAddress)
 freeHeapMem takes the block address and frees the memory from the CMCB start to the LMCB end location and calls reclaimFreeMem() function. More...
 
void reclaimFreeMem ()
 reclaimFreeMem groups free blocks together if they are adjacent. More...
 
void printMCBFunc (void *cmcb)
 printMCBFunc prints the type, block size, block pointer of the blocks in a given list. More...
 
int heapIsEmpty ()
 heapIsEmpty returns if the heap is empty. More...
 
const char * mcbResultToString (e_mcb_result_t result)
 
const char * mcbTypeToString (e_mcb_type_t type)
 
void insertMCBIntoList (cmcb_t *blockToInsert)
 insertMCBIntoList inserts the mcb into the list specified. More...
 
void heapTest ()
 heapTest test function to create sample mcbs. More...
 

Variables

heapLocationPtr_t heapLoc =0
 
linkedList_t mcbFreeList
 
linkedList_t mcbAllocList
 
e_mcb_result_t lastMCBError = MCB_SUCCESS
 

Function Documentation

void* allocateMemFromHeap ( size_t  requestedSize)

allocateMemFromHeap takes the requested size and finds the first block that fits. Implements first fit. Size_t is unsigned.

Parameters
requestedSizesize in bytes to the allocated from the heap.
Returns
a pointer to the beginning of the block of memory requested.

Definition at line 45 of file mcb.c.

References s_cmcb::associatedLMCB, s_cmcb::blockStartAddress, s_lmcb::blockStopAddress, s_cmcb::cmcbType, currentOperatingProcess, ERROR_BLOCK_NOT_ALLOCATED, ERROR_NOT_ENOUGH_MEMORY, insertMCBIntoList(), lastMCBError, s_lmcb::lmcbType, MCB_ALLOCATED, MCB_FREE, MCB_SUCCESS, s_cmcb::mcbBlockSize, s_lmcb::mcbBlockSize, s_cmcb::procName, removeNode(), and s_cmcb::representingNode.

Referenced by heapTest(), and kmain().

46 {
48  if(!requestedSize) //if the size is greater than zero
49  {
51  return 0;
52  }
53  mcbSize_t totalBlockSize = requestedSize + sizeof(lmcb_t) + sizeof(cmcb_t);
54  //requestedSize + sizeof(lmcb_t) + sizeof(cmcb_t)
55  cmcb_t * allocatedCMCB = 0;
56  node_t * itNode = mcbFreeList.head.next;
57  for(; itNode != &(mcbFreeList.tail); itNode = itNode->next)
58  {
59  //should not do subtraction with unsigned types. < 0 IS > 0 because of wrap around
60 
61  if(((cmcb_t*)itNode->data)->mcbBlockSize >= totalBlockSize)
62  {
63  //we found a block large enough
64  //printf("\r\nBlock size %d, total block size %d\r\n", ((cmcb_t*)itNode->data)->mcbBlockSize, totalBlockSize);
65  allocatedCMCB = ((cmcb_t*)itNode->data);
66  break;
67  }
68  }
69  if(!allocatedCMCB) //if there are no blocks large enough
70  {
72  return 0;
73  }
74 
75  (void)removeNode(&(allocatedCMCB->representingNode));
76  //set the new sizes
77  allocatedCMCB->cmcbType = MCB_ALLOCATED;
78  allocatedCMCB->mcbBlockSize = requestedSize;
79 
80  //we have a pointer to a cmcb that is large enough.
81  //first step is
82  //to, firstly calculate the IMMEDIATE following cmcb offset from the cmcb block start
83 
84  cmcb_t* followCMCB = (cmcb_t*)((void*)allocatedCMCB + totalBlockSize);
85 
86  //setup the follow cmcb
87  followCMCB->representingNode.data = (void*)followCMCB;
88  followCMCB->cmcbType = MCB_FREE;
89  //the following cmcb's lmcb is the allocatedCMCBs current lmcb
90  followCMCB->associatedLMCB = allocatedCMCB->associatedLMCB;
91  //calc block start offset
92  followCMCB->blockStartAddress = ((void*)followCMCB)+sizeof(cmcb_t);
93  //the block size is the lmcb's blockStop - cmcb's blockStart
94  followCMCB->associatedLMCB->mcbBlockSize=
95  followCMCB->mcbBlockSize=
96  (followCMCB->associatedLMCB->blockStopAddress-followCMCB->blockStartAddress);
97  followCMCB->procName = "";
98 
99  //setup the allocatedCMCB's new lmcb
100  allocatedCMCB->associatedLMCB = (lmcb_t*)(((void*)allocatedCMCB->blockStartAddress)+allocatedCMCB->mcbBlockSize);
101  allocatedCMCB->associatedLMCB->lmcbType = allocatedCMCB->cmcbType;
102  allocatedCMCB->associatedLMCB->blockStopAddress = allocatedCMCB->associatedLMCB;
103 
104  //put the process's name pointer
106  {
107  allocatedCMCB->procName = currentOperatingProcess->name;
108  }
109  else
110  {
111  allocatedCMCB->procName = "init";
112  }
113 
114 
115  insertMCBIntoList(allocatedCMCB);
116  insertMCBIntoList(followCMCB);
117 
118 
119  return (void*)allocatedCMCB->blockStartAddress;
120 
121 }
typedef of linked list node (see s_ll_node).
The s_cmcb struct the struct type for cmcb&#39;s -> defines the properties.
Definition: mcb.h:41
void insertMCBIntoList(cmcb_t *blockToInsert)
insertMCBIntoList inserts the mcb into the list specified.
Definition: mcb.c:271
size_t mcbSize_t
Definition: mcb.h:34
e_mcb_type_t cmcbType
Definition: mcb.h:45
node_t * removeNode(node_t *nodeToRemove)
will remove the node from the list. the list hierarchy will change to reflect the missing node...
Definition: linked_list.c:120
struct s_lmcb lmcb_t
Definition: mcb.h:30
pcb_t * currentOperatingProcess
Definition: pcb.c:9
e_mcb_type_t lmcbType
Definition: mcb.h:66
mcbSize_t mcbBlockSize
Definition: mcb.h:47
mcbSize_t mcbBlockSize
Definition: mcb.h:64
e_mcb_result_t lastMCBError
Definition: mcb.c:8
mcbAddressPtr_t blockStartAddress
Definition: mcb.h:53
The s_lmcb struct defines the properties of lmcb&#39;s.
Definition: mcb.h:60
linkedList_t mcbFreeList
Definition: mcb.c:5
node_t representingNode
Definition: mcb.h:43
struct s_cmcb cmcb_t
Definition: mcb.h:28
mcbAddressPtr_t blockStopAddress
Definition: mcb.h:62
lmcb_t * associatedLMCB
Definition: mcb.h:51
Definition: mcb.h:23
const char * procName
Definition: mcb.h:49
int freeHeapMem ( void *  blockAddress)

freeHeapMem takes the block address and frees the memory from the CMCB start to the LMCB end location and calls reclaimFreeMem() function.

Parameters
blockAddresspointer to the memory address to be freed.
Returns
the error code if any exists.

Definition at line 154 of file mcb.c.

References s_cmcb::associatedLMCB, s_cmcb::cmcbType, currentOperatingProcess, ERROR_BLOCK_NOT_FOUND, insertMCBIntoList(), lastMCBError, s_lmcb::lmcbType, MCB_ERROR_ACCESS_DENIED, MCB_FREE, MCB_SUCCESS, printMCBFunc(), s_cmcb::procName, reclaimFreeMem(), removeNode(), s_cmcb::representingNode, and searchList().

Referenced by heapTest(), and kmain().

155 {
157  node_t* foundNode = searchList(&mcbAllocList, blockAddress);
158  cmcb_t* foundCMCB = (foundNode)?(cmcb_t*)foundNode->data:0;
159  if(!foundCMCB)
160  {
162  return lastMCBError;
163  }
164  if(currentOperatingProcess->name != foundCMCB->procName)
165  {
167  return lastMCBError;
168  }
169 
170  //printf("\r\nin free%s","\r\n");
171  printMCBFunc(foundCMCB);
172 
173  (void)removeNode(&(foundCMCB->representingNode));
174  foundCMCB->procName = "";
175  foundCMCB->cmcbType=foundCMCB->associatedLMCB->lmcbType=MCB_FREE;
176  insertMCBIntoList(foundCMCB);
177 
178  reclaimFreeMem();
179  reclaimFreeMem();
180 
181  return lastMCBError;
182 }
typedef of linked list node (see s_ll_node).
void reclaimFreeMem()
reclaimFreeMem groups free blocks together if they are adjacent.
Definition: mcb.c:184
The s_cmcb struct the struct type for cmcb&#39;s -> defines the properties.
Definition: mcb.h:41
void insertMCBIntoList(cmcb_t *blockToInsert)
insertMCBIntoList inserts the mcb into the list specified.
Definition: mcb.c:271
e_mcb_type_t cmcbType
Definition: mcb.h:45
node_t * removeNode(node_t *nodeToRemove)
will remove the node from the list. the list hierarchy will change to reflect the missing node...
Definition: linked_list.c:120
pcb_t * currentOperatingProcess
Definition: pcb.c:9
e_mcb_type_t lmcbType
Definition: mcb.h:66
void printMCBFunc(void *cmcb)
printMCBFunc prints the type, block size, block pointer of the blocks in a given list.
Definition: mcb.c:223
e_mcb_result_t lastMCBError
Definition: mcb.c:8
node_t representingNode
Definition: mcb.h:43
lmcb_t * associatedLMCB
Definition: mcb.h:51
Definition: mcb.h:23
linkedList_t mcbAllocList
Definition: mcb.c:6
node_t * searchList(linkedList_t *listToSearch, void *data)
goes through the list using the list&#39;s comparison function to find the node which matches the data...
Definition: linked_list.c:154
const char * procName
Definition: mcb.h:49
int heapIsEmpty ( )

heapIsEmpty returns if the heap is empty.

Returns
an integer, if heap is empty -> returns 0, if heap is not empty -> returns 1.

Definition at line 229 of file mcb.c.

References lastMCBError, and MCB_SUCCESS.

230 {
232  if(mcbAllocList.head.next == &mcbAllocList.tail) return 0;
233  return 1;
234 }
e_mcb_result_t lastMCBError
Definition: mcb.c:8
linkedList_t mcbAllocList
Definition: mcb.c:6
void heapTest ( )

heapTest test function to create sample mcbs.

Definition at line 286 of file mcb.c.

References allocateMemFromHeap(), freeHeapMem(), lastMCBError, MCB_SUCCESS, mcbResultToString(), printf, and printList().

Referenced by mcbFunc().

287 {
288  printf("HEAP TEST%s","\r\n");
289  printf("%s", "Alloc list\r\n")
291  printf("%s", "free list\r\n")
293  printf("%s","\r\n\r\n");
294  int* test = (int*)allocateMemFromHeap(20);
296  {
297  printf("MCB Error! %s\r\n",mcbResultToString(lastMCBError));
298  }
299  printf("test int ptr %d\r\n",(int)test);
300  int *blocks[5];
301  for(*test = 0; *test < 5; (*test)++)
302  {
303  blocks[*test] = (int*)allocateMemFromHeap(20);
304  if(lastMCBError!=MCB_SUCCESS)
305  {
306  printf("MCB Error! %s\r\n",mcbResultToString(lastMCBError));
307  }
308  }
309  printf("%s", "\r\nAlloc list\r\n")
310  printList(&mcbAllocList);
311  printf("%s", "\r\nfree list\r\n")
312  printList(&mcbFreeList);
313  for(*test = 0; *test < 5; (*test)++)
314  {
315  freeHeapMem(blocks[*test]);
316  if(lastMCBError!=MCB_SUCCESS)
317  {
318  printf("MCB Error! %s\r\n",mcbResultToString(lastMCBError));
319  }
320  }
321  freeHeapMem(test);
322  printf("%s", "\r\nAlloc list\r\n")
323  printList(&mcbAllocList);
324  printf("%s", "\r\nfree list\r\n")
325  printList(&mcbFreeList);
326 }
const char * mcbResultToString(e_mcb_result_t result)
Definition: mcb.c:235
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
e_mcb_result_t lastMCBError
Definition: mcb.c:8
void printList(linkedList_t *list)
test function to show list functionality. uses const char* as test data
Definition: linked_list.c:212
void * allocateMemFromHeap(size_t requestedSize)
allocateMemFromHeap takes the requested size and finds the first block that fits. Implements first fi...
Definition: mcb.c:45
linkedList_t mcbFreeList
Definition: mcb.c:5
int freeHeapMem(void *blockAddress)
freeHeapMem takes the block address and frees the memory from the CMCB start to the LMCB end location...
Definition: mcb.c:154
linkedList_t mcbAllocList
Definition: mcb.c:6
void initHeap ( size_t  initialHeapSize)

initHeap initializes the system heap by calling kmalloc with a valid byte size. Creates a CMCB at the beginning and an LMCB at the end. Note the total size is the given size + sizeof(CMCB) + sizeof(LMCB).

Parameters
initialHeapSizethe size in bytes of the heap to be created

Definition at line 11 of file mcb.c.

References s_cmcb::associatedLMCB, s_cmcb::blockStartAddress, s_lmcb::blockStopAddress, s_cmcb::cmcbType, heapLoc, initLinkedList(), insertMCBIntoList(), kmalloc(), s_lmcb::lmcbType, MCB_FREE, s_cmcb::mcbBlockSize, s_lmcb::mcbBlockSize, mcbCompareFunc(), mcbSearchCompFunc(), printf, printMCBFunc(), s_cmcb::procName, s_cmcb::representingNode, setInsertComparisonFunction(), setPrintFunction(), and setSearchComparisonFunction().

Referenced by kmain().

12 {
13 
16 
23 
24  heapLoc = (heapLocationPtr_t)kmalloc(initialHeapSize + sizeof(lmcb_t) + sizeof(cmcb_t));
25 
26  printf("Heap Loc Ptr %d :: total size %d\r\n", (int)heapLoc, ((int)((int)initialHeapSize + sizeof(lmcb_t) + sizeof(cmcb_t))));
27 
28  cmcb_t* heapCMCB = (cmcb_t*)heapLoc;
29  heapCMCB->representingNode.data = (void*)heapCMCB;
30  heapCMCB->associatedLMCB = (lmcb_t*)(heapLoc+initialHeapSize+sizeof(cmcb_t));
31 
32  heapCMCB->cmcbType = heapCMCB->associatedLMCB->lmcbType = MCB_FREE;
33 
34  heapCMCB->blockStartAddress = (mcbAddressPtr_t)(heapLoc+sizeof(cmcb_t));
36  heapCMCB->procName = "Initial Heap";
37  heapCMCB->associatedLMCB->mcbBlockSize=heapCMCB->mcbBlockSize=initialHeapSize;
38  heapCMCB->associatedLMCB->lmcbType=MCB_FREE;
39 
40  insertMCBIntoList(heapCMCB);
41 }
void * heapLocationPtr_t
Definition: mcb.h:36
void setPrintFunction(linkedList_t *list, void(*newPrintFunc)(void *))
sets the function whose job it is to print the list to the screen.
Definition: linked_list.c:207
The s_cmcb struct the struct type for cmcb&#39;s -> defines the properties.
Definition: mcb.h:41
u32int kmalloc(u32int size)
Definition: heap.c:52
void insertMCBIntoList(cmcb_t *blockToInsert)
insertMCBIntoList inserts the mcb into the list specified.
Definition: mcb.c:271
void setSearchComparisonFunction(linkedList_t *list, int(*newSearchFunc)(void *, void *))
sets the function whose job it is to compare the input data with the data in the list. the first void parameter will always be the data contained in the list, the second will be the data passed to the search function.
Definition: linked_list.c:73
e_mcb_type_t cmcbType
Definition: mcb.h:45
void initLinkedList(linkedList_t *list)
initilize list and the optional array that backs the list
Definition: linked_list.c:3
heapLocationPtr_t heapLoc
Definition: mcb.c:3
e_mcb_type_t lmcbType
Definition: mcb.h:66
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
void printMCBFunc(void *cmcb)
printMCBFunc prints the type, block size, block pointer of the blocks in a given list.
Definition: mcb.c:223
void * mcbAddressPtr_t
Definition: mcb.h:32
int mcbCompareFunc(void *mcb, void *mcbToCompare)
mcbCompareFunc compares the start addresses of two mcbs.
Definition: mcb.c:135
mcbSize_t mcbBlockSize
Definition: mcb.h:47
mcbSize_t mcbBlockSize
Definition: mcb.h:64
mcbAddressPtr_t blockStartAddress
Definition: mcb.h:53
The s_lmcb struct defines the properties of lmcb&#39;s.
Definition: mcb.h:60
linkedList_t mcbFreeList
Definition: mcb.c:5
node_t representingNode
Definition: mcb.h:43
void setInsertComparisonFunction(linkedList_t *list, int(*newCompFunc)(void *, void *))
takes in a function pointer and sets the library shared comparison pointer
Definition: linked_list.c:63
struct s_cmcb cmcb_t
Definition: mcb.h:28
mcbAddressPtr_t blockStopAddress
Definition: mcb.h:62
lmcb_t * associatedLMCB
Definition: mcb.h:51
Definition: mcb.h:23
int mcbSearchCompFunc(void *mcb, void *blockStartAddress)
mcbSearchCompFunc searches for the mcb that you want to compare with.
Definition: mcb.c:144
linkedList_t mcbAllocList
Definition: mcb.c:6
const char * procName
Definition: mcb.h:49
void insertMCBIntoList ( cmcb_t blockToInsert)

insertMCBIntoList inserts the mcb into the list specified.

Parameters
blockToInsertthe mcb block to insert into.

Definition at line 271 of file mcb.c.

References s_cmcb::cmcbType, insertNode(), lastMCBError, MCB_ALLOCATED, MCB_FREE, MCB_SUCCESS, and s_cmcb::representingNode.

Referenced by allocateMemFromHeap(), freeHeapMem(), and initHeap().

272 {
274  switch (blockToInsert->cmcbType) {
275  case MCB_FREE:
276  insertNode(&mcbFreeList, &(blockToInsert->representingNode));
277  break;
278  case MCB_ALLOCATED:
279  insertNode(&mcbAllocList, &(blockToInsert->representingNode));
280  break;
281  default:
282  break;
283  }
284 }
e_mcb_type_t cmcbType
Definition: mcb.h:45
e_mcb_result_t lastMCBError
Definition: mcb.c:8
linkedList_t mcbFreeList
Definition: mcb.c:5
node_t representingNode
Definition: mcb.h:43
node_t * insertNode(linkedList_t *list, node_t *newNode)
Will insert the node into the list. This function will attempt to call the list&#39;s insert comparison f...
Definition: linked_list.c:173
Definition: mcb.h:23
linkedList_t mcbAllocList
Definition: mcb.c:6
int mcbCompareFunc ( void *  mcb,
void *  mcbToCompare 
)

mcbCompareFunc compares the start addresses of two mcbs.

Parameters
mcbpointer to mcb
mcbToComparepointer to mcb to compare with.
Returns

Definition at line 135 of file mcb.c.

References blockStartAddress.

Referenced by initHeap().

136 {
137  if(((cmcb_t*)mcb)->blockStartAddress == ((cmcb_t*)mcbToCompare)->blockStartAddress)
138  {
139  return 0;
140  }
141  return (((cmcb_t*)mcb)->blockStartAddress < ((cmcb_t*)mcbToCompare)->blockStartAddress)?-1:1;
142 }
The s_cmcb struct the struct type for cmcb&#39;s -> defines the properties.
Definition: mcb.h:41
mcbAddressPtr_t blockStartAddress
Definition: mcb.h:47
const char* mcbResultToString ( e_mcb_result_t  result)

Definition at line 235 of file mcb.c.

References ERROR_BLOCK_NOT_ALLOCATED, ERROR_BLOCK_NOT_FOUND, ERROR_GENERIC, ERROR_NOT_ENOUGH_MEMORY, MCB_ERROR_ACCESS_DENIED, and MCB_SUCCESS.

Referenced by heapTest().

236 {
237  switch(result)
238  {
239  case MCB_SUCCESS:
240  return "SUCCESS";
242  return "ERROR_BLOCK_NOT_FOUND";
244  return "ERROR_BLOCK_NOT_ALLOCATED";
246  return "ERROR_NOT_ENOUGH_MEMORY";
248  return "ACCESS_DENIED";
249 
250  case ERROR_GENERIC:
251  default:
252  return "ERROR_UNKNOWN";
253  }
254  return "ERROR_UNKNOWN";
255 }
int mcbSearchCompFunc ( void *  mcb,
void *  blockStartAddress 
)

mcbSearchCompFunc searches for the mcb that you want to compare with.

Parameters
mcbpointer to mcb.
blockStartAddressstart address of memory.
Returns

Definition at line 144 of file mcb.c.

Referenced by initHeap().

145 {
147  {
148  return 0;
149  }
150  return (((cmcb_t*)mcb)->blockStartAddress < blockStartAddress)?-1:1;
151 }
The s_cmcb struct the struct type for cmcb&#39;s -> defines the properties.
Definition: mcb.h:41
mcbAddressPtr_t blockStartAddress
Definition: mcb.h:47
const char* mcbTypeToString ( e_mcb_type_t  type)

Definition at line 257 of file mcb.c.

References MCB_ALLOCATED, and MCB_FREE.

Referenced by printMCBFunc().

258 {
259  switch(type)
260  {
261  case MCB_FREE:
262  return "FREE ";
263  case MCB_ALLOCATED:
264  return "ALLOCATED";
265  default:
266  break;
267  }
268  return "UNKNOWN ";
269 }
Definition: mcb.h:23
void printAlloc ( )

printAlloc prints the allocated memory blocks. Traverses through the allocated mcb list.

Definition at line 123 of file mcb.c.

References printf, and printList().

124 {
125  printf("%s", "\r\nAllocated Memory Blocks\r\n");
127 }
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
void printList(linkedList_t *list)
test function to show list functionality. uses const char* as test data
Definition: linked_list.c:212
linkedList_t mcbAllocList
Definition: mcb.c:6
void printFree ( )

printFree prints the free memory blocks. Traverses through the free mcb list.

Definition at line 129 of file mcb.c.

References printf, and printList().

130 {
131  printf("%s", "\r\nFree Memory Blocks\r\n");
133 }
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
void printList(linkedList_t *list)
test function to show list functionality. uses const char* as test data
Definition: linked_list.c:212
linkedList_t mcbFreeList
Definition: mcb.c:5
void printMCBFunc ( void *  cmcb)

printMCBFunc prints the type, block size, block pointer of the blocks in a given list.

Parameters
cmcbCMCB to be printed out.

Definition at line 223 of file mcb.c.

References blockStartAddress, cmcbType, mcbBlockSize, mcbTypeToString(), printf, and procName.

Referenced by freeHeapMem(), and initHeap().

224 {
225  printf("Type: %s || Associ Proc: %s || Block Size: %d || block ptr: %d\r\n", mcbTypeToString(((cmcb_t*)cmcb)->cmcbType),
226  ((cmcb_t*)cmcb)->procName, ((cmcb_t*)cmcb)->mcbBlockSize, (int)((cmcb_t*)cmcb)->blockStartAddress);
227 }
const char * mcbTypeToString(e_mcb_type_t type)
Definition: mcb.c:257
The s_cmcb struct the struct type for cmcb&#39;s -> defines the properties.
Definition: mcb.h:41
const char * procName
Definition: mcb.h:43
mcbSize_t mcbBlockSize
Definition: mcb.h:41
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
e_mcb_type_t cmcbType
Definition: mcb.h:39
mcbAddressPtr_t blockStartAddress
Definition: mcb.h:47
void reclaimFreeMem ( )

reclaimFreeMem groups free blocks together if they are adjacent.

Definition at line 184 of file mcb.c.

References s_cmcb::associatedLMCB, s_cmcb::blockStartAddress, s_lmcb::blockStopAddress, s_cmcb::mcbBlockSize, s_lmcb::mcbBlockSize, removeNode(), and s_cmcb::representingNode.

Referenced by freeHeapMem().

185 {
186  cmcb_t* foundCMCB=(cmcb_t*)mcbFreeList.head.next;
187  for(; &(foundCMCB->representingNode) != &(mcbFreeList.tail); foundCMCB=(cmcb_t*)foundCMCB->representingNode.next)
188  {
189  //check if not beginning of list
190  if(foundCMCB->representingNode.prev != &(mcbFreeList.head))
191  {
192  //chek if contiguous memory
193  if(((void*)((cmcb_t*)foundCMCB->representingNode.prev)->associatedLMCB)+sizeof(lmcb_t*) == foundCMCB)
194  {
195  //cmcbs are contiguous, combine
196 
197  ((cmcb_t*)foundCMCB->representingNode.prev)->associatedLMCB=foundCMCB->associatedLMCB;
198  foundCMCB=((cmcb_t*)foundCMCB->representingNode.prev);
199  foundCMCB->mcbBlockSize=foundCMCB->associatedLMCB->mcbBlockSize=
200  ((void*)foundCMCB->associatedLMCB->blockStopAddress)-
201  ((void*)foundCMCB->blockStartAddress); //set the new mcb block size
202 
203  (void)removeNode(foundCMCB->representingNode.next);
204  }
205  }
206  //check if not end of list
207  if(foundCMCB->representingNode.next != &(mcbFreeList.tail))
208  {
209  //chek if contiguous memory
210  if(((void*)(((cmcb_t*)foundCMCB->representingNode.next)))==(((void*)foundCMCB->associatedLMCB)+sizeof(lmcb_t)))
211  {
212  foundCMCB->associatedLMCB=((cmcb_t*)foundCMCB->representingNode.next)->associatedLMCB;
213  foundCMCB->mcbBlockSize=foundCMCB->associatedLMCB->mcbBlockSize=
214  ((void*)foundCMCB->associatedLMCB->blockStopAddress)-
215  ((void*)foundCMCB->blockStartAddress);
216 
217  (void)removeNode(foundCMCB->representingNode.next);
218  }
219  }
220  }
221 }
The s_cmcb struct the struct type for cmcb&#39;s -> defines the properties.
Definition: mcb.h:41
node_t * removeNode(node_t *nodeToRemove)
will remove the node from the list. the list hierarchy will change to reflect the missing node...
Definition: linked_list.c:120
struct s_lmcb lmcb_t
Definition: mcb.h:30
mcbSize_t mcbBlockSize
Definition: mcb.h:47
mcbSize_t mcbBlockSize
Definition: mcb.h:64
mcbAddressPtr_t blockStartAddress
Definition: mcb.h:53
The s_lmcb struct defines the properties of lmcb&#39;s.
Definition: mcb.h:60
linkedList_t mcbFreeList
Definition: mcb.c:5
node_t representingNode
Definition: mcb.h:43
mcbAddressPtr_t blockStopAddress
Definition: mcb.h:62
lmcb_t * associatedLMCB
Definition: mcb.h:51

Variable Documentation

heapLocationPtr_t heapLoc =0

Definition at line 3 of file mcb.c.

Referenced by initHeap().

e_mcb_result_t lastMCBError = MCB_SUCCESS

Definition at line 8 of file mcb.c.

Referenced by allocateMemFromHeap(), freeHeapMem(), heapIsEmpty(), heapTest(), and insertMCBIntoList().

linkedList_t mcbAllocList

Definition at line 6 of file mcb.c.

Referenced by mcbFunc().

linkedList_t mcbFreeList

Definition at line 5 of file mcb.c.

Referenced by mcbFunc().