Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /** |
||
20 | ------------ |
||
21 | CVS : $Id: oldmem.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $ |
||
22 | |||
23 | File: $File$ |
||
24 | Revision: $Revision: 1.1.1.1 $ |
||
25 | Last update: $Date: 2002-03-29 14:12:52 $ |
||
26 | ------------ |
||
27 | |||
28 | Basic Memory Manager Functions: |
||
29 | A classic fixed partition allocator! |
||
30 | |||
31 | This file comes from the Hartik 3.3.0's vm-mem.c |
||
32 | |||
33 | **/ |
||
34 | |||
35 | /* |
||
36 | * Copyright (C) 2000 Paolo Gai |
||
37 | * |
||
38 | * This program is free software; you can redistribute it and/or modify |
||
39 | * it under the terms of the GNU General Public License as published by |
||
40 | * the Free Software Foundation; either version 2 of the License, or |
||
41 | * (at your option) any later version. |
||
42 | * |
||
43 | * This program is distributed in the hope that it will be useful, |
||
44 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
45 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
46 | * GNU General Public License for more details. |
||
47 | * |
||
48 | * You should have received a copy of the GNU General Public License |
||
49 | * along with this program; if not, write to the Free Software |
||
50 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
51 | * |
||
52 | */ |
||
53 | |||
54 | |||
55 | #include <kernel/kern.h> |
||
56 | |||
57 | #define MAX_PARTITION 50 /* Max available partition */ |
||
58 | |||
59 | static struct { |
||
60 | BYTE used; |
||
61 | BYTE *addr; |
||
62 | DWORD size; |
||
63 | } mem_table[MAX_PARTITION]; |
||
64 | |||
65 | void kern_mem_init(struct multiboot_info *multiboot) |
||
66 | { |
||
67 | register int i; /* a counter */ |
||
68 | DWORD size; /* Memory size */ |
||
69 | BYTE *base; /* base address */ |
||
70 | LIN_ADDR b; /* base address */ |
||
71 | |||
72 | /* Get info about extended memory! We suppose that X has loaded */ |
||
73 | /* there the application; if you switch to DOS memory, then you */ |
||
74 | /* have to change the stuff in order it works; check X_... for */ |
||
75 | /* details. */ |
||
76 | X_meminfo(&b,&size,NULL,NULL); |
||
77 | base = (BYTE *)b; |
||
78 | #ifdef __MEM_DEBUG__ |
||
79 | message("PM Free Mem Base : %lx\n",base); |
||
80 | message("PM null addr (0L) : %lx\n",appl2linear((void *)0L)); |
||
81 | message("PM Free Mem Base (Cnvrtd): %lp\n",base); |
||
82 | #endif |
||
83 | |||
84 | mem_table[0].used = 1; |
||
85 | mem_table[0].addr = base; |
||
86 | mem_table[0].size = size; |
||
87 | for (i = 1; i < MAX_PARTITION; i++) mem_table[i].used = 0; |
||
88 | |||
89 | #ifdef __MEM_DEBUG__ |
||
90 | kern_mem_dump(); |
||
91 | #endif |
||
92 | } |
||
93 | |||
94 | void *kern_alloc(DWORD s) |
||
95 | { |
||
96 | void *p = NULL; |
||
97 | int i = 0; |
||
98 | |||
99 | while (i < MAX_PARTITION && p == NULL) { |
||
100 | if (mem_table[i].used && (mem_table[i].size >= s)) |
||
101 | p = mem_table[i].addr; |
||
102 | else i++; |
||
103 | } |
||
104 | if (p != NULL) { |
||
105 | if (mem_table[i].size > s) { |
||
106 | mem_table[i].size -= s; |
||
107 | mem_table[i].addr += s; |
||
108 | } |
||
109 | else mem_table[i].used = FALSE; |
||
110 | } |
||
111 | return(p); |
||
112 | } |
||
113 | |||
114 | WORD kern_free(void *p,DWORD s) |
||
115 | { |
||
116 | register int i = 1; |
||
117 | unsigned i1 = 0, i2 = 0; |
||
118 | |||
119 | while (i < MAX_PARTITION && ((i1 == 0) || (i2 == 0)) ) { |
||
120 | if (mem_table[i].used) { |
||
121 | if (mem_table[i].addr + mem_table[i].size == p) i1 = i; |
||
122 | if (mem_table[i].addr == (BYTE *)(p) + s) i2 = i; |
||
123 | } |
||
124 | i++; |
||
125 | } |
||
126 | if (i1 != 0 && i2 != 0) { |
||
127 | mem_table[i1].size += mem_table[i2].size + s; |
||
128 | mem_table[i2].used = FALSE; |
||
129 | } |
||
130 | else if (i1 == 0 && i2 != 0) { |
||
131 | mem_table[i2].addr = p; |
||
132 | mem_table[i2].size += s; |
||
133 | } |
||
134 | else if (i1 != 0 && i2 == 0) mem_table[i1].size += s; |
||
135 | else { |
||
136 | i = 0; |
||
137 | while (i < MAX_PARTITION && (mem_table[i].used == TRUE)) i++; |
||
138 | if (i == MAX_PARTITION) return(FALSE); |
||
139 | mem_table[i].addr = p; |
||
140 | mem_table[i].size = s; |
||
141 | mem_table[i].used = TRUE; |
||
142 | } |
||
143 | return(TRUE); |
||
144 | } |
||
145 | |||
146 | void kern_mem_dump(void) |
||
147 | { |
||
148 | register int i; |
||
149 | for (i = 0; i < MAX_PARTITION; i++) { |
||
150 | if (mem_table[i].used) message("Entry : [%d] Addr : %p Size : %ld\n",i,mem_table[i].addr,mem_table[i].size); |
||
151 | } |
||
152 | } |