2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
6 #include <aros/debug.h>
7 #include <proto/exec.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
19 static inline void AccessTest(ULONG
*ptr
, BOOL trash
)
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
;
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
);
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
));
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 */
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
);
69 FreeMem(start
, allocsize
);
70 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY
));
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
));
81 output("Allocation failed!\n");
82 result
= RETURN_ERROR
;
89 int main(int argc
, char **argv
)
91 LONG result
= RETURN_OK
;
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"))
107 else if (!strcmp(argv
[i
], "leak"))
109 else if (!strcmp(argv
[i
], "notlsf"))
114 /* We Forbid() in order to see how our allocations influence free memory size */
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
);
129 output("Freeing the block...\n");
130 FreeMem(block0
, 256 * 1024);
131 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY
));
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
);
160 output("Freeing the block...\n");
161 FreeMem(block0
, 4096);
162 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY
));
167 output("Allocation failed!\n");
168 result
= RETURN_ERROR
;
171 output("\nFinal available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY
));