Skip to content

Commit

Permalink
FAudio_SetCustomAllocator
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Oct 19, 2018
1 parent b6be7c6 commit d36e46b
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 46 deletions.
4 changes: 0 additions & 4 deletions Makefile
Expand Up @@ -22,10 +22,6 @@ ifeq ($(WINDOWS_TARGET),1)
TARGET_SUFFIX = dll
UTIL_SUFFIX = .exe
LDFLAGS += -static-libgcc
ifeq ($(COM_WRAPPER), 1)
CFLAGS += -DFAUDIO_COM_WRAPPER
LDFLAGS += -lole32
endif
else ifeq ($(UNAME), Darwin)
CFLAGS += -mmacosx-version-min=10.6 -fpic -fPIC
TARGET_PREFIX = lib
Expand Down
18 changes: 9 additions & 9 deletions cpp/Makefile
@@ -1,5 +1,5 @@
CXXFLAGS += -I../src -DFAUDIOCPP_EXPORTS -D_WIN32_WINNT=0x0600
LDFLAGS += -Wl,--enable-stdcall-fixup -static-libgcc -static-libstdc++ -L.. -lFAudio
LDFLAGS += -Wl,--enable-stdcall-fixup -static-libgcc -static-libstdc++ -L.. -lFAudio -lole32

DLLTOOL ?= dlltool

Expand Down Expand Up @@ -202,28 +202,28 @@ $(X3DAUDIO_TARGET) : directories $(X3DAUDIO_OBJ)
cp $(X3DAUDIO_TARGET) $(BUILD_DIR)/x3daudio1_6.dll

$(XACT30_TARGET) : directories $(XACT30_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT30_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT30_OBJ) $(LDFLAGS)

$(XACT31_TARGET) : directories $(XACT31_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT31_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT31_OBJ) $(LDFLAGS)

$(XACT32_TARGET) : directories $(XACT32_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT32_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT32_OBJ) $(LDFLAGS)

$(XACT33_TARGET) : directories $(XACT33_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT33_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT33_OBJ) $(LDFLAGS)

$(XACT34_TARGET) : directories $(XACT34_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT34_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT34_OBJ) $(LDFLAGS)

$(XACT35_TARGET) : directories $(XACT35_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT35_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT35_OBJ) $(LDFLAGS)

$(XACT36_TARGET) : directories $(XACT36_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT36_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT36_OBJ) $(LDFLAGS)

$(XACT37_TARGET) : directories $(XACT37_OBJ)
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT37_OBJ) $(LDFLAGS) -lole32
$(CXX) $(CXXFLAGS) -shared -o $@ $(XACT37_OBJ) $(LDFLAGS)

$(BUILD_DIR)/xaudiodll_x20.o: xaudio2_7.def
$(DLLTOOL) -d $< -e $@
Expand Down
12 changes: 12 additions & 0 deletions cpp/XAudio2fx.cpp
Expand Up @@ -67,16 +67,28 @@ class XAudio2Reverb : public CXAPOParametersBase
// Create functions
//

GENERATE_COTASKMEM_CFUNCS

void *CreateAudioVolumeMeterInternal()
{
FAPO *fapo_object = NULL;
FAudio_SetCustomAllocator(
CTM_Malloc,
CTM_Free,
CTM_Realloc
);
FAudioCreateVolumeMeter(&fapo_object, 0);
return new XAudio2VolumeMeter(fapo_object);
}

