23#include <l4/cxx/std_alloc>
24#include <l4/cxx/hlist>
25#include <l4/sys/consts.h>
41 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
63 inline Slab_head() noexcept : num_free(0), free(0), cache(0)
70 static_assert(Obj_size >=
sizeof(
void *),
71 "Object size must be at least the size of a pointer.");
72 static_assert(Obj_size <= Slab_size -
sizeof(Slab_head),
73 "Object_size exceeds slab capability.");
92 Free_o *object(
unsigned obj)
noexcept
93 {
return reinterpret_cast<Free_o*
>(_o +
object_size * obj); }
97 struct Slab_i :
public Slab_store,
public Slab_head
104 typedef void Obj_type;
121 void add_slab(
Slab_i *s)
noexcept
130 Free_o *f = s->free = s->object(0);
133 f->next = s->object(i);
147 Slab_i *s = _alloc.alloc();
166 void shrink() noexcept
168 if (!_alloc.can_free)
173 Slab_i *s = _empty_slabs.
front();
183 : _alloc(
alloc), _num_free(0), _num_slabs(0), _full_slabs(),
184 _partial_slabs(), _empty_slabs()
187 ~Base_slab() noexcept
189 while (!_empty_slabs.
empty())
191 Slab_i *o = _empty_slabs.
front();
195 while (!_partial_slabs.
empty())
197 Slab_i *o = _partial_slabs.
front();
201 while (!_full_slabs.
empty())
203 Slab_i *o = _full_slabs.
front();
222 free = &_empty_slabs;
224 if (
free->empty() && !grow())
231 if (
free == &_empty_slabs)
244 else if (
free == &_empty_slabs)
262 unsigned long addr =
reinterpret_cast<unsigned long>(_o);
268 if (s->cache !=
this)
271 Free_o *o =
reinterpret_cast<Free_o*
>(_o);
276 bool was_full =
false;
325 s != _partial_slabs.
end(); ++s)
326 count += s->num_free;
344template<
typename Type,
int Slab_size =
L4_PAGESIZE,
345 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
349 typedef Base_slab<
sizeof(Type), Slab_size, Max_free, Alloc> Base_type;
352 typedef Type Obj_type;
354 Slab(
typename Base_type::Slab_alloc
const &
alloc
355 =
typename Base_type::Slab_alloc()) noexcept
368 return reinterpret_cast<Type *
>(Base_type::alloc());
397template<
int Obj_size,
int Slab_size =
L4_PAGESIZE,
398 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
405 typedef void Obj_type;
455template<
int _O,
int _S,
int _M,
template<
typename A>
class Alloc >
456typename Base_slab_static<_O,_S,_M,Alloc>::_A
457 Base_slab_static<_O,_S,_M,Alloc>::_a;
474template<
typename Type,
int Slab_size =
L4_PAGESIZE,
475 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
481 typedef Type Obj_type;
491 return reinterpret_cast<Type *
>(
Merged slab allocator (allocators for objects of the same size are merged together).
@ max_free_slabs
Maximum number of free slabs.
@ slab_size
Size of a slab.
@ objects_per_slab
Number of objects per slab.
@ object_size
Size of an object.
unsigned total_objects() const noexcept
Get the total number of objects managed by the slab allocator.
void * alloc() noexcept
Allocate an object.
void free(void *p) noexcept
Free the given object (p).
unsigned free_objects() const noexcept
Get the number of free objects in the slab allocator.
unsigned free_objects() const noexcept
Get the number of objects which can be allocated before a new empty slab needs to be added to the sla...
Alloc< Slab_i > Slab_alloc
Type of the backend allocator.
void * alloc() noexcept
Allocate a new object.
@ max_free_slabs
Maximum number of free slabs.
@ slab_size
Size of a slab.
@ object_size
Size of an object.
@ objects_per_slab
Objects per slab.
unsigned total_objects() const noexcept
Get the total number of objects managed by the slab allocator.
void free(void *_o) noexcept
Free the given object (_o).
Const_iterator end() const
Return a const iterator to the end of the list.
bool empty() const
Check if the list is empty.
Iterator begin()
Return an iterator to the beginning of the list.
Value_type front() const
Return the first element in the list.
Basic element type for a double-linked H_list.
General double-linked list of unspecified cxx::H_list_item elements.
static void remove(T *e)
Remove the given element from its list.
void push_front(T *e)
Add element to the front of the list.
Helper type to distinguish the oeprator new version that does not throw exceptions.
Merged slab allocator (allocators for objects of the same size are merged together).
Type * alloc() noexcept
Allocate an object of type Type.
Slab allocator for object of type Type.
Type * alloc() noexcept
Allocate an object of type Type.
void free(Type *o) noexcept
Free the object addressed by o.
#define L4_PAGESIZE
Minimal page size (in bytes).