1 #include "KePlatform.h"
2 #include <KeLumpAllocator.h>
5 using namespace Knight;
8 * :TODO: don't make this part of the page, because touching it means
9 * dirtying a page. Instead, we should have a separate linked list.
10 * Maybe that linked list itself could be marshalled from one page.
25 KeLumpAllocator() : m_pUsableRegions(NULL), m_pUnusableRegions(NULL)
27 m_DefLumpSize = 65536;
29 #if defined KE_PLATFORM_WINDOWS
33 if (info.dwAllocationGranularity > m_DefLumpSize)
35 m_DefLumpSize = info.dwAllocationGranularity;
42 FreeRegionChain(m_pUsableRegions);
43 FreeRegionChain(m_pUnusableRegions);
50 /* Find the tail of the usable regions. */
51 region = m_pUsableRegions;
52 while (region != NULL)
54 if (region->next == NULL)
60 /* Link the unusable chain into the usable chain. */
63 m_pUsableRegions = m_pUnusableRegions;
67 region->next = m_pUnusableRegions;
68 m_pUnusableRegions->prev = region;
70 m_pUnusableRegions = NULL;
72 region = m_pUsableRegions;
73 while (region != NULL)
75 region->avail = region->size;
76 region->cur = region->base;
77 region = region->next;
81 void FreeRegionChain(KeLumpRegion *region)
85 while (region != NULL)
89 #if defined KE_PLATFORM_WINDOWS
90 VirtualFree(region, 0, MEM_RELEASE);
96 void *Alloc(size_t size)
107 region = FindRegionForSize(size);
110 region->avail -= size;
114 * Technically we could make one last small allocation, but
115 * this edge case is not worth the extra work.
117 if (region->avail < 8)
119 /* Unlink us from the usable list */
120 if (region == m_pUsableRegions)
122 m_pUsableRegions = m_pUsableRegions->next;
123 m_pUsableRegions->prev = NULL;
127 region->prev->next = region->next;
128 if (region->next != NULL)
130 region->next->prev = region->prev;
134 /* Link us into the unusable list */
136 region->next = m_pUnusableRegions;
138 if (m_pUnusableRegions != NULL)
140 m_pUnusableRegions->prev = region;
143 m_pUnusableRegions = region;
150 KeLumpRegion *FindRegionForSize(size_t size)
153 KeLumpRegion *region;
154 size_t size_of_region;
157 * :TODO: replace this with a priority queue or something
158 * that's actually fast. Even worse is we dirty pages by
161 region = m_pUsableRegions;
162 while (region != NULL)
164 if (region->avail >= size)
168 region = region->next;
171 /* Make sure regions end at 8-byte alignment. */
172 size_of_region = sizeof(KeLumpRegion);
173 if (size_of_region % 8 != 0)
176 size_of_region -= size_of_region % 8;
179 /* If the size is too big, fix that. */
180 if (size > m_DefLumpSize - size_of_region)
182 size += m_DefLumpSize;
183 size -= size % m_DefLumpSize;
187 size = m_DefLumpSize;
190 #if defined KE_PLATFORM_WINDOWS
191 base = (char *)VirtualAlloc(
194 MEM_COMMIT|MEM_RESERVE,
198 /* Initialize the region */
199 region = (KeLumpRegion *)base;
200 region->base = &base[size_of_region];
201 region->size = size - size_of_region;
202 region->cur = region->base;
203 region->avail = region->size;
205 region->next = m_pUsableRegions;
207 if (m_pUsableRegions != NULL)
209 m_pUsableRegions->prev = region;
212 m_pUsableRegions = region;
218 KeLumpRegion *m_pUsableRegions;
219 KeLumpRegion *m_pUnusableRegions;
220 size_t m_DefLumpSize;
223 inline KeLumpAllocator *ke_LumpFromAllocator(ke_allocator_t *arena)
225 return (KeLumpAllocator *)arena->user;
228 void *ke_LumpAlloc(ke_allocator_t *arena, size_t size)
230 return ke_LumpFromAllocator(arena)->Alloc(size);
233 void ke_LumpFree(ke_allocator_t *arena, void *ptr)
237 ke_allocator_t * KE_LINK Knight::KE_CreateLumpAllocator()
239 ke_allocator_t *alloc;
241 alloc = new ke_allocator_t;
242 alloc->alloc = ke_LumpAlloc;
243 alloc->dealloc = ke_LumpFree;
244 alloc->user = new KeLumpAllocator();
249 void KE_LINK Knight::KE_DestroyLumpAllocator(ke_allocator_t *alloc)
251 delete ke_LumpFromAllocator(alloc);
255 void KE_LINK Knight::KE_ResetLumpAllocator(ke_allocator_t *alloc)
257 ke_LumpFromAllocator(alloc)->Reset();