Removed the hardcoded limit on generated buffers. default tip
authorRyan C. Gordon <icculus@icculus.org>
Mon, 31 Dec 2012 01:03:03 -0500
changeset 95 0a953a090334
parent 94 c258b2c78a4b
Removed the hardcoded limit on generated buffers.

Fixes audio eventually dropping out in Psychonauts.
osx/alBuffer.c
osx/alContext.c
osx/alInternal.h
osx/alSource.c
--- a/osx/alBuffer.c	Mon Dec 31 01:01:17 2012 -0500
+++ b/osx/alBuffer.c	Mon Dec 31 01:03:03 2012 -0500
@@ -63,13 +63,15 @@
 
     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) )
@@ -109,7 +111,7 @@
         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)
         {
@@ -118,25 +120,44 @@
                 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
@@ -194,7 +215,7 @@
 	    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;
--- a/osx/alContext.c	Mon Dec 31 01:01:17 2012 -0500
+++ b/osx/alContext.c	Mon Dec 31 01:03:03 2012 -0500
@@ -270,7 +270,6 @@
 
         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);
@@ -345,7 +344,9 @@
 
     __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.
 
--- a/osx/alInternal.h	Mon Dec 31 01:01:17 2012 -0500
+++ b/osx/alInternal.h	Mon Dec 31 01:03:03 2012 -0500
@@ -69,7 +69,6 @@
 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
@@ -343,7 +342,8 @@
     // !!! 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);
--- a/osx/alSource.c	Mon Dec 31 01:01:17 2012 -0500
+++ b/osx/alSource.c	Mon Dec 31 01:03:03 2012 -0500
@@ -443,7 +443,7 @@
                     {
                         // !!! 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)