Skip to content

Commit b3f2c4a

Browse files
committed
This modifes aligned_alloc code to support a wider range of possibilities
1 parent b7d12b0 commit b3f2c4a

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/core/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ set_target_properties(openjph PROPERTIES POSITION_INDEPENDENT_CODE ON)
139139
target_compile_definitions(openjph PUBLIC _FILE_OFFSET_BITS=64)
140140
target_include_directories(openjph PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/common> $<INSTALL_INTERFACE:include>)
141141

142+
## Check to see if aligned_alloc or posix_memalign is available
143+
# We want to the code to compile for C11 and C++11.
144+
# std::aligned_alloc is only availabe in C++17.
145+
# So here we try to see what API is available and adapt the code to support it
146+
if (NOT MSVC)
147+
include(CheckSymbolExists)
148+
check_symbol_exists(aligned_alloc "stdlib.h" OJPH_ALIGNED_ALLOC_EXISTS)
149+
if (OJPH_ALIGNED_ALLOC_EXISTS)
150+
target_compile_definitions(openjph PRIVATE OJPH_ALIGNED_ALLOC_EXISTS)
151+
else()
152+
check_symbol_exists(posix_memalign "stdlib.h" OJPH_POSIX_MEMALIGN_EXISTS)
153+
if (OJPH_POSIX_MEMALIGN_EXISTS)
154+
target_compile_definitions(openjph PRIVATE OJPH_POSIX_MEMALIGN_EXISTS)
155+
endif()
156+
endif()
157+
endif()
158+
142159
if (MSVC)
143160
set(OJPH_LIB_NAME_STRING "openjph.${OPENJPH_VERSION_MAJOR}.${OPENJPH_VERSION_MINOR}")
144161
set_target_properties(openjph

src/core/others/ojph_mem.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
// Date: 17 October 2025
3636
//***************************************************************************/
3737

38+
#include <assert.h>
3839
#include <stdlib.h>
40+
#include <stdint.h>
3941

4042
////////////////////////////////////////////////////////////////////////////////
4143
// OS detection definitions for C only
@@ -59,25 +61,69 @@
5961
#define OJPH_EXPORT
6062
#endif
6163

62-
////////////////////////////////////////////////////////////////////////////
64+
////////////////////////////////////////////////////////////////////////////////
6365
#ifdef OJPH_OS_WINDOWS
6466
OJPH_EXPORT void* ojph_aligned_malloc(size_t alignment, size_t size)
6567
{
68+
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
6669
return _aligned_malloc(size, alignment);
6770
}
6871

6972
OJPH_EXPORT void ojph_aligned_free(void* pointer)
7073
{
7174
_aligned_free(pointer);
7275
}
73-
#else
76+
#elif (defined OJPH_ALIGNED_ALLOC_EXISTS)
7477
void* ojph_aligned_malloc(size_t alignment, size_t size)
7578
{
79+
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
7680
return aligned_alloc(alignment, size);
7781
}
7882

7983
void ojph_aligned_free(void* pointer)
8084
{
8185
free(pointer);
8286
}
87+
#elif (defined OJPH_POSIX_MEMALIGN_EXISTS)
88+
void* ojph_aligned_malloc(size_t alignment, size_t size)
89+
{
90+
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
91+
void *p = NULL;
92+
int e = posix_memalign(&p, alignment, size);
93+
return (e ? NULL : p);
94+
}
95+
96+
void ojph_aligned_free(void* pointer)
97+
{
98+
free(pointer);
99+
}
100+
#else
101+
void* ojph_aligned_malloc(size_t alignment, size_t size)
102+
{
103+
assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
104+
105+
// emulate aligned_alloc
106+
void* orig_ptr = malloc(size + alignment + sizeof(void*));
107+
if (orig_ptr == NULL)
108+
return NULL; // Allocation failed
109+
110+
uintptr_t start_of_mem = (uintptr_t)orig_ptr + sizeof(void*);
111+
uintptr_t aligned_addr = (start_of_mem + alignment - 1) & ~(alignment - 1);
112+
113+
void** ptr_to_orig_ptr = (void**)aligned_addr;
114+
ptr_to_orig_ptr[-1] = orig_ptr;
115+
116+
return (void*)aligned_addr;
117+
}
118+
119+
void ojph_aligned_free(void* pointer)
120+
{
121+
if (pointer) {
122+
// Retrieve the original pointer stored just before aligned pointer
123+
void** ptr_to_orig_ptr = (void**)pointer;
124+
void* orig_ptr = ptr_to_orig_ptr[-1];
125+
126+
free(orig_ptr);
127+
}
128+
}
83129
#endif

0 commit comments

Comments
 (0)