Simplify ma_allocation_callbacks_init_copy().

This returns the ma_allocation_callbacks object instead of a result
code.
This commit is contained in:
David Reid
2026-04-28 19:21:30 +10:00
parent d7802c3b14
commit 70d52e5eaa
3 changed files with 50 additions and 73 deletions
+1 -1
View File
@@ -120,7 +120,7 @@ static ma_result ma_libopus_init_internal(const ma_decoding_backend_config* pCon
} }
memset(pOpus, 0, sizeof(*pOpus)); memset(pOpus, 0, sizeof(*pOpus));
ma_allocation_callbacks_init_copy(&pOpus->allocationCallbacks, pAllocationCallbacks); pOpus->allocationCallbacks = ma_allocation_callbacks_init_copy(pAllocationCallbacks);
pOpus->format = ma_format_f32; /* f32 by default. */ pOpus->format = ma_format_f32; /* f32 by default. */
if (pConfig != NULL && (pConfig->preferredFormat == ma_format_f32 || pConfig->preferredFormat == ma_format_s16)) { if (pConfig != NULL && (pConfig->preferredFormat == ma_format_f32 || pConfig->preferredFormat == ma_format_s16)) {
@@ -123,7 +123,7 @@ static ma_result ma_libvorbis_init_internal(const ma_decoding_backend_config* pC
} }
memset(pVorbis, 0, sizeof(*pVorbis)); memset(pVorbis, 0, sizeof(*pVorbis));
ma_allocation_callbacks_init_copy(&pVorbis->allocationCallbacks, pAllocationCallbacks); pVorbis->allocationCallbacks = ma_allocation_callbacks_init_copy(pAllocationCallbacks);
pVorbis->format = ma_format_f32; /* f32 by default. */ pVorbis->format = ma_format_f32; /* f32 by default. */
if (pConfig != NULL && (pConfig->preferredFormat == ma_format_f32 || pConfig->preferredFormat == ma_format_s16)) { if (pConfig != NULL && (pConfig->preferredFormat == ma_format_f32 || pConfig->preferredFormat == ma_format_s16)) {
+29 -52
View File
@@ -6314,7 +6314,7 @@ typedef struct
ma_uint32 sampleRate; ma_uint32 sampleRate;
ma_uint64 sizeInFrames; ma_uint64 sizeInFrames;
const void* pData; /* If set to NULL, will allocate a block of memory for you. */ const void* pData; /* If set to NULL, will allocate a block of memory for you. */
ma_allocation_callbacks allocationCallbacks; const ma_allocation_callbacks* pAllocationCallbacks;
} ma_audio_buffer_config; } ma_audio_buffer_config;
MA_API ma_audio_buffer_config ma_audio_buffer_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint64 sizeInFrames, const void* pData, const ma_allocation_callbacks* pAllocationCallbacks); MA_API ma_audio_buffer_config ma_audio_buffer_config_init(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint64 sizeInFrames, const void* pData, const ma_allocation_callbacks* pAllocationCallbacks);
@@ -6507,7 +6507,7 @@ Retrieves a human readable description of the given result code.
MA_API const char* ma_result_description(ma_result result); MA_API const char* ma_result_description(ma_result result);
MA_API ma_allocation_callbacks ma_allocation_callbacks_init_default(void); MA_API ma_allocation_callbacks ma_allocation_callbacks_init_default(void);
MA_API ma_result ma_allocation_callbacks_init_copy(ma_allocation_callbacks* pDst, const ma_allocation_callbacks* pSrc); MA_API ma_allocation_callbacks ma_allocation_callbacks_init_copy(const ma_allocation_callbacks* pSrc);
/* /*
malloc() malloc()
@@ -14053,7 +14053,7 @@ MA_API ma_result ma_log_init(const ma_allocation_callbacks* pAllocationCallbacks
} }
MA_ZERO_OBJECT(pLog); MA_ZERO_OBJECT(pLog);
ma_allocation_callbacks_init_copy(&pLog->allocationCallbacks, pAllocationCallbacks); pLog->allocationCallbacks = ma_allocation_callbacks_init_copy(pAllocationCallbacks);
/* We need a mutex for thread safety. */ /* We need a mutex for thread safety. */
#ifndef MA_NO_THREADING #ifndef MA_NO_THREADING
@@ -18941,7 +18941,7 @@ MA_API ma_result ma_thread_create(ma_thread* pThread, ma_thread_priority priorit
pProxyData->entryProc = entryProc; pProxyData->entryProc = entryProc;
pProxyData->pData = pData; pProxyData->pData = pData;
ma_allocation_callbacks_init_copy(&pProxyData->allocationCallbacks, pAllocationCallbacks); pProxyData->allocationCallbacks = ma_allocation_callbacks_init_copy(pAllocationCallbacks);
#if defined(MA_THREADING_BACKEND_VITA) #if defined(MA_THREADING_BACKEND_VITA)
result = ma_thread_create__vita(pThread, priority, stackSize, ma_thread_entry_proxy, pProxyData); result = ma_thread_create__vita(pThread, priority, stackSize, ma_thread_entry_proxy, pProxyData);
@@ -50333,10 +50333,7 @@ MA_API ma_result ma_context_init(const ma_device_backend_config* pBackends, ma_u
} }
/* Allocation callbacks need to come first because they'll be passed around to other areas. */ /* Allocation callbacks need to come first because they'll be passed around to other areas. */
result = ma_allocation_callbacks_init_copy(&pContext->allocationCallbacks, &pConfig->allocationCallbacks); pContext->allocationCallbacks = ma_allocation_callbacks_init_copy(&pConfig->allocationCallbacks);
if (result != MA_SUCCESS) {
return result;
}
/* Get a lot set up first so we can start logging ASAP. */ /* Get a lot set up first so we can start logging ASAP. */
if (pConfig->pLog != NULL) { if (pConfig->pLog != NULL) {
@@ -51195,10 +51192,7 @@ MA_API ma_result ma_device_init_ex(const ma_device_backend_config* pBackends, ma
} }
if (pContextConfig != NULL) { if (pContextConfig != NULL) {
result = ma_allocation_callbacks_init_copy(&allocationCallbacks, &pContextConfig->allocationCallbacks); allocationCallbacks = ma_allocation_callbacks_init_copy(&pContextConfig->allocationCallbacks);
if (result != MA_SUCCESS) {
return result;
}
} else { } else {
allocationCallbacks = ma_allocation_callbacks_init_default(); allocationCallbacks = ma_allocation_callbacks_init_default();
} }
@@ -68463,7 +68457,7 @@ MA_API ma_result ma_audio_ring_buffer_init(const ma_audio_ring_buffer_config* pC
pRingBuffer->sampleRate = pConfig->sampleRate; pRingBuffer->sampleRate = pConfig->sampleRate;
pRingBuffer->isOwnerOfBuffer = isOwnerOfBuffer; pRingBuffer->isOwnerOfBuffer = isOwnerOfBuffer;
pRingBuffer->pBuffer = pBuffer; pRingBuffer->pBuffer = pBuffer;
ma_allocation_callbacks_init_copy(&pRingBuffer->allocationCallbacks, pConfig->pAllocationCallbacks); pRingBuffer->allocationCallbacks = ma_allocation_callbacks_init_copy(pConfig->pAllocationCallbacks);
return MA_SUCCESS; return MA_SUCCESS;
} }
@@ -68754,27 +68748,22 @@ MA_API ma_allocation_callbacks ma_allocation_callbacks_init_default(void)
return callbacks; return callbacks;
} }
MA_API ma_result ma_allocation_callbacks_init_copy(ma_allocation_callbacks* pDst, const ma_allocation_callbacks* pSrc) MA_API ma_allocation_callbacks ma_allocation_callbacks_init_copy(const ma_allocation_callbacks* pSrc)
{ {
if (pDst == NULL) { if (pSrc == NULL) {
return MA_INVALID_ARGS; return ma_allocation_callbacks_init_default();
} }
if (pSrc == NULL) {
*pDst = ma_allocation_callbacks_init_default();
} else {
if (pSrc->pUserData == NULL && pSrc->onFree == NULL && pSrc->onMalloc == NULL && pSrc->onRealloc == NULL) { if (pSrc->pUserData == NULL && pSrc->onFree == NULL && pSrc->onMalloc == NULL && pSrc->onRealloc == NULL) {
*pDst = ma_allocation_callbacks_init_default(); return ma_allocation_callbacks_init_default();
} else { } else {
if (pSrc->onFree == NULL || (pSrc->onMalloc == NULL && pSrc->onRealloc == NULL)) { if (pSrc->onFree == NULL || (pSrc->onMalloc == NULL && pSrc->onRealloc == NULL)) {
return MA_INVALID_ARGS; /* Invalid allocation callbacks. */ MA_ASSERT(!"Allocation callbacks must have either all callbacks non-NULL, or all callbacks NULL. Fix your code.");
return ma_allocation_callbacks_init_default();
} else { } else {
*pDst = *pSrc; return *pSrc;
} }
} }
}
return MA_SUCCESS;
} }
MA_API void* ma_malloc(size_t sz, const ma_allocation_callbacks* pAllocationCallbacks) MA_API void* ma_malloc(size_t sz, const ma_allocation_callbacks* pAllocationCallbacks)
@@ -70053,7 +70042,7 @@ MA_API ma_audio_buffer_config ma_audio_buffer_config_init(ma_format format, ma_u
config.sampleRate = sampleRate; config.sampleRate = sampleRate;
config.sizeInFrames = sizeInFrames; config.sizeInFrames = sizeInFrames;
config.pData = pData; config.pData = pData;
ma_allocation_callbacks_init_copy(&config.allocationCallbacks, pAllocationCallbacks); config.pAllocationCallbacks = pAllocationCallbacks;
return config; return config;
} }
@@ -70081,7 +70070,7 @@ static ma_result ma_audio_buffer_init_ex(const ma_audio_buffer_config* pConfig,
return result; return result;
} }
ma_allocation_callbacks_init_copy(&pAudioBuffer->allocationCallbacks, &pConfig->allocationCallbacks); pAudioBuffer->allocationCallbacks = ma_allocation_callbacks_init_copy(pConfig->pAllocationCallbacks);
if (doCopy) { if (doCopy) {
ma_uint64 allocationSizeInBytes; ma_uint64 allocationSizeInBytes;
@@ -70158,14 +70147,14 @@ MA_API ma_result ma_audio_buffer_alloc_and_init(const ma_audio_buffer_config* pC
} }
innerConfig = *pConfig; innerConfig = *pConfig;
ma_allocation_callbacks_init_copy(&innerConfig.allocationCallbacks, &pConfig->allocationCallbacks); innerConfig.pAllocationCallbacks = pConfig->pAllocationCallbacks;
allocationSizeInBytes = sizeof(*pAudioBuffer) - sizeof(pAudioBuffer->_pExtraData) + (pConfig->sizeInFrames * ma_get_bytes_per_frame(pConfig->format, pConfig->channels)); allocationSizeInBytes = sizeof(*pAudioBuffer) - sizeof(pAudioBuffer->_pExtraData) + (pConfig->sizeInFrames * ma_get_bytes_per_frame(pConfig->format, pConfig->channels));
if (allocationSizeInBytes > MA_SIZE_MAX) { if (allocationSizeInBytes > MA_SIZE_MAX) {
return MA_OUT_OF_MEMORY; /* Too big. */ return MA_OUT_OF_MEMORY; /* Too big. */
} }
pAudioBuffer = (ma_audio_buffer*)ma_malloc((size_t)allocationSizeInBytes, &innerConfig.allocationCallbacks); /* Safe cast to size_t. */ pAudioBuffer = (ma_audio_buffer*)ma_malloc((size_t)allocationSizeInBytes, innerConfig.pAllocationCallbacks); /* Safe cast to size_t. */
if (pAudioBuffer == NULL) { if (pAudioBuffer == NULL) {
return MA_OUT_OF_MEMORY; return MA_OUT_OF_MEMORY;
} }
@@ -70180,7 +70169,7 @@ MA_API ma_result ma_audio_buffer_alloc_and_init(const ma_audio_buffer_config* pC
result = ma_audio_buffer_init_ex(&innerConfig, MA_FALSE, pAudioBuffer); result = ma_audio_buffer_init_ex(&innerConfig, MA_FALSE, pAudioBuffer);
if (result != MA_SUCCESS) { if (result != MA_SUCCESS) {
ma_free(pAudioBuffer, &innerConfig.allocationCallbacks); ma_free(pAudioBuffer, innerConfig.pAllocationCallbacks);
return result; return result;
} }
@@ -71549,7 +71538,7 @@ MA_API ma_result ma_default_vfs_init(ma_default_vfs* pVFS, const ma_allocation_c
pVFS->cb.onSeek = ma_default_vfs_seek; pVFS->cb.onSeek = ma_default_vfs_seek;
pVFS->cb.onTell = ma_default_vfs_tell; pVFS->cb.onTell = ma_default_vfs_tell;
pVFS->cb.onInfo = ma_default_vfs_info; pVFS->cb.onInfo = ma_default_vfs_info;
ma_allocation_callbacks_init_copy(&pVFS->allocationCallbacks, pAllocationCallbacks); pVFS->allocationCallbacks = ma_allocation_callbacks_init_copy(pAllocationCallbacks);
return MA_SUCCESS; return MA_SUCCESS;
} }
@@ -75582,15 +75571,14 @@ static ma_result ma_decoder_init_from_memory__internal(const ma_decoding_backend
static ma_result ma_decoder__init_allocation_callbacks(const ma_decoder_config* pConfig, ma_decoder* pDecoder) static void ma_decoder__init_allocation_callbacks(const ma_decoder_config* pConfig, ma_decoder* pDecoder)
{ {
MA_ASSERT(pDecoder != NULL); MA_ASSERT(pDecoder != NULL);
if (pConfig != NULL) { if (pConfig != NULL) {
return ma_allocation_callbacks_init_copy(&pDecoder->allocationCallbacks, &pConfig->allocationCallbacks); pDecoder->allocationCallbacks = ma_allocation_callbacks_init_copy(&pConfig->allocationCallbacks);
} else { } else {
pDecoder->allocationCallbacks = ma_allocation_callbacks_init_default(); pDecoder->allocationCallbacks = ma_allocation_callbacks_init_default();
return MA_SUCCESS;
} }
} }
@@ -75661,12 +75649,7 @@ static ma_result ma_decoder__preinit(ma_decoder_read_proc onRead, ma_decoder_see
pDecoder->onSeek = onSeek; pDecoder->onSeek = onSeek;
pDecoder->onTell = onTell; pDecoder->onTell = onTell;
pDecoder->pUserData = pUserData; pDecoder->pUserData = pUserData;
ma_decoder__init_allocation_callbacks(pConfig, pDecoder);
result = ma_decoder__init_allocation_callbacks(pConfig, pDecoder);
if (result != MA_SUCCESS) {
ma_data_source_base_uninit(&pDecoder->ds);
return result;
}
return MA_SUCCESS; return MA_SUCCESS;
} }
@@ -76822,8 +76805,6 @@ MA_API ma_encoder_config ma_encoder_config_init(ma_encoding_format encodingForma
MA_API ma_result ma_encoder_preinit(const ma_encoder_config* pConfig, ma_encoder* pEncoder) MA_API ma_result ma_encoder_preinit(const ma_encoder_config* pConfig, ma_encoder* pEncoder)
{ {
ma_result result;
if (pEncoder == NULL) { if (pEncoder == NULL) {
return MA_INVALID_ARGS; return MA_INVALID_ARGS;
} }
@@ -76839,11 +76820,7 @@ MA_API ma_result ma_encoder_preinit(const ma_encoder_config* pConfig, ma_encoder
} }
pEncoder->config = *pConfig; pEncoder->config = *pConfig;
pEncoder->config.allocationCallbacks = ma_allocation_callbacks_init_copy(&pConfig->allocationCallbacks);
result = ma_allocation_callbacks_init_copy(&pEncoder->config.allocationCallbacks, &pConfig->allocationCallbacks);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS; return MA_SUCCESS;
} }
@@ -77825,7 +77802,7 @@ MA_API ma_result ma_noise_init(const ma_noise_config* pConfig, const ma_allocati
return result; return result;
} }
ma_allocation_callbacks_init_copy(&pNoise->allocationCallbacks, pAllocationCallbacks); pNoise->allocationCallbacks = ma_allocation_callbacks_init_copy(pAllocationCallbacks);
pNoise->_ownsHeap = MA_TRUE; pNoise->_ownsHeap = MA_TRUE;
return MA_SUCCESS; return MA_SUCCESS;
@@ -78901,7 +78878,7 @@ MA_API ma_result ma_resource_manager_init(const ma_resource_manager_config* pCon
#endif #endif
pResourceManager->config = *pConfig; pResourceManager->config = *pConfig;
ma_allocation_callbacks_init_copy(&pResourceManager->config.allocationCallbacks, &pConfig->allocationCallbacks); pResourceManager->config.allocationCallbacks = ma_allocation_callbacks_init_copy(&pConfig->allocationCallbacks);
/* Get the log set up early so we can start using it as soon as possible. */ /* Get the log set up early so we can start using it as soon as possible. */
if (pResourceManager->config.pLog == NULL) { if (pResourceManager->config.pLog == NULL) {
@@ -82551,7 +82528,7 @@ MA_API ma_result ma_node_graph_init(const ma_node_graph_config* pConfig, const m
} }
MA_ZERO_OBJECT(pNodeGraph); MA_ZERO_OBJECT(pNodeGraph);
ma_allocation_callbacks_init_copy(&pNodeGraph->allocationCallbacks, pAllocationCallbacks); pNodeGraph->allocationCallbacks = ma_allocation_callbacks_init_copy(pAllocationCallbacks);
if (pConfig == NULL) { if (pConfig == NULL) {
return MA_INVALID_ARGS; return MA_INVALID_ARGS;
@@ -86448,7 +86425,7 @@ MA_API ma_result ma_engine_init(const ma_engine_config* pConfig, ma_engine* pEng
pEngine->pProcessUserData = engineConfig.pProcessUserData; pEngine->pProcessUserData = engineConfig.pProcessUserData;
pEngine->pitchResamplingConfig = engineConfig.pitchResampling; pEngine->pitchResamplingConfig = engineConfig.pitchResampling;
pEngine->noClip = (ma_bool8)engineConfig.noClip; pEngine->noClip = (ma_bool8)engineConfig.noClip;
ma_allocation_callbacks_init_copy(&pEngine->allocationCallbacks, &engineConfig.allocationCallbacks); pEngine->allocationCallbacks = ma_allocation_callbacks_init_copy(&engineConfig.allocationCallbacks);
#if !defined(MA_NO_RESOURCE_MANAGER) #if !defined(MA_NO_RESOURCE_MANAGER)
{ {
@@ -86628,7 +86605,7 @@ MA_API ma_result ma_engine_init(const ma_engine_config* pConfig, ma_engine* pEng
resourceManagerConfig.decodedFormat = ma_format_f32; resourceManagerConfig.decodedFormat = ma_format_f32;
resourceManagerConfig.decodedChannels = 0; /* Leave the decoded channel count as 0 so we can get good spatialization. */ resourceManagerConfig.decodedChannels = 0; /* Leave the decoded channel count as 0 so we can get good spatialization. */
resourceManagerConfig.decodedSampleRate = ma_engine_get_sample_rate(pEngine); resourceManagerConfig.decodedSampleRate = ma_engine_get_sample_rate(pEngine);
ma_allocation_callbacks_init_copy(&resourceManagerConfig.allocationCallbacks, &pEngine->allocationCallbacks); resourceManagerConfig.allocationCallbacks = ma_allocation_callbacks_init_copy(&pEngine->allocationCallbacks);
resourceManagerConfig.pVFS = engineConfig.pResourceManagerVFS; resourceManagerConfig.pVFS = engineConfig.pResourceManagerVFS;
resourceManagerConfig.resampling = engineConfig.resourceManagerResampling; resourceManagerConfig.resampling = engineConfig.resourceManagerResampling;