void *CreateAudioReverbInternal()
{
FAPO *fapo_object = NULL;
FAudio_SetCustomAllocator(
CTM_Malloc,
CTM_Free,
CTM_Realloc
);
FAudioCreateReverb(&fapo_object, 0);
return new XAudio2Reverb(fapo_object);
}
Expand Down
28 changes: 28 additions & 0 deletions cpp/com_utils.h
Expand Up @@ -7,6 +7,34 @@ extern "C" {

#include <FAudio.h>

// CoTaskMem functionality, needed for XAudio2 compat
#if defined _WIN32 || defined __CYGWIN__
#define DLLIMPORT __declspec(dllimport)
#else
#if __GNUC__ >= 4
#define DLLIMPORT __attribute__((visibility ("default")))
#else
#define DLLIMPORT
#endif
#endif
extern "C" DLLIMPORT void * __stdcall CoTaskMemAlloc(size_t cb);
extern "C" DLLIMPORT void __stdcall CoTaskMemFree(void* ptr);
extern "C" DLLIMPORT void * __stdcall CoTaskMemRealloc(void* ptr, size_t cb);

#define GENERATE_COTASKMEM_CFUNCS \
static void* __cdecl CTM_Malloc(size_t cb) \
{ \
return CoTaskMemAlloc(cb); \
} \
static void __cdecl CTM_Free(void* ptr) \
{ \
CoTaskMemFree(ptr); \
} \
static void* __cdecl CTM_Realloc(void* ptr, size_t cb) \
{ \
return CoTaskMemRealloc(ptr, cb); \
}

// common windows types
#ifndef FAUDIO_USE_STD_TYPES

Expand Down
2 changes: 0 additions & 2 deletions cpp/visualc/FAudio.vcxproj
Expand Up @@ -48,7 +48,6 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>FAUDIO_COM_WRAPPER;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -61,7 +60,6 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>FAUDIO_COM_WRAPPER;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down
18 changes: 7 additions & 11 deletions cpp/xact3.cpp
Expand Up @@ -5,17 +5,6 @@

#include <assert.h>

#if defined _WIN32 || defined __CYGWIN__
#define DLLIMPORT __declspec(dllimport)
#else
#if __GNUC__ >= 4
#define DLLIMPORT __attribute__((visibility ("default")))
#else
#define DLLIMPORT
#endif
#endif
extern "C" DLLIMPORT void * __stdcall CoTaskMemAlloc(size_t cb);

/* IXACT3Cue Implementation */

class XACT3CueImpl : public IXACT3Cue
Expand Down Expand Up @@ -705,7 +694,14 @@ class XACT3EngineImpl : public IXACT3Engine

/* Create Function */

GENERATE_COTASKMEM_CFUNCS

void *CreateXACT3EngineInternal()
{
FAudio_SetCustomAllocator(
CTM_Malloc,
CTM_Free,
CTM_Realloc
);
return new XACT3EngineImpl();
}
13 changes: 13 additions & 0 deletions cpp/xaudio2.cpp
Expand Up @@ -1495,15 +1495,28 @@ class XAudio2Impl : public IXAudio2
// Create function
//

GENERATE_COTASKMEM_CFUNCS

void *CreateXAudio2Internal()
{
FAudio_SetCustomAllocator(
CTM_Malloc,
CTM_Free,
CTM_Realloc
);
return new XAudio2Impl();
}

#if XAUDIO2_VERSION >= 8

FAUDIOCPP_API XAudio2Create(IXAudio2 **ppXAudio2, UINT32 Flags, XAUDIO2_PROCESSOR XAudio2Processor)
{
FAudio_SetCustomAllocator(
CTM_Malloc,
CTM_Free,
CTM_Realloc
);

// FAudio only accepts one processor
*ppXAudio2 = new XAudio2Impl(Flags, FAUDIO_DEFAULT_PROCESSOR);
return S_OK;
Expand Down
12 changes: 12 additions & 0 deletions src/FAudio.h
Expand Up @@ -724,6 +724,18 @@ struct FAudioVoiceCallback
OnVoiceProcessingPassStartFunc OnVoiceProcessingPassStart;
};

/* FAudio Custom Allocator API */

typedef void* (FAUDIOCALL * FAudioMallocFunc)(size_t size);
typedef void (FAUDIOCALL * FAudioFreeFunc)(void* ptr);
typedef void* (FAUDIOCALL * FAudioReallocFunc)(void* ptr, size_t size);

FAUDIOAPI void FAudio_SetCustomAllocator(
FAudioMallocFunc customMalloc,
FAudioFreeFunc customFree,
FAudioReallocFunc customRealloc
);

/* FAudio I/O API */

#define FAUDIO_SEEK_SET 0
Expand Down
25 changes: 5 additions & 20 deletions src/FAudio_internal.h
Expand Up @@ -33,9 +33,6 @@
#include <math.h>
#include <assert.h>

#define FAudio_malloc(size) malloc(size)
#define FAudio_realloc(mem, size) realloc(mem, size)
#define FAudio_free(mem) free(mem)
#define FAudio_alloca(x) alloca(uint8_t, x)
#define FAudio_dealloca(x) dealloca(x)
#define FAudio_zero(ptr, size) memset(ptr, '\0', size)
Expand Down Expand Up @@ -77,23 +74,6 @@
#include <SDL_stdinc.h>
#include <SDL_assert.h>

/* When running with the COM wrapper, you'll need to make sure that memory is
* allocated with CoTaskMemAlloc because the application as well as XAPOFX
* expects memory to be allocated/freed using these routines.
* -flibit
*/
#ifdef FAUDIO_COM_WRAPPER
__declspec(dllimport) void * __stdcall CoTaskMemAlloc(size_t cb);
__declspec(dllimport) void * __stdcall CoTaskMemRealloc(void* pv, size_t cb);
__declspec(dllimport) void __stdcall CoTaskMemFree(void* pv);
#define FAudio_malloc CoTaskMemAlloc
#define FAudio_realloc CoTaskMemRealloc
#define FAudio_free CoTaskMemFree
#else
#define FAudio_malloc(size) SDL_malloc(size)
#define FAudio_realloc(mem, size) SDL_realloc(mem, size)
#define FAudio_free(mem) SDL_free(mem)
#endif
#define FAudio_alloca(x) SDL_stack_alloc(uint8_t, x)
#define FAudio_dealloca(x) SDL_stack_free(x)
#define FAudio_zero(ptr, size) SDL_memset(ptr, '\0', size)
Expand Down Expand Up @@ -481,6 +461,11 @@ uint32_t FAudio_PlatformResample(
uint32_t outLen
);

/* Allocator functions, can be overridden with FAudio_SetCustomAllocator. */
extern void* FAudio_malloc(size_t size);
extern void FAudio_free(void* ptr);
extern void* FAudio_realloc(void* ptr, size_t size);

/* Threading */

FAudioThread FAudio_PlatformCreateThread(
Expand Down
31 changes: 31 additions & 0 deletions src/FAudio_platform_sdl2.c
Expand Up @@ -553,6 +553,37 @@ uint32_t FAudio_timems()
return SDL_GetTicks();
}

/* Allocators */

FAudioMallocFunc FAudio_INTERNAL_customMalloc = SDL_malloc;
FAudioFreeFunc FAudio_INTERNAL_customFree = SDL_free;
FAudioReallocFunc FAudio_INTERNAL_customRealloc = SDL_realloc;

void* FAudio_malloc(size_t size)
{
return FAudio_INTERNAL_customMalloc(size);
}

void FAudio_free(void* ptr)
{
FAudio_INTERNAL_customFree(ptr);
}

void* FAudio_realloc(void* ptr, size_t size)
{
return FAudio_INTERNAL_customRealloc(ptr, size);
}

void FAudio_SetCustomAllocator(
FAudioMallocFunc customMalloc,
FAudioFreeFunc customFree,
FAudioReallocFunc customRealloc
) {
FAudio_INTERNAL_customMalloc = customMalloc;
FAudio_INTERNAL_customFree = customFree;
FAudio_INTERNAL_customRealloc = customRealloc;
}

/* FAudio I/O */

FAudioIOStream* FAudio_fopen(const char *path)
Expand Down

0 comments on commit d36e46b

Please sign in to comment.