23 #include <l4/cxx/std_alloc>
24 #include <l4/cxx/hlist>
25 #include <l4/sys/consts.h>
38 template<
int Obj_size,
int Slab_size =
L4_PAGESIZE,
39 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
52 struct Slab_head :
public H_list_item
58 inline Slab_head()
throw() : num_free(0), free(0), cache(0)
77 Free_o *object(
unsigned obj)
throw()
78 {
return reinterpret_cast<Free_o*
>(_o +
object_size * obj); }
81 struct Slab_i :
public Slab_store,
public Slab_head
88 typedef void Obj_type;
94 H_list<Slab_i> _full_slabs;
95 H_list<Slab_i> _partial_slabs;
96 H_list<Slab_i> _empty_slabs;
99 void add_slab(Slab_i *s)
throw()
108 Free_o *f = s->free = s->object(0);
111 f->next = s->object(i);
117 _empty_slabs.push_front(s);
125 Slab_i *s = _alloc.alloc();
136 void shrink() throw()
138 if (!_alloc.can_free)
143 Slab_i *s = _empty_slabs.front();
144 _empty_slabs.remove(s);
153 : _alloc(alloc), _num_free(0), _num_slabs(0), _full_slabs(0),
154 _partial_slabs(0), _empty_slabs(0)
159 while (!_empty_slabs.empty())
161 Slab_i *o = _empty_slabs.front();
162 _empty_slabs.remove(o);
165 while (!_partial_slabs.empty())
167 Slab_i *o = _partial_slabs.front();
168 _partial_slabs.remove(o);
171 while (!_full_slabs.empty())
173 Slab_i *o = _full_slabs.front();
174 _full_slabs.remove(o);
179 void *alloc() throw()
181 H_list<Slab_i> *free = &_partial_slabs;
183 free = &_empty_slabs;
185 if (free->empty() && !grow())
188 Slab_i *s = free->front();
192 if (free == &_empty_slabs)
194 _empty_slabs.remove(s);
202 _partial_slabs.remove(s);
203 _full_slabs.push_front(s);
205 else if (free == &_empty_slabs)
206 _partial_slabs.push_front(s);
213 void free(
void *_o)
throw()
218 unsigned long addr = (
unsigned long)_o;
220 Slab_i *s = (Slab_i*)addr;
222 if (s->cache !=
this)
225 Free_o *o =
reinterpret_cast<Free_o*
>(_o);
230 bool was_full =
false;
234 _full_slabs.remove(s);
243 _partial_slabs.remove(s);
245 _empty_slabs.push_front(s);
253 _partial_slabs.push_front(s);
276 for (
typename H_list<Slab_i>::Const_iterator s = _partial_slabs.begin();
277 s != _partial_slabs.end(); ++s)
278 count += s->num_free;
295 template<
typename Type,
int Slab_size =
L4_PAGESIZE,
296 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
303 typedef Type Obj_type;
316 return (Type*)
Base_slab<
sizeof(Type), Slab_size,
317 Max_free, Alloc>::
alloc();
346 template<
int Obj_size,
int Slab_size =
L4_PAGESIZE,
347 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
354 typedef void Obj_type;
365 void *
alloc() throw() {
return _a.alloc(); }
371 void free(
void *p)
throw() { _a.free(p); }
393 template<
int _O,
int _S,
int _M,
template<
typename A>
class Alloc >
394 typename Base_slab_static<_O,_S,_M,Alloc>::_A
395 Base_slab_static<_O,_S,_M,Alloc>::_a;
413 template<
typename Type,
int Slab_size =
L4_PAGESIZE,
414 int Max_free = 2,
template<
typename A>
class Alloc = New_allocator >
420 typedef Type Obj_type;
428 Max_free, Alloc>::
alloc();