Skip to content

Commit

Permalink
util(memory): #387 use mimalloc as memory allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Feb 28, 2024
2 parents 578c079 + 1c4da3a commit 98947e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmake/ExternalProjects.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ conan_cmake_configure(
"cpprestsdk/2.10.18@#ed9788e9d202d6eadd92581368ddfc2f"
"flatbuffers/2.0.5@#c6a9508bd476da080f7aecbe7a094b68"
"hiredis/1.0.2@#370dad964286cadb1f15dc90252e8ef3"
"mimalloc/2.1.2@#93a294cba11166006270536859c3c9ef"
"openssl/3.0.2@#269fa93e5afe8c34bd9a0030d2b8f0fe"
"protobuf/3.20.0@#8e4de7081bea093469c9e6076149b2b4"
"readerwriterqueue/1.0.6@#a95c8da3d68822dec4d4c13fff4b5c96"
Expand Down Expand Up @@ -88,6 +89,7 @@ find_package(cpprestsdk REQUIRED)
find_package(FlatBuffers REQUIRED)
find_package(fmt REQUIRED)
find_package(hiredis REQUIRED)
find_package(mimalloc 2.1.2 REQUIRED)
# 27/01/2023 - Pin OpenSSL to a specific version to avoid incompatibilities
# with the system's (i.e. Ubuntu 22.04) OpenSSL
find_package(OpenSSL 3.0.2 REQUIRED)
Expand Down Expand Up @@ -155,6 +157,7 @@ target_link_libraries(faabric_common_dependencies INTERFACE
cpprestsdk::cpprestsdk
flatbuffers::flatbuffers
hiredis::hiredis
mimalloc::mimalloc
nng::nng
protobuf::libprotobuf
readerwriterqueue::readerwriterqueue
Expand Down
10 changes: 8 additions & 2 deletions include/faabric/util/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <functional>
#include <memory>
#include <mimalloc.h>
#include <span>
#include <string>
#include <unistd.h>
Expand All @@ -15,12 +16,17 @@ namespace faabric::util {
// malloc implementations.
inline void* malloc(std::size_t size)
{
return std::malloc(size);
return mi_malloc(size);
}

inline void free(void* ptr)
{
return std::free(ptr);
return mi_free(ptr);
}

inline void* realloc(void* ptr, std::size_t newSize)
{
return mi_realloc(ptr, newSize);
}

/*
Expand Down
13 changes: 13 additions & 0 deletions tests/test/util/test_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ using namespace faabric::util;

namespace tests {

TEST_CASE("Test faabric-namespaced memory allocators")
{
// Smoke test to make sure the symbols are defined and work as expected
size_t mallocSize = 0x100 * sizeof(int);
void* ptr = faabric::util::malloc(mallocSize);
REQUIRE(ptr != nullptr);

void* newPtr = faabric::util::realloc(ptr, mallocSize * 2);
REQUIRE(newPtr != nullptr);

REQUIRE_NOTHROW(faabric::util::free(newPtr));
}

TEST_CASE("Test rounding down offsets to page size", "[util][memory]")
{
REQUIRE(faabric::util::alignOffsetDown(2 * faabric::util::HOST_PAGE_SIZE) ==
Expand Down

0 comments on commit 98947e7

Please sign in to comment.