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: 19566 $
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     m_pBuffer = pBuffer;    // ストリームバッファのアドレス (全チャンネル分)
33     m_BufferSize = size;    // 全チャンネル分のストリームバッファサイズ
34     m_BlockCount = blockCount;          // 何チャンネル分か? (bcsar のストリームチャンネル数)
35     m_BlockSize = size / blockCount;    // 1 チャンネルあたりのバッファサイズ
36     m_AllocCount = 0;
37     std::memset( m_AllocFlags, 0, sizeof(m_AllocFlags) );
38     // std::memset( m_pBuffer, 0, m_BufferSize );
39 
40     NW_ASSERTMSG( m_BlockCount <= BLOCK_MAX, "Too large stream buffer size." );
41 }
42 
Finalize()43 void StreamBufferPool::Finalize()
44 {
45     m_pBuffer = NULL;
46     m_BufferSize = 0;
47     m_BlockSize = 0;
48     m_BlockCount = 0;
49 }
50 
Alloc()51 void* StreamBufferPool::Alloc()
52 {
53     if ( m_AllocCount >= m_BlockCount ) return NULL;
54 
55     const int availableByte = ut::RoundUp( m_BlockCount, BIT_PER_BYTE ) / BIT_PER_BYTE;
56 
57     for( int byteIndex = 0 ; byteIndex < availableByte ; byteIndex++ )
58     {
59         const u8 byte = m_AllocFlags[ byteIndex ];
60         if ( byte == 0xff ) continue;
61 
62         u8 mask = ( 1 << 0 );
63         for( int bitIndex = 0 ; bitIndex < BIT_PER_BYTE ; bitIndex++, mask<<=1 )
64         {
65             if ( ( byte & mask ) == 0 )
66             {
67                 m_AllocFlags[ byteIndex ] |= mask;
68                 m_AllocCount++;
69 
70                 const int totalIndex = byteIndex * BIT_PER_BYTE + bitIndex;
71                 return ut::AddOffsetToPtr( m_pBuffer, m_BlockSize * totalIndex );
72             }
73         }
74     }
75 
76     return NULL;
77 }
78 
Free(void * pPtr)79 void StreamBufferPool::Free( void* pPtr )
80 {
81     PtrDiff offset = ut::GetOffsetFromPtr( m_pBuffer, pPtr );
82 
83     const unsigned long totalIndex = static_cast<unsigned long>( offset / m_BlockSize );
84     NW_ASSERT( totalIndex < BLOCK_MAX );
85     const unsigned long byteIndex = ( totalIndex / BIT_PER_BYTE );
86     const unsigned long bitIndex  = ( totalIndex % BIT_PER_BYTE );
87 
88     const int mask = ( 1 << bitIndex );
89     NW_ASSERT( ( m_AllocFlags[ byteIndex ] & mask ) != 0 );
90     m_AllocFlags[ byteIndex ] &= ~mask;
91     m_AllocCount--;
92     NW_ASSERT( m_AllocCount >= 0 );
93 }
94 
95 } // namespace nw::snd::internal::driver
96 } // namespace nw::snd::internal
97 } // namespace nw::snd
98 } // namespace nw
99 
100