Skip to content

Commit

Permalink
Removed the hardcoded limit on generated buffers.
Browse files Browse the repository at this point in the history
Fixes audio eventually dropping out in Psychonauts.
  • Loading branch information
icculus committed Dec 31, 2012
1 parent 8def887 commit 21629ac
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
63 changes: 42 additions & 21 deletions osx/alBuffer.c
Expand Up @@ -63,13 +63,15 @@ static ALcontext *__alGrabContextAndGetBuffer(ALuint bufid, ALbuffer **bufout)

bufid--; // account for Buffer Zero.

if (bufid > AL_MAXBUFFERS)
ctx = __alGrabCurrentContext();

if (bufid >= ctx->numBuffers)
{
__alSetError(AL_INVALID_NAME);
__alUngrabContext(ctx);
return(NULL);
} // if

ctx = __alGrabCurrentContext();
*bufout = &ctx->buffers[bufid];

if ( !((*bufout)->inUse) )
Expand Down Expand Up @@ -109,7 +111,7 @@ ALAPI ALvoid ALAPIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
return;

// check if there're enough buffers available...
for (i = 0, buf = ctx->buffers; i < AL_MAXBUFFERS; i++, buf++)
for (i = 0, buf = ctx->buffers; i < ctx->numBuffers; i++, buf++)
{
if (!buf->inUse)
{
Expand All @@ -118,25 +120,44 @@ ALAPI ALvoid ALAPIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
break;
} // if
} // for

if (iCount < n)
__alSetError(AL_OUT_OF_MEMORY); // !!! FIXME: Better error?
else

if (iCount < n) // allocate more buffers?
{
const ALsizei needed = n - iCount;
ALsizei newAlloc = ctx->numBuffers * 2;
if (newAlloc == 0)
newAlloc = 1;
while (newAlloc < needed)
newAlloc *= 2;

void *ptr = realloc(ctx->buffers, sizeof (ALbuffer) * (newAlloc));
if (ptr == NULL)
{
__alSetError(AL_OUT_OF_MEMORY);
__alUngrabContext(ctx);
return;
}
else
{
ctx->buffers = (ALbuffer *) ptr;
__alBuffersInit(ctx->buffers + ctx->numBuffers, newAlloc - ctx->numBuffers);
ctx->numBuffers = newAlloc;
} // else
} // if

iCount = 0;
for (i = 0, buf = ctx->buffers; i < ctx->numBuffers; i++, buf++)
{
iCount = 0;
for (i = 0, buf = ctx->buffers; i < AL_MAXBUFFERS; i++, buf++)
if (!buf->inUse)
{
if (!buf->inUse)
{
__alBuffersInit(buf, 1);
buf->inUse = AL_TRUE;
buffers[iCount] = i + 1; // see comments about the plus 1.
iCount++;
if (iCount >= n)
break;
} // if
} // for
} // else
__alBuffersInit(buf, 1);
buf->inUse = AL_TRUE;
buffers[iCount] = i + 1; // see comments about the plus 1.
iCount++;
if (iCount >= n)
break;
} // if
} // for

__alUngrabContext(ctx);
} // alGenBuffers
Expand Down Expand Up @@ -194,7 +215,7 @@ ALAPI ALvoid ALAPIENTRY alDeleteBuffers(ALsizei n, ALuint *buffers)
for (i = 0; i < n; i++)
{
name = buffers[i] - 1; // minus one to account for Buffer Zero.
if ((name < AL_MAXBUFFERS) && (buf[name].inUse))
if ((name < ctx->numBuffers) && (buf[name].inUse))
{
__alBuffersShutdown(buf + name, 1);
buf[name].inUse = AL_FALSE;
Expand Down
5 changes: 3 additions & 2 deletions osx/alContext.c
Expand Up @@ -270,7 +270,6 @@ ALCAPI void * ALCAPIENTRY alcCreateContext(ALCdevice *device, ALint *attrlist)

dev->createdContexts[i] = ctx;
memset(ctx, '\0', sizeof (ALcontext));
__alBuffersInit(ctx->buffers, AL_MAXBUFFERS);
__alSourcesInit(ctx->sources, AL_MAXSOURCES);
__alListenerInit(&ctx->listener);
__alCreateLock(&ctx->contextLock);
Expand Down Expand Up @@ -345,7 +344,9 @@ ALCAPI ALCenum ALCAPIENTRY alcDestroyContext(ALCcontext *context)

__alListenerShutdown(&ctx->listener);
__alSourcesShutdown(ctx->sources, AL_MAXSOURCES);
__alBuffersShutdown(ctx->buffers, AL_MAXBUFFERS);
__alBuffersShutdown(ctx->buffers, ctx->numBuffers);
free(ctx->buffers);
ctx->buffers = NULL;

__alUngrabDevice(dev); // start audio callback, etc in motion again.

Expand Down
4 changes: 2 additions & 2 deletions osx/alInternal.h
Expand Up @@ -69,7 +69,6 @@ struct ALsource_struct;
typedef pthread_mutex_t ALlock;

// These numbers might need adjustment up or down.
#define AL_MAXBUFFERS 1024 * 2
#define AL_MAXBUFFERQUEUE 128
#define AL_MAXSOURCES 1024 * 5
#define AL_MAXCONTEXTS 4
Expand Down Expand Up @@ -343,7 +342,8 @@ typedef struct ALcontext_struct
// !!! FIXME: Eventually, these should make it at least to device
// !!! FIXME: resolution, or perhaps globally if mutexes aren't a pain
// !!! FIXME: to implement well.
ALbuffer buffers[AL_MAXBUFFERS];
ALbuffer *buffers;
ALsizei numBuffers;
} ALcontext;

ALcontext *__alGrabCurrentContext(ALvoid);
Expand Down
2 changes: 1 addition & 1 deletion osx/alSource.c
Expand Up @@ -443,7 +443,7 @@ ALAPI ALvoid ALAPIENTRY alSourcei (ALuint source, ALenum pname, ALint value)
{
// !!! FIXME: This is yucky. Use logic in alBuffer.c...
ALboolean valid = AL_FALSE;
valid = ( (value > 0) && ((value - 1) < AL_MAXBUFFERS) &&
valid = ( (value > 0) && ((value - 1) < ctx->numBuffers) &&
(ctx->buffers[value - 1].inUse) );

if (valid)
Expand Down

0 comments on commit 21629ac

Please sign in to comment.