1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_StreamBufferPool.cpp
4 
5   Copyright (C)2009-2010 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   $Revision: 30089 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/snd/snd_StreamBufferPool.h>
19 
20 namespace nw {
21 namespace snd {
22 namespace internal {
23 namespace driver {
24 
Initialize(void * pBuffer,size_t size,int blockCount)25 void StreamBufferPool::Initialize( void* pBuffer, size_t size, int blockCount )
26 {
27     if ( blockCount == 0 )
28     {
29         return;
30     }
31 
32     // ストリームバッファのアドレス (全チャンネル分)
33     m_pBuffer = pBuffer;
34 
35     // 全チャンネル分のストリームバッファサイズ
36     m_BufferSize = size;
37 
38     // 何チャンネル分か? (bcsar のストリームチャンネル数)
39     m_BlockCount = blockCount;
40 
41     // 1 チャンネルあたりのバッファサイズ
42     // (各バッファ頭が 32 バイト整列するように 32 バイトの倍数にする)
43     m_BlockSize = ut::RoundDown( size / blockCount, 32 );
44 
45     m_AllocCount = 0;
46     std::memset( m_AllocFlags, 0, sizeof(m_AllocFlags) );
47     // std::memset( m_pBuffer, 0, m_BufferSize );
48 
49     NW_ASSERTMSG( m_BlockCount <= BLOCK_MAX, "Too large stream buffer size." );
50 }
51 
Finalize()52 void StreamBufferPool::Finalize()
53 {
54     m_pBuffer = NULL;
55     m_BufferSize = 0;
56     m_BlockSize = 0;
57     m_BlockCount = 0;
58 }
59 
Alloc()60 void* StreamBufferPool::Alloc()
61 {
62     if ( m_AllocCount >= m_BlockCount ) return NULL;
63 
64     const int availableByte = ut::RoundUp( m_BlockCount, BIT_PER_BYTE ) / BIT_PER_BYTE;
65 
66     for( int byteIndex = 0 ; byteIndex < availableByte ; byteIndex++ )
67     {
68         const u8 byte = m_AllocFlags[ byteIndex ];
69         if ( byte == 0xff ) continue;
70 
71         u8 mask = ( 1 << 0 );
72         for( int bitIndex = 0 ; bitIndex < BIT_PER_BYTE ; bitIndex++, mask<<=1 )
73         {
74             if ( ( byte & mask ) == 0 )
75             {
76                 m_AllocFlags[ byteIndex ] |= mask;
77                 m_AllocCount++;
78 
79                 const int totalIndex = byteIndex * BIT_PER_BYTE + bitIndex;
80                 return ut::AddOffsetToPtr( m_pBuffer, m_BlockSize * totalIndex );
81             }
82         }
83     }
84 
85     return NULL;
86 }
87 
Free(void * pPtr)88 void StreamBufferPool::Free( void* pPtr )
89 {
90     PtrDiff offset = ut::GetOffsetFromPtr( m_pBuffer, pPtr );
91 
92     const unsigned long totalIndex = static_cast<unsigned long>( offset / m_BlockSize );
93     NW_ASSERT( totalIndex < BLOCK_MAX );
94     const unsigned long byteIndex = ( totalIndex / BIT_PER_BYTE );
95     const unsigned long bitIndex  = ( totalIndex % BIT_PER_BYTE );
96 
97     const int mask = ( 1 << bitIndex );
98     NW_ASSERT( ( m_AllocFlags[ byteIndex ] & mask ) != 0 );
99     m_AllocFlags[ byteIndex ] &= ~mask;
100     m_AllocCount--;
101     NW_ASSERT( m_AllocCount >= 0 );
102 }
103 
104 } // namespace nw::snd::internal::driver
105 } // namespace nw::snd::internal
106 } // namespace nw::snd
107 } // namespace nw
108 
109