Skip to content

Commit

Permalink
Use alloca for dynamic stack allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
detomon committed Dec 15, 2024
1 parent c1552be commit 2f84cf4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
/* Defines SDL version */
#undef BK_SDL_VERSION

/* Define to 1 if you have the <alloca.h> header file. */
#undef HAVE_ALLOCA_H

/* Define to 1 if you have the <fcntl.h> header file. */
#undef HAVE_FCNTL_H

Expand All @@ -13,6 +16,9 @@
to 0 otherwise. */
#undef HAVE_MALLOC

/* Define to 1 if you have the <malloc.h> header file. */
#undef HAVE_MALLOC_H

/* Define to 1 if you have the <math.h> header file. */
#undef HAVE_MATH_H

Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.61])
AC_INIT([BlipKit],[0.17.6],[simon@monoxid.net],[blipkit],[https://blipkit.audio])
AC_INIT([BlipKit], [0.17.6], [simon@monoxid.net], [blipkit], [https://blipkit.audio])

AC_CONFIG_SRCDIR([src/BKBase.c])
AC_CONFIG_HEADER([config.h])
Expand Down Expand Up @@ -35,7 +35,7 @@ AM_CFLAGS="$AM_CFLAGS -Wall \
CHECK_COMPILE_FLAG([-std=c11], [AM_CFLAGS])

# Checks for header files.
AC_CHECK_HEADERS([fcntl.h math.h stdarg.h stddef.h stdint.h stdlib.h string.h unistd.h])
AC_CHECK_HEADERS([alloca.h fcntl.h malloc.h math.h stdarg.h stddef.h stdint.h stdlib.h string.h unistd.h])

AC_ARG_WITH([sdl],
AS_HELP_STRING([--without-sdl], [do not link agains SDL library]))
Expand Down
7 changes: 6 additions & 1 deletion src/BKContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

#include "BKContext.h"
#include "BKUnit.h"
#ifdef HAVE_ALLOCA_H // Assume GNU.
#include <alloca.h>
#elif HAVE_MALLOC_H // Assume MSVC.
#include <malloc.h>
#endif

extern BKInt BKContextSetAttrInt(BKContext* ctx, BKEnum attr, BKInt value);

Expand Down Expand Up @@ -276,6 +281,7 @@ BKInt BKContextGenerate(BKContext* ctx, BKFrame outFrames[], BKUInt size) {

BKInt BKContextGenerateToTime(BKContext* ctx, BKTime endTime, BKInt (*write)(BKFrame inFrames[], BKUInt size, void* info), void* info) {
BKInt numFrames = 0;
BKFrame* frames = (BKFrame*)alloca(sizeof(BKFrame) * ctx->numChannels * BK_MAX_GENERATE_SAMPLES);

while (BKTimeIsLess(ctx->currentTime, endTime)) {
BKTime deltaTime = BKTimeSub(endTime, ctx->currentTime);
Expand All @@ -293,7 +299,6 @@ BKInt BKContextGenerateToTime(BKContext* ctx, BKTime endTime, BKInt (*write)(BKF
// write frames if buffer filled or end time is reached
if (BKBufferSize(&ctx->channels[0]) >= BK_MAX_GENERATE_SAMPLES || BKTimeIsGreaterEqual(ctx->currentTime, endTime)) {
BKInt size;
BKFrame frames[ctx->numChannels * BK_MAX_GENERATE_SAMPLES];

size = BKContextRead(ctx, frames, BK_MAX_GENERATE_SAMPLES);
numFrames += size;
Expand Down

0 comments on commit 2f84cf4

Please sign in to comment.