Skip to content

Commit

Permalink
use aligned buffer under Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Scholz committed Sep 11, 2024
1 parent 04e483e commit 55777df
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
14 changes: 11 additions & 3 deletions ext/pylon/gstpylondsnvmmbufferfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

#include "gstpylondsnvmmbufferfactory.h"

#include <stdlib.h>
#include <unistd.h>

#include <cuda_runtime.h>
#include <gst/video/video.h>

Expand Down Expand Up @@ -82,9 +85,14 @@ void GstPylonDsNvmmBufferFactory::AllocateBuffer(size_t buffer_size,
intptr_t &buffer_context) {
void *buffer_mem = nullptr;

buffer_mem = g_malloc(buffer_size);
const size_t PAGE_SIZE = getpagesize();
const size_t aligned_buffer_size = RoundUp(buffer_size, PAGE_SIZE);

int ret = posix_memalign(&buffer_mem, PAGE_SIZE, aligned_buffer_size);
if (ret)
buffer_mem = nullptr;

create_params.params.size = buffer_size;
create_params.params.size = aligned_buffer_size;

/*
* The optimal approach would be to us the nvsurface being allocated below
Expand All @@ -111,7 +119,7 @@ void GstPylonDsNvmmBufferFactory::FreeBuffer(void *p_created_buffer,
NvBufSurface *surf = reinterpret_cast<NvBufSurface *>(buffer_context);

NvBufSurfaceDestroy(surf);
g_free(p_created_buffer);
free(p_created_buffer);
}

void GstPylonDsNvmmBufferFactory::DestroyBufferFactory() { delete this; }
4 changes: 3 additions & 1 deletion ext/pylon/gstpylondsnvmmbufferfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ class GstPylonDsNvmmBufferFactory : public GstPylonBufferFactory {
virtual void DestroyBufferFactory() override;

private:
size_t RoundUp(size_t N, size_t S) { return ((((N) + (S)-1) / (S)) * (S)); }

NvBufSurfaceLayout layout;
guint gpu_id;
NvBufSurfaceAllocateParams create_params;
};

#endif
#endif
16 changes: 14 additions & 2 deletions ext/pylon/gstpylonsysmembufferfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@

#include "gstpylonsysmembufferfactory.h"

#include <stdlib.h>
#include <unistd.h>

void GstPylonSysMemBufferFactory::AllocateBuffer(
size_t buffer_size, void **p_created_buffer,
intptr_t & /*buffer_context*/) {
*p_created_buffer = g_malloc(buffer_size);

#if defined(__GNUC__)
const size_t PAGE_SIZE = getpagesize();
const size_t aligned_buffer_size = RoundUp(buffer_size, PAGE_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
}

void GstPylonSysMemBufferFactory::FreeBuffer(void *p_created_buffer,
intptr_t /* buffer_context */) {
g_free(p_created_buffer);
free(p_created_buffer);
}

void GstPylonSysMemBufferFactory::DestroyBufferFactory() { delete this; }
3 changes: 3 additions & 0 deletions ext/pylon/gstpylonsysmembufferfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class GstPylonSysMemBufferFactory : public GstPylonBufferFactory {
virtual void FreeBuffer(void *p_created_buffer,
intptr_t buffer_context) override;
virtual void DestroyBufferFactory() override;

private:
size_t RoundUp(size_t N, size_t S) { return ((((N) + (S)-1) / (S)) * (S)); }
};

#endif

0 comments on commit 55777df

Please sign in to comment.