1 | #include <stdlib.h> |
---|
2 | #include <errno.h> |
---|
3 | #include <string.h> |
---|
4 | #include "malloc.h" |
---|
5 | #include "core.h" |
---|
6 | #include <syslinux/memscan.h> |
---|
7 | #include <dprintf.h> |
---|
8 | |
---|
9 | struct free_arena_header __core_malloc_head[NHEAP]; |
---|
10 | |
---|
11 | //static __hugebss char main_heap[128 << 10]; |
---|
12 | extern char __lowmem_heap[]; |
---|
13 | extern char free_high_memory[]; |
---|
14 | |
---|
15 | #define E820_MEM_MAX 0xfff00000 /* 4 GB - 1 MB */ |
---|
16 | int scan_highmem_area(void *data, addr_t start, addr_t len, |
---|
17 | enum syslinux_memmap_types type) |
---|
18 | { |
---|
19 | struct free_arena_header *fp; |
---|
20 | |
---|
21 | (void)data; |
---|
22 | |
---|
23 | dprintf("start = %x, len = %x, type = %d", start, len, type); |
---|
24 | |
---|
25 | if (start < 0x100000 || start > E820_MEM_MAX |
---|
26 | || type != SMT_FREE) |
---|
27 | return 0; |
---|
28 | |
---|
29 | if (start < __com32.cs_memsize) { |
---|
30 | len -= __com32.cs_memsize - start; |
---|
31 | start = __com32.cs_memsize; |
---|
32 | } |
---|
33 | if (len > E820_MEM_MAX - start) |
---|
34 | len = E820_MEM_MAX - start; |
---|
35 | |
---|
36 | if (len >= 2 * sizeof(struct arena_header)) { |
---|
37 | fp = (struct free_arena_header *)start; |
---|
38 | fp->a.attrs = ARENA_TYPE_USED | (HEAP_MAIN << ARENA_HEAP_POS); |
---|
39 | #ifdef DEBUG_MALLOC |
---|
40 | fp->a.magic = ARENA_MAGIC; |
---|
41 | #endif |
---|
42 | ARENA_SIZE_SET(fp->a.attrs, len); |
---|
43 | dprintf("will inject a block start:0x%x size 0x%x", start, len); |
---|
44 | __inject_free_block(fp); |
---|
45 | } |
---|
46 | |
---|
47 | __com32.cs_memsize = start + len; /* update the HighMemSize */ |
---|
48 | return 0; |
---|
49 | } |
---|
50 | |
---|
51 | #if 0 |
---|
52 | static void mpool_dump(enum heap heap) |
---|
53 | { |
---|
54 | struct free_arena_header *head = &__core_malloc_head[heap]; |
---|
55 | struct free_arena_header *fp; |
---|
56 | int size, type, i = 0; |
---|
57 | addr_t start, end; |
---|
58 | |
---|
59 | fp = head->next_free; |
---|
60 | while (fp != head) { |
---|
61 | size = ARENA_SIZE_GET(fp->a.attrs); |
---|
62 | type = ARENA_TYPE_GET(fp->a.attrs); |
---|
63 | start = (addr_t)fp; |
---|
64 | end = start + size; |
---|
65 | printf("area[%d]: start = 0x%08x, end = 0x%08x, type = %d\n", |
---|
66 | i++, start, end, type); |
---|
67 | fp = fp->next_free; |
---|
68 | } |
---|
69 | } |
---|
70 | #endif |
---|
71 | |
---|
72 | uint16_t *bios_free_mem; |
---|
73 | void mem_init(void) |
---|
74 | { |
---|
75 | struct free_arena_header *fp; |
---|
76 | int i; |
---|
77 | |
---|
78 | //dprintf("enter"); |
---|
79 | |
---|
80 | /* Initialize the head nodes */ |
---|
81 | fp = &__core_malloc_head[0]; |
---|
82 | for (i = 0 ; i < NHEAP ; i++) { |
---|
83 | fp->a.next = fp->a.prev = fp->next_free = fp->prev_free = fp; |
---|
84 | fp->a.attrs = ARENA_TYPE_HEAD | (i << ARENA_HEAP_POS); |
---|
85 | fp->a.tag = MALLOC_HEAD; |
---|
86 | fp++; |
---|
87 | } |
---|
88 | |
---|
89 | //dprintf("__lowmem_heap = 0x%p bios_free = 0x%p", |
---|
90 | // __lowmem_heap, *bios_free_mem); |
---|
91 | |
---|
92 | /* Initialize the lowmem heap */ |
---|
93 | fp = (struct free_arena_header *)__lowmem_heap; |
---|
94 | fp->a.attrs = ARENA_TYPE_USED | (HEAP_LOWMEM << ARENA_HEAP_POS); |
---|
95 | ARENA_SIZE_SET(fp->a.attrs, (*bios_free_mem << 10) - (uintptr_t)fp); |
---|
96 | #ifdef DEBUG_MALLOC |
---|
97 | fp->a.magic = ARENA_MAGIC; |
---|
98 | #endif |
---|
99 | __inject_free_block(fp); |
---|
100 | |
---|
101 | /* Initialize the main heap */ |
---|
102 | __com32.cs_memsize = (size_t)free_high_memory; |
---|
103 | syslinux_scan_memory(scan_highmem_area, NULL); |
---|
104 | } |
---|