23 #include <l4/cxx/std_alloc>
24 #include <l4/cxx/hlist>
25 #include <l4/sys/consts.h>
40 template<
int Obj_size,
int Slab_size =
L4_PAGESIZE,
41 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
63 inline Slab_head()
throw() : 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)
throw()
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)
throw()
130 Free_o *f = s->free = s->object(0);
133 f->next = s->object(i);
147 Slab_i *s = _alloc.alloc();
166 void shrink() throw()
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()
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 = (
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;
344 template<
typename Type,
int Slab_size =
L4_PAGESIZE,
345 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
352 typedef Type Obj_type;
378 {
Base_slab<
sizeof(Type), Slab_size, Max_free, Alloc>::
free(o); }
397 template<
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;
455 template<
int _O,
int _S,
int _M,
template<
typename A>
class Alloc >
456 typename Base_slab_static<_O,_S,_M,Alloc>::_A
457 Base_slab_static<_O,_S,_M,Alloc>::_a;
474 template<
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).
unsigned total_objects() const
Get the total number of objects managed by the slab allocator.
unsigned free_objects() const
Get the number of free objects in the slab allocator.
void * alloc()
Allocate an object.
void free(void *p)
Free the given object (p).
@ 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.
Alloc< Slab_i > Slab_alloc
Type of the backend allocator.
void * alloc()
Allocate a new object.
unsigned free_objects() const
Get the number of objects which can be allocated before a new empty slab needs to be added to the sla...
unsigned total_objects() const
Get the total number of objects managed by the slab allocator.
@ 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.
void free(void *_o)
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()
Allocate an object of type Type.
Slab allocator for object of type Type.
Type * alloc()
Allocate an object of type Type.
void free(Type *o)
Free the object addressed by o.
#define L4_PAGESIZE
Minimal page size (in bytes).