From 35a5bda6063025e2fc99904e1c47702152924c08 Mon Sep 17 00:00:00 2001 From: Giulio Moro Date: Wed, 8 May 2019 15:23:15 +0000 Subject: [PATCH] Overriding operator new for Heavy_{{name}} class, so that heap allocation returns aligned memory. Closes https://github.com/enzienaudio/hvcc/issues/18 --- generators/ir2c/templates/Heavy_NAME.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/generators/ir2c/templates/Heavy_NAME.hpp b/generators/ir2c/templates/Heavy_NAME.hpp index 75777a7c..fe21bfc4 100644 --- a/generators/ir2c/templates/Heavy_NAME.hpp +++ b/generators/ir2c/templates/Heavy_NAME.hpp @@ -3,6 +3,7 @@ #ifndef _HEAVY_CONTEXT_{{name|upper}}_HPP_ #define _HEAVY_CONTEXT_{{name|upper}}_HPP_ +#include // for std::bad_alloc // object includes #include "HeavyContext.hpp" {%- for i in include_set %} @@ -15,6 +16,17 @@ class Heavy_{{name}} : public HeavyContext { Heavy_{{name}}(double sampleRate, int poolKb=10, int inQueueKb=2, int outQueueKb=0); ~Heavy_{{name}}(); + // ensure desired alignment is achieved even for heap allocation + void* operator new(size_t sz) { + printf("alignment: %d\n", alignof(Heavy_{{name}})); + auto ptr = aligned_alloc(alignof(Heavy_{{name}}), sz); + if(!ptr) + { + std::bad_alloc e; + throw(e); + } + return ptr; + } const char *getName() override { return "{{name}}"; } int getNumInputChannels() override { return {{signal.numInputBuffers}}; } int getNumOutputChannels() override { return {{signal.numOutputBuffers}}; }