5 #include <cxx/type_traits>
10 struct Block : cxx::S_list_item
13 typedef cxx::S_list_bss<Block> Block_list;
15 static Block_list _free;
18 template< typename Base >
19 class Boot_object : public Base, public Boot_alloced
22 Boot_object() = default;
24 template< typename... A >
25 Boot_object(A&&... args) : Base(cxx::forward<A>(args)...) {}
33 #include "kmem_alloc.h"
36 Boot_alloced::Block_list Boot_alloced::_free;
40 Boot_alloced::alloc(size_t size)
42 printf("Boot_alloc: size=0x%lx\n", (unsigned long)size);
44 // this is best fit list-based allocation
46 Block_list::Iterator best = _free.end();
47 for (Block_list::Iterator curr = _free.begin(); curr != _free.end(); ++curr)
49 if (((best == _free.end()) || curr->size < best->size)
50 && curr->size >= size)
54 if (best == _free.end())
57 unsigned long alloc_size = 1024;
59 // look for a size suitable and buddy friendly
60 while (alloc_size < size)
63 Block *b = (Block*)Kmem_alloc::allocator()->unaligned_alloc(alloc_size);
64 printf("Boot_alloc: allocated extra memory block @%p (size=%lx)\n",
77 Block *rem = (Block *)(((Address)b + size + sizeof(Block) - 1) & ~(sizeof(Block) - 1));
78 long rem_sz = (Address)b + (*best)->size - (Address)rem;
79 printf("Boot_alloc: @ %p\n", b);
80 if (rem_sz > (long)sizeof(Block))
83 _free.replace(best, rem);
84 printf("Boot_alloc: remaining free block @ %p (size=%lx)\n", rem, rem_sz);
92 Boot_alloced::operator new (size_t size) throw()
93 { return alloc(size); }
96 Boot_alloced::operator new [] (size_t size) throw()
97 { return alloc(size); }
100 Boot_alloced::operator delete (void *b)
102 WARN("Boot_alloc: trying to delete boot-time allocated object @ %p\n", b);
106 Boot_alloced::operator delete [] (void *b)
108 WARN("Boot_alloc: trying to delete boot-time allocated object @ %p\n", b);