52 lines
1.5 KiB
C
52 lines
1.5 KiB
C
// The free disassembly translated to C so that my poor brain understands what's going on.
|
|
#include <stdint.h>
|
|
|
|
typedef struct MemoryBlock
|
|
{
|
|
struct MemoryBlock *prev;
|
|
struct MemoryBlock *next;
|
|
uint32_t status;
|
|
} MemoryBlock_t;
|
|
|
|
int free_list(uint32_t memory_block_addr)
|
|
{
|
|
MemoryBlock_t *current_block = (MemoryBlock_t*) (memory_block_addr - sizeof(MemoryBlock_t));
|
|
uint32_t current_block_status = current_block->status;
|
|
current_block_status &= 0xfffe; // clear alloc flag
|
|
current_block->status = current_block_status;
|
|
|
|
MemoryBlock_t *prev_block = current_block->prev;
|
|
uint32_t prev_block_status = prev_block->status;
|
|
|
|
if ((prev_block_status & 0x1) == 0) // prev block is free
|
|
{
|
|
prev_block_status += 6;
|
|
prev_block_status += current_block_status;
|
|
prev_block->status = prev_block_status;
|
|
prev_block->next = current_block->next;
|
|
|
|
MemoryBlock_t *next_block = current_block->next;
|
|
next_block->prev = prev_block;
|
|
current_block = current_block->prev;
|
|
}
|
|
|
|
MemoryBlock_t *next_block = current_block->next;
|
|
uint32_t next_block_status = next_block->status;
|
|
if ((next_block_status & 0x1) == 0) // next block is free
|
|
{
|
|
next_block_status += current_block->status;
|
|
next_block_status += 6;
|
|
current_block->status = next_block_status;
|
|
current_block->next = next_block->next;
|
|
next_block->prev = current_block;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
free_list(0); // not actually meant to be run; just check if it compiles
|
|
return 0;
|
|
}
|