1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     osl_MbufPool.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  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: 17658 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_NET_OSL_OSL_MBUF_POOL_H_
17 #define NN_NET_OSL_OSL_MBUF_POOL_H_
18 
19 #include <nn/net/osl/osl_Mbuf.h>
20 
21 #ifdef __cplusplus
22 
23 namespace nn {
24 namespace net {
25 namespace osl {
26 
27 class MbufPool
28 {
29 public:
30     typedef u16         IndexInPool;
31     typedef s32         FreeIndex;
32     class SharedArea;
33 
34     static const u16    INVALID_INDEX = 0xffff;
35 
36     MbufPool();
37     ~MbufPool();
38 
39     void Initialize(u16 id, s16 unitSize, s32 count, uptr base, bool bServer);
40     void Finalize(void);
41 
42     s32 GetMaxCount(void) const;
43     s32 GetFreeCount(void) const;
44 
45     size_t GetUnitSize(void) const;
46     size_t GetDataSize(void) const;
47     size_t GetUsingSize(void) const;
48 
49     Mbuf* TryAllocate(void);
50     void Free(Mbuf* pMbuf);
51 
52     Mbuf* GetPtrFromIndex(s32 index);
53     IndexInPool GetIndexFromPtr(const Mbuf* pMbuf) const;
54 
55     static size_t GetRequiredMemorySize(s16 unitSize, s32 count);
56 
57     bool CheckClean();
58 
59 private:
60     void ConstructFreeChain(void);
61 
62     s32* GetWaitCountPtr();
63     s32* GetFreeIndexPtr(void);
64 
65     void IncrementUsed(void);
66     void DecrementUsed(void);
67 
68     SharedArea*             m_pSharedArea;
69     uptr                    m_base;
70     s32                     m_count;
71     s16                     m_unitSize;
72     u16                     m_id;
73 };
74 
75 class MbufPool::SharedArea
76 {
77 public:
78     SharedArea(size_t unitSize, s32 count);
79     bool CheckInitialized(void) const;
80 
81     u32         m_magic;
82     size_t      m_unitSize;
83     s32         m_maxCount;
84     s32         m_usedCount;
85     FreeIndex   m_freeIndex;
86 
87 private:
88     static const u32    MAGIC   = 0x0b0f0b0f;
89 };
90 
GetUnitSize(void)91 inline size_t MbufPool::GetUnitSize(void) const
92 {
93     return m_unitSize;
94 }
95 
GetDataSize(void)96 inline size_t MbufPool::GetDataSize(void) const
97 {
98     return m_unitSize - Mbuf::HEADER_SIZE;
99 }
100 
GetPtrFromIndex(s32 index)101 inline Mbuf* MbufPool::GetPtrFromIndex(s32 index)
102 {
103     NN_TASSERTMSG_(0 <= index && index < m_count, "index = %d, m_count = %d", index, m_count);
104     return reinterpret_cast<Mbuf*>(m_base + m_unitSize * index);
105 }
106 
GetFreeIndexPtr(void)107 inline s32* MbufPool::GetFreeIndexPtr(void)
108 {
109     return &m_pSharedArea->m_freeIndex;
110 }
111 
GetUsingSize(void)112 inline size_t MbufPool::GetUsingSize(void) const
113 {
114     return GetRequiredMemorySize(m_unitSize, m_count);
115 }
116 
GetMaxCount(void)117 inline s32 MbufPool::GetMaxCount(void) const
118 {
119     return m_pSharedArea->m_maxCount;
120 }
121 
GetFreeCount(void)122 inline s32 MbufPool::GetFreeCount(void) const
123 {
124     return m_pSharedArea->m_maxCount - m_pSharedArea->m_usedCount;
125 }
126 
127 }
128 }
129 } // namespace nn::net::osl
130 
131 #endif // __cplusplus
132 
133 #endif // NN_NET_OSL_OSL_MBUF_POOL_H_
134