fetch_and_build only uses a virtual target
[AROS-Contrib.git] / vpdf / system / memory.c
blob591dc1695f63557de231960084934b9b29dad3a4
2 /*
3 Simple memory allocation routines
4 */
6 #include <exec/memory.h>
7 #include <proto/exec.h>
8 #include <string.h>
9 #include <stdlib.h>
11 typedef struct memheader_t
13 unsigned int magic;
14 int size;
15 int pad[2]; /* to make header 16 bytes alligned */
16 } MemHeader;
18 static APTR memoryPool = NULL;
20 static void MemoryExit(void);
21 void kprintf(char *fmt,...);
23 void MemoryInit(void)
25 // initialize memory pool:
26 // puddle size: 1MB
27 // threshold : 256kB ( FIXME: find out better value )
29 memoryPool = CreatePool(MEMF_ANY | MEMF_SEM_PROTECTED, 1024*1024, 64*1024);
31 atexit(MemoryExit);
34 static void MemoryExit(void)
36 if (memoryPool != NULL)
37 DeletePool(memoryPool);
41 void mfree(void *mem)
43 if (memoryPool != NULL)
45 if (mem != 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));
54 return;
56 else
58 mh->magic = 0xABADCAFE;
59 FreePooled(memoryPool, mh, mh->size + sizeof(MemHeader) + 4);
65 void *mmalloc(int size)
67 if (size == 0)
68 return NULL;
70 if (memoryPool != NULL)
72 MemHeader *mh;
73 unsigned char *mem;
75 mh = AllocPooledAligned(memoryPool, size + sizeof(MemHeader) + 4, 16, 0);
76 mem = (unsigned char*)mh + sizeof(MemHeader);
78 if (mh != NULL)
80 mh->size = size;
81 mh->magic = 0xDEADBEEF;
82 *(unsigned int*)(mem + size) = 0xDEADBEEF;
84 return mem;
88 return NULL;
92 void *mcalloc(int a, int b)
94 void *mem = mmalloc(a * b);
96 if (mem != NULL)
98 memset(mem, 0, a * b);
99 return mem;
102 return NULL;