Converted bz2_au.library into the OS3, OS4 and MorphOS-compatible
[AROS.git] / test / exec / pool.c
blob8a5225c37349bfe4fd3af3c02493982892d5c55b
1 #include <aros/debug.h>
2 #include <proto/exec.h>
4 #include <stdio.h>
6 /*
7 * Re-define output to bug in order to get exact measurements.
8 * Console's history consumes memory, so this may look like a
9 * memory leak.
11 #define output printf
13 int main(void)
15 APTR chunks[200];
16 APTR pool;
17 int i;
19 /* We Forbid() in order to see how our allocations influence free memory size */
20 Forbid();
22 output("Available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
24 pool = CreatePool(MEMF_ANY, 4096, 4096);
25 output("Created pool 0x%p, available memory: %lu bytes\n", pool, (unsigned long)AvailMem(MEMF_ANY));
27 if (!pool)
29 Permit();
30 output("Failed to create pool!\n");
31 return 1;
34 output("Available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
36 output("Allocating 200 small chunks...\n");
37 for (i = 0; i < 200; i++)
39 chunks[i] = AllocPooled(pool, 50);
40 if (!chunks[i])
42 output("Failed to allocate chunk %u!\n", i);
43 break;
46 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
48 output("Now freeing them...\n");
49 for (i = 0; i < 200; i++)
51 if (!chunks[i])
52 break;
53 FreePooled(pool, chunks[i], 50);
55 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
57 output("Now allocating the whole puddle...\n");
58 chunks[0] = AllocPooled(pool, 4096);
59 output("Allocated at 0x%p, available memory: %lu bytes\n", chunks[0], (unsigned long)AvailMem(MEMF_ANY));
61 output("Now allocating a BIG chunk...\n");
62 chunks[1] = AllocPooled(pool, 16384);
63 output("Allocated at 0x%p, available memory: %lu bytes\n", chunks[1], (unsigned long)AvailMem(MEMF_ANY));
65 output("Freeing both chunks...\n");
66 FreePooled(pool, chunks[0], 4096);
67 FreePooled(pool, chunks[1], 16384);
69 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
71 output("Now attempting to re-allocate big chunk...\n");
72 chunks[1] = AllocPooled(pool, 16384);
73 output("Allocated at 0x%p, available memory: %lu bytes\n", chunks[1], (unsigned long)AvailMem(MEMF_ANY));
75 output("Freeing the whole pool...\n");
76 DeletePool(pool);
77 output("Done, available memory: %lu bytes\n", (unsigned long)AvailMem(MEMF_ANY));
79 Permit();
80 return 0;