client/runtime/guest_heap.c (9651B)
1 #include "guest_heap.h" 2 #include "guest_memory.h" 3 #include <stdlib.h> 4 #include <string.h> 5 #include <stdio.h> 6 #ifdef _WIN32 7 #define WIN32_LEAN_AND_MEAN 8 #include <windows.h> 9 #else 10 #include <pthread.h> 11 #endif 12 13 /* Host-only bookkeeping: guest objects contain only 32-bit guest addresses. */ 14 struct block { 15 uint32_t address, size; 16 int free; 17 struct block *previous, *next; 18 struct block *free_previous, *free_next, *hash_next; 19 }; 20 #ifdef _WIN32 21 static SRWLOCK mutex = SRWLOCK_INIT; 22 #else 23 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 24 #endif 25 static struct block *free_blocks[32]; 26 /* One bucket per aligned offset in a 1 MiB arena; collisions retain full addresses. */ 27 static struct block *allocated[65536]; 28 static uint32_t next_address = 0x60000000u; 29 30 /* Small objects share page metadata instead of one host allocation per block. */ 31 struct small_page { 32 uint32_t address, size, used, next_unused, first_free; 33 uint32_t allocated[128]; 34 struct small_page *previous, *next; 35 }; 36 static struct small_page *small_pages[65536]; 37 static struct small_page *small_available[16], *small_empty; 38 39 static void remove_small_page_locked(struct small_page *page) 40 { 41 if (page->previous) page->previous->next = page->next; 42 else small_available[page->size / 16 - 1] = page->next; 43 if (page->next) page->next->previous = page->previous; 44 } 45 46 static void add_small_page_locked(struct small_page *page) 47 { 48 unsigned index = page->size / 16 - 1; 49 page->previous = NULL; 50 page->next = small_available[index]; 51 if (page->next) page->next->previous = page; 52 small_available[index] = page; 53 } 54 55 static unsigned small_index_locked(struct small_page *page, uint32_t address) 56 { 57 unsigned index = (address - page->address) / page->size; 58 if ((address - page->address) % page->size || index >= page->next_unused || 59 !(page->allocated[index / 32] & (1u << (index % 32)))) { 60 fprintf(stderr, "Invalid guest allocation: %08x\n", address); 61 abort(); 62 } 63 return index; 64 } 65 66 static uint32_t small_malloc_locked(uint32_t size) 67 { 68 struct small_page *page = small_available[size / 16 - 1]; 69 if (!page) { 70 /* Reassign completely empty pages before mapping another guest page. */ 71 page = small_empty; 72 if (page) small_empty = page->next; 73 else { 74 if (next_address > 0xeffe0000u) return 0; 75 page = calloc(1, sizeof(*page)); 76 if (!page) return 0; 77 if (!guest_map("small heap", next_address, 0x10000u)) { free(page); return 0; } 78 page->address = next_address; 79 small_pages[next_address >> 16] = page; 80 next_address += 0x10000u; 81 } 82 page->size = size; 83 page->used = page->next_unused = 0; 84 page->first_free = UINT32_MAX; 85 add_small_page_locked(page); 86 } 87 88 /* Freed slots link through their unused payload; new slots need no list. */ 89 unsigned index; 90 if (page->first_free != UINT32_MAX) { 91 index = page->first_free; 92 page->first_free = *(uint32_t *)guest_address(page->address + index * size); 93 } else index = page->next_unused++; 94 page->allocated[index / 32] |= 1u << (index % 32); 95 if (++page->used == 0x10000u / size) remove_small_page_locked(page); 96 return page->address + index * size; 97 } 98 99 static void small_free_locked(struct small_page *page, uint32_t address) 100 { 101 unsigned index = small_index_locked(page, address); 102 if (page->used == 0x10000u / page->size) add_small_page_locked(page); 103 page->allocated[index / 32] &= ~(1u << (index % 32)); 104 *(uint32_t *)guest_address(address) = page->first_free; 105 page->first_free = index; 106 if (--page->used) return; 107 108 /* Empty pages can serve any small-object size on their next allocation. */ 109 remove_small_page_locked(page); 110 page->next = small_empty; 111 small_empty = page; 112 } 113 114 /* Free ranges of comparable sizes share a bucket. Sizes are always nonzero. */ 115 static unsigned free_bucket(uint32_t size) 116 { 117 return 31u - __builtin_clz(size); 118 } 119 120 static struct block **allocation_link_locked(uint32_t address) 121 { 122 struct block **link = &allocated[(address >> 4) & 65535]; 123 while (*link && (*link)->address != address) link = &(*link)->hash_next; 124 if (!*link) { 125 fprintf(stderr, "Invalid guest allocation: %08x\n", address); 126 abort(); 127 } 128 return link; 129 } 130 131 static void remove_free_locked(struct block *block) 132 { 133 if (block->free_previous) block->free_previous->free_next = block->free_next; 134 else free_blocks[free_bucket(block->size)] = block->free_next; 135 if (block->free_next) block->free_next->free_previous = block->free_previous; 136 } 137 138 static void add_free_locked(struct block *block) 139 { 140 block->free = 1; 141 block->free_previous = NULL; 142 unsigned bucket = free_bucket(block->size); 143 block->free_next = free_blocks[bucket]; 144 if (free_blocks[bucket]) free_blocks[bucket]->free_previous = block; 145 free_blocks[bucket] = block; 146 } 147 148 static uint32_t guest_malloc_locked(uint32_t size) 149 { 150 uint64_t aligned = ((uint64_t)(size ? size : 1) + 15) & ~15ull; 151 if (aligned > 0x8fff0000u) return 0; 152 uint32_t needed = (uint32_t)aligned; 153 if (needed <= 256) return small_malloc_locked(needed); 154 155 /* Preserve large holes by choosing the smallest suitable free range. */ 156 struct block *block = NULL; 157 for (unsigned bucket = free_bucket(needed); bucket < 32 && !block; ++bucket) { 158 for (struct block *candidate = free_blocks[bucket]; candidate; candidate = candidate->free_next) { 159 if (candidate->size < needed || (block && candidate->size >= block->size)) continue; 160 block = candidate; 161 if (block->size == needed) break; 162 } 163 } 164 165 /* Reuse and split free blocks within one contiguous host allocation. */ 166 if (block) { 167 if (block->size > needed) { 168 struct block *remainder = calloc(1, sizeof(*remainder)); 169 if (!remainder) return 0; 170 remove_free_locked(block); 171 remainder->address = block->address + needed; 172 remainder->size = block->size - needed; 173 remainder->previous = block; 174 remainder->next = block->next; 175 if (block->next) block->next->previous = remainder; 176 block->next = remainder; 177 block->size = needed; 178 add_free_locked(remainder); 179 } else remove_free_locked(block); 180 block->free = 0; 181 unsigned bucket = (block->address >> 4) & 65535; 182 block->hash_next = allocated[bucket]; 183 allocated[bucket] = block; 184 return block->address; 185 } 186 187 /* Grow in at least 1 MiB ranges; no holes need backing host memory. */ 188 uint32_t span = (needed + 0xffffu) & ~0xffffu; 189 if (span < 0x100000u) span = 0x100000u; 190 if (span > 0xefff0000u - next_address) return 0; 191 block = calloc(1, sizeof(*block)); 192 if (!block) return 0; 193 if (!guest_map("heap", next_address, span)) { free(block); return 0; } 194 block->address = next_address; 195 block->size = span; 196 add_free_locked(block); 197 next_address += span; 198 return guest_malloc_locked(size); 199 } 200 201 static void guest_free_locked(uint32_t address) 202 { 203 if (!address) return; 204 if (small_pages[address >> 16]) { small_free_locked(small_pages[address >> 16], address); return; } 205 struct block **link = allocation_link_locked(address); 206 struct block *block = *link; 207 *link = block->hash_next; 208 209 /* Only adjacent free neighbors can coalesce; arena boundaries have no links. */ 210 if (block->next && block->next->free) { 211 struct block *next = block->next; 212 remove_free_locked(next); 213 block->size += next->size; 214 block->next = next->next; 215 if (block->next) block->next->previous = block; 216 free(next); 217 } 218 if (block->previous && block->previous->free) { 219 struct block *previous = block->previous; 220 remove_free_locked(previous); 221 previous->size += block->size; 222 previous->next = block->next; 223 if (block->next) block->next->previous = previous; 224 free(block); 225 block = previous; 226 } 227 add_free_locked(block); 228 } 229 230 static uint32_t guest_realloc_locked(uint32_t address, uint32_t size) 231 { 232 if (!address) return guest_malloc_locked(size); 233 if (!size) { guest_free_locked(address); return 0; } 234 uint32_t previous_size; 235 if (small_pages[address >> 16]) { 236 small_index_locked(small_pages[address >> 16], address); 237 previous_size = small_pages[address >> 16]->size; 238 } else previous_size = (*allocation_link_locked(address))->size; 239 if (size <= previous_size) return address; 240 uint32_t replacement = guest_malloc_locked(size); 241 if (!replacement) return 0; 242 memcpy((void *)guest_address(replacement), (void *)guest_address(address), previous_size); 243 guest_free_locked(address); 244 return replacement; 245 } 246 247 /* Allocation metadata is shared by every guest thread. */ 248 static void lock_heap(void) 249 { 250 #ifdef _WIN32 251 AcquireSRWLockExclusive(&mutex); 252 #else 253 if (pthread_mutex_lock(&mutex)) abort(); 254 #endif 255 } 256 257 static void unlock_heap(void) 258 { 259 #ifdef _WIN32 260 ReleaseSRWLockExclusive(&mutex); 261 #else 262 if (pthread_mutex_unlock(&mutex)) abort(); 263 #endif 264 } 265 266 uint32_t guest_malloc(uint32_t size) 267 { 268 lock_heap(); 269 uint32_t address = guest_malloc_locked(size); 270 unlock_heap(); 271 return address; 272 } 273 274 void guest_free(uint32_t address) 275 { 276 lock_heap(); 277 guest_free_locked(address); 278 unlock_heap(); 279 } 280 281 uint32_t guest_realloc(uint32_t address, uint32_t size) 282 { 283 lock_heap(); 284 uint32_t replacement = guest_realloc_locked(address, size); 285 unlock_heap(); 286 return replacement; 287 }