mirror of
https://github.com/genodelabs/genode.git
synced 2024-12-22 15:02:25 +00:00
eba9c15746
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
169 lines
3.5 KiB
C++
169 lines
3.5 KiB
C++
/*
|
|
* \brief Slab allocator
|
|
* \author Norman Feske
|
|
* \date 2006-04-18
|
|
*/
|
|
|
|
/*
|
|
* Copyright (C) 2006-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__SLAB_H_
|
|
#define _INCLUDE__BASE__SLAB_H_
|
|
|
|
#include <base/allocator.h>
|
|
#include <base/stdint.h>
|
|
|
|
namespace Genode { class Slab; }
|
|
|
|
/**
|
|
* Transitional type definition, for API compatibility only
|
|
*
|
|
* \deprecated To be removed once all Slab users are updated.
|
|
*/
|
|
namespace Genode { typedef void Slab_block; }
|
|
|
|
|
|
/**
|
|
* Slab allocator
|
|
*/
|
|
class Genode::Slab : public Allocator
|
|
{
|
|
private:
|
|
|
|
struct Block;
|
|
struct Entry;
|
|
|
|
size_t const _slab_size; /* size of one slab entry */
|
|
size_t const _block_size; /* size of slab block */
|
|
size_t const _entries_per_block; /* number of slab entries per block */
|
|
|
|
Block *_initial_sb; /* initial (static) slab block */
|
|
bool _nested; /* indicator for nested call of alloc */
|
|
|
|
size_t _num_blocks = 0;
|
|
size_t _total_avail = 0;
|
|
|
|
/**
|
|
* Block used for attempting the next allocation
|
|
*/
|
|
Block *_curr_sb = nullptr;
|
|
|
|
Allocator *_backing_store;
|
|
|
|
/**
|
|
* Allocate and initialize new slab block
|
|
*/
|
|
Block *_new_slab_block();
|
|
|
|
|
|
/*****************************
|
|
** Methods used by 'Block' **
|
|
*****************************/
|
|
|
|
void _release_backing_store(Block *);
|
|
|
|
/**
|
|
* Insert block into slab block ring
|
|
*
|
|
* \noapi
|
|
*/
|
|
void _insert_sb(Block *);
|
|
|
|
/**
|
|
* Release slab block
|
|
*/
|
|
void _free_curr_sb();
|
|
|
|
/**
|
|
* Free slab entry
|
|
*/
|
|
void _free(void *addr);
|
|
|
|
/*
|
|
* Noncopyable
|
|
*/
|
|
Slab(Slab const &);
|
|
Slab &operator = (Slab const &);
|
|
|
|
public:
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* At construction time, there exists one initial slab
|
|
* block that is used for the first couple of allocations,
|
|
* especially for the allocation of the second slab
|
|
* block.
|
|
*/
|
|
Slab(size_t slab_size, size_t block_size, void *initial_sb,
|
|
Allocator *backing_store = 0);
|
|
|
|
/**
|
|
* Destructor
|
|
*/
|
|
~Slab();
|
|
|
|
/**
|
|
* Return number of bytes consumed per slab entry
|
|
*
|
|
* The function takes the slab-internal meta-data needs and the actual
|
|
* slab entry into account.
|
|
*/
|
|
static size_t entry_costs(size_t slab_size, size_t block_size);
|
|
|
|
/**
|
|
* Return number of unused slab entries
|
|
*/
|
|
size_t avail_entries() const { return _total_avail; }
|
|
|
|
/**
|
|
* Add new slab block as backing store
|
|
*
|
|
* The specified 'ptr' has to point to a buffer with the size of one
|
|
* slab block.
|
|
*/
|
|
void insert_sb(void *ptr);
|
|
|
|
/**
|
|
* Return a used slab element, or nullptr if empty
|
|
*/
|
|
void *any_used_elem();
|
|
|
|
/**
|
|
* Define/request backing-store allocator
|
|
*
|
|
* \noapi
|
|
*/
|
|
void backing_store(Allocator *bs) { _backing_store = bs; }
|
|
|
|
/**
|
|
* Request backing-store allocator
|
|
*
|
|
* \noapi
|
|
*/
|
|
Allocator *backing_store() { return _backing_store; }
|
|
|
|
|
|
/*************************
|
|
** Allocator interface **
|
|
*************************/
|
|
|
|
/**
|
|
* Allocate slab entry
|
|
*
|
|
* The 'size' parameter is ignored as only slab entries with
|
|
* preconfigured slab-entry size are allocated.
|
|
*/
|
|
bool alloc(size_t size, void **addr) override;
|
|
void free(void *addr, size_t) override { _free(addr); }
|
|
size_t consumed() const override;
|
|
size_t overhead(size_t) const override { return _block_size/_entries_per_block; }
|
|
bool need_size_for_free() const override { return false; }
|
|
};
|
|
|
|
#endif /* _INCLUDE__BASE__SLAB_H_ */
|