+/****************************************************************
+ * low-level memory reservations
+ ****************************************************************/
+
+// Find and reserve space from a given zone
+static void *
+allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
+{
+ struct allocinfo_s *info;
+ for (info = zone->info; info; info = info->next) {
+ void *dataend = info->dataend;
+ void *allocend = info->allocend;
+ void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
+ if (newallocend >= dataend && newallocend <= allocend) {
+ // Found space - now reserve it.
+ struct allocinfo_s **pprev = info->pprev;
+ if (!fill)
+ fill = newallocend;
+ fill->next = info;
+ fill->pprev = pprev;
+ fill->data = newallocend;
+ fill->dataend = newallocend + size;
+ fill->allocend = allocend;
+
+ info->allocend = newallocend;
+ info->pprev = &fill->next;
+ *pprev = fill;
+ return newallocend;
+ }
+ }
+ return NULL;
+}
+
+// Release space allocated with allocSpace()
+static void
+freeSpace(struct allocinfo_s *info)
+{
+ struct allocinfo_s *next = info->next;
+ struct allocinfo_s **pprev = info->pprev;
+ *pprev = next;
+ if (next) {
+ if (next->allocend == info->data)
+ next->allocend = info->allocend;
+ next->pprev = pprev;
+ }
+}
+
+// Add new memory to a zone
+static void
+addSpace(struct zone_s *zone, void *start, void *end)
+{
+ // Find position to add space
+ struct allocinfo_s **pprev = &zone->info, *info;
+ for (;;) {
+ info = *pprev;
+ if (!info || info->data < start)
+ break;
+ pprev = &info->next;
+ }
+
+ // Add space using temporary allocation info.
+ struct allocdetail_s tempdetail;
+ tempdetail.datainfo.next = info;
+ tempdetail.datainfo.pprev = pprev;
+ tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
+ tempdetail.datainfo.allocend = end;
+ *pprev = &tempdetail.datainfo;
+ if (info)
+ info->pprev = &tempdetail.datainfo.next;
+
+ // Allocate final allocation info.
+ struct allocdetail_s *detail = allocSpace(
+ &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
+ if (!detail) {
+ detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
+ , MALLOC_MIN_ALIGN, NULL);
+ if (!detail) {
+ *tempdetail.datainfo.pprev = tempdetail.datainfo.next;
+ if (tempdetail.datainfo.next)
+ tempdetail.datainfo.next->pprev = tempdetail.datainfo.pprev;
+ warn_noalloc();
+ return;
+ }
+ }
+
+ // Replace temp alloc space with final alloc space
+ memcpy(&detail->datainfo, &tempdetail.datainfo, sizeof(detail->datainfo));
+ detail->handle = PMM_DEFAULT_HANDLE;
+
+ *tempdetail.datainfo.pprev = &detail->datainfo;
+ if (tempdetail.datainfo.next)
+ tempdetail.datainfo.next->pprev = &detail->datainfo.next;
+}
+
+// Search all zones for an allocation obtained from allocSpace()
+static struct allocinfo_s *
+findAlloc(void *data)
+{
+ int i;
+ for (i=0; i<ARRAY_SIZE(Zones); i++) {
+ struct zone_s *zone = Zones[i];
+ struct allocinfo_s *info;
+ for (info = zone->info; info; info = info->next)
+ if (info->data == data)
+ return info;
+ }
+ return NULL;
+}
+
+// Return the last sentinal node of a zone
+static struct allocinfo_s *
+findLast(struct zone_s *zone)
+{
+ struct allocinfo_s *info = zone->info;
+ if (!info)
+ return NULL;
+ for (;;) {
+ struct allocinfo_s *next = info->next;
+ if (!next)
+ return info;
+ info = next;
+ }
+}
+
+
+/****************************************************************
+ * Setup
+ ****************************************************************/
+
+void
+malloc_setup(void)
+{
+ ASSERT32FLAT();
+ dprintf(3, "malloc setup\n");
+
+ // Populate temp high ram
+ u32 highram = 0;
+ int i;
+ for (i=e820_count-1; i>=0; i--) {
+ struct e820entry *en = &e820_list[i];
+ u64 end = en->start + en->size;
+ if (end < 1024*1024)
+ break;
+ if (en->type != E820_RAM || end > 0xffffffff)
+ continue;
+ u32 s = en->start, e = end;
+ if (!highram) {
+ u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
+ if (newe <= e && newe >= s) {
+ highram = newe;
+ e = newe;
+ }
+ }
+ addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
+ }
+
+ // Populate other regions
+ addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
+ addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
+ addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
+ if (highram) {
+ addSpace(&ZoneHigh, (void*)highram
+ , (void*)highram + CONFIG_MAX_HIGHTABLE);
+ add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
+ }
+}
+
+// Update pointers after code relocation.
+void
+malloc_fixupreloc(void)
+{
+ ASSERT32FLAT();
+ if (!CONFIG_RELOCATE_INIT)
+ return;
+ dprintf(3, "malloc fixup reloc\n");
+
+ int i;
+ for (i=0; i<ARRAY_SIZE(Zones); i++) {
+ struct zone_s *zone = Zones[i];
+ zone->info->pprev = &zone->info;
+ }
+
+ // Add space free'd during relocation in f-segment to ZoneFSeg
+ extern u8 code32init_end[];
+ if ((u32)code32init_end > BUILD_BIOS_ADDR) {
+ memset((void*)BUILD_BIOS_ADDR, 0, (u32)code32init_end - BUILD_BIOS_ADDR);
+ addSpace(&ZoneFSeg, (void*)BUILD_BIOS_ADDR, code32init_end);
+ }
+}
+
+void
+malloc_finalize(void)
+{
+ ASSERT32FLAT();
+ dprintf(3, "malloc finalize\n");
+
+ // Reserve more low-mem if needed.
+ u32 endlow = GET_BDA(mem_size_kb)*1024;
+ add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
+
+ // Give back unused high ram.
+ struct allocinfo_s *info = findLast(&ZoneHigh);
+ if (info) {
+ u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
+ add_e820((u32)info->dataend, giveback, E820_RAM);
+ dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
+ }
+}
+
+