2 Copyright © 1995-2017, 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(%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
);
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(%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
);
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 static LONG
test_allocpooled()
91 LONG result
= RETURN_OK
;
92 APTR pool
, allocation
;
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));
106 output("Allocated at 0x%p\n", pool
);
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));
116 output("Allocated at 0x%p\n", pool
);
118 output("Allocation failed!\n");
119 result
= RETURN_ERROR
;
122 output("Freeing the pool\n");
128 output("Allocation failed!\n\n");
135 int main(int argc
, char **argv
)
137 LONG result
= RETURN_OK
;
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"))
153 else if (!strcmp(argv
[i
], "leak"))
155 else if (!strcmp(argv
[i
], "notlsf"))
160 /* We Forbid() in order to see how our allocations influence free memory size */
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
);
175 output("Freeing the block...\n");
176 FreeMem(block0
, 256 * 1024);
177 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY
));
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
);
206 output("Freeing the block...\n");
207 FreeMem(block0
, 4096);
208 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY
));
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
));