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 <base/allocator_guard.h>
|
|
|
|
#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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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 };
|
2017-01-05 16:19:16 +01:00
|
|
|
static Genode::Allocator_guard alloc(&heap, ~0UL);
|
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
|
|
|
}
|