include our private headers
[AROS.git] / test / exec / allocmem.c
blobed45446f4cd9c57c8cc56455469c84df6f552caf
1 /*
2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/debug.h>
7 #include <proto/exec.h>
9 #include <stdio.h>
10 #include <string.h>
13 * Re-define output to bug in order to get exact measurements.
14 * Console's history consumes memory, so this may look like a
15 * memory leak.
17 #define output printf
19 static inline void AccessTest(ULONG *ptr, BOOL trash)
21 if (!trash)
22 return;
24 ptr[-1] = 0x40302010; /* This should NOT cause mungwall warning */
25 ptr[0] = 0x01020304; /* This SHOULD produce mungwall warning */
28 static LONG test_allocabs(APTR block0, BOOL trash, BOOL leak, BOOL notlsf)
30 LONG result = RETURN_OK;
31 APTR start, block1;
32 const ULONG allocsize = 4096;
34 start = block0 + 1027; /* Add some non-round displacement to make life harder */
35 output("\nTesting AllocAbs(%d, 0x%p) ...\n", allocsize, start);
36 if ((block1 = AllocAbs(allocsize, start)) != NULL)
38 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
40 AccessTest(start + allocsize, trash);
42 if (!leak)
44 output("Freeing the block at 0x%p of %lu bytes...\n", block1, (unsigned long)(allocsize + start - block1));
45 FreeMem(block1, allocsize + start - block1);
46 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
49 else
51 output("Allocation failed!\n");
52 result = RETURN_ERROR;
55 /* Only perform the second AllocAbs() if leak isn't specified,
56 otherwise we just duplicate the previous test */
57 if (!leak)
59 output("\nTesting AllocAbs(%u, 0x%p), but free using its requested start address...\n", allocsize, start);
60 if ((block1 = AllocAbs(allocsize, start)) != NULL)
62 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
64 AccessTest(start + allocsize, trash);
66 output("Freeing the block at 0x%p of %lu bytes...\n", start, (unsigned long)allocsize);
67 if (notlsf)
69 FreeMem(start, allocsize);
70 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
72 else
74 FreeMem(block1, allocsize + start - block1);
75 output("NOT SUPPORTED UNDER TLSF, freeing using returned address\n");
76 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
79 else
81 output("Allocation failed!\n");
82 result = RETURN_ERROR;
86 return result;
89 int main(int argc, char **argv)
91 LONG result = RETURN_OK;
92 int i;
93 APTR block0;
94 BOOL trash = FALSE;
95 BOOL leak = FALSE;
96 BOOL notlsf = FALSE;
99 * Do some memory trashing if started with "trash" argument.
100 * It's not adviced to do this without mungwall enabled.
101 * The actual purpose of this is to test mungwall functionality.
103 for (i = 1; i < argc; i++)
105 if (!strcmp(argv[i], "trash"))
106 trash = TRUE;
107 else if (!strcmp(argv[i], "leak"))
108 leak = TRUE;
109 else if (!strcmp(argv[i], "notlsf"))
110 notlsf = TRUE;
114 /* We Forbid() in order to see how our allocations influence free memory size */
115 Forbid();
117 output("Available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
118 output("Largest chunk: %lu bytes\n\n", (unsigned long)AvailMem(MEMF_ANY|MEMF_LARGEST));
120 output("Testing AllocMem(256 * 1024, MEMF_ANY) ...\n");
121 if ((block0 = AllocMem(256 * 1024, MEMF_ANY)) != NULL)
123 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
125 AccessTest(block0 + 256 * 1024, trash);
127 if (!leak)
129 output("Freeing the block...\n");
130 FreeMem(block0, 256 * 1024);
131 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
134 else
136 output("Allocation failed!\n");
137 result = RETURN_ERROR;
140 if(test_allocabs(block0, trash, leak, notlsf) != RETURN_OK)
141 result = RETURN_ERROR;
143 output("\nTesting AllocMem(4096, MEMF_ANY|MEMF_REVERSE) ...\n");
144 if ((block0 = AllocMem(4096, MEMF_ANY|MEMF_REVERSE)) != NULL)
146 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
148 /* This test actually proves that we don't hit for example MMIO region */
149 *((volatile ULONG *)block0) = 0xC0DEBAD;
150 if (*((volatile ULONG *)block0) != 0xC0DEBAD)
152 output("Invalid memory allocated!!!\n");
153 result = RETURN_ERROR;
156 AccessTest(block0 + 4096, trash);
158 if (!leak)
160 output("Freeing the block...\n");
161 FreeMem(block0, 4096);
162 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
165 else
167 output("Allocation failed!\n");
168 result = RETURN_ERROR;
171 output("\nFinal available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
173 Permit();
174 return result;