Trust uboot's device list only if it does not look suspicious.
[AROS.git] / test / exec / allocmem.c
blobdf8285b92957b65494da5caa3454309420dc2002
1 #include <aros/debug.h>
2 #include <proto/exec.h>
4 #include <stdio.h>
5 #include <string.h>
7 /*
8 * Re-define output to bug in order to get exact measurements.
9 * Console's history consumes memory, so this may look like a
10 * memory leak.
12 #define output printf
14 static BOOL trash = FALSE;
15 static BOOL leak = FALSE;
17 static inline void AccessTest(ULONG *ptr)
19 if (!trash)
20 return;
22 ptr[-1] = 0x40302010; /* This should NOT cause mungwall warning */
23 ptr[0] = 0x01020304; /* This SHOULD produce mungwall warning */
26 int main(int argc, char **argv)
28 int i;
29 APTR block0, start, block1;
32 * Do some memory trashing if started with "trash" argument.
33 * It's not adviced to do this without mungwall enabled.
34 * The actual purpose of this is to test mungwall functionality.
36 for (i = 1; i < argc; i++)
38 if (!strcmp(argv[i], "trash"))
39 trash = TRUE;
40 else if (!strcmp(argv[i], "leak"))
41 leak = TRUE;
44 /* We Forbid() in order to see how our allocations influence free memory size */
45 Forbid();
47 output("Available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
49 output("Allocating 256 KB...\n");
50 block0 = AllocMem(256 * 1024, MEMF_ANY);
51 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
53 AccessTest(block0 + 256 * 1024);
55 if (!leak)
57 output("Freeing the block...\n");
58 FreeMem(block0, 256 * 1024);
59 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
62 start = block0 + 1027; /* Add some none-round displacement to make the life harder */
63 output("Now trying AllocAbs() 4 KB at 0x%p\n", start);
64 block1 = AllocAbs(4096, start);
65 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
67 AccessTest(start + 4096);
69 if (!leak)
71 output("Freeing the block...\n");
72 FreeMem(block1, 4096 + start - block1);
73 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
76 output("Now repeat this AllocAbs(), but free using our requested start address...\n");
77 block1 = AllocAbs(4096, start);
78 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
80 if (block1)
82 AccessTest(start + 4096);
84 if (!leak)
86 output("Freeing the block...\n");
87 FreeMem(start, 4096);
88 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
92 output("And now trying MEMF_REVERSE...\n");
93 block0 = AllocMem(4096, MEMF_REVERSE);
94 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
96 /* This test actually proves that we don't hit for example MMIO region */
97 *((volatile ULONG *)block0) = 0xC0DEBAD;
98 if (*((volatile ULONG *)block0) != 0xC0DEBAD)
99 output("It's not a memory!!!\n");
101 AccessTest(block0 + 4096);
103 if (!leak)
105 output("Freeing the block...\n");
106 FreeMem(block0, 4096);
107 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
110 Permit();
111 return 0;