From c0abd6ef3f6106d9b5f00cb2f47bfbd7c33839f2 Mon Sep 17 00:00:00 2001 From: Blaise Tine Date: Tue, 9 Mar 2021 03:25:45 -0800 Subject: [PATCH] Aligned memory allocation workaround for PACE clusters --- driver/opae/vlsim/opae_sim.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/driver/opae/vlsim/opae_sim.cpp b/driver/opae/vlsim/opae_sim.cpp index b5d253ae..dd5a84c1 100644 --- a/driver/opae/vlsim/opae_sim.cpp +++ b/driver/opae/vlsim/opae_sim.cpp @@ -62,8 +62,23 @@ opae_sim::~opae_sim() { delete vortex_afu_; } +static void *__aligned_malloc(size_t alignment, size_t size) { + // reserve margin for alignment and storing of unaligned address + size_t margin = (alignment-1) + sizeof(void*); + void *unaligned_addr = malloc(size + margin); + void **aligned_addr = (void**)((uintptr_t)(unaligned_addr + margin) & ~(alignment-1)); + aligned_addr[-1] = unaligned_addr; + return aligned_addr; +} + +static void __aligned_free(void *ptr) { + // retreive the stored unaligned address and use it to free the allocation + void* unaligned_addr = ((void**)ptr)[-1]; + free(unaligned_addr); +} + int opae_sim::prepare_buffer(uint64_t len, void **buf_addr, uint64_t *wsid, int flags) { - auto alloc = aligned_alloc(CACHE_BLOCK_SIZE, len); + auto alloc = __aligned_malloc(CACHE_BLOCK_SIZE, len); if (alloc == NULL) return -1; host_buffer_t buffer; @@ -80,7 +95,7 @@ int opae_sim::prepare_buffer(uint64_t len, void **buf_addr, uint64_t *wsid, int void opae_sim::release_buffer(uint64_t wsid) { auto it = host_buffers_.find(wsid); if (it != host_buffers_.end()) { - free(it->second.data); + __aligned_free(it->second.data); host_buffers_.erase(it); } }