3 Simple memory allocation routines
6 #include <exec/memory.h>
7 #include <proto/exec.h>
11 typedef struct memheader_t
15 int pad
[2]; /* to make header 16 bytes alligned */
18 static APTR memoryPool
= NULL
;
20 static void MemoryExit(void);
21 void kprintf(char *fmt
,...);
25 // initialize memory pool:
27 // threshold : 256kB ( FIXME: find out better value )
29 memoryPool
= CreatePool(MEMF_ANY
| MEMF_SEM_PROTECTED
, 1024*1024, 64*1024);
34 static void MemoryExit(void)
36 if (memoryPool
!= NULL
)
37 DeletePool(memoryPool
);
43 if (memoryPool
!= NULL
)
47 MemHeader
*mh
= (MemHeader
*)( (unsigned char*)mem
- sizeof(MemHeader
));
48 unsigned int *endofmem
= (unsigned int*)((unsigned char*)mem
+ mh
->size
);
50 if (mh
->magic
!= 0xDEADBEEF || *endofmem
!= 0xDEADBEEF)
52 kprintf("[Memory]: Invalid memory header! ptr = 0x%x size = %d head = 0x%x, tail = 0x%x\n", mem
, mh
->size
, mh
->magic
, *endofmem
);
53 DumpTaskState(FindTask(NULL
));
58 mh
->magic
= 0xABADCAFE;
59 FreePooled(memoryPool
, mh
, mh
->size
+ sizeof(MemHeader
) + 4);
65 void *mmalloc(int size
)
70 if (memoryPool
!= NULL
)
75 mh
= AllocPooledAligned(memoryPool
, size
+ sizeof(MemHeader
) + 4, 16, 0);
76 mem
= (unsigned char*)mh
+ sizeof(MemHeader
);
81 mh
->magic
= 0xDEADBEEF;
82 *(unsigned int*)(mem
+ size
) = 0xDEADBEEF;
92 void *mcalloc(int a
, int b
)
94 void *mem
= mmalloc(a
* b
);
98 memset(mem
, 0, a
* b
);