1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MI - include
3   File:     allocator.h
4 
5   Copyright 2007-2008 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   $Date:: 2008-11-04#$
14   $Rev: 9197 $
15   $Author: yosizaki $
16  *---------------------------------------------------------------------------*/
17 #ifndef	NITRO_MI_ALLOCATOR_H_
18 #define	NITRO_MI_ALLOCATOR_H_
19 
20 
21 #include <nitro/misc.h>
22 #include <nitro/types.h>
23 #include <nitro/platform.h>
24 
25 
26 #ifdef __cplusplus
27 extern  "C"
28 {
29 #endif
30 
31 
32 /*---------------------------------------------------------------------------*/
33 /* Declarations */
34 
35 /* Memory Allocator Function Prototypes */
36 typedef void* (*MIAllocatorAllocFunction)(void *userdata, u32 length, u32 alignment);
37 typedef void  (*MIAllocatorFreeFunction)(void *userdata, void *buffer);
38 
39 /* Memory Allocator Structures */
40 typedef struct MIAllocator
41 {
42     void                       *userdata;
43     MIAllocatorAllocFunction    Alloc;
44     MIAllocatorFreeFunction     Free;
45 }
46 MIAllocator;
47 
48 
49 /*---------------------------------------------------------------------------*/
50 /* Functions */
51 
52 /*---------------------------------------------------------------------------*
53   Name:         MI_InitAllocator
54 
55   Description:  Initializes the allocator.
56 
57   Arguments:    allocator        The MIAllocator structure to be initialized.
58                 userdata         Arbitrary user-defined argument.
59                 alloc            Pointer to the memory allocation function
60                 free             Pointer to the memory deallocation function
61 
62   Returns:      None.
63  *---------------------------------------------------------------------------*/
64 PLATFORM_ATTRIBUTE_INLINE
MI_InitAllocator(MIAllocator * allocator,void * userdata,MIAllocatorAllocFunction alloc,MIAllocatorFreeFunction free)65 void MI_InitAllocator(MIAllocator *allocator, void *userdata,
66                       MIAllocatorAllocFunction alloc,
67                       MIAllocatorFreeFunction free)
68 {
69     allocator->userdata = userdata;
70     allocator->Alloc = alloc;
71     allocator->Free = free;
72 }
73 
74 /*---------------------------------------------------------------------------*
75   Name:         MI_CallAlloc
76 
77   Description:  Allocates memory from the allocator.
78 
79   Arguments:    allocator        The initialized MIAllocator structure.
80                 length           The size to be allocated.
81                 alignment        The required byte-alignment (must be a power of 2).
82 
83   Returns:      Either the allocated memory or NULL.
84  *---------------------------------------------------------------------------*/
85 PLATFORM_ATTRIBUTE_INLINE
MI_CallAlloc(MIAllocator * allocator,u32 length,u32 alignment)86 void* MI_CallAlloc(MIAllocator *allocator, u32 length, u32 alignment)
87 {
88     return allocator->Alloc(allocator->userdata, length, alignment);
89 }
90 
91 /*---------------------------------------------------------------------------*
92   Name:         MI_CallFree
93 
94   Description:  Deallocates memory back to the allocator.
95 
96   Arguments:    allocator        The initialized MIAllocator structure.
97                 buffer           The memory to deallocate.
98 
99   Returns:      None.
100  *---------------------------------------------------------------------------*/
101 PLATFORM_ATTRIBUTE_INLINE
MI_CallFree(MIAllocator * allocator,void * buffer)102 void MI_CallFree(MIAllocator *allocator, void *buffer)
103 {
104     allocator->Free(allocator->userdata, buffer);
105 }
106 
107 
108 /*---------------------------------------------------------------------------*/
109 
110 
111 #ifdef __cplusplus
112 } /* extern "C" */
113 #endif
114 
115 
116 #endif /* NITRO_MI_ALLOCATOR_H_ */
117