genode/repos/os/src/test/slab/main.cc

164 lines
3.7 KiB
C++
Raw Normal View History

/*
* \brief Slab allocator test
* \author Norman Feske
* \date 2015-03-31
*/
/*
* Copyright (C) 2015-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#include <base/component.h>
#include <base/heap.h>
#include <base/slab.h>
#include <base/log.h>
#include <timer_session/connection.h>
using Genode::size_t;
using Genode::log;
using Genode::error;
struct Array_of_slab_elements
{
Genode::Slab & slab;
Genode::Allocator & alloc;
size_t const num_elem;
size_t const slab_size;
void **elem;
/**
* Return size of 'elem' array in bytes
*/
size_t _elem_array_size() const { return num_elem*sizeof(void *); }
struct Alloc_failed { };
Array_of_slab_elements(Genode::Slab &slab, size_t num_elem, size_t slab_size,
Genode::Allocator & alloc)
: slab(slab), alloc(alloc), num_elem(num_elem), slab_size(slab_size),
elem((void**) alloc.alloc(_elem_array_size()))
{
log(" allocate ", num_elem, " elements");
for (size_t i = 0; i < num_elem; i++)
if (!slab.alloc(slab_size, &elem[i])) throw Alloc_failed();
}
~Array_of_slab_elements()
{
log(" free ", num_elem, " elements");
for (size_t i = 0; i < num_elem; i++)
slab.free(elem[i], slab_size);
alloc.free(elem, _elem_array_size());
}
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
private:
/*
* Noncopyable
*/
Array_of_slab_elements(Array_of_slab_elements const &);
Array_of_slab_elements &operator = (Array_of_slab_elements const &);
};
void Component::construct(Genode::Env & env)
{
static Genode::Heap heap(env.ram(), env.rm());
log("--- slab test ---");
static Timer::Connection timer(env);
enum { SLAB_SIZE = 16, BLOCK_SIZE = 256 };
struct Tracked_alloc : Genode::Allocator
{
size_t _consumed = 0;
Allocator &_alloc;
bool alloc(size_t size, void **out_addr) override
{
if (_alloc.alloc(size, out_addr)) {
_consumed += size;
return true;
}
return false;
}
void free(void *addr, size_t size) override
{
_alloc.free(addr, size);
_consumed -= size;
}
size_t overhead(size_t) const override { return 0; }
size_t consumed() const override { return _consumed; }
bool need_size_for_free() const override { return true; }
Tracked_alloc(Allocator &alloc) : _alloc(alloc) { }
};
Tracked_alloc alloc { heap };
{
Genode::Slab slab(SLAB_SIZE, BLOCK_SIZE, nullptr, &alloc);
for (unsigned i = 1; i <= 10; i++) {
log("round ", i, " ("
"used quota: ", alloc.consumed(), " "
"time: ", timer.elapsed_ms(), " ms)");
Array_of_slab_elements array(slab, i*100000, SLAB_SIZE, heap);
log(" allocation completed (used quota: ", alloc.consumed(), ")");
}
log(" finished (used quota: ", alloc.consumed(), ", "
"time: ", timer.elapsed_ms(), " ms)");
/*
* The slab keeps two empty blocks around. For the test, we also need to
* take the overhead of the two block allocations at the heap into
* account.
*/
enum { HEAP_OVERHEAD = 9*sizeof(long) };
if (alloc.consumed() > 2*(BLOCK_SIZE + HEAP_OVERHEAD)) {
error("slab failed to release empty slab blocks");
return;
}
}
/* validate slab destruction */
log("destructed slab (used quota: ", alloc.consumed(), ")");
if (alloc.consumed() > 0) {
error("slab failed to release all backing store");
return;
}
{
log("test double-free detection - error message is expected");
Genode::Slab slab(SLAB_SIZE, BLOCK_SIZE, nullptr, &alloc);
void *p = nullptr;
{
Array_of_slab_elements array(slab, 4096, SLAB_SIZE, heap);
p = array.elem[1705];
}
slab.free(p, SLAB_SIZE);
{
Array_of_slab_elements array(slab, 4096, SLAB_SIZE, heap);
}
}
log("Test done");
}