Skip to content

Commit

Permalink
Add cheat sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
mempler committed Dec 11, 2023
1 parent 912b324 commit 2a34417
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 101 deletions.
19 changes: 19 additions & 0 deletions cheat-sheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# A simple cheat-sheet to provide a quick reference for the most common commands and operations.

## Table of Contents

### Code coverage

To generate a code coverage report, run the following command:

```shell
bazel coverage -c dbg --nocache_test_results --combined_report=lcov //...
```

To view the report

```shell
genhtml --branch-coverage --output genhtml "$(bazel info output_path)/_coverage/_coverage_report.dat"
```

references [bazel.build/configure/coverage](https://bazel.build/configure/coverage)
20 changes: 18 additions & 2 deletions lib/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cc_library(
)

cc_test(
name = "tests",
name = "common_test",
# This is a core library, it must be small and fast.
size = "small",
timeout = "short",
Expand All @@ -36,7 +36,23 @@ cc_test(
"tests/TypeDefs.cpp",
],
linkstatic = True,
shard_count = 6,
deps = [
":runtime",
"@catch2//:catch2_main",
],
)

cc_test(
name = "allocator_test",
# This is a core library, it must be small and fast.
size = "small",
timeout = "short",
srcs = [
"source/Memory/SlabAllocatorUtilities.h",
"tests/Allocators/BumpAllocator.cpp",
"tests/Allocators/SlabAllocatorUtilities.cpp",
],
linkstatic = True,
deps = [
":runtime",
"@catch2//:catch2_main",
Expand Down
102 changes: 102 additions & 0 deletions lib/runtime/tests/Allocators/BumpAllocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <RuntimeLib.h>

#include <catch2/catch_test_macros.hpp>

TEST_CASE ("AllocatePages", "[BumpAllocator]") {
BUMP_ALLOCATOR allocator { };

RtlBumpAllocatorInitialize (&allocator);

// Setup code
allocator.bumpers[0].heap_start = (UINTN) new UINT8[PAGE_SIZE_4K];
allocator.bumpers[0].heap_end = allocator.bumpers[0].heap_start + PAGE_SIZE_4K;
allocator.bumpers[0].next = allocator.bumpers[0].heap_start;

allocator.bumpers[1].heap_start = (UINTN) new UINT8[PAGE_SIZE_4K * 2];
allocator.bumpers[1].heap_end = allocator.bumpers[1].heap_start + PAGE_SIZE_4K * 2;
allocator.bumpers[1].next = allocator.bumpers[1].heap_start;

SECTION ("Allocates 4K page") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (allocator.bumpers[0].next == allocator.bumpers[0].heap_end);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[0].heap_start);
}

SECTION ("Allocate multiple pages") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 2, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (allocator.bumpers[1].next == allocator.bumpers[1].heap_end);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[1].heap_start);
}

SECTION ("Making sure we cannot over-allocate") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, PAGE_SIZE_4K, PAGE_SIZE_4K);

REQUIRE (status == STATUS_OUT_OF_MEMORY);
REQUIRE (address == NULL);
}

SECTION ("Try unaligned pages") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 1, 1025);

REQUIRE (status == STATUS_INVALID_ALIGNMENT);
REQUIRE (address == NULL);
}

SECTION ("Make sure zero pages are not allocated") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 0, PAGE_SIZE_4K);

REQUIRE (status == STATUS_INVALID_PARAMETER);
REQUIRE (address == NULL);
}

SECTION ("Make sure NULL Address is not allocated") {
STATUS status = PA_ALLOCATE (&allocator, NULL, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_INVALID_PARAMETER);
}

SECTION ("Allocate multiple pages, multiple times") {
VOID* address = NULL;
STATUS status;

// First allocation, this is guaranteed to succeed
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[0].heap_start);
REQUIRE (allocator.bumpers[0].next == allocator.bumpers[0].heap_end);

// Second allocation, this is also guaranteed, but we must be on the second bumper
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[1].heap_start);

// Same as above, but we should reach the end of the second bumper
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)(allocator.bumpers[1].heap_start + PAGE_SIZE_4K));
REQUIRE (allocator.bumpers[1].next == allocator.bumpers[1].heap_end);

// Third allocation, this should fail as we have no more bumpers available.
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_OUT_OF_MEMORY);
REQUIRE (address == NULL);
}
}
3 changes: 3 additions & 0 deletions lib/runtime/tests/Allocators/SlabAllocatorUtilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <RuntimeLib.h>

#include <catch2/catch_test_macros.hpp>
99 changes: 0 additions & 99 deletions lib/runtime/tests/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,102 +121,3 @@ TEST_CASE ("RtlCopyMemory", "[Memory]") {
REQUIRE (status == STATUS_SUCCESS);
}
}

TEST_CASE ("AllocatePages", "[BumpAllocator]") {
BUMP_ALLOCATOR allocator { };

RtlBumpAllocatorInitialize (&allocator);

// Setup code
allocator.bumpers[0].heap_start = (UINTN) new UINT8[PAGE_SIZE_4K];
allocator.bumpers[0].heap_end = allocator.bumpers[0].heap_start + PAGE_SIZE_4K;
allocator.bumpers[0].next = allocator.bumpers[0].heap_start;

allocator.bumpers[1].heap_start = (UINTN) new UINT8[PAGE_SIZE_4K * 2];
allocator.bumpers[1].heap_end = allocator.bumpers[1].heap_start + PAGE_SIZE_4K * 2;
allocator.bumpers[1].next = allocator.bumpers[1].heap_start;

SECTION ("Allocates 4K page") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (allocator.bumpers[0].next == allocator.bumpers[0].heap_end);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[0].heap_start);
}

SECTION ("Allocate multiple pages") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 2, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (allocator.bumpers[1].next == allocator.bumpers[1].heap_end);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[1].heap_start);
}

SECTION ("Making sure we cannot over-allocate") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, PAGE_SIZE_4K, PAGE_SIZE_4K);

REQUIRE (status == STATUS_OUT_OF_MEMORY);
REQUIRE (address == NULL);
}

SECTION ("Try unaligned pages") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 1, 1025);

REQUIRE (status == STATUS_INVALID_ALIGNMENT);
REQUIRE (address == NULL);
}

SECTION ("Make sure zero pages are not allocated") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 0, PAGE_SIZE_4K);

REQUIRE (status == STATUS_INVALID_PARAMETER);
REQUIRE (address == NULL);
}

SECTION ("Make sure NULL Address is not allocated") {
STATUS status = PA_ALLOCATE (&allocator, NULL, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_INVALID_PARAMETER);
}

SECTION ("Allocate multiple pages, multiple times") {
VOID* address = NULL;
STATUS status;

// First allocation, this is guaranteed to succeed
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[0].heap_start);
REQUIRE (allocator.bumpers[0].next == allocator.bumpers[0].heap_end);

// Second allocation, this is also guaranteed, but we must be on the second bumper
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)allocator.bumpers[1].heap_start);

// Same as above, but we should reach the end of the second bumper
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_SUCCESS);
REQUIRE (address != NULL);
REQUIRE (address == (VOID*)(allocator.bumpers[1].heap_start + PAGE_SIZE_4K));
REQUIRE (allocator.bumpers[1].next == allocator.bumpers[1].heap_end);

// Third allocation, this should fail as we have no more bumpers available.
status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);

REQUIRE (status == STATUS_OUT_OF_MEMORY);
REQUIRE (address == NULL);
}
}

0 comments on commit 2a34417

Please sign in to comment.