1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     cx_Utility.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd./HAL Laboratory, Inc.  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   $Rev: 47707 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_LIBRARIES_CX_CX_UTILITY_H_
17 #define NN_LIBRARIES_CX_CX_UTILITY_H_
18 
19 #include <nn/types.h>
20 
21 #define NN_CX_CHECK_ALIGN(exp, align)                                         \
22     NN_TWARNING_((reinterpret_cast<uptr>(exp)) % (align) == 0,                \
23                  "%s(=0x%08x) should be %d byte aligned.", #exp, (exp), align)
24 
25 namespace nn {
26 namespace cx {
27 namespace internal {
28 
ConvertEndian(u32 x)29 inline u32 ConvertEndian( u32 x )
30 {
31 #if ( NN_ENDIAN == NN_ENDIAN_VALUE_BIG )
32     // Reflect the endianness on a big-endian platform
33     return ((x & 0xFF000000) >> 24) |
34            ((x & 0x00FF0000) >>  8) |
35            ((x & 0x0000FF00) <<  8) |
36            ((x & 0x000000FF) << 24);
37 #else
38     return x;
39 #endif
40 }
41 
ForceConvertEndian(u32 x)42 inline u32 ForceConvertEndian( u32 x )
43 {
44     return ((x & 0xFF000000) >> 24) |
45            ((x & 0x00FF0000) >>  8) |
46            ((x & 0x0000FF00) <<  8) |
47            ((x & 0x000000FF) << 24);
48 }
49 
ConvertEndian16(u16 x)50 inline u16 ConvertEndian16( u16 x )
51 {
52 #if ( NN_ENDIAN == NN_ENDIAN_VALUE_BIG )
53     // Reflect the endianness on a big-endian platform
54     return (u16)( ((x & 0xFF00) >> 8) |
55                   ((x & 0x00FF) << 8) );
56 #else
57     return x;
58 #endif
59 }
60 
ForceConvertEndian16(u16 x)61 inline u16 ForceConvertEndian16( u16 x )
62 {
63     return (u16)( ((x & 0xFF00) >> 8) |
64                   ((x & 0x00FF) << 8) );
65 }
66 
67 // LittleEndian 32bit Read
Read32Le(const u8 * p)68 inline u32 Read32Le(const u8* p)
69 {
70     return (p[0] <<  0) | (p[1] <<  8) | (p[2] << 16) | (p[3] << 24);
71 }
72 
73 }   // namespace internal
74 
75     namespace detail
76     {
77         class ZlibAllocator
78         {
79         public:
ZlibAllocator(void * p,size_t size)80             ZlibAllocator(void* p, size_t size)
81             : m_pHead(reinterpret_cast<bit8*>(p))
82             , m_pEnd (reinterpret_cast<bit8*>(p) + size)
83             {
84             }
85 
Allocate(void * pObj,unsigned numItems,size_t size)86             static void* Allocate(void* pObj, unsigned numItems, size_t size)
87             {
88                 return reinterpret_cast<ZlibAllocator*>(pObj)->Allocate(numItems * size);
89             }
90 
Free(void * pObj,void * pMemory)91             static void Free(void* pObj, void* pMemory)
92             {
93                 // Depending on the zlib implementation, after deallocation, does nothing, assuming there will be no allocation after freeing memory
94                 //
95                 //
96                 NN_UNUSED_VAR(pObj);
97                 NN_UNUSED_VAR(pMemory);
98             }
99 
100         private:
Allocate(size_t size)101             void* Allocate(size_t size)
102             {
103                 if( m_pHead + size <= m_pEnd )
104                 {
105                     void* pRet = m_pHead;
106                     m_pHead += size;
107                     return pRet;
108                 }
109                 else
110                 {
111                     return NULL;
112                 }
113             }
114 
115         private:
116             bit8*   m_pHead;
117             bit8*   m_pEnd;
118         };
119 
120         s32 ConvertZlibReturnCode(int zlibReturnCode);
121     }
122 
123 }   // namespace cx
124 }   // namespace nn
125 
126 /* NN_LIBRARIES_CX_CX_UTILITY_H_ */
127 #endif
128