1 /*---------------------------------------------------------------------------*
2   Project:     MEM library
3   File:        allocator.c
4   Programmers: Takano Makoto
5 
6   Copyright 2005 Nintendo. All rights reserved.
7 
8   These coded instructions, statements, and computer programs contain
9   proprietary information of Nintendo of America Inc. and/or Nintendo
10   Company Ltd., and are protected by Federal copyright law.  They may
11   not be disclosed to third parties or copied or duplicated in any form,
12   in whole or in part, without the prior written consent of Nintendo.
13  *---------------------------------------------------------------------------*/
14 
15 /*---------------------------------------------------------------------------*
16   Note:         a demo for using the expansion heap
17 
18         1. Creates extended heap, frame heap, unit heap, and OS heap from the arena.
19 
20         2. Creates allocators from these heaps, and performs a memory allocation test using the AllocateTest function.
21 
22  *---------------------------------------------------------------------------*/
23 
24 #include <revolution/mem.h>
25 
26 #define HEAP_SIZE           4096
27 
28 #define UNIT_BLOCK_SIZE     64
29 
30 
31 /*---------------------------------------------------------------------------*
32   Name:         AllocateTest
33 
34   Description:  Allocates a memory block having the specified size from the allocator.
35 
36   Arguments:    allocator   Specifies the allocator to allocate memory.
37                 size        Specifies the memory size to allocate.
38 
39   Returns:      None.
40  *---------------------------------------------------------------------------*/
41 static void
AllocateTest(MEMAllocator * allocator,u32 size)42 AllocateTest( MEMAllocator* allocator, u32 size )
43 {
44     void* addr;
45 
46     // Allocates memory.
47     //   If a size larger than the block size is specified for the unit heap, allocation fails, and NULL is returned.
48     //
49     addr = MEMAllocFromAllocator( allocator, size );
50 
51     if ( addr )
52     {
53         OSReport("Success: allocate %ld byte\n", size);
54         // Release memory.
55         //   Nothing is released in the frame heap because it is not possible to free memory in memory block units.
56         //
57         MEMFreeToAllocator( allocator, addr );
58     }
59     else
60     {
61         OSReport("Error: can't allocate %ld bytes\n", size );
62     }
63 }
64 
65 
66 
67 /*---------------------------------------------------------------------------*
68   Name:         main
69  *---------------------------------------------------------------------------*/
70 void
main(void)71 main( void )
72 {
73     u8* arenaLo;
74     u8* arenaHi;
75     OSInit();
76     arenaLo = OSGetArenaLo();
77     arenaHi = OSGetArenaHi();
78     arenaLo = OSInitAlloc( arenaLo, arenaHi, 1 );
79     OSSetArenaLo( arenaLo );
80 
81     {
82         // When creating the heap, the provided region is aligned internally as necessary.
83 
84         // Creation of the expanded heap
85         MEMHeapHandle hExpHeap  = MEMCreateExpHeap ( arenaLo, HEAP_SIZE );
86 
87         // Creation of the frame heap
88         MEMHeapHandle hFrmHeap  = MEMCreateFrmHeap ( arenaLo + HEAP_SIZE, HEAP_SIZE );
89 
90         // Creation of the unit heap
91         MEMHeapHandle hUnitHeap = MEMCreateUnitHeap( arenaLo + HEAP_SIZE * 2, HEAP_SIZE, UNIT_BLOCK_SIZE );
92 
93         // Creation of the OS heap
94         OSHeapHandle  hOSHeap   = OSCreateHeap( arenaLo + HEAP_SIZE * 3, arenaLo + HEAP_SIZE * 4 );
95 
96         // Extended heap test.
97         {
98             MEMAllocator allocator;
99 
100             MEMInitAllocatorForExpHeap( &allocator, hExpHeap, 4 );
101 
102             OSReport(" Exp Heap ---------------------------\n");
103 
104             AllocateTest( &allocator, 50 );
105             AllocateTest( &allocator, 100 );
106 
107         }
108 
109         // Frame heap test
110         {
111             MEMAllocator allocator;
112             MEMInitAllocatorForFrmHeap( &allocator, hFrmHeap, 4 );
113 
114             OSReport(" Frm Heap ---------------------------\n");
115 
116             AllocateTest( &allocator, 50 );
117             AllocateTest( &allocator, 100 );
118         }
119 
120         // Unit heap test
121         {
122             MEMAllocator allocator;
123             MEMInitAllocatorForUnitHeap( &allocator, hUnitHeap );
124 
125             OSReport(" Unit Heap ---------------------------\n");
126 
127             AllocateTest( &allocator, 50 );
128             AllocateTest( &allocator, 100 );
129         }
130 
131         // OS heap test
132         {
133             MEMAllocator allocator;
134             MEMInitAllocatorForOSHeap( &allocator, hOSHeap );
135 
136             OSReport(" OS Heap ---------------------------\n");
137 
138             AllocateTest( &allocator, 50 );
139             AllocateTest( &allocator, 100 );
140         }
141     }
142 
143 
144     while(1) {}
145 }
146 
147 
148 
149