1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     osl_MbufSystem.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_SYSTEM_H_
17 #define NN_NET_OSL_OSL_MBUF_SYSTEM_H_
18 
19 #ifdef __cplusplus
20 
21 #include <nn/types.h>
22 #include <nn/os/os_Event.h>
23 #include <nn/os/os_SharedMemory.h>
24 #include <nn/util/util_Singleton.h>
25 #include <nn/net/osl/osl_Mbuf.h>
26 #include <nn/net/osl/osl_MbufIndexed.h>
27 #include <nn/net/osl/osl_MbufPool.h>
28 
29 namespace nn {
30 namespace net {
31 namespace osl {
32 
33 class MbufSystem : public nn::util::Singleton<MbufSystem>
34 {
35 public:
36     typedef nnnetOslMbufIndex Index;
37     static const s32 MAX_POOL = 16;
38     struct Config
39     {
40         size_t  unitSize;
41         s32     count;
42     };
43 
44 protected:
45     static const u32 MAGIC = 0x9e3fbaba;
46     typedef nn::fnd::InterlockedVariable<s32> is32;
47     struct HeaderBlock
48     {
49         u32     magic;
50         u32     poolCount;
51         Config  configs[MAX_POOL];
52     };
53 
54 public:
55     static const size_t HEADER_SIZE = sizeof(HeaderBlock);
56 
Finalize(void)57     void Finalize(void){};
58     s32 GetPoolCount(void) const;
59 
60     void WaitFree(void);
61     void NotifyFree(void);
62 
63     static size_t GetRequiredMemorySize(const Config pConfigs[], s32 count);
64     static Mbuf* Allocate(size_t size);
65     static Mbuf* TryAllocate(size_t size);
66     static void Free(Mbuf* pMbuf);
67     static MbufIndexed* GetPtrFromIndex(Index index);
68     static Index GetIndexFromPtr(const Mbuf* pMbuf);
69 
70 protected:
71     MbufSystem();
72     ~MbufSystem();
73 
74     MbufPool                m_Pools[MAX_POOL];
75     os::SharedMemoryBlock   m_SharedMemory;
76     os::Event               m_Event;
77     HeaderBlock*            m_pHeaderBlock;
78 };
79 
GetPoolCount(void)80 inline s32 MbufSystem::GetPoolCount(void) const
81 {
82     return m_pHeaderBlock->poolCount;
83 }
84 
WaitFree(void)85 inline void MbufSystem::WaitFree(void)
86 {
87     m_Event.Wait();
88 }
89 
NotifyFree(void)90 inline void MbufSystem::NotifyFree(void)
91 {
92     m_Event.Signal();
93 }
94 
95 
96 class MbufServer : public MbufSystem
97 {
98 public:
MbufServer()99     MbufServer(){}
~MbufServer()100     ~MbufServer(){}
101 
102     void Initialize(const Config pConfigs[], s32 count);
103     void Finalize();
104 
105     void GetInfomation(nn::Handle* phSharedMemory, size_t* pSize, nn::Handle* phEvent);
106 };
107 
108 class MbufClient : public MbufSystem
109 {
110 public:
MbufClient()111     MbufClient(){}
~MbufClient()112     ~MbufClient(){}
113 
114     void Initialize(nn::Handle hSharedMemory, size_t size, nn::Handle hEvent);
Finalize()115     void Finalize(){}
116 };
117 
118 }
119 }
120 } // namespace nn::net::osl
121 
122 #endif
123 
124 #endif // NN_NET_OSL_OSL_MBUF_SYSTEM_H_
125