Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* Project: OSLib |
2 | * Description: The OS Construction Kit |
||
3 | * Date: 1.6.2000 |
||
4 | * Idea by: Luca Abeni & Gerardo Lamastra |
||
5 | * |
||
6 | * OSLib is an SO project aimed at developing a common, easy-to-use |
||
7 | * low-level infrastructure for developing OS kernels and Embedded |
||
8 | * Applications; it partially derives from the HARTIK project but it |
||
9 | * currently is independently developed. |
||
10 | * |
||
11 | * OSLib is distributed under GPL License, and some of its code has |
||
12 | * been derived from the Linux kernel source; also some important |
||
13 | * ideas come from studying the DJGPP go32 extender. |
||
14 | * |
||
15 | * We acknowledge the Linux Community, Free Software Foundation, |
||
16 | * D.J. Delorie and all the other developers who believe in the |
||
17 | * freedom of software and ideas. |
||
18 | * |
||
19 | * For legalese, check out the included GPL license. |
||
20 | */ |
||
21 | |||
22 | /* Basic Memory Manager Functions: |
||
23 | A classic fixed partition allocator! */ |
||
24 | |||
25 | #include <ll/i386/stdlib.h> |
||
26 | #include <ll/i386/string.h> |
||
27 | #include <ll/i386/mem.h> |
||
28 | #include <ll/i386/cons.h> |
||
29 | #include <ll/i386/error.h> |
||
30 | |||
31 | #include <ll/sys/ll/ll-mem.h> |
||
32 | |||
33 | FILE(Memory); |
||
34 | |||
40 | pj | 35 | #define MAX_PARTITION 50 /* Max available partition */ |
2 | pj | 36 | |
37 | static struct { |
||
40 | pj | 38 | BYTE used; |
2 | pj | 39 | #ifdef __WC16__ |
40 | pj | 40 | BYTE __huge *addr; |
2 | pj | 41 | #else |
40 | pj | 42 | BYTE *addr; |
2 | pj | 43 | #endif |
40 | pj | 44 | DWORD size; |
2 | pj | 45 | } mem_table[MAX_PARTITION]; |
46 | |||
47 | void ll_mem_init(void *base, DWORD size) |
||
48 | { |
||
49 | register int i; |
||
50 | mem_table[0].used = 1; |
||
51 | mem_table[0].addr = base; |
||
52 | mem_table[0].size = size; |
||
40 | pj | 53 | for (i = 1; i < MAX_PARTITION; i++) mem_table[i].used = 0; |
2 | pj | 54 | } |
55 | |||
56 | void *ll_alloc(DWORD s) |
||
57 | { |
||
58 | void *p = NULL; |
||
59 | int i = 0; |
||
60 | |||
61 | while (i < MAX_PARTITION && p == NULL) { |
||
62 | if (mem_table[i].used && (mem_table[i].size >= s)) |
||
40 | pj | 63 | p = mem_table[i].addr; |
64 | else i++; |
||
2 | pj | 65 | } |
66 | if (p != NULL) { |
||
67 | if (mem_table[i].size > s) { |
||
68 | mem_table[i].size -= s; |
||
69 | mem_table[i].addr += s; |
||
40 | pj | 70 | } |
71 | else mem_table[i].used = FALSE; |
||
2 | pj | 72 | } |
40 | pj | 73 | return(p); |
2 | pj | 74 | } |
75 | |||
40 | pj | 76 | WORD ll_free(void *p,DWORD s) |
2 | pj | 77 | { |
78 | register int i = 1; |
||
79 | unsigned i1 = 0, i2 = 0; |
||
80 | |||
40 | pj | 81 | while (i < MAX_PARTITION && ((i1 == 0) || (i2 == 0)) ) { |
2 | pj | 82 | if (mem_table[i].used) { |
40 | pj | 83 | if (mem_table[i].addr + mem_table[i].size == p) i1 = i; |
84 | if (mem_table[i].addr == (BYTE *)(p) + s) i2 = i; |
||
2 | pj | 85 | } |
86 | i++; |
||
87 | } |
||
88 | if (i1 != 0 && i2 != 0) { |
||
89 | mem_table[i1].size += mem_table[i2].size + s; |
||
90 | mem_table[i2].used = FALSE; |
||
40 | pj | 91 | } |
92 | else if (i1 == 0 && i2 != 0) { |
||
2 | pj | 93 | mem_table[i2].addr = p; |
94 | mem_table[i2].size += s; |
||
40 | pj | 95 | } |
96 | else if (i1 != 0 && i2 == 0) mem_table[i1].size += s; |
||
2 | pj | 97 | else { |
98 | i = 0; |
||
40 | pj | 99 | while (i < MAX_PARTITION && (mem_table[i].used == TRUE)) i++; |
100 | if (i == MAX_PARTITION) return(FALSE); |
||
2 | pj | 101 | mem_table[i].addr = p; |
102 | mem_table[i].size = s; |
||
103 | mem_table[i].used = TRUE; |
||
104 | } |
||
40 | pj | 105 | return(TRUE); |
2 | pj | 106 | } |
107 | |||
108 | void ll_mem_dump(void) |
||
109 | { |
||
110 | register int i; |
||
111 | for (i = 0; i < MAX_PARTITION; i++) { |
||
40 | pj | 112 | if (mem_table[i].used) message("Entry : [%d] Addr : %p Size : %ld\n",i,mem_table[i].addr,mem_table[i].size); |
2 | pj | 113 | } |
114 | } |