1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) 2010-2011 Nintendo.  All rights reserved.
4 
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law.  They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10 
11  *---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*
13   Project:     MEM library
14   File:        blockHeap.h
15   Programmers: k2
16 
17   Copyright (C) 2005 Nintendo.  All rights reserved.
18 
19   These coded instructions, statements, and computer programs contain
20   proprietary information of Nintendo of America Inc. and/or Nintendo
21   Company Ltd., and are protected by Federal copyright law.  They may
22   not be disclosed to third parties or copied or duplicated in any form,
23   in whole or in part, without the prior written consent of Nintendo.
24  *---------------------------------------------------------------------------*/
25 
26 #ifndef MEM_BLOCKHEAP_H__
27 #define MEM_BLOCKHEAP_H__
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <types.h>
34 #include <cafe/mem/heapCommon.h>
35 
36 /* ========================================================================
37    defines
38    ======================================================================== */
39 #define MEM_BLKHEAP_BYTES_PER_BLOCK_TRACK   20
40 #define MEM_BLKHEAP_TRACK_GROUP_OVERHEAD    16
41 
42 #define MEM_BLKHEAP_TRACKING_BYTES(numTrack)  \
43     ((numTrack * MEM_BLKHEAP_BYTES_PER_BLOCK_TRACK) + MEM_BLKHEAP_TRACK_GROUP_OVERHEAD)
44 
45 /* ========================================================================
46     these are usually statically allocated
47    ======================================================================== */
48 typedef struct _MEMBlockHeap
49 {
50     // must be first thing in structure
51     MEMiHeapHead    head;
52     u8              null_block[MEM_BLKHEAP_TRACKING_BYTES(1)];
53     void *          first_block;
54     void *          last_block;
55     void *          next_free_block;
56     u32             num_free_blocks_left;
57 } MEMBlockHeap;
58 
59 /* ========================================================================
60    functions
61    ======================================================================== */
62 
63 void        MEMiDumpBlockHeap( MEMHeapHandle heap );
64 
65 MEMHeapHandle  MEMInitBlockHeap(
66                         MEMBlockHeap *  tracking,
67                         void *          start_addr,
68                         void *          end_addr,
69                         void *          init_track_mem,
70                         u32             init_track_mem_bytes,
71                         u16             optFlag
72                         );
73 
74 int         MEMAddBlockHeapTracking(MEMHeapHandle heap, void *track_mem, u32 track_mem_bytes);
75 
76 void*       MEMDestroyBlockHeap  ( MEMHeapHandle heap );
77 
78 void*       MEMAllocFromBlockHeapAt( MEMHeapHandle heap, void *place, u32 size );
79 
80 void*       MEMAllocFromBlockHeapEx( MEMHeapHandle heap, u32 size, int alignment );
81 
82 void        MEMFreeToBlockHeap(
83                         MEMHeapHandle  heap,
84                         void*          memBlock );
85 
86 u32         MEMGetTrackingLeftInBlockHeap( MEMHeapHandle heap );
87 
88 u32         MEMGetTotalFreeSizeForBlockHeap( MEMHeapHandle heap );
89 
90 u32         MEMGetAllocatableSizeForBlockHeapEx( MEMHeapHandle heap, int alignment );
91 
92 /* ========================================================================
93    inlines
94    ======================================================================== */
95 static inline void *
MEMAllocFromBlockHeap(MEMHeapHandle heap,u32 size)96 MEMAllocFromBlockHeap(
97     MEMHeapHandle    heap,
98     u32              size
99 )
100 {
101     return MEMAllocFromBlockHeapEx( heap, size, MEM_HEAP_DEFAULT_ALIGNMENT );
102 }
103 
104 static inline u32
MEMGetAllocatableSizeForBlockHeap(MEMHeapHandle heap)105 MEMGetAllocatableSizeForBlockHeap( MEMHeapHandle heap )
106 {
107     return MEMGetAllocatableSizeForBlockHeapEx( heap, MEM_HEAP_DEFAULT_ALIGNMENT );
108 }
109 
110 
111 #ifdef __cplusplus
112 } /* extern "C" */
113 #endif
114 
115 /* MEM_UNITHEAP_H__ */
116 #endif
117 
118