mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 08:24:32 -08:00
Add memory manager tests
This commit is contained in:
@@ -3,8 +3,7 @@
|
|||||||
namespace kutil {
|
namespace kutil {
|
||||||
|
|
||||||
using assert_callback =
|
using assert_callback =
|
||||||
[[noreturn]] void (*)
|
void (*) (const char *file, unsigned line, const char *message);
|
||||||
(const char *file, unsigned line, const char *message);
|
|
||||||
|
|
||||||
/// Set the global kernel assert callback
|
/// Set the global kernel assert callback
|
||||||
/// \args f The new callback
|
/// \args f The new callback
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
|
||||||
void * operator new (size_t, void *p) { return p; }
|
void * operator new (size_t, void *p) noexcept { return p; }
|
||||||
|
|
||||||
namespace kutil {
|
namespace kutil {
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
|
|
||||||
|
|
||||||
using addr_t = uint64_t;
|
using addr_t = uint64_t;
|
||||||
void * operator new (size_t, void *p);
|
|
||||||
|
void * operator new (size_t, void *p) noexcept;
|
||||||
|
|
||||||
|
|
||||||
namespace kutil {
|
namespace kutil {
|
||||||
|
|||||||
@@ -31,8 +31,13 @@ public:
|
|||||||
/// \arg p A pointer previously retuned by allocate()
|
/// \arg p A pointer previously retuned by allocate()
|
||||||
void free(void *p);
|
void free(void *p);
|
||||||
|
|
||||||
|
/// Minimum block size is (2^min_size). Must be at least 6.
|
||||||
|
static const unsigned min_size = 6;
|
||||||
|
|
||||||
private:
|
/// Maximum block size is (2^max_size). Must be less than 64.
|
||||||
|
static const unsigned max_size = 16;
|
||||||
|
|
||||||
|
protected:
|
||||||
class mem_header;
|
class mem_header;
|
||||||
|
|
||||||
/// Expand the size of memory
|
/// Expand the size of memory
|
||||||
@@ -43,17 +48,15 @@ private:
|
|||||||
void ensure_block(unsigned size);
|
void ensure_block(unsigned size);
|
||||||
|
|
||||||
/// Helper accessor for the list of blocks of a given size
|
/// Helper accessor for the list of blocks of a given size
|
||||||
|
/// \arg size Size category of the block we want
|
||||||
|
/// \returns A mutable reference to the head of the list
|
||||||
mem_header *& get_free(unsigned size) { return m_free[size - min_size]; }
|
mem_header *& get_free(unsigned size) { return m_free[size - min_size]; }
|
||||||
|
|
||||||
/// Helper to get a block of the given size, growing if necessary
|
/// Helper to get a block of the given size, growing if necessary
|
||||||
|
/// \arg size Size category of the block we want
|
||||||
|
/// \returns A detached block of the given size
|
||||||
mem_header * pop_free(unsigned size);
|
mem_header * pop_free(unsigned size);
|
||||||
|
|
||||||
/// Minimum block size is (2^min_size). Must be at least 6.
|
|
||||||
static const unsigned min_size = 6;
|
|
||||||
|
|
||||||
/// Maximum block size is (2^max_size). Must be less than 64.
|
|
||||||
static const unsigned max_size = 16;
|
|
||||||
|
|
||||||
mem_header *m_free[max_size - min_size];
|
mem_header *m_free[max_size - min_size];
|
||||||
void *m_start;
|
void *m_start;
|
||||||
size_t m_length;
|
size_t m_length;
|
||||||
|
|||||||
13050
src/tests/catch.hpp
Normal file
13050
src/tests/catch.hpp
Normal file
File diff suppressed because it is too large
Load Diff
2
src/tests/main.cpp
Normal file
2
src/tests/main.cpp
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include "catch.hpp"
|
||||||
114
src/tests/memory_manager.cpp
Normal file
114
src/tests/memory_manager.cpp
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <random>
|
||||||
|
#include <vector>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "kutil/memory.h"
|
||||||
|
#include "kutil/memory_manager.h"
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
using namespace kutil;
|
||||||
|
|
||||||
|
|
||||||
|
void *memory = nullptr;
|
||||||
|
size_t total_alloc_size = 0;
|
||||||
|
size_t total_alloc_calls = 0;
|
||||||
|
|
||||||
|
const size_t hs = 0x10; // header size
|
||||||
|
const size_t max_block = 1 << 16;
|
||||||
|
|
||||||
|
|
||||||
|
void grow_callback(void *start, size_t length)
|
||||||
|
{
|
||||||
|
total_alloc_size += length;
|
||||||
|
total_alloc_calls += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE( "Buddy blocks tests", "[memory buddy]" )
|
||||||
|
{
|
||||||
|
using clock = std::chrono::system_clock;
|
||||||
|
unsigned seed = clock::now().time_since_epoch().count();
|
||||||
|
std::default_random_engine rng(seed);
|
||||||
|
|
||||||
|
memory = aligned_alloc(max_block, 4 * max_block);
|
||||||
|
|
||||||
|
memory_manager mm(memory, grow_callback);
|
||||||
|
|
||||||
|
// The ctor should have allocated an initial block
|
||||||
|
CHECK( total_alloc_size == max_block );
|
||||||
|
CHECK( total_alloc_calls == 1 );
|
||||||
|
|
||||||
|
// Blocks should be:
|
||||||
|
// 16: 0-64K
|
||||||
|
|
||||||
|
std::vector<void *> allocs(6);
|
||||||
|
for (int i = 0; i < 6; ++i)
|
||||||
|
allocs[i] = mm.allocate(150); // size 8
|
||||||
|
|
||||||
|
// Should not have grown
|
||||||
|
CHECK( total_alloc_size == max_block );
|
||||||
|
CHECK( total_alloc_calls == 1 );
|
||||||
|
|
||||||
|
// Blocks should be:
|
||||||
|
// 16: [0-64K]
|
||||||
|
// 15: [0-32K], 32K-64K
|
||||||
|
// 14: [0-16K], 16K-32K
|
||||||
|
// 13: [0-8K], 8K-16K
|
||||||
|
// 12: [0-4K], 4K-8K
|
||||||
|
// 11: [0-2K], 2K-4K
|
||||||
|
// 10: [0-1K, 1-2K]
|
||||||
|
// 9: [0, 512, 1024], 1536
|
||||||
|
// 8: [0, 256, 512, 768, 1024, 1280]
|
||||||
|
|
||||||
|
// We have free memory at 1526 and 2K, but we should get 4K
|
||||||
|
void *big = mm.allocate(4000); // size 12
|
||||||
|
REQUIRE( big == offset_pointer(memory, 4096 + hs) );
|
||||||
|
mm.free(big);
|
||||||
|
|
||||||
|
// free up 512
|
||||||
|
mm.free(allocs[3]);
|
||||||
|
mm.free(allocs[4]);
|
||||||
|
|
||||||
|
// Blocks should be:
|
||||||
|
// ...
|
||||||
|
// 9: [0, 512, 1024], 1536
|
||||||
|
// 8: [0, 256, 512], 768, 1024, [1280]
|
||||||
|
|
||||||
|
// A request for a 512-block should not cross the buddy divide
|
||||||
|
big = mm.allocate(500); // size 9
|
||||||
|
REQUIRE( big >= offset_pointer(memory, 1536 + hs) );
|
||||||
|
mm.free(big);
|
||||||
|
|
||||||
|
mm.free(allocs[0]);
|
||||||
|
mm.free(allocs[1]);
|
||||||
|
mm.free(allocs[2]);
|
||||||
|
mm.free(allocs[5]);
|
||||||
|
allocs.clear();
|
||||||
|
|
||||||
|
std::vector<size_t> sizes = {
|
||||||
|
16000, 8000, 4000, 4000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 150,
|
||||||
|
150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 48, 48, 48, 13 };
|
||||||
|
|
||||||
|
std::shuffle(sizes.begin(), sizes.end(), rng);
|
||||||
|
|
||||||
|
allocs.reserve(sizes.size());
|
||||||
|
for (size_t size : sizes)
|
||||||
|
allocs.push_back(mm.allocate(size));
|
||||||
|
|
||||||
|
std::shuffle(allocs.begin(), allocs.end(), rng);
|
||||||
|
for (void *p: allocs)
|
||||||
|
mm.free(p);
|
||||||
|
allocs.clear();
|
||||||
|
|
||||||
|
big = mm.allocate(64000);
|
||||||
|
|
||||||
|
// If everything was freed / joined correctly, that should not have allocated
|
||||||
|
CHECK( total_alloc_size == max_block );
|
||||||
|
CHECK( total_alloc_calls == 1 );
|
||||||
|
|
||||||
|
// And we should have gotten back the start of memory
|
||||||
|
CHECK( big == offset_pointer(memory, hs) );
|
||||||
|
}
|
||||||
39
src/tests/wscript
Normal file
39
src/tests/wscript
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
|
||||||
|
def configure(ctx):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def build(bld):
|
||||||
|
sources = bld.path.ant_glob("**/*.cpp")
|
||||||
|
|
||||||
|
from waflib import Task
|
||||||
|
@Task.deep_inputs
|
||||||
|
class utest(Task.Task):
|
||||||
|
color = 'PINK'
|
||||||
|
quiet = False
|
||||||
|
def run(self):
|
||||||
|
import sys
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
args = [self.inputs[0].abspath()]
|
||||||
|
output = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
output = subprocess.check_output(args)
|
||||||
|
except subprocess.CalledProcessError, e:
|
||||||
|
sys.stdout.write(e.output)
|
||||||
|
return "Failed"
|
||||||
|
|
||||||
|
sys.stdout.write(output)
|
||||||
|
|
||||||
|
bld.program(
|
||||||
|
source = sources,
|
||||||
|
name = 'test',
|
||||||
|
target = 'test',
|
||||||
|
use = 'kutil',
|
||||||
|
)
|
||||||
|
|
||||||
|
run_tests = utest(env = bld.env)
|
||||||
|
run_tests.set_inputs(bld.path.get_bld().make_node('test'))
|
||||||
|
bld.add_to_group(run_tests)
|
||||||
|
|
||||||
|
# vim: ft=python et
|
||||||
65
wscript
65
wscript
@@ -2,6 +2,12 @@ top = '.'
|
|||||||
out = 'build'
|
out = 'build'
|
||||||
|
|
||||||
|
|
||||||
|
from waflib.Build import BuildContext
|
||||||
|
class TestContext(BuildContext):
|
||||||
|
cmd = 'test'
|
||||||
|
variant = 'tests'
|
||||||
|
|
||||||
|
|
||||||
def options(opt):
|
def options(opt):
|
||||||
opt.load("nasm gcc g++")
|
opt.load("nasm gcc g++")
|
||||||
|
|
||||||
@@ -43,6 +49,7 @@ def configure(ctx):
|
|||||||
version = subprocess.check_output("git describe --always", shell=True).strip()
|
version = subprocess.check_output("git describe --always", shell=True).strip()
|
||||||
git_sha = subprocess.check_output("git rev-parse --short HEAD", shell=True).strip()
|
git_sha = subprocess.check_output("git rev-parse --short HEAD", shell=True).strip()
|
||||||
|
|
||||||
|
env = ctx.env
|
||||||
major, minor, patch_dirty = version.split(".")
|
major, minor, patch_dirty = version.split(".")
|
||||||
dirty = 'dirty' in patch_dirty
|
dirty = 'dirty' in patch_dirty
|
||||||
patch = patch_dirty.split('-')[0]
|
patch = patch_dirty.split('-')[0]
|
||||||
@@ -51,7 +58,21 @@ def configure(ctx):
|
|||||||
ctx.env.KERNEL_FILENAME = ctx.options.kernel_filename
|
ctx.env.KERNEL_FILENAME = ctx.options.kernel_filename
|
||||||
ctx.env.FONT_NAME = ctx.options.font
|
ctx.env.FONT_NAME = ctx.options.font
|
||||||
|
|
||||||
ctx.env.EXTERNAL = join(ctx.path.abspath(), "external")
|
ctx.env.ARCH_D = join(str(ctx.path), "src", "arch",
|
||||||
|
ctx.env.POPCORN_ARCH)
|
||||||
|
|
||||||
|
ctx.env.append_value('INCLUDES', [
|
||||||
|
join(ctx.path.abspath(), "src", "include"),
|
||||||
|
join(ctx.path.abspath(), "src", "include", ctx.env.POPCORN_ARCH),
|
||||||
|
join(ctx.path.abspath(), "src", "modules"),
|
||||||
|
])
|
||||||
|
|
||||||
|
modules = []
|
||||||
|
mod_root = join("src", "modules")
|
||||||
|
for module in os.listdir(mod_root):
|
||||||
|
mod_path = join(mod_root, module)
|
||||||
|
if exists(join(mod_path, "wscript")):
|
||||||
|
modules.append(mod_path)
|
||||||
|
|
||||||
baseflags = [
|
baseflags = [
|
||||||
'-nostdlib',
|
'-nostdlib',
|
||||||
@@ -111,12 +132,6 @@ def configure(ctx):
|
|||||||
|
|
||||||
ctx.env.append_value('ASFLAGS', ['-felf64'])
|
ctx.env.append_value('ASFLAGS', ['-felf64'])
|
||||||
|
|
||||||
ctx.env.append_value('INCLUDES', [
|
|
||||||
join(ctx.path.abspath(), "src", "include"),
|
|
||||||
join(ctx.path.abspath(), "src", "include", ctx.env.POPCORN_ARCH),
|
|
||||||
join(ctx.path.abspath(), "src", "modules"),
|
|
||||||
])
|
|
||||||
|
|
||||||
ctx.env.append_value('LINKFLAGS', [
|
ctx.env.append_value('LINKFLAGS', [
|
||||||
'-g',
|
'-g',
|
||||||
'-nostdlib',
|
'-nostdlib',
|
||||||
@@ -125,9 +140,6 @@ def configure(ctx):
|
|||||||
'-nostartfiles',
|
'-nostartfiles',
|
||||||
])
|
])
|
||||||
|
|
||||||
ctx.env.ARCH_D = join(str(ctx.path), "src", "arch",
|
|
||||||
ctx.env.POPCORN_ARCH)
|
|
||||||
|
|
||||||
env = ctx.env
|
env = ctx.env
|
||||||
ctx.setenv('boot', env=env)
|
ctx.setenv('boot', env=env)
|
||||||
ctx.recurse(join("src", "boot"))
|
ctx.recurse(join("src", "boot"))
|
||||||
@@ -136,19 +148,36 @@ def configure(ctx):
|
|||||||
ctx.env.append_value('CFLAGS', ['-mcmodel=large'])
|
ctx.env.append_value('CFLAGS', ['-mcmodel=large'])
|
||||||
ctx.env.append_value('CXXFLAGS', ['-mcmodel=large'])
|
ctx.env.append_value('CXXFLAGS', ['-mcmodel=large'])
|
||||||
|
|
||||||
mod_root = join("src", "modules")
|
ctx.env.MODULES = modules
|
||||||
for module in os.listdir(mod_root):
|
for mod_path in ctx.env.MODULES:
|
||||||
mod_path = join(mod_root, module)
|
|
||||||
if exists(join(mod_path, "wscript")):
|
|
||||||
ctx.env.append_value('MODULES', mod_path)
|
|
||||||
ctx.recurse(mod_path)
|
ctx.recurse(mod_path)
|
||||||
|
|
||||||
ctx.recurse(join("src", "kernel"))
|
ctx.recurse(join("src", "kernel"))
|
||||||
|
|
||||||
|
## Testing configuration
|
||||||
|
##
|
||||||
|
from waflib.ConfigSet import ConfigSet
|
||||||
|
ctx.setenv('tests', env=ConfigSet())
|
||||||
|
ctx.load("g++")
|
||||||
|
|
||||||
|
ctx.env.append_value('INCLUDES', [
|
||||||
|
join(ctx.path.abspath(), "src", "include"),
|
||||||
|
join(ctx.path.abspath(), "src", "modules"),
|
||||||
|
])
|
||||||
|
|
||||||
|
ctx.env.CXXFLAGS = ['-g', '-std=c++14', '-fno-rtti']
|
||||||
|
ctx.env.LINKFLAGS = ['-g']
|
||||||
|
|
||||||
|
ctx.env.MODULES = modules
|
||||||
|
for mod_path in ctx.env.MODULES:
|
||||||
|
ctx.recurse(mod_path)
|
||||||
|
ctx.recurse(join("src", "tests"))
|
||||||
|
|
||||||
|
|
||||||
def build(bld):
|
def build(bld):
|
||||||
from os.path import join
|
from os.path import join
|
||||||
|
|
||||||
|
if not bld.variant:
|
||||||
bld.env = bld.all_envs['boot']
|
bld.env = bld.all_envs['boot']
|
||||||
bld.recurse(join("src", "boot"))
|
bld.recurse(join("src", "boot"))
|
||||||
|
|
||||||
@@ -187,6 +216,12 @@ def build(bld):
|
|||||||
]),
|
]),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif bld.variant == 'tests':
|
||||||
|
for mod_path in bld.env.MODULES:
|
||||||
|
bld.recurse(mod_path)
|
||||||
|
|
||||||
|
bld.recurse(join("src", "tests"))
|
||||||
|
|
||||||
|
|
||||||
def qemu(ctx):
|
def qemu(ctx):
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|||||||
Reference in New Issue
Block a user