From 7e7c14fa70180aebd713df297e9975fdaf664cb9 Mon Sep 17 00:00:00 2001 From: Steven Scholz Date: Wed, 11 Sep 2024 12:34:46 +0000 Subject: [PATCH] use aligned buffer under Linux --- ext/pylon/gstpylondsnvmmbufferfactory.cpp | 10 ++++++++-- ext/pylon/gstpylonsysmembufferfactory.cpp | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ext/pylon/gstpylondsnvmmbufferfactory.cpp b/ext/pylon/gstpylondsnvmmbufferfactory.cpp index 43a5346..59a86fa 100644 --- a/ext/pylon/gstpylondsnvmmbufferfactory.cpp +++ b/ext/pylon/gstpylondsnvmmbufferfactory.cpp @@ -32,6 +32,9 @@ #include "gstpylondsnvmmbufferfactory.h" +#include +#include + #include #include @@ -82,10 +85,13 @@ void GstPylonDsNvmmBufferFactory::AllocateBuffer(size_t buffer_size, intptr_t &buffer_context) { void *buffer_mem = nullptr; - const size_t PAGE_SIZE = 4096; + const size_t PAGE_SIZE = getpagesize(); const size_t aligned_buffer_size = RoundUp(buffer_size, PAGE_SIZE); - buffer_mem = aligned_alloc(PAGE_SIZE, aligned_buffer_size); + int ret = posix_memalign(&buffer_mem, PAGE_SIZE, aligned_buffer_size); + if (ret) + buffer_mem = nullptr; + create_params.params.size = aligned_buffer_size; /* diff --git a/ext/pylon/gstpylonsysmembufferfactory.cpp b/ext/pylon/gstpylonsysmembufferfactory.cpp index cd787df..4fbda2d 100644 --- a/ext/pylon/gstpylonsysmembufferfactory.cpp +++ b/ext/pylon/gstpylonsysmembufferfactory.cpp @@ -32,14 +32,19 @@ #include "gstpylonsysmembufferfactory.h" +#include +#include + void GstPylonSysMemBufferFactory::AllocateBuffer( size_t buffer_size, void **p_created_buffer, intptr_t & /*buffer_context*/) { #if defined(__GNUC__) - const size_t PAGE_SIZE = 4096; + const size_t PAGE_SIZE = getpagesize(); const size_t aligned_buffer_size = RoundUp(buffer_size, PAGE_SIZE); - *p_created_buffer = aligned_alloc(PAGE_SIZE, aligned_buffer_size); + int ret = posix_memalign(p_created_buffer, PAGE_SIZE, aligned_buffer_size); + if (ret) + *p_created_buffer = nullptr; #else *p_created_buffer = malloc(buffer_size); #endif