forked from pavel-kirienko/o1heap
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6d63ae
commit 1510e4a
Showing
1 changed file
with
11 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,9 @@ | |
// Authors: Pavel Kirienko <[email protected]> | ||
|
||
#include "internal.hpp" | ||
#include <cstdlib> | ||
#include <algorithm> | ||
#include <array> | ||
#include <cstdlib> | ||
#include <iostream> | ||
#include <random> | ||
|
||
|
@@ -27,24 +27,16 @@ constexpr std::size_t KiB = 1024U; | |
constexpr std::size_t MiB = KiB * KiB; | ||
|
||
#ifdef _WIN32 | ||
void* my_aligned_alloc(size_t align, size_t size) | ||
{ | ||
return _aligned_malloc(size, align); | ||
} | ||
|
||
void my_aligned_free(void* mem) | ||
auto make_arena(size_t align, size_t size) -> std::shared_ptr<std::byte> | ||
{ | ||
_aligned_free(mem); | ||
return std::shared_ptr<std::byte>(static_cast<std::byte*>(_aligned_malloc(size, align)), | ||
&_aligned_free); | ||
} | ||
#else | ||
void* my_aligned_alloc(size_t align, size_t size) | ||
{ | ||
return std::aligned_alloc(align, size); | ||
} | ||
|
||
void my_aligned_free(void* mem) | ||
auto make_arena(size_t align, size_t size) -> std::shared_ptr<std::byte> | ||
{ | ||
std::aligned_free(mem); | ||
return std::shared_ptr<std::byte>(static_cast<std::byte*>(std::aligned_alloc(align, size)), | ||
&_aligned_free); | ||
} | ||
#endif | ||
|
||
|
@@ -159,7 +151,7 @@ TEST_CASE("General: allocate: OOM") | |
{ | ||
constexpr auto MiB256 = MiB * 256U; | ||
constexpr auto ArenaSize = MiB256 + MiB; | ||
const std::shared_ptr<std::byte> arena(static_cast<std::byte*>(my_aligned_alloc(64U, ArenaSize)), &my_aligned_free); | ||
const std::shared_ptr<std::byte> arena = make_arena(64U, ArenaSize); | ||
|
||
auto heap = init(arena.get(), ArenaSize); | ||
REQUIRE(heap != nullptr); | ||
|
@@ -200,7 +192,7 @@ TEST_CASE("General: allocate: smallest") | |
using internal::Fragment; | ||
|
||
constexpr auto ArenaSize = MiB * 300U; | ||
const std::shared_ptr<std::byte> arena(static_cast<std::byte*>(my_aligned_alloc(64U, ArenaSize)), &my_aligned_free); | ||
const std::shared_ptr<std::byte> arena = make_arena(64U, ArenaSize); | ||
|
||
auto heap = init(arena.get(), ArenaSize); | ||
REQUIRE(heap != nullptr); | ||
|
@@ -232,7 +224,7 @@ TEST_CASE("General: allocate: size_t overflow") | |
constexpr auto size_max = std::numeric_limits<uint32_t>::max(); | ||
|
||
constexpr auto ArenaSize = MiB * 300U; | ||
const std::shared_ptr<std::byte> arena(static_cast<std::byte*>(my_aligned_alloc(64U, ArenaSize)), &my_aligned_free); | ||
const std::shared_ptr<std::byte> arena = make_arena(64U, ArenaSize); | ||
|
||
auto heap = init(arena.get(), ArenaSize); | ||
REQUIRE(heap != nullptr); | ||
|
@@ -504,7 +496,7 @@ TEST_CASE("General: random A") | |
using internal::Fragment; | ||
|
||
constexpr auto ArenaSize = MiB * 300U; | ||
const std::shared_ptr<std::byte> arena(static_cast<std::byte*>(my_aligned_alloc(64U, ArenaSize)), &my_aligned_free); | ||
const std::shared_ptr<std::byte> arena = make_arena(64U, ArenaSize); | ||
std::generate_n(arena.get(), ArenaSize, getRandomByte); // Random-fill the ENTIRE arena! | ||
auto heap = init(arena.get(), ArenaSize); | ||
REQUIRE(heap != nullptr); | ||
|