Copyright clean-up (part 1):
[AROS.git] / test / exec / allocmem.c
blobbbf40ca143447c977dccc049bdcb04829c4deed8
1 /*
2 Copyright © 1995-2014, 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 int i;
34 APTR block0, start, block1;
37 * Do some memory trashing if started with "trash" argument.
38 * It's not adviced to do this without mungwall enabled.
39 * The actual purpose of this is to test mungwall functionality.
41 for (i = 1; i < argc; i++)
43 if (!strcmp(argv[i], "trash"))
44 trash = TRUE;
45 else if (!strcmp(argv[i], "leak"))
46 leak = TRUE;
49 /* We Forbid() in order to see how our allocations influence free memory size */
50 Forbid();
52 output("Available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
54 output("Allocating 256 KB...\n");
55 block0 = AllocMem(256 * 1024, MEMF_ANY);
56 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
58 AccessTest(block0 + 256 * 1024);
60 if (!leak)
62 output("Freeing the block...\n");
63 FreeMem(block0, 256 * 1024);
64 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
67 start = block0 + 1027; /* Add some none-round displacement to make the life harder */
68 output("Now trying AllocAbs() 4 KB at 0x%p\n", start);
69 block1 = AllocAbs(4096, start);
70 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
72 AccessTest(start + 4096);
74 if (!leak)
76 output("Freeing the block...\n");
77 FreeMem(block1, 4096 + start - block1);
78 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
81 output("Now repeat this AllocAbs(), but free using our requested start address...\n");
82 block1 = AllocAbs(4096, start);
83 output("Allocated at 0x%p, available memory: %lu bytes\n", block1, (unsigned long)AvailMem(MEMF_ANY));
85 if (block1)
87 AccessTest(start + 4096);
89 if (!leak)
91 output("Freeing the block...\n");
92 FreeMem(start, 4096);
93 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
97 output("And now trying MEMF_REVERSE...\n");
98 block0 = AllocMem(4096, MEMF_REVERSE);
99 output("Allocated at 0x%p, available memory: %lu bytes\n", block0, (unsigned long)AvailMem(MEMF_ANY));
101 /* This test actually proves that we don't hit for example MMIO region */
102 *((volatile ULONG *)block0) = 0xC0DEBAD;
103 if (*((volatile ULONG *)block0) != 0xC0DEBAD)
104 output("It's not a memory!!!\n");
106 AccessTest(block0 + 4096);
108 if (!leak)
110 output("Freeing the block...\n");
111 FreeMem(block0, 4096);
112 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
115 Permit();
116 return 0;