/*---------------------------------------------------------------------------* Project: Horizon File: osl_MbufPool.h Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 17658 $ *---------------------------------------------------------------------------*/ #ifndef NN_NET_OSL_OSL_MBUF_POOL_H_ #define NN_NET_OSL_OSL_MBUF_POOL_H_ #include #ifdef __cplusplus namespace nn { namespace net { namespace osl { class MbufPool { public: typedef u16 IndexInPool; typedef s32 FreeIndex; class SharedArea; static const u16 INVALID_INDEX = 0xffff; MbufPool(); ~MbufPool(); void Initialize(u16 id, s16 unitSize, s32 count, uptr base, bool bServer); void Finalize(void); s32 GetMaxCount(void) const; s32 GetFreeCount(void) const; size_t GetUnitSize(void) const; size_t GetDataSize(void) const; size_t GetUsingSize(void) const; Mbuf* TryAllocate(void); void Free(Mbuf* pMbuf); Mbuf* GetPtrFromIndex(s32 index); IndexInPool GetIndexFromPtr(const Mbuf* pMbuf) const; static size_t GetRequiredMemorySize(s16 unitSize, s32 count); bool CheckClean(); private: void ConstructFreeChain(void); s32* GetWaitCountPtr(); s32* GetFreeIndexPtr(void); void IncrementUsed(void); void DecrementUsed(void); SharedArea* m_pSharedArea; uptr m_base; s32 m_count; s16 m_unitSize; u16 m_id; }; class MbufPool::SharedArea { public: SharedArea(size_t unitSize, s32 count); bool CheckInitialized(void) const; u32 m_magic; size_t m_unitSize; s32 m_maxCount; s32 m_usedCount; FreeIndex m_freeIndex; private: static const u32 MAGIC = 0x0b0f0b0f; }; inline size_t MbufPool::GetUnitSize(void) const { return m_unitSize; } inline size_t MbufPool::GetDataSize(void) const { return m_unitSize - Mbuf::HEADER_SIZE; } inline Mbuf* MbufPool::GetPtrFromIndex(s32 index) { NN_TASSERTMSG_(0 <= index && index < m_count, "index = %d, m_count = %d", index, m_count); return reinterpret_cast(m_base + m_unitSize * index); } inline s32* MbufPool::GetFreeIndexPtr(void) { return &m_pSharedArea->m_freeIndex; } inline size_t MbufPool::GetUsingSize(void) const { return GetRequiredMemorySize(m_unitSize, m_count); } inline s32 MbufPool::GetMaxCount(void) const { return m_pSharedArea->m_maxCount; } inline s32 MbufPool::GetFreeCount(void) const { return m_pSharedArea->m_maxCount - m_pSharedArea->m_usedCount; } } } } // namespace nn::net::osl #endif // __cplusplus #endif // NN_NET_OSL_OSL_MBUF_POOL_H_