Skip to content

Commit 9305d4c

Browse files
committed
Remove palloc
1 parent 8eb594a commit 9305d4c

12 files changed

Lines changed: 46 additions & 346 deletions

File tree

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
[submodule "third_party/palloc"]
2-
path = third_party/palloc
3-
url = https://github.com/AutoCookies/palloc.git

CMakeLists.txt

Lines changed: 17 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,9 @@ option(POMAI_BUILD_BENCH "Build benchmarks" OFF)
1414
# Prefer integer (SQ8/FP16) distance paths where data is quantized; reduces float use on embedded.
1515
option(POMAI_PREFER_INTEGER_MATH "Prefer integer/SQ8/FP16 paths for distance (embedded)" ON)
1616

17-
# =========================
18-
# Phase 3: palloc (git submodule at third_party/palloc)
19-
# =========================
20-
option(POMAI_USE_PALLOC "Use palloc as global allocator (PA_OVERRIDE=ON)" ON)
21-
22-
# Ensure POMAI_USE_PALLOC and POMAI_PREFER_INTEGER_MATH are defined for source code (0 or 1)
23-
add_compile_definitions(POMAI_USE_PALLOC=$<BOOL:${POMAI_USE_PALLOC}>)
17+
# Ensure POMAI_PREFER_INTEGER_MATH is defined for source code (0 or 1)
2418
add_compile_definitions(POMAI_PREFER_INTEGER_MATH=$<BOOL:${POMAI_PREFER_INTEGER_MATH}>)
2519

26-
if (POMAI_USE_PALLOC)
27-
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc/CMakeLists.txt)
28-
message(FATAL_ERROR
29-
"palloc submodule not initialized. Run:\n git submodule update --init third_party/palloc")
30-
endif()
31-
# Apply designator-order fix for init.c (required when building with C++/PA_USE_CXX; submodule has wrong order)
32-
set(PALLOC_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc)
33-
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/PatchPallocInit.cmake)
34-
# palloc submodule uses PA_* options — set BEFORE add_subdirectory
35-
set(PA_BUILD_SHARED OFF CACHE BOOL "" FORCE)
36-
set(PA_BUILD_OBJECT OFF CACHE BOOL "" FORCE)
37-
set(PA_BUILD_TESTS OFF CACHE BOOL "" FORCE)
38-
set(PA_OVERRIDE ON CACHE BOOL "" FORCE)
39-
set(PA_INSTALL_TOPLEVEL OFF CACHE BOOL "" FORCE)
40-
set(PA_USE_CXX ON CACHE BOOL "" FORCE)
41-
set(PALLOC_BUILD_MODE "" CACHE STRING "palloc build mode (legacy for pomaidb)" FORCE)
42-
add_subdirectory(third_party/palloc EXCLUDE_FROM_ALL)
43-
message(STATUS "[pomai] palloc: enabled (static, PA_OVERRIDE=ON)")
44-
else()
45-
message(STATUS "[pomai] palloc: disabled")
46-
endif()
47-
4820

