Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ set_target_properties(openjph PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_definitions(openjph PUBLIC _FILE_OFFSET_BITS=64)
target_include_directories(openjph PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/common> $<INSTALL_INTERFACE:include>)

## This is to check if aligned_alloc or posix_memalign is available
# We want the code to compile for C11 and C++11.
# std::aligned_alloc is only availabe in C++17.
# So here we try to see which API is available and adapt the code to use it
if (NOT MSVC)
include(CheckSymbolExists)
check_symbol_exists(aligned_alloc "stdlib.h" OJPH_ALIGNED_ALLOC_EXISTS)
if (OJPH_ALIGNED_ALLOC_EXISTS)
target_compile_definitions(openjph PRIVATE OJPH_ALIGNED_ALLOC_EXISTS)
else()
check_symbol_exists(posix_memalign "stdlib.h" OJPH_POSIX_MEMALIGN_EXISTS)
if (OJPH_POSIX_MEMALIGN_EXISTS)
target_compile_definitions(openjph PRIVATE OJPH_POSIX_MEMALIGN_EXISTS)
endif()
endif()
endif()

if (MSVC)
set(OJPH_LIB_NAME_STRING "openjph.${OPENJPH_VERSION_MAJOR}.${OPENJPH_VERSION_MINOR}")
set_target_properties(openjph
Expand Down
2 changes: 1 addition & 1 deletion src/core/common/ojph_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@

#define OPENJPH_VERSION_MAJOR 0
#define OPENJPH_VERSION_MINOR 25
#define OPENJPH_VERSION_PATCH 2
#define OPENJPH_VERSION_PATCH 3
50 changes: 48 additions & 2 deletions src/core/others/ojph_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
// Date: 17 October 2025
//***************************************************************************/

#include <assert.h>
#include <stdlib.h>
#include <stdint.h>

////////////////////////////////////////////////////////////////////////////////
// OS detection definitions for C only
Expand All @@ -59,25 +61,69 @@
#define OJPH_EXPORT
#endif

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#ifdef OJPH_OS_WINDOWS
OJPH_EXPORT void* ojph_aligned_malloc(size_t alignment, size_t size)
{
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
return _aligned_malloc(size, alignment);
}

OJPH_EXPORT void ojph_aligned_free(void* pointer)
{
_aligned_free(pointer);
}
#else
#elif (defined OJPH_ALIGNED_ALLOC_EXISTS)
void* ojph_aligned_malloc(size_t alignment, size_t size)
{
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
return aligned_alloc(alignment, size);
}

void ojph_aligned_free(void* pointer)
{
free(pointer);
}
#elif (defined OJPH_POSIX_MEMALIGN_EXISTS)
void* ojph_aligned_malloc(size_t alignment, size_t size)
{
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
void *p = NULL;
int e = posix_memalign(&p, alignment, size);
return (e ? NULL : p);
}

void ojph_aligned_free(void* pointer)
{
free(pointer);
}
#else
void* ojph_aligned_malloc(size_t alignment, size_t size)
{
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);

// emulate aligned_alloc
void* orig_ptr = malloc(size + alignment + sizeof(void*));
if (orig_ptr == NULL)
return NULL; // Allocation failed

uintptr_t start_of_mem = (uintptr_t)orig_ptr + sizeof(void*);
uintptr_t aligned_addr = (start_of_mem + alignment - 1) & ~(alignment - 1);

void** ptr_to_orig_ptr = (void**)aligned_addr;
ptr_to_orig_ptr[-1] = orig_ptr;

return (void*)aligned_addr;
}

void ojph_aligned_free(void* pointer)
{
if (pointer) {
// Retrieve the original pointer stored just before aligned pointer
void** ptr_to_orig_ptr = (void**)pointer;
void* orig_ptr = ptr_to_orig_ptr[-1];

free(orig_ptr);
}
}
#endif
Loading