2016-03-31 17:16:18 +02:00
|
|
|
/*
|
|
|
|
* \brief Slab allocator test
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2015-03-31
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2015-2017 Genode Labs GmbH
|
2016-03-31 17:16:18 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2016-03-31 17:16:18 +02:00
|
|
|
*/
|
|
|
|
|
2017-01-05 16:19:16 +01:00
|
|
|
#include <base/component.h>
|
|
|
|
#include <base/heap.h>
|
2016-03-31 17:16:18 +02:00
|
|
|
#include <base/slab.h>
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
#include <base/log.h>
|
2016-03-31 17:16:18 +02:00
|
|
|
#include <timer_session/connection.h>
|
|
|
|
|
|
|
|
|
|
|
|
using Genode::size_t;
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
using Genode::log;
|
|
|
|
using Genode::error;
|
2016-03-31 17:16:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
struct Array_of_slab_elements
|
|
|
|
{
|
2017-01-05 16:19:16 +01:00
|
|
|
Genode::Slab & slab;
|
|
|
|
Genode::Allocator & alloc;
|
2016-03-31 17:16:18 +02:00
|
|
|
|
|
|
|
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 { };
|
|
|
|
|
2017-01-05 16:19:16 +01:00
|
|
|
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()))
|
2016-03-31 17:16:18 +02:00
|
|
|
{
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
log(" allocate ", num_elem, " elements");
|
2016-03-31 17:16:18 +02:00
|
|
|
for (size_t i = 0; i < num_elem; i++)
|
2017-01-05 16:19:16 +01:00
|
|
|
if (!slab.alloc(slab_size, &elem[i])) throw Alloc_failed();
|
2016-03-31 17:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~Array_of_slab_elements()
|
|
|
|
{
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
log(" free ", num_elem, " elements");
|
2016-03-31 17:16:18 +02:00
|
|
|
for (size_t i = 0; i < num_elem; i++)
|
|
|
|
slab.free(elem[i], slab_size);
|
|
|
|
|
2017-01-05 16:19:16 +01:00
|
|
|
alloc.free(elem, _elem_array_size());
|
2016-03-31 17:16:18 +02:00
|
|
|
}
|
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 &);
|
2016-03-31 17:16:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-05 16:19:16 +01:00
|
|
|
void Component::construct(Genode::Env & env)
|
2016-03-31 17:16:18 +02:00
|
|
|
{
|
2017-03-23 23:20:35 +01:00
|
|
|
static Genode::Heap heap(env.ram(), env.rm());
|
2017-01-05 16:19:16 +01:00
|
|
|
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
log("--- slab test ---");
|
2016-03-31 17:16:18 +02:00
|
|
|
|
2017-01-05 16:19:16 +01:00
|
|
|
static Timer::Connection timer(env);
|
2016-03-31 17:16:18 +02:00
|
|
|
|
|
|
|
enum { SLAB_SIZE = 16, BLOCK_SIZE = 256 };
|
2020-05-05 14:27:29 +02:00
|
|
|
|
|
|
|
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 };
|
2016-03-31 17:16:18 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Genode::Slab slab(SLAB_SIZE, BLOCK_SIZE, nullptr, &alloc);
|
|
|
|
|
|
|
|
for (unsigned i = 1; i <= 10; i++) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
log("round ", i, " ("
|
|
|
|
"used quota: ", alloc.consumed(), " "
|
|
|
|
"time: ", timer.elapsed_ms(), " ms)");
|
2016-03-31 17:16:18 +02:00
|
|
|
|
2017-01-05 16:19:16 +01:00
|
|
|
Array_of_slab_elements array(slab, i*100000, SLAB_SIZE, heap);
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
log(" allocation completed (used quota: ", alloc.consumed(), ")");
|
2016-03-31 17:16:18 +02:00
|
|
|
}
|
|
|
|
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
log(" finished (used quota: ", alloc.consumed(), ", "
|
|
|
|
"time: ", timer.elapsed_ms(), " ms)");
|
2016-03-31 17:16:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-23 23:20:35 +01:00
|
|
|
enum { HEAP_OVERHEAD = 9*sizeof(long) };
|
2016-03-31 17:16:18 +02:00
|
|
|
if (alloc.consumed() > 2*(BLOCK_SIZE + HEAP_OVERHEAD)) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
error("slab failed to release empty slab blocks");
|
2017-01-05 16:19:16 +01:00
|
|
|
return;
|
2016-03-31 17:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* validate slab destruction */
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
log("destructed slab (used quota: ", alloc.consumed(), ")");
|
2016-03-31 17:16:18 +02:00
|
|
|
if (alloc.consumed() > 0) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
error("slab failed to release all backing store");
|
2017-01-05 16:19:16 +01:00
|
|
|
return;
|
2016-03-31 17:16:18 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 23:20:35 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 16:19:16 +01:00
|
|
|
log("Test done");
|
2016-03-31 17:16:18 +02:00
|
|
|
}
|