4921
# =========================
5022
# Native HNSW (Replaces FAISS HNSW)
@@ -105,7 +77,6 @@ target_include_directories(pomai
10577
${CMAKE_CURRENT_SOURCE_DIR}
10678
SYSTEM PRIVATE
10779
${CMAKE_CURRENT_SOURCE_DIR}/third_party
108-
${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc/include
10980
)
11081

11182
# OpenMP for parallel builds if needed
@@ -114,25 +85,15 @@ if (OpenMP_CXX_FOUND)
11485
target_link_libraries(pomai PRIVATE OpenMP::OpenMP_CXX)
11586
endif()
11687

117-
# Phase 3: mimalloc — TWO linking modes
118-
#
119-
# Mode A (full MI_OVERRIDE): for standalone executables/static binaries only.
120-
# malloc/free/new/delete are globally replaced. Fast, but UNSAFE when
121-
# loaded as a .so by a host process (e.g. Python) that has its own heap.
122-
#
123-
# Mode B (no-override, libpomai_c.so): mimalloc is used internally via
124-
# mi_heap_* APIs. The global malloc/free are NOT replaced, so Python's
125-
# glibc heap and mimalloc coexist without conflict.
126-
#
127-
# We achieve this by NOT linking mimalloc into libpomai (the intermediate static
128-
# lib). Instead:
129-
# - libpomai_c.so links mimalloc-static with MI_OVERRIDE=OFF (internal-only)
130-
# - test/bench executables link mimalloc-static with MI_OVERRIDE=ON
131-
# (done in the executable's own target_link_libraries)
132-
#
133-
# Note: MI_OVERRIDE is set at mimalloc *build time* (above), so we build ONE
134-
# mimalloc with MI_OVERRIDE=ON. For the .so we suppress interposition by NOT
135-
# exporting gnu-versioned malloc symbols — achieved via -Wl,--exclude-libs.
88+
if (MSVC)
89+
target_compile_options(pomai PRIVATE /W4 /permissive-)
90+
else()
91+
# Strict warnings for pomaidb code; then suppress vendored third_party (simd) warnings
92+
target_compile_options(pomai PRIVATE
93+
-Wall -Wextra -Wpedantic -Wconversion -Wshadow
94+
-Wno-cpp -Wno-unknown-pragmas -Wno-conversion -Wno-float-conversion -Wno-unused-function
95+
)
96+
endif()
13697

13798

13899
# =========================
@@ -152,44 +113,6 @@ set_target_properties(pomai_c_static PROPERTIES OUTPUT_NAME pomai_c)
152113
target_link_libraries(pomai_c PRIVATE pomai)
153114
target_link_libraries(pomai_c_static PRIVATE pomai)
154115

155-
# Phase 3 fix: shared library (libpomai_c.so) must resolve palloc symbols when loaded by Python/FFI.
156-
# We link a palloc built with PALLOC_OVERRIDE=OFF so the .so is self-contained and does not
157-
# override the process malloc (safe for ctypes/JNI/FFI). Static wrapper and exes use palloc with OVERRIDE=ON.
158-
#
159-
if (POMAI_USE_PALLOC)
160-
# Static wrapper: link palloc (override ON) for standalone executables.
161-
target_link_libraries(pomai_c_static PRIVATE palloc-static)
162-
target_compile_definitions(pomai_c_static PRIVATE POMAI_USE_PALLOC=1)
163-
164-
# Build palloc a second time with PA_OVERRIDE=OFF for the shared library (no malloc override at dlopen).
165-
include(ExternalProject)
166-
set(PALLOC_NOOVERRIDE_LIB "${CMAKE_BINARY_DIR}/palloc_nooverride-build/libpalloc.a")
167-
ExternalProject_Add(palloc_nooverride
168-
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc
169-
BINARY_DIR ${CMAKE_BINARY_DIR}/palloc_nooverride-build
170-
CMAKE_ARGS
171-
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
172-
-DPALLOC_BUILD_MODE=
173-
-DPA_OVERRIDE=OFF
174-
-DPA_BUILD_SHARED=OFF
175-
-DPA_BUILD_OBJECT=OFF
176-
-DPA_BUILD_TESTS=OFF
177-
-DPA_USE_CXX=ON
178-
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
179-
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
180-
INSTALL_COMMAND ""
181-
BUILD_BYPRODUCTS ${PALLOC_NOOVERRIDE_LIB}
182-
)
183-
add_library(palloc-static-nooverride STATIC IMPORTED GLOBAL)
184-
set_target_properties(palloc-static-nooverride PROPERTIES
185-
IMPORTED_LOCATION ${PALLOC_NOOVERRIDE_LIB}
186-
)
187-
add_dependencies(palloc-static-nooverride palloc_nooverride)
188-
add_dependencies(pomai_c palloc_nooverride)
189-
target_link_libraries(pomai_c PRIVATE palloc-static-nooverride)
190-
target_link_libraries(pomai_c PRIVATE pthread rt atomic)
191-
endif()
192-
193116
# Export C API symbols when building shared library on Windows.
194117
target_compile_definitions(pomai_c PRIVATE POMAI_C_BUILD_DLL)
195118

@@ -205,10 +128,6 @@ target_include_directories(pomai_c_static
205128
PRIVATE
206129
${CMAKE_CURRENT_SOURCE_DIR}/src
207130
)
208-
if (POMAI_USE_PALLOC)
209-
target_include_directories(pomai_c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc/include)
210-
target_include_directories(pomai_c_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc/include)
211-
endif()
212131

213132
if (MSVC)
214133
target_compile_options(pomai_c PRIVATE /W4 /permissive-)
@@ -218,16 +137,6 @@ else()
218137
target_compile_options(pomai_c_static PRIVATE -Wall -Wextra -Wpedantic -Wconversion -Wshadow)
219138
endif()
220139

221-
if (MSVC)
222-
target_compile_options(pomai PRIVATE /W4 /permissive-)
223-
else()
224-
# Strict warnings for pomaidb code; then suppress vendored third_party (simd, palloc) warnings
225-
target_compile_options(pomai PRIVATE
226-
-Wall -Wextra -Wpedantic -Wconversion -Wshadow
227-
-Wno-cpp -Wno-unknown-pragmas -Wno-conversion -Wno-float-conversion -Wno-unused-function
228-
)
229-
endif()
230-
231140
# =========================
232141
# Tests
233142
# =========================
@@ -246,11 +155,8 @@ if (POMAI_BUILD_TESTS)
246155
endif()
247156

248157
function(pomai_setup_test target)
249-
# Link against main library (and palloc when used, so exe resolves palloc symbols)
158+
# Link against main library
250159
target_link_libraries(${target} PRIVATE pomai)
251-
if (POMAI_USE_PALLOC)
252-
target_link_libraries(${target} PRIVATE palloc-static)
253-
endif()
254160

255161
# One main per test binary (your harness)
256162
target_sources(${target} PRIVATE tests/common/test_runner.cc)
@@ -267,7 +173,6 @@ if (POMAI_BUILD_TESTS)
267173
${CMAKE_CURRENT_SOURCE_DIR} # "tests/common/..."
268174
${CMAKE_CURRENT_SOURCE_DIR}/include # public API
269175
${CMAKE_CURRENT_SOURCE_DIR}/src # internal headers: util/, core/, table/, storage/
270-
${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc/include # palloc.h for util/aligned_vector.h etc.
271176
)
272177

273178
# Make TSAN builds more stable on some distros (PIE can trigger weird mmap layouts).
@@ -423,9 +328,6 @@ if (POMAI_BUILD_TESTS)
423328

424329
# Manual setup loosely based on pomai_setup_test but without test_runner.cc
425330
target_link_libraries(pomai_crash_test PRIVATE pomai)
426-
if (POMAI_USE_PALLOC)
427-
target_link_libraries(pomai_crash_test PRIVATE palloc-static)
428-
endif()
429331
if (MSVC)
430332
target_compile_options(pomai_crash_test PRIVATE /W4 /permissive-)
431333
else()
@@ -435,7 +337,6 @@ if (POMAI_BUILD_TESTS)
435337
${CMAKE_CURRENT_SOURCE_DIR}
436338
${CMAKE_CURRENT_SOURCE_DIR}/include
437339
${CMAKE_CURRENT_SOURCE_DIR}/src
438-
${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc/include
439340
)
440341
if (POMAI_IS_TSAN)
441342
target_compile_options(pomai_crash_test PRIVATE -fno-pie)
@@ -473,11 +374,7 @@ if (POMAI_BUILD_BENCH)
473374
# target_include_directories(my_bench PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src)
474375
endif()
475376

476-
# Helper: when palloc is used, executables that link pomai (or pomai_c) must also link palloc.
477377
set(POMAI_EXE_DEPS pomai)
478-
if (POMAI_USE_PALLOC)
479-
list(APPEND POMAI_EXE_DEPS palloc-static)
480-
endif()
481378

482379
# Baseline Benchmark
483380
add_executable(bench_baseline tests/bench_baseline.cc)
@@ -515,9 +412,6 @@ target_include_directories(ci_perf_bench PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inc
515412
add_executable(benchmark_a benchmarks/palloc_env_stress.cc)
516413
target_link_libraries(benchmark_a PRIVATE ${POMAI_EXE_DEPS})
517414
target_include_directories(benchmark_a PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
518-
if (POMAI_USE_PALLOC)
519-
target_include_directories(benchmark_a PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/palloc/include)
520-
endif()
521415

522416
# =========================
523417
# Tools
@@ -532,8 +426,6 @@ target_include_directories(pomai_inspect PRIVATE
532426
# =========================
533427
# C API Tests & Examples
534428
# =========================
535-
# When POMAI_USE_PALLOC=ON, pomai_c.so does not link palloc (for safe Python/FFI use).
536-
# C exes that link only the .so would then have undefined palloc refs; build them only when palloc is off.
537429
if (POMAI_BUILD_TESTS)
538430
add_executable(c_api_test tests/test_c_api_basic.cpp)
539431
target_link_libraries(c_api_test PRIVATE pomai_c_static)
@@ -542,15 +434,13 @@ if (POMAI_BUILD_TESTS)
542434
add_test(NAME c_api_test COMMAND c_api_test)
543435
endif()
544436

545-
if (NOT POMAI_USE_PALLOC)
546-
add_executable(c_basic_example examples/c_basic.c)
547-
target_link_libraries(c_basic_example PRIVATE pomai_c)
548-
target_include_directories(c_basic_example PRIVATE include)
437+
add_executable(c_basic_example examples/c_basic.c)
438+
target_link_libraries(c_basic_example PRIVATE pomai_c)
439+
target_include_directories(c_basic_example PRIVATE include)
549440

550-
add_executable(c_scan_export_example examples/c_scan_export.c)
551-
target_link_libraries(c_scan_export_example PRIVATE pomai_c)
552-
target_include_directories(c_scan_export_example PRIVATE include)
553-
endif()
441+
add_executable(c_scan_export_example examples/c_scan_export.c)
442+
target_link_libraries(c_scan_export_example PRIVATE pomai_c)
443+
target_include_directories(c_scan_export_example PRIVATE include)
554444

555445
install(TARGETS pomai pomai_c pomai_c_static
556446
ARCHIVE DESTINATION lib
@@ -563,5 +453,3 @@ install(FILES
563453
include/pomai/c_version.h
564454
include/pomai/database.h
565455
DESTINATION include/pomai)
566-
add_executable(palloc_perf_verify tests/palloc_perf_verify.cc)
567-
target_link_libraries(palloc_perf_verify PRIVATE pomai palloc-static pthread)

cmake/PatchPallocInit.cmake

Lines changed: 0 additions & 70 deletions
This file was deleted.

include/palloc_compat.h

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
// Compatibility header: map PomaiDB's palloc_* names to the pa_* API from third_party/palloc (submodule).
2-
// When POMAI_USE_PALLOC=0, provides stubs using the system allocator (for CI sanitizer builds etc.).
1+
// Compatibility shim: provides palloc_* memory API using the system allocator.
2+
// PomaiDB no longer depends on third_party/palloc; all aligned allocation goes
3+
// through posix_memalign (Linux/macOS) or _aligned_malloc (Windows).
34
#pragma once
45

5-
#if defined(POMAI_USE_PALLOC) && POMAI_USE_PALLOC
6-
#include "palloc.h"
7-
8-
#ifdef __cplusplus
9-
#define palloc_heap_t pa_heap_t
10-
#define palloc_free pa_free
11-
#define palloc_malloc_aligned pa_malloc_aligned
12-
#define palloc_heap_new pa_heap_new
13-
#define palloc_heap_delete pa_heap_delete
14-
#define palloc_heap_malloc_aligned pa_heap_malloc_aligned
15-
#define palloc_option_set pa_option_set
16-
#define palloc_option_reserve_huge_os_pages pa_option_reserve_huge_os_pages
17-
#endif
18-
19-
#else
20-
// Stub: system allocator when palloc is disabled (no link to third_party/palloc).
216
#include <cstddef>
227
#include <cstdlib>
238
#ifdef _WIN32
@@ -26,6 +11,7 @@
2611

2712
#ifdef __cplusplus
2813
typedef void* palloc_heap_t;
14+
2915
static inline void* palloc_malloc_aligned(std::size_t size, std::size_t alignment) {
3016
if (alignment < sizeof(void*)) alignment = sizeof(void*);
3117
#if defined(_WIN32) || defined(_WIN64)
@@ -35,7 +21,15 @@ static inline void* palloc_malloc_aligned(std::size_t size, std::size_t alignmen
3521
return (posix_memalign(&p, alignment, size) == 0) ? p : nullptr;
3622
#endif
3723
}
38-
static inline void palloc_free(void* p) { std::free(p); }
24+
25+
static inline void palloc_free(void* p) {
26+
#if defined(_WIN32) || defined(_WIN64)
27+
_aligned_free(p);
28+
#else
29+
std::free(p);
30+
#endif
31+
}
32+
3933
static inline palloc_heap_t* palloc_heap_new(void) { return nullptr; }
4034
static inline void palloc_heap_delete(palloc_heap_t*) {}
4135
static inline void* palloc_heap_malloc_aligned(palloc_heap_t*, std::size_t size, std::size_t alignment) {
@@ -44,4 +38,3 @@ static inline void* palloc_heap_malloc_aligned(palloc_heap_t*, std::size_t size,
4438
static inline void palloc_option_set(long, long) {}
4539
static constexpr long palloc_option_reserve_huge_os_pages = 0;
4640
#endif
47-
#endif

0 commit comments

Comments
 (0)