test/exec: Partially revert 49874. AllocAbs start+size has to be within range of...
[AROS.git] / test / exec / allocmem.c
blobb3547db7febb28eb5dcaa1203989fd6f1a18eb34
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 BOOL trash = FALSE;
20 static BOOL leak = FALSE;
22 static inline void AccessTest(ULONG *ptr)
24 if (!trash)
25 return;
27 ptr[-1] = 0x40302010; /* This should NOT cause mungwall warning */
28 ptr[0] = 0x01020304; /* This SHOULD produce mungwall warning */
31 int main(int argc, char **argv)
33 LONG result = RETURN_OK;
34 int i;
35 APTR block0, start, block1;
38 * Do some memory trashing if started with "trash" argument.
39 * It's not adviced to do this without mungwall enabled.
40 * The actual purpose of this is to test mungwall functionality.
42 for (i = 1; i < argc; i++)
44 if (!strcmp(argv[i], "trash"))
45 trash = TRUE;
46 else if (!strcmp(argv[i], "leak"))
47 leak = TRUE;
50 /* We Forbid() in order to see how our allocations influence free memory size */
51 Forbid();
53 output("Available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
54 output("Largest chunk: %lu bytes\n\n", (unsigned long)AvailMem(MEMF_ANY|MEMF_LARGEST));
56 output("Testing AllocMem(256, MEMF_ANY) ...\n");
57 if ((block0 = AllocMem(256 * 1024, MEMF_ANY)) != NULL)
59 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
61 AccessTest(block0 + 256 * 1024);
63 if (!leak)
65 output("Freeing the block...\n");
66 FreeMem(block0, 256 * 1024);
67 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
70 else
72 output("Allocation failed!\n");
73 result = RETURN_ERROR;
76 start = block0 + 1027; /* Add some non-round displacement to make life harder */
77 output("\nTesting AllocAbs(4096, 0x%p) ...\n", start);
78 if ((block1 = AllocAbs(4096, start)) != NULL)
80 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
82 AccessTest(start + 4096);
84 if (!leak)
86 output("Freeing the block...\n");
87 FreeMem(block1, 4096 + start - block1);
88 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
91 else
93 output("Allocation failed!\n");
94 result = RETURN_ERROR;
97 /* Only perform the second AllocAbs() if leak isn't specified,
98 otherwise we just duplicate the previous test */
99 if (!leak)
101 output("\nTesting AllocAbs(4096, 0x%p), but free using its requested start address...\n", start);
102 if ((block1 = AllocAbs(4096, start)) != NULL)
104 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
106 AccessTest(start + 4096);
108 output("Freeing the block...\n");
109 FreeMem(start, 4096);
110 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
112 else
114 output("Allocation failed!\n");
115 result = RETURN_ERROR;
119 output("\nTesting AllocMem(4096, MEMF_ANY|MEMF_REVERSE) ...\n");
120 if ((block0 = AllocMem(4096, MEMF_ANY|MEMF_REVERSE)) != NULL)
122 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
124 /* This test actually proves that we don't hit for example MMIO region */
125 *((volatile ULONG *)block0) = 0xC0DEBAD;
126 if (*((volatile ULONG *)block0) != 0xC0DEBAD)
128 output("Invalid memory allocated!!!\n");
129 result = RETURN_ERROR;
132 AccessTest(block0 + 4096);
134 if (!leak)
136 output("Freeing the block...\n");
137 FreeMem(block0, 4096);
138 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
141 else
143 output("Allocation failed!\n");
144 result = RETURN_ERROR;
147 output("\nFinal available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
149 Permit();
150 return result;