mirror of
https://github.com/genodelabs/genode.git
synced 2025-05-05 02:03:07 +00:00
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
177 lines
4.6 KiB
C++
177 lines
4.6 KiB
C++
/*
|
|
* \brief Utility to allocate and locally attach a RAM dataspace
|
|
* \author Norman Feske
|
|
* \date 2008-03-22
|
|
*/
|
|
|
|
/*
|
|
* Copyright (C) 2008-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.
|
|
*/
|
|
|
|
#ifndef _INCLUDE__BASE__ATTACHED_RAM_DATASPACE_H_
|
|
#define _INCLUDE__BASE__ATTACHED_RAM_DATASPACE_H_
|
|
|
|
#include <util/touch.h>
|
|
#include <base/ram_allocator.h>
|
|
#include <base/env.h>
|
|
|
|
namespace Genode { class Attached_ram_dataspace; }
|
|
|
|
|
|
/*
|
|
* Utility for allocating and attaching a RAM dataspace
|
|
*
|
|
* The combination of RAM allocation and a local RM attachment is a frequent
|
|
* use case. Each function may fail, which makes error handling inevitable.
|
|
* This utility class encapsulates this functionality to handle both operations
|
|
* as a transaction. When embedded as a member, this class also takes care
|
|
* about freeing and detaching the dataspace at destruction time.
|
|
*/
|
|
class Genode::Attached_ram_dataspace
|
|
{
|
|
private:
|
|
|
|
size_t _size = 0;
|
|
Ram_allocator *_ram = nullptr;
|
|
Region_map *_rm = nullptr;
|
|
Ram_dataspace_capability _ds { };
|
|
void *_local_addr = nullptr;
|
|
Cache_attribute const _cached = CACHED;
|
|
|
|
template <typename T>
|
|
static void _swap(T &v1, T &v2) { T tmp = v1; v1 = v2; v2 = tmp; }
|
|
|
|
void _detach_and_free_dataspace()
|
|
{
|
|
if (_local_addr)
|
|
_rm->detach(_local_addr);
|
|
|
|
if (_ds.valid())
|
|
_ram->free(_ds);
|
|
}
|
|
|
|
void _alloc_and_attach()
|
|
{
|
|
if (!_size) return;
|
|
|
|
try {
|
|
_ds = _ram->alloc(_size, _cached);
|
|
_local_addr = _rm->attach(_ds);
|
|
}
|
|
/* revert allocation if attaching the dataspace failed */
|
|
catch (Region_map::Region_conflict) { _ram->free(_ds); throw; }
|
|
catch (Region_map::Invalid_dataspace) { _ram->free(_ds); throw; }
|
|
|
|
/*
|
|
* Eagerly map dataspace if used for DMA
|
|
*
|
|
* On some platforms, namely Fiasco.OC on ARMv7, the handling
|
|
* of page faults interferes with the caching attributes used
|
|
* for uncached DMA memory. See issue #452 for more details
|
|
* (https://github.com/genodelabs/genode/issues/452). As a
|
|
* work-around for this issues, we eagerly map the whole
|
|
* dataspace before writing actual content to it.
|
|
*/
|
|
if (_cached != CACHED) {
|
|
enum { PAGE_SIZE = 4096 };
|
|
unsigned char volatile *base = (unsigned char volatile *)_local_addr;
|
|
for (size_t i = 0; i < _size; i += PAGE_SIZE)
|
|
touch_read_write(base + i);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Noncopyable
|
|
*/
|
|
Attached_ram_dataspace(Attached_ram_dataspace const &);
|
|
Attached_ram_dataspace &operator = (Attached_ram_dataspace const &);
|
|
|
|
public:
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* \throw Out_of_ram
|
|
* \throw Out_of_caps
|
|
* \throw Region_map::Region_conflict
|
|
* \throw Region_map::Invalid_dataspace
|
|
*/
|
|
Attached_ram_dataspace(Ram_allocator &ram, Region_map &rm,
|
|
size_t size, Cache_attribute cached = CACHED)
|
|
:
|
|
_size(size), _ram(&ram), _rm(&rm), _cached(cached)
|
|
{
|
|
_alloc_and_attach();
|
|
}
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* \noapi
|
|
* \deprecated Use the constructor with the 'Ram_allocator &' and
|
|
* 'Region_map &' arguments instead.
|
|
*/
|
|
Attached_ram_dataspace(Ram_allocator *ram, size_t size,
|
|
Cache_attribute cached = CACHED) __attribute__((deprecated))
|
|
:
|
|
_size(size), _ram(ram), _rm(env_deprecated()->rm_session()), _cached(cached)
|
|
{
|
|
_alloc_and_attach();
|
|
}
|
|
|
|
/**
|
|
* Destructor
|
|
*/
|
|
~Attached_ram_dataspace() { _detach_and_free_dataspace(); }
|
|
|
|
/**
|
|
* Return capability of the used RAM dataspace
|
|
*/
|
|
Ram_dataspace_capability cap() const { return _ds; }
|
|
|
|
/**
|
|
* Request local address
|
|
*
|
|
* This is a template to avoid inconvenient casts at
|
|
* the caller. A newly allocated RAM dataspace is
|
|
* untyped memory anyway.
|
|
*/
|
|
template <typename T>
|
|
T *local_addr() const { return static_cast<T *>(_local_addr); }
|
|
|
|
/**
|
|
* Return size
|
|
*/
|
|
size_t size() const { return _size; }
|
|
|
|
void swap(Attached_ram_dataspace &other)
|
|
{
|
|
_swap(_size, other._size);
|
|
_swap(_ram, other._ram);
|
|
_swap(_ds, other._ds);
|
|
_swap(_local_addr, other._local_addr);
|
|
}
|
|
|
|
/**
|
|
* Re-allocate dataspace with a new size
|
|
*
|
|
* The content of the original dataspace is not retained.
|
|
*/
|
|
void realloc(Ram_allocator *ram_allocator, size_t new_size)
|
|
{
|
|
if (new_size < _size) return;
|
|
|
|
_detach_and_free_dataspace();
|
|
|
|
_size = new_size;
|
|
_ram = ram_allocator;
|
|
|
|
_alloc_and_attach();
|
|
}
|
|
};
|
|
|
|
#endif /* _INCLUDE__BASE__ATTACHED_RAM_DATASPACE_H_ */
|