Skip to content

Commit 8120253

Browse files
authored
Make aligned_alloc/free C functions (#227)
This PR makes ojph_aligned_alloc and ojph_aligned_free C functions, called using extern "C" {} This is the correct way to support aligned_alloc which is a C11 function; it is only available in C++ 17, which we do not support.
1 parent 26a4bf9 commit 8120253

File tree

7 files changed

+102
-32
lines changed

7 files changed

+102
-32
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.12.0)
44
include(ojph_version.cmake)
55

66
## project
7-
project (openjph VERSION ${OPENJPH_VERSION} DESCRIPTION "Open source implementation of JPH" LANGUAGES CXX)
7+
project (openjph VERSION ${OPENJPH_VERSION} DESCRIPTION "Open source implementation of JPH" LANGUAGES C CXX)
88
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
99

1010
################################################################################################
@@ -118,6 +118,11 @@ endif()
118118
message(STATUS "Building ${CMAKE_BUILD_TYPE}")
119119

120120
## C++ version and flags
121+
# C11 is needed for aligned_alloc
122+
if (NOT CMAKE_C_STANDARD)
123+
set(CMAKE_C_STANDARD 11)
124+
endif()
125+
message(STATUS "C Standard is set to ${CMAKE_C_STANDARD}")
121126
# C++14 is needed for gtest, otherwise, C++11 is sufficient for the library
122127
if (NOT CMAKE_CXX_STANDARD)
123128
set(CMAKE_CXX_STANDARD 14)

src/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ file(GLOB CODING_WASM "coding/*_wasm.cpp")
1111
file(GLOB CODING_AVX2 "coding/*_avx2.cpp")
1212
file(GLOB CODING_AVX512 "coding/*_avx512.cpp")
1313
file(GLOB COMMON "common/*.h")
14-
file(GLOB OTHERS "others/*.cpp")
14+
file(GLOB OTHERS "others/*.cpp" "others/*.c")
1515
file(GLOB TRANSFORM "transform/*.cpp" "transform/*.h")
1616
file(GLOB TRANSFORM_SSE "transform/*_sse.cpp")
1717
file(GLOB TRANSFORM_SSE2 "transform/*_sse2.cpp")

src/core/common/ojph_defs.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// This software is released under the 2-Clause BSD license, included
33
// below.
44
//
5-
// Copyright (c) 2019, Aous Naman
5+
// Copyright (c) 2019, Aous Naman
66
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
77
// Copyright (c) 2019, The University of New South Wales, Australia
8-
//
8+
//
99
// Redistribution and use in source and binary forms, with or without
1010
// modification, are permitted provided that the following conditions are
1111
// met:
12-
//
12+
//
1313
// 1. Redistributions of source code must retain the above copyright
1414
// notice, this list of conditions and the following disclaimer.
15-
//
15+
//
1616
// 2. Redistributions in binary form must reproduce the above copyright
1717
// notice, this list of conditions and the following disclaimer in the
1818
// documentation and/or other materials provided with the distribution.
19-
//
19+
//
2020
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2121
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2222
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A

src/core/common/ojph_mem.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,10 @@
4848

