Skip to content

Commit 1c4da3a

Browse files
committed
util(memory): use mimalloc memory allocator
1 parent 7483943 commit 1c4da3a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

cmake/ExternalProjects.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ conan_cmake_configure(
3939
"cpprestsdk/2.10.18@#ed9788e9d202d6eadd92581368ddfc2f"
4040
"flatbuffers/2.0.5@#c6a9508bd476da080f7aecbe7a094b68"
4141
"hiredis/1.0.2@#370dad964286cadb1f15dc90252e8ef3"
42+
"mimalloc/2.1.2@#93a294cba11166006270536859c3c9ef"
4243
"openssl/3.0.2@#269fa93e5afe8c34bd9a0030d2b8f0fe"
4344
"protobuf/3.20.0@#8e4de7081bea093469c9e6076149b2b4"
4445
"readerwriterqueue/1.0.6@#a95c8da3d68822dec4d4c13fff4b5c96"
@@ -88,6 +89,7 @@ find_package(cpprestsdk REQUIRED)
8889
find_package(FlatBuffers REQUIRED)
8990
find_package(fmt REQUIRED)
9091
find_package(hiredis REQUIRED)
92+
find_package(mimalloc 2.1.2 REQUIRED)
9193
# 27/01/2023 - Pin OpenSSL to a specific version to avoid incompatibilities
9294
# with the system's (i.e. Ubuntu 22.04) OpenSSL
9395
find_package(OpenSSL 3.0.2 REQUIRED)
@@ -155,6 +157,7 @@ target_link_libraries(faabric_common_dependencies INTERFACE
155157
cpprestsdk::cpprestsdk
156158
flatbuffers::flatbuffers
157159
hiredis::hiredis
160+
mimalloc::mimalloc
158161
nng::nng
159162
protobuf::libprotobuf
160163
readerwriterqueue::readerwriterqueue

include/faabric/util/memory.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstdint>
44
#include <functional>
55
#include <memory>
6+
#include <mimalloc.h>
67
#include <span>
78
#include <string>
89
#include <unistd.h>
@@ -15,12 +16,17 @@ namespace faabric::util {
1516
// malloc implementations.
1617
inline void* malloc(std::size_t size)
1718
{
18-
return std::malloc(size);
19+
return mi_malloc(size);
1920
}
2021

2122
inline void free(void* ptr)
2223
{
23-
return std::free(ptr);
24+
return mi_free(ptr);
25+
}
26+
27+
inline void* realloc(void* ptr, std::size_t newSize)
28+
{
29+
return mi_realloc(ptr, newSize);
2430
}
2531

2632
/*

tests/test/util/test_memory.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ using namespace faabric::util;
1414

1515
namespace tests {
1616

17+
TEST_CASE("Test faabric-namespaced memory allocators")
18+
{
19+
// Smoke test to make sure the symbols are defined and work as expected
20+
size_t mallocSize = 0x100 * sizeof(int);
21+
void* ptr = faabric::util::malloc(mallocSize);
22+
REQUIRE(ptr != nullptr);
23+
24+
void* newPtr = faabric::util::realloc(ptr, mallocSize * 2);
25+
REQUIRE(newPtr != nullptr);
26+
27+
REQUIRE_NOTHROW(faabric::util::free(newPtr));
28+
}
29+
1730
TEST_CASE("Test rounding down offsets to page size", "[util][memory]")
1831
{
1932
REQUIRE(faabric::util::alignOffsetDown(2 * faabric::util::HOST_PAGE_SIZE) ==

0 commit comments

Comments
 (0)