02.01.2015 Views

CooCox CoOS User's Guide

CooCox CoOS User's Guide

CooCox CoOS User's Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

while allocate the memory. To all the memory blocks, whether they are free or<br />

have been allocated, <strong>CooCox</strong> <strong>CoOS</strong> will link them through a doubly linked list.<br />

Thus, when an allocated memory releases, there is no need to find the<br />

insertion point of the memory from the beginning of the free list. You only need<br />

to find the former free block in the doubly linked list and then insert the<br />

memory into the free list, which has greatly improved the speed of memory<br />

releasing.<br />

In <strong>CooCox</strong> <strong>CoOS</strong>, since all the memory is 4-byte alignment (if the space that<br />

needs to allocate is not 4-byte alignment, force it to be), the last two bits of the<br />

previous and next memory address that saved in the head of the memory are all<br />

invalid. Therefore, <strong>CooCox</strong> <strong>CoOS</strong> determines whether the memory is free<br />

through the least significant bit: bit 0 refers to the free block, otherwise refers<br />

to the allocated ones. If the memory list points to the free block, get the list<br />

address directly. If it points to the allocated one, decrease it by one.<br />

Code 3 The head of allocated memory block<br />

typedef struct UsedMemBlk<br />

{<br />

void* nextMB;<br />

void* preMB;<br />

}UMB,*P_UMB;<br />

Code 4 The head of free memory block<br />

typedef struct FreeMemBlk<br />

{<br />

struct FreeMemBlk* nextFMB;<br />

struct UsedMemBlk* nextUMB;<br />

struct UsedMemBlk* preUMB;<br />

}FMB,*P_FMB;<br />

For memory block 1 and 2, the memory block itself preserves the address of<br />

the previous free memory block when released, so it is very easy to plug back to<br />

the free list. You only need to decide whether to merge them according to if the<br />

memory block’s next block is free.<br />

For memory block 3 and 4, its previous memory block is not a free memory<br />

block when released. It is essential to get the previous free memory address<br />

through two-way list when plug it back to the free list.<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!