3 #include "mapped_alloc.h"
5 #include "lock_guard.h"
9 class Mem_region_map_base;
12 class Kmem_alloc : public Mapped_allocator
17 typedef Buddy_alloc Alloc;
19 typedef Spin_lock Lock;
22 static unsigned long _orig_free;
33 #include "mem_region.h"
34 #include "buddy_alloc.h"
37 static Kmem_alloc::Alloc _a;
38 Kmem_alloc::Alloc *Kmem_alloc::a = &_a;
39 Kmem_alloc::Lock Kmem_alloc::lock;
40 unsigned long Kmem_alloc::_orig_free;
42 PUBLIC static FIASCO_INIT
47 Mapped_allocator::allocator(&al);
52 Kmem_alloc::dump() const
57 Kmem_alloc::alloc(size_t o)
59 return unaligned_alloc(1UL << o);
65 Kmem_alloc::free(size_t o, void *p)
67 unaligned_free(1UL << o, p);
72 Kmem_alloc::unaligned_alloc(unsigned long size)
74 assert(size >=8 /*NEW INTERFACE PARANIOIA*/);
78 Lock_guard<Lock> guard(&lock);
84 Mapped_alloc_reaper::morecore (/* desperate= */ true);
86 Lock_guard<Lock> guard (&lock);
95 Kmem_alloc::unaligned_free(unsigned long size, void *page)
97 assert(size >=8 /*NEW INTERFACE PARANIOIA*/);
98 Lock_guard<Lock> guard (&lock);
103 PRIVATE static FIASCO_INIT
105 Kmem_alloc::create_free_map(Kip const *kip, Mem_region_map_base *map)
107 unsigned long available_size = 0;
108 Mem_desc const *md = kip->mem_descs();
109 Mem_desc const *const md_end = md + kip->num_mem_descs();
111 for (; md < md_end; ++md)
115 const_cast<Mem_desc*>(md)->type(Mem_desc::Undefined);
119 if (md->is_virtual())
122 unsigned long s = md->start();
123 unsigned long e = md->end();
125 // Sweep out stupid descriptors (that have the end before the start)
129 case Mem_desc::Conventional:
130 s = (s + Config::PAGE_SIZE - 1) & ~(Config::PAGE_SIZE - 1);
131 e = ((e + 1) & ~(Config::PAGE_SIZE - 1)) - 1;
134 available_size += e - s + 1;
135 if (!map->add(Mem_region(s, e)))
136 panic("Kmem_alloc::create_free_map(): memory map too small");
138 case Mem_desc::Reserved:
139 case Mem_desc::Dedicated:
140 case Mem_desc::Shared:
142 case Mem_desc::Bootloader:
143 s = s & ~(Config::PAGE_SIZE - 1);
144 e = ((e + Config::PAGE_SIZE) & ~(Config::PAGE_SIZE - 1)) - 1;
145 if (!map->sub(Mem_region(s, e)))
146 panic("Kmem_alloc::create_free_map(): memory map too small");
153 return available_size;