1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_StreamBufferPool.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/snd/snd_StreamBufferPool.h>
21 
22 namespace nw {
23 namespace snd {
24 namespace internal {
25 namespace driver {
26 
Initialize(void * pBuffer,size_t size,int blockCount)27 void StreamBufferPool::Initialize( void* pBuffer, size_t size, int blockCount )
28 {
29     if ( blockCount == 0 )
30     {
31         return;
32     }
33 
34     // ストリームバッファのアドレス (全チャンネル分)
35     m_pBuffer = pBuffer;
36 
37     // 全チャンネル分のストリームバッファサイズ
38     m_BufferSize = size;
39 
40     // 何チャンネル分か? (bcsar のストリームチャンネル数)
41     m_BlockCount = blockCount;
42 
43     // 1 チャンネルあたりのバッファサイズ
44     // (各バッファ頭が 32 バイト整列するように 32 バイトの倍数にする)
45     m_BlockSize = ut::RoundDown( size / blockCount, 32 );
46 
47     m_AllocCount = 0;
48     std::memset( m_AllocFlags, 0, sizeof(m_AllocFlags) );
49     // std::memset( m_pBuffer, 0, m_BufferSize );
50 
51     NW_ASSERTMSG( m_BlockCount <= BLOCK_MAX, "Too large stream buffer size." );
52 }
53 
Finalize()54 void StreamBufferPool::Finalize()
55 {
56     m_pBuffer = NULL;
57     m_BufferSize = 0;
58     m_BlockSize = 0;
59     m_BlockCount = 0;
60 }
61 
Alloc()62 void* StreamBufferPool::Alloc()
63 {
64     if ( m_AllocCount >= m_BlockCount ) return NULL;
65 
66     const int availableByte = ut::RoundUp( m_BlockCount, BIT_PER_BYTE ) / BIT_PER_BYTE;
67 
68     for( int byteIndex = 0 ; byteIndex < availableByte ; byteIndex++ )
69     {
70         const u8 byte = m_AllocFlags[ byteIndex ];
71         if ( byte == 0xff ) continue;
72 
73         u8 mask = ( 1 << 0 );
74         for( int bitIndex = 0 ; bitIndex < BIT_PER_BYTE ; bitIndex++, mask<<=1 )
75         {
76             if ( ( byte & mask ) == 0 )
77             {
78                 m_AllocFlags[ byteIndex ] |= mask;
79                 m_AllocCount++;
80 
81                 const int totalIndex = byteIndex * BIT_PER_BYTE + bitIndex;
82                 return ut::AddOffsetToPtr( m_pBuffer, m_BlockSize * totalIndex );
83             }
84         }
85     }
86 
87     return NULL;
88 }
89 
Free(void * pPtr)90 void StreamBufferPool::Free( void* pPtr )
91 {
92     PtrDiff offset = ut::GetOffsetFromPtr( m_pBuffer, pPtr );
93 
94     const unsigned long totalIndex = static_cast<unsigned long>( offset / m_BlockSize );
95     NW_ASSERT( totalIndex < BLOCK_MAX );
96     const unsigned long byteIndex = ( totalIndex / BIT_PER_BYTE );
97     const unsigned long bitIndex  = ( totalIndex % BIT_PER_BYTE );
98 
99     const int mask = ( 1 << bitIndex );
100     NW_ASSERT( ( m_AllocFlags[ byteIndex ] & mask ) != 0 );
101     m_AllocFlags[ byteIndex ] &= ~mask;
102     m_AllocCount--;
103     NW_ASSERT( m_AllocCount >= 0 );
104 }
105 
106 } // namespace nw::snd::internal::driver
107 } // namespace nw::snd::internal
108 } // namespace nw::snd
109 } // namespace nw
110 
111