Skip to content

Commit

Permalink
Fix compilation with mimalloc and LTO
Browse files Browse the repository at this point in the history
  • Loading branch information
Oipo committed Nov 4, 2024
1 parent 4274aaa commit d7fc27e
Show file tree
Hide file tree
Showing 28 changed files with 90 additions and 65 deletions.
30 changes: 13 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
if(LTO_SUPPORTED)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
if(ICHOR_COMPILER_ID STREQUAL "clang")
set(CMAKE_C_COMPILE_OPTIONS_IPO "-flto=full")
set(CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=full")
endif()
set(ICHOR_LTO ON)
message(STATUS "Enabled LTO")
else()
Expand Down Expand Up @@ -129,15 +133,12 @@ endif()

file(GLOB SPDLOG_SOURCES ${ICHOR_EXTERNAL_DIR}/spdlog/src/*.cpp)

add_library(ichor ${FMT_SOURCES} ${ICHOR_FRAMEWORK_SOURCES} ${ICHOR_LOGGING_SOURCES} ${ICHOR_TCP_SOURCES} ${ICHOR_METRICS_SOURCES} ${ICHOR_TIMER_SOURCES} ${ICHOR_IO_SOURCES} ${ICHOR_BASE64_SOURCES} ${ICHOR_STL_SOURCES})

if(ICHOR_USE_MIMALLOC AND NOT ICHOR_USE_SYSTEM_MIMALLOC)
set(MIMALLOC_SOURCES ${ICHOR_TOP_DIR}/external/mimalloc/src/static.c)
add_library(ichor-mimalloc SHARED ${MIMALLOC_SOURCES})
target_link_libraries(ichor PUBLIC ichor-mimalloc)
set(MIMALLOC_TARGET ichor-mimalloc)
set(ICHOR_FRAMEWORK_SOURCES ${ICHOR_FRAMEWORK_SOURCES} ${ICHOR_TOP_DIR}/external/mimalloc/src/static.c)
endif()

add_library(ichor ${FMT_SOURCES} ${ICHOR_FRAMEWORK_SOURCES} ${ICHOR_LOGGING_SOURCES} ${ICHOR_TCP_SOURCES} ${ICHOR_METRICS_SOURCES} ${ICHOR_TIMER_SOURCES} ${ICHOR_IO_SOURCES} ${ICHOR_BASE64_SOURCES} ${ICHOR_STL_SOURCES})

if(ICHOR_ENABLE_INTERNAL_DEBUGGING)
target_compile_definitions(ichor PUBLIC ICHOR_ENABLE_INTERNAL_DEBUGGING)
endif()
Expand Down Expand Up @@ -486,29 +487,24 @@ if(ICHOR_USE_MIMALLOC)
target_compile_definitions(ichor PRIVATE ICHOR_USE_SYSTEM_MIMALLOC)
else()
if(ICHOR_USE_HARDENING OR ICHOR_ENABLE_INTERNAL_DEBUGGING)
target_compile_definitions(ichor-mimalloc PRIVATE MI_SECURE=4)
target_compile_definitions(ichor PRIVATE MI_SECURE=4)
endif()
if(ICHOR_ENABLE_INTERNAL_DEBUGGING)
target_compile_definitions(ichor-mimalloc PRIVATE MI_DEBUG=3)
target_compile_definitions(ichor PRIVATE MI_DEBUG=3)
endif()
if(ICHOR_USE_SANITIZERS AND NOT WIN32)
target_compile_definitions(ichor-mimalloc PRIVATE MI_TRACK_ASAN=1)
target_compile_definitions(ichor PRIVATE MI_TRACK_ASAN=1)
endif()
if(ICHOR_USE_THREAD_SANITIZER AND NOT WIN32)
target_compile_definitions(ichor-mimalloc PRIVATE MI_TSAN=1)
target_compile_definitions(ichor PRIVATE MI_TSAN=1)
endif()
if(ICHOR_MUSL)
target_compile_definitions(ichor-mimalloc PRIVATE MI_LIBC_MUSL=1)
target_compile_definitions(ichor PRIVATE MI_LIBC_MUSL=1)
endif()
if(NOT WIN32)
target_compile_options(ichor-mimalloc PUBLIC -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free)
target_compile_options(ichor PUBLIC -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free)
endif()
target_compile_definitions(ichor PRIVATE ICHOR_USE_MIMALLOC)
target_compile_definitions(ichor-mimalloc PRIVATE ICHOR_USE_MIMALLOC)
target_include_directories(ichor-mimalloc PUBLIC
$<BUILD_INTERFACE:${ICHOR_EXTERNAL_DIR}/mimalloc/include>
$<INSTALL_INTERFACE:include/ichor/external>)
target_compile_definitions(ichor PUBLIC ICHOR_USE_MIMALLOC)
target_include_directories(ichor PUBLIC
$<BUILD_INTERFACE:${ICHOR_EXTERNAL_DIR}/mimalloc/include>
$<INSTALL_INTERFACE:include/ichor/external>)
Expand Down
4 changes: 4 additions & 0 deletions IchorConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ if(ICHOR_LTO)
endif()
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_C_COMPILE_OPTIONS_IPO "-flto=full")
set(CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=full")
endif()
message(STATUS "Enabled LTO")
else()
message(FATAL_ERROR "LTO not supported but Ichor built with LTO: ${LTO_ERROR}")
Expand Down
1 change: 1 addition & 0 deletions benchmarks/beast_benchmark/boost_beast_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <string>
#include <fmt/format.h>
#include <ichor/glaze.h>
#include <ichor/ichor-mimalloc.h>

namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/logging/NullLogger.h>
#include <ichor/services/metrics/MemoryUsageFunctions.h>
#include <ichor/ichor-mimalloc.h>
#include <iostream>
#include <thread>
#include <array>
Expand Down
1 change: 1 addition & 0 deletions benchmarks/event_benchmark/ichor_event_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/logging/NullLogger.h>
#include <ichor/services/metrics/MemoryUsageFunctions.h>
#include <ichor/ichor-mimalloc.h>
#include <iostream>
#include <thread>
#include <array>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ichor/services/serialization/ISerializer.h>
#include <ichor/services/logging/NullLogger.h>
#include <ichor/services/metrics/MemoryUsageFunctions.h>
#include <ichor/ichor-mimalloc.h>
#include <iostream>
#include <thread>
#include <array>
Expand Down
1 change: 1 addition & 0 deletions benchmarks/start_benchmark/ichor_start_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/logging/NullLogger.h>
#include <ichor/services/metrics/MemoryUsageFunctions.h>
#include <ichor/ichor-mimalloc.h>
#include <iostream>
#include <thread>
#include <array>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/logging/NullLogger.h>
#include <ichor/services/metrics/MemoryUsageFunctions.h>
#include <ichor/ichor-mimalloc.h>
#include <iostream>
#include <thread>
#include <array>
Expand Down
1 change: 1 addition & 0 deletions benchmarks/utils_benchmark/ichor_utils_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ichor/stl/StringUtils.h>
#include <ichor/services/metrics/MemoryUsageFunctions.h>
#include <ichor/services/network/http/IHttpHostService.h>
#include <ichor/ichor-mimalloc.h>
#include "../../examples/common/lyra.hpp"


Expand Down
1 change: 1 addition & 0 deletions examples/etcd_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ichor/services/network/boost/HttpConnectionService.h>
#include <ichor/services/network/boost/AsioContextService.h>
#include <ichor/services/network/ClientFactory.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/event_statistics_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/metrics/EventStatisticsService.h>
#include <ichor/services/timer/TimerFactoryFactory.h>
#include <ichor/ichor-mimalloc.h>
#include "UsingStatisticsService.h"

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
Expand Down
1 change: 1 addition & 0 deletions examples/factory_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "RuntimeCreatedService.h"
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/http_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ichor/services/network/boost/HttpConnectionService.h>
#include <ichor/services/network/ClientFactory.h>
#include <ichor/services/serialization/ISerializer.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/http_ping_pong/ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ichor/services/serialization/ISerializer.h>
#include <ichor/services/logging/NullLogger.h>
#include <ichor/services/timer/TimerFactoryFactory.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/http_ping_pong/pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ichor/services/network/boost/AsioContextService.h>
#include <ichor/services/serialization/ISerializer.h>
#include <ichor/services/logging/NullLogger.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/introspection_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/timer/TimerFactoryFactory.h>
#include <ichor/ichor-mimalloc.h>
#include "IntrospectionService.h"

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
Expand Down
1 change: 1 addition & 0 deletions examples/minimal_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/DependencyManager.h>
#include <ichor/services/timer/TimerFactoryFactory.h>
#include <ichor/ichor-mimalloc.h>
#include <csignal>

using namespace Ichor;
Expand Down
1 change: 1 addition & 0 deletions examples/multithreaded_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "OtherService.h"
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/optional_dependency_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "OptionalService.h"
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/realtime_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "OptionalService.h"
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/ichor-mimalloc.h>
#include "GlobalRealtimeSettings.h"

#if defined(NDEBUG)
Expand Down
1 change: 1 addition & 0 deletions examples/serializer_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ichor/event_queues/PriorityQueue.h>
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/serialization/ISerializer.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/tcp_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/services/network/ClientFactory.h>
#include <ichor/services/serialization/ISerializer.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/timer_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UsingTimerService.h"
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/websocket_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ichor/services/network/boost/WsConnectionService.h>
#include <ichor/services/network/ClientFactory.h>
#include <ichor/services/serialization/ISerializer.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
1 change: 1 addition & 0 deletions examples/yielding_timer_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UsingTimerService.h"
#include <ichor/services/logging/LoggerFactory.h>
#include <ichor/ichor-mimalloc.h>

// Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it.
#ifdef ICHOR_USE_SPDLOG
Expand Down
49 changes: 49 additions & 0 deletions include/ichor/ichor-mimalloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#if defined(ICHOR_USE_SYSTEM_MIMALLOC) || defined(ICHOR_USE_MIMALLOC)
#include <mimalloc-new-delete.h>

#if (!defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)) || defined(__CYGWIN__)
extern "C" {
void *malloc(size_t size) {
return mi_malloc(size);
}

void free(void *ptr) {
mi_free(ptr);
}

void *realloc(void *ptr, size_t new_size) {
return mi_realloc(ptr, new_size);
}

void *calloc(size_t num, size_t size) {
return mi_calloc(num, size);
}

void *aligned_alloc(size_t alignment, size_t size) {
return mi_aligned_alloc(alignment, size);
}

size_t malloc_usable_size(void *ptr) {
return mi_malloc_usable_size(ptr);
}

void *memalign(size_t alignment, size_t size) {
return mi_memalign(alignment, size);
}

int posix_memalign(void **memptr, size_t alignment, size_t size) {
return mi_posix_memalign(memptr, alignment, size);
}

void *valloc(size_t size) {
return mi_valloc(size);
}

void *pvalloc(size_t size) {
return mi_pvalloc(size);
}
}
#endif
#endif
48 changes: 0 additions & 48 deletions src/ichor/DependencyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,6 @@
#include <ichor/dependency_management/IServiceInterestedLifecycleManager.h>
#include <fmt/format.h>

#if defined(ICHOR_USE_SYSTEM_MIMALLOC) || defined(ICHOR_USE_MIMALLOC)
#include <mimalloc-new-delete.h>

#if (!defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)) || defined(__CYGWIN__)
extern "C" {
void *malloc(size_t size) {
return mi_malloc(size);
}

void free(void *ptr) {
mi_free(ptr);
}

void *realloc(void *ptr, size_t new_size) {
return mi_realloc(ptr, new_size);
}

void *calloc(size_t num, size_t size) {
return mi_calloc(num, size);
}

void *aligned_alloc(size_t alignment, size_t size) {
return mi_aligned_alloc(alignment, size);
}

size_t malloc_usable_size(void *ptr) {
return mi_malloc_usable_size(ptr);
}

void *memalign(size_t alignment, size_t size) {
return mi_memalign(alignment, size);
}

int posix_memalign(void **memptr, size_t alignment, size_t size) {
return mi_posix_memalign(memptr, alignment, size);
}

void *valloc(size_t size) {
return mi_valloc(size);
}

void *pvalloc(size_t size) {
return mi_pvalloc(size);
}
}
#endif
#endif

#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
#include <processthreadsapi.h>
#include <fmt/xchar.h>
Expand Down
1 change: 1 addition & 0 deletions test/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <catch2/matchers/catch_matchers.hpp>
#include <ichor/DependencyManager.h>
#include <ichor/services/logging/CoutFrameworkLogger.h>
#include <ichor/ichor-mimalloc.h>

using namespace Ichor;

Expand Down

0 comments on commit d7fc27e

Please sign in to comment.