Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Overriding operator new for Heavy_{{name}} class, so that heap alloca…
Browse files Browse the repository at this point in the history
…tion returns aligned memory. Closes #18
  • Loading branch information
giuliomoro committed May 8, 2019
1 parent 30e4732 commit 35a5bda
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions generators/ir2c/templates/Heavy_NAME.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef _HEAVY_CONTEXT_{{name|upper}}_HPP_
#define _HEAVY_CONTEXT_{{name|upper}}_HPP_

#include <new> // for std::bad_alloc
// object includes
#include "HeavyContext.hpp"
{%- for i in include_set %}
Expand All @@ -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}}; }
Expand Down

0 comments on commit 35a5bda

Please sign in to comment.