1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin OS Overview - Multiple heap demo
3   File:     allocdemo3-multipleheaps.c
4 
5   Copyright 1998, 1999 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: allocdemo3-multipleheaps.c,v $
14   Revision 1.2  02/20/2006 04:13:11  mitu
15   changed include path from dolphin/ to revolution/.
16 
17   Revision 1.1  01/13/2006 11:24:12  hiratsu
18   Initial check in.
19 
20 
21     2     6/10/99 11:34p Hashida
22     Changed not to do OSRoundDown32B() to arenaHi so that the local arenaHi
23     will be consistent with system arenaHi.
24 
25     1     6/04/99 3:04p Tianli01
26     Initial check-in
27   $NoKeywords: $
28  *---------------------------------------------------------------------------*/
29 
30 /*---------------------------------------------------------------------------*
31   This program shows how to set up and use multiple heaps
32  *---------------------------------------------------------------------------*/
33 
34 
35 #include <revolution.h>
36 
37 // Heap sizes MUST be multiples of 32
38 #define HEAP1_SIZE      65536
39 #define HEAP2_SIZE      4096
40 
41 #define OBJ1SIZE        1024
42 #define OBJ2SIZE        2048
43 
44 OSHeapHandle            Heap1, Heap2;
45 
main()46 void main ()
47 {
48     u8*     arenaLo;
49     u8*     arenaHi;
50     void*   fromHeap1;
51     void*   fromHeap2;
52 
53     OSInit();
54 
55     OSReport("\n-----------------------------------");
56     OSReport("\n  Hit Command+Q to quit this demo");
57     OSReport("\n-----------------------------------\n\n");
58 
59     arenaLo = OSGetArenaLo();
60     arenaHi = OSGetArenaHi();
61     arenaLo = OSInitAlloc(arenaLo, arenaHi, 2); // 2 heaps
62     OSSetArenaLo(arenaLo);
63 
64     // Ensure boundary is 32B aligned
65     arenaLo = (void*)OSRoundUp32B(arenaLo);
66 
67     Heap1 = OSCreateHeap(arenaLo, arenaLo + HEAP1_SIZE);
68     arenaLo += HEAP1_SIZE;
69     Heap2 = OSCreateHeap(arenaLo, arenaLo + HEAP2_SIZE);
70     arenaLo += HEAP2_SIZE;
71 
72     OSSetArenaLo(arenaLo);
73 
74     OSSetCurrentHeap(Heap1);
75     fromHeap1 = OSAlloc(OBJ1SIZE);
76 
77     // Some code allocating from heap1 goes here
78     // OSFree will free to heap1 as well
79 
80     OSSetCurrentHeap(Heap2);
81     fromHeap2 = OSAlloc(OBJ2SIZE);
82 
83     // Some code allocating from heap2 goes here
84     // OSFree will free to heap2
85 
86     OSFreeToHeap(Heap1, fromHeap1);
87     OSFreeToHeap(Heap2, fromHeap2);
88 
89     // example of allocation overriding the current heap
90     fromHeap1 = OSAllocFromHeap(Heap1, OBJ2SIZE);
91 
92     OSHalt("Demo complete");
93 }
94 
95