genode/repos/os/src/drivers/gpu/intel/ppgtt_allocator.h

97 lines
2.2 KiB
C
Raw Normal View History

/*
* \brief PPGTT translation table allocator
* \author Josef Soentgen
* \data 2017-03-15
*/
/*
* Copyright (C) 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 _PPGTT_ALLOCATOR_H_
#define _PPGTT_ALLOCATOR_H_
/* local includes */
#include <types.h>
#include <utils.h>
#include <allocator_guard.h>
namespace Igd {
class Ppgtt_allocator;
}
class Igd::Ppgtt_allocator : public Genode::Translation_table_allocator
{
private:
Genode::Region_map &_rm;
Genode::Allocator_guard &_guard;
Utils::Backend_alloc &_backend;
enum { ELEMENTS = 256, };
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
Utils::Address_map<ELEMENTS> _map { };
public:
Ppgtt_allocator(Genode::Region_map &rm,
Genode::Allocator_guard &guard,
Utils::Backend_alloc &backend)
: _rm(rm), _guard(guard), _backend(backend) { }
/*************************
** Allocator interface **
*************************/
bool alloc(size_t size, void **out_addr) override
{
Genode::Ram_dataspace_capability ds = _backend.alloc(_guard, size);
if (!ds.valid()) { return false; }
*out_addr = _rm.attach(ds);
return _map.add(ds, *out_addr);
}
void free(void *addr, size_t) override
{
if (addr == nullptr) { return; }
Genode::Ram_dataspace_capability cap = _map.remove(addr);
if (!cap.valid()) {
Genode::error("could not lookup capability for addr: ", addr);
return;
}
_rm.detach(addr);
_backend.free(_guard, cap);
}
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
bool need_size_for_free() const override { return false; }
size_t overhead(size_t) const override { return 0; }
/*******************************************
** Translation_table_allocator interface **
*******************************************/
void *phys_addr(void *va) override
{
if (va == nullptr) { return nullptr; }
typename Utils::Address_map<ELEMENTS>::Element *e = _map.phys_addr(va);
return e ? (void*)e->pa : nullptr;
}
void *virt_addr(void *pa) override
{
if (pa == nullptr) { return nullptr; }
typename Utils::Address_map<ELEMENTS>::Element *e = _map.virt_addr(pa);
return e ? (void*)e->va : nullptr;
}
};
#endif /* _PPGTT_ALLOCATOR_H_ */