wip commit. acpi button driver.
[AROS.git] / test / exec / allocmem.c
blob161f45d7d9253052740051e012a0e1cd4e7a9577
1 /*
2 Copyright © 1995-2017, 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(%lu, 0x%p) ...\n", (unsigned long)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(%lu, 0x%p), but free using its requested start address...\n", (unsigned long)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 static LONG test_allocpooled()
91 LONG result = RETURN_OK;
92 APTR pool, allocation;
93 int allocstep;
95 for (allocstep = 1; allocstep < 10; allocstep++)
97 output("\nTesting CreatePool(MEMF_CLEAR, %d, %d) ...\n", (allocstep << 10), (allocstep << 9));
98 if ((pool = CreatePool(MEMF_CLEAR, (allocstep << 10), (allocstep << 9))) != NULL)
100 output("Allocated at 0x%p\n", pool);
102 /* one that fits .. */
103 output("\nTesting AllocPooled(0x%p, %d) ...\n", pool, (allocstep << 8));
104 allocation = AllocPooled(pool, (allocstep << 8));
105 if(allocation) {
106 output("Allocated at 0x%p\n", pool);
107 } else {
108 output("Allocation failed!\n");
109 result = RETURN_ERROR;
112 /* and one that doesnt .. */
113 output("\nTesting AllocPooled(0x%p, %d) ...\n", pool, (allocstep << 11));
114 allocation = AllocPooled(pool, (allocstep << 11));
115 if(allocation) {
116 output("Allocated at 0x%p\n", pool);
117 } else {
118 output("Allocation failed!\n");
119 result = RETURN_ERROR;
122 output("Freeing the pool\n");
123 DeletePool(pool);
124 output("Done\n\n");
126 else
128 output("Allocation failed!\n\n");
131 return result;
135 int main(int argc, char **argv)
137 LONG result = RETURN_OK;
138 int i;
139 APTR block0;
140 BOOL trash = FALSE;
141 BOOL leak = FALSE;
142 BOOL notlsf = FALSE;
145 * Do some memory trashing if started with "trash" argument.
146 * It's not adviced to do this without mungwall enabled.
147 * The actual purpose of this is to test mungwall functionality.
149 for (i = 1; i < argc; i++)
151 if (!strcmp(argv[i], "trash"))
152 trash = TRUE;
153 else if (!strcmp(argv[i], "leak"))
154 leak = TRUE;
155 else if (!strcmp(argv[i], "notlsf"))
156 notlsf = TRUE;
160 /* We Forbid() in order to see how our allocations influence free memory size */
161 Forbid();
163 output("Available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
164 output("Largest chunk: %lu bytes\n\n", (unsigned long)AvailMem(MEMF_ANY|MEMF_LARGEST));
166 output("Testing AllocMem(256 * 1024, MEMF_ANY) ...\n");
167 if ((block0 = AllocMem(256 * 1024, MEMF_ANY)) != NULL)
169 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
171 AccessTest(block0 + 256 * 1024, trash);
173 if (!leak)
175 output("Freeing the block...\n");
176 FreeMem(block0, 256 * 1024);
177 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
180 else
182 output("Allocation failed!\n");
183 result = RETURN_ERROR;
186 if(test_allocabs(block0, trash, leak, notlsf) != RETURN_OK)
187 result = RETURN_ERROR;
189 output("\nTesting AllocMem(4096, MEMF_ANY|MEMF_REVERSE) ...\n");
190 if ((block0 = AllocMem(4096, MEMF_ANY|MEMF_REVERSE)) != NULL)
192 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
194 /* This test actually proves that we don't hit for example MMIO region */
195 *((volatile ULONG *)block0) = 0xC0DEBAD;
196 if (*((volatile ULONG *)block0) != 0xC0DEBAD)
198 output("Invalid memory allocated!!!\n");
199 result = RETURN_ERROR;
202 AccessTest(block0 + 4096, trash);
204 if (!leak)
206 output("Freeing the block...\n");
207 FreeMem(block0, 4096);
208 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
211 else
213 output("Allocation failed!\n");
214 result = RETURN_ERROR;
217 result = test_allocpooled();
219 output("\nFinal available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
221 Permit();
222 return result;