2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Slab allocator
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-04-18
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 20:44:47 +00:00
|
|
|
* Copyright (C) 2006-2013 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__BASE__SLAB_H_
|
|
|
|
#define _INCLUDE__BASE__SLAB_H_
|
|
|
|
|
|
|
|
#include <base/allocator.h>
|
|
|
|
#include <base/stdint.h>
|
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
namespace Genode { class Slab; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
/**
|
|
|
|
* Transitional type definition, for API compatibility only
|
|
|
|
*
|
|
|
|
* \deprecated To be removed once all Slab users are updated.
|
|
|
|
*/
|
|
|
|
namespace Genode { typedef void Slab_block; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Slab allocator
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
class Genode::Slab : public Allocator
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
struct Block;
|
|
|
|
struct Entry;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
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 */
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
Block *_first_sb; /* first slab block */
|
|
|
|
Block *_initial_sb; /* initial (static) slab block */
|
|
|
|
bool _alloc_state; /* indicator for 'currently in service' */
|
2015-03-20 16:50:41 +00:00
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
Allocator *_backing_store;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Allocate and initialize new slab block
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
Block *_new_slab_block();
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
/*****************************
|
|
|
|
** Methods used by 'Block' **
|
|
|
|
*****************************/
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Remove block from slab block list
|
2015-03-04 20:12:14 +00:00
|
|
|
*
|
2016-03-31 16:17:07 +00:00
|
|
|
* \noapi
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
void _remove_sb(Block *sb);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Insert block into slab block list
|
|
|
|
*
|
|
|
|
* \noapi
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
void _insert_sb(Block *sb, Block *at = 0);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Free slab entry
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
void _free(void *addr);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Return true if number of free slab entries is higher than n
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
bool _num_free_entries_higher_than(int n);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
Slab(size_t slab_size, size_t block_size, void *initial_sb,
|
2015-03-04 20:12:14 +00:00
|
|
|
Allocator *backing_store = 0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
~Slab();
|
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Add new slab block as backing store
|
2015-03-20 16:50:41 +00:00
|
|
|
*
|
2016-03-31 16:17:07 +00:00
|
|
|
* The specified 'ptr' has to point to a buffer with the size of one
|
|
|
|
* slab block.
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
void insert_sb(void *ptr);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-31 16:17:07 +00:00
|
|
|
* Return a used slab element, or nullptr if empty
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-03-31 16:17:07 +00:00
|
|
|
void *any_used_elem();
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Define/request backing-store allocator
|
2015-03-20 16:50:41 +00:00
|
|
|
*
|
|
|
|
* \noapi
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
|
|
|
void backing_store(Allocator *bs) { _backing_store = bs; }
|
2015-03-20 16:50:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request backing-store allocator
|
|
|
|
*
|
|
|
|
* \noapi
|
|
|
|
*/
|
2015-03-04 20:12:14 +00:00
|
|
|
Allocator *backing_store() { return _backing_store; }
|
|
|
|
|
2016-03-31 16:17:07 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/*************************
|
|
|
|
** Allocator interface **
|
|
|
|
*************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate slab entry
|
|
|
|
*
|
|
|
|
* The 'size' parameter is ignored as only slab entries with
|
|
|
|
* preconfigured slab-entry size are allocated.
|
|
|
|
*/
|
2015-04-14 12:14:20 +00:00
|
|
|
bool alloc(size_t size, void **addr) override;
|
2016-03-31 16:17:07 +00:00
|
|
|
void free(void *addr, size_t) override { _free(addr); }
|
2015-04-14 12:14:20 +00:00
|
|
|
size_t consumed() const override;
|
2016-03-31 16:17:07 +00:00
|
|
|
size_t overhead(size_t) const override { return _block_size/_entries_per_block; }
|
2015-03-04 20:12:14 +00:00
|
|
|
bool need_size_for_free() const override { return false; }
|
|
|
|
};
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
#endif /* _INCLUDE__BASE__SLAB_H_ */
|