Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,18 @@ subdirlist(TEST_CATEGORIES ${TESTDIR})
foreach(SUPER_SLAB_SIZE 1;16)
foreach(TEST_CATEGORY ${TEST_CATEGORIES})
subdirlist(TESTS ${TESTDIR}/${TEST_CATEGORY})
if(MSVC)
Comment thread
Theodus marked this conversation as resolved.
list(REMOVE_ITEM TESTS "malloc")
endif()
foreach(TEST ${TESTS})
unset(SRC)
aux_source_directory(${TESTDIR}/${TEST_CATEGORY}/${TEST} SRC)
set(TESTNAME "${TEST_CATEGORY}-${TEST}-${SUPER_SLAB_SIZE}")
add_executable(${TESTNAME} ${SRC} src/override/new.cc)
if(${TEST} STREQUAL "malloc")
add_executable(${TESTNAME} ${SRC} src/override/malloc.cc)
else()
add_executable(${TESTNAME} ${SRC} src/override/new.cc)
endif()
if (${SUPER_SLAB_SIZE} EQUAL 1)
target_compile_definitions(${TESTNAME} PRIVATE IS_ADDRESS_SPACE_CONSTRAINED)
endif()
Expand Down
8 changes: 4 additions & 4 deletions src/override/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern "C"
if (overflow)
{
errno = ENOMEM;
return 0;
return nullptr;
}
// Include size 0 in the first sizeclass.
sz = ((sz - 1) >> (bits::BITS - 1)) + sz;
Expand Down Expand Up @@ -135,7 +135,8 @@ extern "C"
return nullptr;
}

uint8_t sc = size_to_sizeclass((std::max)(size, alignment));
size = (std::max)(size, alignment);
uint8_t sc = size_to_sizeclass(size);
if (sc >= NUM_SIZECLASSES)
{
// large allocs are 16M aligned.
Expand All @@ -149,8 +150,7 @@ extern "C"
return SNMALLOC_NAME_MANGLE(aligned_alloc)(alignment, size);
}
}
assert(false);
return nullptr;
return SNMALLOC_NAME_MANGLE(malloc)(SUPERSLAB_SIZE);
}

SNMALLOC_EXPORT int SNMALLOC_NAME_MANGLE(posix_memalign)(
Expand Down
2 changes: 1 addition & 1 deletion src/pal/pal_free_bsd_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace snmalloc
kmem_unback(kernel_object, addr, size);
}

/// Notify platform that we will not be using these pages
/// Notify platform that we will be using these pages
template<ZeroMem zero_mem>
void notify_using(void* p, size_t size)
{
Expand Down
2 changes: 1 addition & 1 deletion src/pal/pal_freebsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace snmalloc
madvise(p, size, MADV_FREE);
}

/// Notify platform that we will not be using these pages
/// Notify platform that we will be using these pages
template<ZeroMem zero_mem>
void notify_using(void* p, size_t size) noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion src/pal/pal_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace snmalloc
UNUSED(size);
}

/// Notify platform that we will not be using these pages
/// Notify platform that we will be using these pages
template<ZeroMem zero_mem>
void notify_using(void* p, size_t size) noexcept
{
Expand Down
4 changes: 2 additions & 2 deletions src/pal/pal_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace snmalloc
error("VirtualFree failed");
}

/// Notify platform that we will not be using these pages
/// Notify platform that we will be using these pages
template<ZeroMem zero_mem>
void notify_using(void* p, size_t size) noexcept
{
Expand Down Expand Up @@ -108,4 +108,4 @@ namespace snmalloc
}
};
}
#endif
#endif
118 changes: 118 additions & 0 deletions src/test/func/malloc/malloc.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <cerrno>
#include <malloc.h>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has been deprecated for about 20 years and causes a hard error on FreeBSD, it probably should be stdlib.h.

#include <snmalloc.h>

using namespace snmalloc;

void check_result(size_t size, size_t align, void* p, int err, bool null)
{
if (errno != err)
abort();

if (null)
{
if (p != nullptr)
abort();
}
else
{
if (malloc_usable_size(p) < size)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a non-standard function, so we can't expect it to be in any headers. It isn't on FreeBSD or macOS.

abort();

if (((uintptr_t)p % align) != 0)
abort();

free(p);
}
}

void test_calloc(size_t nmemb, size_t size, int err, bool null)
{
fprintf(stderr, "calloc(%d, %d)\n", (int)nmemb, (int)size);
errno = 0;
void* p = calloc(nmemb, size);

if ((p != nullptr) && (errno == 0))
{
for (size_t i = 0; i < (size * nmemb); i++)
{
if (((uint8_t*)p)[i] != 0)
abort();
}
}
check_result(nmemb * size, 1, p, err, null);
}

void test_realloc(void* p, size_t size, int err, bool null)
{
fprintf(stderr, "realloc(%p(%d), %d)\n", p, int(size), (int)size);
errno = 0;
p = realloc(p, size);
check_result(size, 1, p, err, null);
}

void test_posix_memalign(size_t size, size_t align, int err, bool null)
{
fprintf(stderr, "posix_memalign(&p, %d, %d)\n", (int)align, (int)size);
void* p = nullptr;
errno = posix_memalign(&p, align, size);
check_result(size, align, p, err, null);
}

void test_memalign(size_t size, size_t align, int err, bool null)
{
fprintf(stderr, "memalign(%d, %d)\n", (int)align, (int)size);
errno = 0;
void* p = memalign(align, size);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memalign is deprecated in glibc and doesn't exist anywhere else, so we probably need to explicitly declare it if we want to test our version.

check_result(size, align, p, err, null);
}

int main(int argc, char** argv)
{
UNUSED(argc);
UNUSED(argv);

constexpr int SUCCESS = 0;

test_calloc(0, 0, SUCCESS, false);

for (uint8_t sc = 0; sc < NUM_SIZECLASSES; sc++)
{
const size_t size = sizeclass_to_size(sc);

bool overflow = false;
for (size_t n = 1; bits::umul(size, n, overflow) <= SUPERSLAB_SIZE; n *= 5)
{
if (overflow)
break;

test_calloc(n, size, SUCCESS, false);
test_calloc(n, 0, SUCCESS, false);
}
test_calloc(0, size, SUCCESS, false);

test_realloc(malloc(size), size, SUCCESS, false);
test_realloc(malloc(size), 0, SUCCESS, true);
test_realloc(nullptr, size, SUCCESS, false);
test_realloc(malloc(size), (size_t)-1, ENOMEM, true);
}

test_posix_memalign(0, 0, EINVAL, true);
test_posix_memalign((size_t)-1, 0, EINVAL, true);

for (size_t align = sizeof(size_t); align <= SUPERSLAB_SIZE; align <<= 1)
{
for (uint8_t sc = 0; sc < NUM_SIZECLASSES; sc++)
{
const size_t size = sizeclass_to_size(sc);
test_posix_memalign(size, align, SUCCESS, false);
test_posix_memalign(size, 0, EINVAL, true);
test_memalign(size, align, SUCCESS, false);
}
test_posix_memalign(0, align, SUCCESS, false);
test_posix_memalign((size_t)-1, align, ENOMEM, true);
test_posix_memalign(0, align + 1, EINVAL, true);
}

return 0;
}
3 changes: 0 additions & 3 deletions src/test/func/rounding/rounding.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <snmalloc.h>
#include <test/opt.h>
#include <test/xoroshiro.h>
#include <unordered_set>

using namespace snmalloc;

Expand Down