Skip to content

Commit 752c173

Browse files
1 parent 6fbdb4d commit 752c173

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

o1heap/o1heap.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// Copyright (c) Pavel Kirienko
1515
// Authors: Pavel Kirienko <[email protected]>
1616

17-
// ReSharper disable CppDFANullDereference
17+
// ReSharper disable CppDFANullDereference CppRedundantCastExpression
1818

1919
#include "o1heap.h"
2020
#include <assert.h>
@@ -258,11 +258,12 @@ O1HEAP_PRIVATE void unbin(O1HeapInstance* const handle, const Fragment* const fr
258258

259259
// ---------------------------------------- PUBLIC API IMPLEMENTATION ----------------------------------------
260260

261+
const size_t o1heapMinArenaSize = INSTANCE_SIZE_PADDED + FRAGMENT_SIZE_MIN;
262+
261263
O1HeapInstance* o1heapInit(void* const base, const size_t size)
262264
{
263265
O1HeapInstance* out = NULL;
264-
if ((base != NULL) && ((((size_t) base) % O1HEAP_ALIGNMENT) == 0U) &&
265-
(size >= (INSTANCE_SIZE_PADDED + FRAGMENT_SIZE_MIN)))
266+
if ((base != NULL) && ((((size_t) base) % O1HEAP_ALIGNMENT) == 0U) && (size >= o1heapMinArenaSize))
266267
{
267268
// Allocate the core heap metadata structure in the beginning of the arena.
268269
O1HEAP_ASSERT(((size_t) base) % sizeof(O1HeapInstance*) == 0U);

o1heap/o1heap.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ typedef struct
6666
uint64_t oom_count;
6767
} O1HeapDiagnostics;
6868

69+
/// o1heapInit() will fail unless the arena size is at least this large.
70+
/// This value depends only on the machine architecture.
71+
/// The other reason to fail is if the arena pointer is not aligned at O1HEAP_ALIGNMENT.
72+
extern const size_t o1heapMinArenaSize;
73+
6974
/// The arena base pointer shall be aligned at O1HEAP_ALIGNMENT, otherwise NULL is returned.
7075
///
7176
/// The total heap capacity cannot exceed approx. (SIZE_MAX/2). If the arena size allows for a larger heap,
@@ -76,10 +81,13 @@ typedef struct
7681
/// own needs (normally about 40..600 bytes depending on the architecture, but this parameter is not characterized).
7782
/// A pointer to the newly initialized instance is returned.
7883
///
79-
/// If the provided space is insufficient, NULL is returned.
84+
/// The function fails and returns NULL iff:
85+
/// - The provided space is less than o1heapMinArenaSize.
86+
/// - The base pointer is not aligned at O1HEAP_ALIGNMENT.
87+
/// - The base pointer is NULL.
8088
///
81-
/// An initialized instance does not hold any resources. Therefore, if the instance is no longer needed,
82-
/// it can be discarded without any de-initialization procedures.
89+
/// An initialized instance does not hold any resources except for the arena memory.
90+
/// Therefore, if the instance is no longer needed, it can be discarded without any de-initialization procedures.
8391
///
8492
/// The heap is not thread-safe; external synchronization may be required.
8593
O1HeapInstance* o1heapInit(void* const base, const size_t size);

tests/test_general.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,13 @@ TEST_CASE("General: random A")
562562
}
563563
}
564564

565+
TEST_CASE("General: min arena size")
566+
{
567+
alignas(128U) std::array<std::byte, 1024> arena{};
568+
REQUIRE(nullptr == init(arena.data(), o1heapMinArenaSize - 1U));
569+
REQUIRE(nullptr != init(arena.data(), o1heapMinArenaSize));
570+
}
571+
565572
TEST_CASE("General: max allocation size")
566573
{
567574
alignas(128U) std::array<std::byte, 4096U + sizeof(internal::O1HeapInstance) + O1HEAP_ALIGNMENT - 1U> arena{};

0 commit comments

Comments
 (0)