4949
namespace ojph {
5050

51-
////////////////////////////////////////////////////////////////////////////
52-
#ifdef OJPH_OS_WINDOWS
53-
inline void* ojph_aligned_malloc(size_t alignment, size_t size)
54-
{
55-
return _aligned_malloc(size, alignment);
56-
}
57-
58-
inline void ojph_aligned_free(void* pointer)
59-
{
60-
return _aligned_free(pointer);
61-
}
62-
#else
63-
inline void* ojph_aligned_malloc(size_t alignment, size_t size)
64-
{
65-
return aligned_alloc(alignment, size);
66-
}
67-
68-
inline void ojph_aligned_free(void* pointer)
69-
{
70-
return free(pointer);
51+
extern "C" {
52+
void* ojph_aligned_malloc(size_t alignment, size_t size);
53+
void ojph_aligned_free(void* pointer);
7154
}
72-
#endif
7355

7456
/////////////////////////////////////////////////////////////////////////////
7557
class mem_fixed_allocator

src/core/common/ojph_version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
//***************************************************************************/
3535

3636
#define OPENJPH_VERSION_MAJOR 0
37-
#define OPENJPH_VERSION_MINOR 24
38-
#define OPENJPH_VERSION_PATCH 5
37+
#define OPENJPH_VERSION_MINOR 25
38+
#define OPENJPH_VERSION_PATCH 0

src/core/others/ojph_mem.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//***************************************************************************/
2+
// This software is released under the 2-Clause BSD license, included
3+
// below.
4+
//
5+
// Copyright (c) 2025, Aous Naman
6+
// Copyright (c) 2025, Kakadu Software Pty Ltd, Australia
7+
// Copyright (c) 2025, The University of New South Wales, Australia
8+
//
9+
// Redistribution and use in source and binary forms, with or without
10+
// modification, are permitted provided that the following conditions are
11+
// met:
12+
//
13+
// 1. Redistributions of source code must retain the above copyright
14+
// notice, this list of conditions and the following disclaimer.
15+
//
16+
// 2. Redistributions in binary form must reproduce the above copyright
17+
// notice, this list of conditions and the following disclaimer in the
18+
// documentation and/or other materials provided with the distribution.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23+
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
//***************************************************************************/
32+
// This file is part of the OpenJPH software implementation.
33+
// File: ojph_mem.c
34+
// Author: Aous Naman
35+
// Date: 17 October 2025
36+
//***************************************************************************/
37+
38+
#include <stdlib.h>
39+
40+
////////////////////////////////////////////////////////////////////////////////
41+
// OS detection definitions for C only
42+
////////////////////////////////////////////////////////////////////////////////
43+
#if (defined WIN32) || (defined _WIN32) || (defined _WIN64)
44+
#define OJPH_OS_WINDOWS
45+
#elif (defined __APPLE__)
46+
#define OJPH_OS_APPLE
47+
#elif (defined __ANDROID__)
48+
#define OJPH_OS_ANDROID
49+
#elif (defined __linux)
50+
#define OJPH_OS_LINUX
51+
#endif
52+
53+
////////////////////////////////////////////////////////////////////////////////
54+
// Defines for dll in C only
55+
////////////////////////////////////////////////////////////////////////////////
56+
#if defined(OJPH_OS_WINDOWS) && defined(OJPH_BUILD_SHARED_LIBRARY)
57+
#define OJPH_EXPORT __declspec(dllexport)
58+
#else
59+
#define OJPH_EXPORT
60+
#endif
61+
62+
////////////////////////////////////////////////////////////////////////////
63+
#ifdef OJPH_OS_WINDOWS
64+
OJPH_EXPORT void* ojph_aligned_malloc(size_t alignment, size_t size)
65+
{
66+
return _aligned_malloc(size, alignment);
67+
}
68+
69+
OJPH_EXPORT void ojph_aligned_free(void* pointer)
70+
{
71+
_aligned_free(pointer);
72+
}
73+
#else
74+
void* ojph_aligned_malloc(size_t alignment, size_t size)
75+
{
76+
return aligned_alloc(alignment, size);
77+
}
78+
79+
void ojph_aligned_free(void* pointer)
80+
{
81+
free(pointer);
82+
}
83+
#endif

tests/mse_pae.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ include_directories(../src/apps/common)
99
include_directories(../src/core/common)
1010

1111
# Configure source files
12-
set(SOURCES mse_pae.cpp "../src/apps/others/ojph_img_io.cpp" "../src/core/others/ojph_message.cpp" "../src/core/others/ojph_file.cpp" "../src/core/others/ojph_mem.cpp" "../src/core/others/ojph_arch.cpp")
12+
set(SOURCES mse_pae.cpp "../src/apps/others/ojph_img_io.cpp" "../src/core/others/ojph_message.cpp" "../src/core/others/ojph_file.cpp" "../src/core/others/ojph_mem.cpp" "../src/core/others/ojph_mem.c" "../src/core/others/ojph_arch.cpp")
1313
set(OJPH_IMG_IO_SSE41 "../src/apps/others/ojph_img_io_sse41.cpp")
1414
set(OJPH_IMG_IO_AVX2 "../src/apps/others/ojph_img_io_avx2.cpp")
1515

1616
# if SIMD are not disabled
1717
if (NOT OJPH_DISABLE_SIMD)
18-
if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_X86_64")
18+
if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_X86_64")
1919
OR ("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_I386")
2020
OR MULTI_GEN_X86_64)
2121

0 commit comments

Comments
 (0)