diff options
author | Chris Robinson <[email protected]> | 2016-05-21 03:27:51 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2016-05-21 03:27:51 -0700 |
commit | 2e7ec3979aec71f11c45b737b77d58978cbee7e2 (patch) | |
tree | c931d2f9b55cc6803a00896f92793a58de8fdc11 /OpenAL32 | |
parent | 7bf64eaee0788b7eb64c7410384a9ee66f75c4ce (diff) |
Avoid using realloc in a number of places
Diffstat (limited to 'OpenAL32')
-rw-r--r-- | OpenAL32/Include/alBuffer.h | 1 | ||||
-rw-r--r-- | OpenAL32/alBuffer.c | 35 | ||||
-rw-r--r-- | OpenAL32/alSource.c | 4 |
3 files changed, 27 insertions, 13 deletions
diff --git a/OpenAL32/Include/alBuffer.h b/OpenAL32/Include/alBuffer.h index 4807dd16..351c81bc 100644 --- a/OpenAL32/Include/alBuffer.h +++ b/OpenAL32/Include/alBuffer.h @@ -80,6 +80,7 @@ typedef struct ALbuffer { enum FmtChannels FmtChannels; enum FmtType FmtType; + ALuint BytesAlloc; enum UserFmtChannels OriginalChannels; enum UserFmtType OriginalType; diff --git a/OpenAL32/alBuffer.c b/OpenAL32/alBuffer.c index 8e2c5cf2..193ab44f 100644 --- a/OpenAL32/alBuffer.c +++ b/OpenAL32/alBuffer.c @@ -985,7 +985,6 @@ ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, enum FmtChannels DstChannels; enum FmtType DstType; ALuint64 newsize; - ALvoid *temp; if(DecomposeFormat(NewFormat, &DstChannels, &DstType) == AL_FALSE || (long)SrcChannels != (long)DstChannels) @@ -1007,13 +1006,25 @@ ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei frames, return AL_INVALID_OPERATION; } - temp = realloc(ALBuf->data, (size_t)newsize); - if(!temp && newsize) + /* Round up to the next 16-byte multiple. This could reallocate only when + * increasing or the new size is less than half the current, but then the + * buffer's AL_SIZE would not be very reliable for accounting buffer memory + * usage, and reporting the real size could cause problems for apps that + * use AL_SIZE to try to get the buffer's play length. + */ + newsize = (newsize+15) & ~0xf; + if(newsize != ALBuf->BytesAlloc) { - WriteUnlock(&ALBuf->lock); - return AL_OUT_OF_MEMORY; + void *temp = al_calloc(16, (size_t)newsize); + if(!temp && newsize) + { + WriteUnlock(&ALBuf->lock); + return AL_OUT_OF_MEMORY; + } + al_free(ALBuf->data); + ALBuf->data = temp; + ALBuf->BytesAlloc = (ALuint)newsize; } - ALBuf->data = temp; if(data != NULL) ConvertData(ALBuf->data, (enum UserFmtType)DstType, data, SrcType, NewChannels, frames, align); @@ -1349,7 +1360,7 @@ ALbuffer *NewBuffer(ALCcontext *context) ALbuffer *buffer; ALenum err; - buffer = calloc(1, sizeof(ALbuffer)); + buffer = al_calloc(16, sizeof(ALbuffer)); if(!buffer) SET_ERROR_AND_RETURN_VALUE(context, AL_OUT_OF_MEMORY, NULL); RWLockInit(&buffer->lock); @@ -1361,7 +1372,7 @@ ALbuffer *NewBuffer(ALCcontext *context) { FreeThunkEntry(buffer->id); memset(buffer, 0, sizeof(ALbuffer)); - free(buffer); + al_free(buffer); SET_ERROR_AND_RETURN_VALUE(context, err, NULL); } @@ -1374,10 +1385,10 @@ void DeleteBuffer(ALCdevice *device, ALbuffer *buffer) RemoveBuffer(device, buffer->id); FreeThunkEntry(buffer->id); - free(buffer->data); + al_free(buffer->data); memset(buffer, 0, sizeof(*buffer)); - free(buffer); + al_free(buffer); } @@ -1394,10 +1405,10 @@ ALvoid ReleaseALBuffers(ALCdevice *device) ALbuffer *temp = device->BufferMap.array[i].value; device->BufferMap.array[i].value = NULL; - free(temp->data); + al_free(temp->data); FreeThunkEntry(temp->id); memset(temp, 0, sizeof(ALbuffer)); - free(temp); + al_free(temp); } } diff --git a/OpenAL32/alSource.c b/OpenAL32/alSource.c index a0e40c1d..22774fa4 100644 --- a/OpenAL32/alSource.c +++ b/OpenAL32/alSource.c @@ -2268,14 +2268,16 @@ AL_API ALvoid AL_APIENTRY alSourcePlayv(ALsizei n, const ALuint *sources) newcount = context->MaxVoices << 1; if(newcount > 0) - temp = realloc(context->Voices, newcount * sizeof(context->Voices[0])); + temp = al_malloc(16, newcount * sizeof(context->Voices[0])); if(!temp) { UnlockContext(context); SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done); } + memcpy(temp, context->Voices, context->MaxVoices * sizeof(temp[0])); memset(&temp[context->MaxVoices], 0, (newcount-context->MaxVoices) * sizeof(temp[0])); + al_free(context->Voices); context->Voices = temp; context->MaxVoices = newcount; } |