mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-12-14 12:09:36 +00:00
More work. Audio driver works enough to play music now.
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <new>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace PortabilityLayer
|
||||
{
|
||||
@@ -13,16 +15,19 @@ namespace PortabilityLayer
|
||||
public:
|
||||
void Init() override;
|
||||
void Shutdown() override;
|
||||
|
||||
void *Alloc(size_t size) override;
|
||||
void Release(void *buf) override;
|
||||
|
||||
void *Alloc(size_t size) override;
|
||||
void *Realloc(void *buf, size_t newSize) override;
|
||||
void Release(void *buf) override;
|
||||
|
||||
MMHandleBlock *AllocHandle(size_t size) override;
|
||||
bool ResizeHandle(MMHandleBlock *hdl, size_t newSize) override;
|
||||
void ReleaseHandle(MMHandleBlock *hdl) override;
|
||||
|
||||
static MemoryManagerImpl *GetInstance();
|
||||
|
||||
private:
|
||||
|
||||
static MemoryManagerImpl ms_instance;
|
||||
};
|
||||
|
||||
@@ -32,6 +37,41 @@ namespace PortabilityLayer
|
||||
|
||||
void MemoryManagerImpl::Shutdown()
|
||||
{
|
||||
}
|
||||
|
||||
void *MemoryManagerImpl::Realloc(void *buf, size_t newSize)
|
||||
{
|
||||
assert(buf != nullptr);
|
||||
|
||||
const size_t mmBlockSize = MMBlock::AlignedSize();
|
||||
uint8_t *oldBufBytes = static_cast<uint8_t*>(buf);
|
||||
const MMBlock *oldBufMMBlock = reinterpret_cast<const MMBlock*>(oldBufBytes - MMBlock::AlignedSize());
|
||||
|
||||
const size_t oldBufOffsetFromAlignLoc = oldBufMMBlock->m_offsetFromAllocLocation;
|
||||
uint8_t *oldBufBase = oldBufBytes - MMBlock::AlignedSize() - oldBufOffsetFromAlignLoc;
|
||||
|
||||
const size_t mmBlockSizeWithMaxPadding = MMBlock::AlignedSize() + PL_SYSTEM_MEMORY_ALIGNMENT - 1;
|
||||
if (SIZE_MAX - newSize < mmBlockSizeWithMaxPadding)
|
||||
return nullptr;
|
||||
|
||||
const size_t newBufferSize = newSize + mmBlockSizeWithMaxPadding;
|
||||
uint8_t *newBuffer = static_cast<uint8_t*>(realloc(oldBufBase, newSize + mmBlockSizeWithMaxPadding));
|
||||
if (!newBuffer)
|
||||
return nullptr;
|
||||
|
||||
const intptr_t offsetFromAlignPoint = reinterpret_cast<intptr_t>(newBuffer) & static_cast<intptr_t>(PL_SYSTEM_MEMORY_ALIGNMENT - 1);
|
||||
intptr_t alignPadding = 0;
|
||||
if (offsetFromAlignPoint != 0)
|
||||
alignPadding = static_cast<intptr_t>(PL_SYSTEM_MEMORY_ALIGNMENT) - offsetFromAlignPoint;
|
||||
|
||||
// Check if the alignment changed, if so relocate
|
||||
if (static_cast<size_t>(alignPadding) != oldBufOffsetFromAlignLoc)
|
||||
memmove(newBuffer + alignPadding, newBuffer + oldBufOffsetFromAlignLoc, MMBlock::AlignedSize() + newSize);
|
||||
|
||||
MMBlock *newMMBlock = reinterpret_cast<MMBlock*>(newBuffer + alignPadding);
|
||||
newMMBlock->m_offsetFromAllocLocation = static_cast<SmallestUInt<PL_SYSTEM_MEMORY_ALIGNMENT>::ValueType_t>(alignPadding);
|
||||
|
||||
return newBuffer + alignPadding + MMBlock::AlignedSize();
|
||||
}
|
||||
|
||||
void *MemoryManagerImpl::Alloc(size_t size)
|
||||
@@ -77,6 +117,24 @@ namespace PortabilityLayer
|
||||
MMHandleBlock *handleBlock = static_cast<MMHandleBlock*>(Alloc(sizeof(MMHandleBlock)));
|
||||
|
||||
return new (handleBlock) MMHandleBlock(contents, size);
|
||||
}
|
||||
|
||||
bool MemoryManagerImpl::ResizeHandle(MMHandleBlock *hdl, size_t newSize)
|
||||
{
|
||||
if (hdl->m_contents == nullptr)
|
||||
return false;
|
||||
|
||||
if (newSize != hdl->m_size)
|
||||
{
|
||||
void *newBuf = Realloc(hdl->m_contents, newSize);
|
||||
if (!newBuf)
|
||||
return false;
|
||||
|
||||
hdl->m_contents = newBuf;
|
||||
hdl->m_size = newSize;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MemoryManagerImpl::ReleaseHandle(MMHandleBlock *hdl)
|
||||
@@ -84,8 +142,10 @@ namespace PortabilityLayer
|
||||
if (!hdl)
|
||||
return;
|
||||
|
||||
if (hdl->m_rmSelfRef != nullptr)
|
||||
hdl->m_rmSelfRef->m_handle = nullptr;
|
||||
assert(hdl->m_rmSelfRef == nullptr);
|
||||
|
||||
if (hdl->m_contents)
|
||||
Release(hdl->m_contents);
|
||||
|
||||
hdl->~MMHandleBlock();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user