2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Heap that stores each block at a separate dataspace
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-08-16
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2006-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01: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.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
2016-04-28 16:39:45 +02:00
|
|
|
#include <util/construct_at.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <base/heap.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>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
|
2017-05-07 23:49:43 +02:00
|
|
|
Sliced_heap::Sliced_heap(Ram_allocator &ram_alloc, Region_map ®ion_map)
|
2016-04-28 16:39:45 +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
|
|
|
_ram_alloc(ram_alloc), _region_map(region_map)
|
2016-04-28 16:39:45 +02:00
|
|
|
{ }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
Sliced_heap::~Sliced_heap()
|
|
|
|
{
|
2016-04-28 16:39:45 +02:00
|
|
|
for (Block *b; (b = _blocks.first()); ) {
|
|
|
|
/*
|
|
|
|
* Compute pointer to payload, which follows the meta-data header.
|
|
|
|
* Note the pointer arithmetics. By adding 1 to 'b', we end up with
|
|
|
|
* 'payload' pointing to the data portion of the block.
|
|
|
|
*/
|
|
|
|
void * const payload = b + 1;
|
|
|
|
free(payload, b->size);
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool Sliced_heap::alloc(size_t size, void **out_addr)
|
|
|
|
{
|
|
|
|
/* allocation includes space for block meta data and is page-aligned */
|
|
|
|
size = align_addr(size + sizeof(Block), 12);
|
|
|
|
|
|
|
|
Ram_dataspace_capability ds_cap;
|
2016-04-28 16:39:45 +02:00
|
|
|
Block *block = nullptr;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
try {
|
2017-05-07 23:49:43 +02:00
|
|
|
ds_cap = _ram_alloc.alloc(size);
|
2016-04-28 16:39:45 +02:00
|
|
|
block = _region_map.attach(ds_cap);
|
2017-05-08 19:55:54 +02:00
|
|
|
}
|
Streamline exception types
This patch reduces the number of exception types by facilitating
globally defined exceptions for common usage patterns shared by most
services. In particular, RPC functions that demand a session-resource
upgrade not longer reflect this condition via a session-specific
exception but via the 'Out_of_ram' or 'Out_of_caps' types.
Furthermore, the 'Parent::Service_denied', 'Parent::Unavailable',
'Root::Invalid_args', 'Root::Unavailable', 'Service::Invalid_args',
'Service::Unavailable', and 'Local_service::Factory::Denied' types have
been replaced by the single 'Service_denied' exception type defined in
'session/session.h'.
This consolidation eases the error handling (there are fewer exceptions
to handle), alleviates the need to convert exceptions along the
session-creation call chain, and avoids possible aliasing problems
(catching the wrong type with the same name but living in a different
scope).
2017-05-07 22:03:22 +02:00
|
|
|
catch (Region_map::Region_conflict) {
|
|
|
|
error("sliced_heap: region conflict while attaching dataspace");
|
|
|
|
_ram_alloc.free(ds_cap);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (Region_map::Invalid_dataspace) {
|
|
|
|
error("sliced_heap: attempt to attach invalid dataspace");
|
2017-05-07 23:49:43 +02:00
|
|
|
_ram_alloc.free(ds_cap);
|
2011-12-22 16:19:25 +01:00
|
|
|
return false;
|
2017-05-08 19:55:54 +02:00
|
|
|
}
|
|
|
|
catch (Out_of_ram) {
|
2011-12-22 16:19:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-10 11:20:15 +01:00
|
|
|
/* serialize access to block list */
|
2020-01-24 13:59:27 +01:00
|
|
|
Mutex::Guard guard(_mutex);
|
2012-12-10 11:20:15 +01:00
|
|
|
|
2016-04-28 16:39:45 +02:00
|
|
|
construct_at<Block>(block, ds_cap, size);
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
_consumed += size;
|
2016-04-28 16:39:45 +02:00
|
|
|
_blocks.insert(block);
|
|
|
|
|
|
|
|
/* skip meta data prepended to the payload portion of the block */
|
|
|
|
*out_addr = block + 1;
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
void Sliced_heap::free(void *addr, size_t)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2012-12-10 11:20:15 +01:00
|
|
|
Ram_dataspace_capability ds_cap;
|
2016-04-28 16:39:45 +02:00
|
|
|
void *local_addr = nullptr;
|
2012-12-10 11:20:15 +01:00
|
|
|
{
|
|
|
|
/* serialize access to block list */
|
2020-01-24 13:59:27 +01:00
|
|
|
Mutex::Guard guard(_mutex);
|
2012-12-10 11:20:15 +01:00
|
|
|
|
2016-04-28 16:39:45 +02:00
|
|
|
/*
|
|
|
|
* The 'addr' argument points to the payload. We use pointer
|
|
|
|
* arithmetics to determine the pointer to the block's meta data that
|
|
|
|
* is prepended to the payload.
|
|
|
|
*/
|
|
|
|
Block * const block = reinterpret_cast<Block *>(addr) - 1;
|
|
|
|
|
|
|
|
_blocks.remove(block);
|
|
|
|
_consumed -= block->size;
|
|
|
|
ds_cap = block->ds;
|
|
|
|
local_addr = block;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call destructor to properly destruct the dataspace capability
|
|
|
|
* member of the 'Block'.
|
|
|
|
*/
|
|
|
|
block->~Block();
|
2012-12-10 11:20:15 +01:00
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2016-04-28 16:39:45 +02:00
|
|
|
_region_map.detach(local_addr);
|
2017-05-07 23:49:43 +02:00
|
|
|
_ram_alloc.free(ds_cap);
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-14 14:14:20 +02:00
|
|
|
size_t Sliced_heap::overhead(size_t size) const
|
|
|
|
{
|
|
|
|
return align_addr(size + sizeof(Block), 12) - size;
|
|
|
|
}
|