add rust support (#113)

* add rust support

* move aligned_size to sizeclass.h

* add static qualifier

* adjust CMakeLists.txt, may broke CI tests

* fix msvc's complaining on c++17

* use SNMALLOC_FAST_PATH as the decorator of aligned_size

* adapt new alignment algorithm and add related test

Co-authored-by: mjp41 <mattpark@microsoft.com>

* fix test cases for msvc

* add extra test for size == 0

* treat memory block of same sizeclass as the same

* fix formatting problem

* remove extra declarations

Co-authored-by: Matthew Parkinson <mjp41@users.noreply.github.com>
This commit is contained in:
SchrodingerZhu
2020-01-23 15:08:18 +08:00
committed by Matthew Parkinson
parent 4212ac8e4e
commit 8304dedd17
6 changed files with 145 additions and 45 deletions

View File

@@ -104,34 +104,6 @@ extern "C"
}
#endif
inline size_t aligned_size(size_t alignment, size_t size)
{
// Client responsible for checking alignment is not zero
assert(alignment != 0);
// Client responsible for checking alignment is not above SUPERSLAB_SIZE
assert(alignment <= SUPERSLAB_SIZE);
// Client responsible for checking alignment is a power of two
assert(bits::next_pow2(alignment) == alignment);
size = bits::max(size, alignment);
snmalloc::sizeclass_t sc = size_to_sizeclass(size);
if (sc >= NUM_SIZECLASSES)
{
// large allocs are 16M aligned, which is maximum we guarantee
return size;
}
for (; sc < NUM_SIZECLASSES; sc++)
{
size = sizeclass_to_size(sc);
if ((size & (~size + 1)) >= alignment)
{
return size;
}
}
// Give max alignment.
return SUPERSLAB_SIZE;
}
SNMALLOC_EXPORT void*
SNMALLOC_NAME_MANGLE(memalign)(size_t alignment, size_t size)
{
@@ -142,13 +114,15 @@ extern "C"
errno = EINVAL;
return nullptr;
}
if ((size + alignment) < size)
{
errno = ENOMEM;
return nullptr;
}
return SNMALLOC_NAME_MANGLE(malloc)(aligned_size(alignment, size));
return SNMALLOC_NAME_MANGLE(malloc)(
size ? aligned_size(alignment, size) : alignment);
}
SNMALLOC_EXPORT void*