2 * Copyright (c) 1992, 1993, 1996
3 * Berkeley Software Design, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Berkeley Software
18 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * BSDI mem.c,v 2.2 1996/04/08 19:32:57 bostic Exp
38 #define Mark(x) (*(char *) (x))
39 #define Owner(x) (*(u_short *) ((char *)(x)+1))
40 #define Size(x) (*(u_short *) ((char *)(x)+3))
41 #define Next(x) ((char *)(x) + (Size(x)+1)*16)
47 static int dosmem_size;
49 static char *next_p = (char *)0;
50 static char *end_p = (char *)0xB0000L;
58 *size = (*size & ~0xfff) + 0x1000;
61 *size = end_p - next_p;
64 if (next_p + *size > end_p) {
74 mem_free_owner(int owner)
78 debug(D_MEMORY, " : freeow(%04x)\n", owner);
80 for (mp = dosmem; ; mp = Next(mp)) {
81 if (Owner(mp) == owner)
94 for (mp = dosmem; ; mp = Next(mp)) {
95 debug(D_ALWAYS, "%8p: mark %c owner %04x size %04x\n",
96 mp, Mark(mp), Owner(mp), Size(mp));
104 mem_change_owner(int addr, int owner)
108 debug(D_MEMORY, "%04x: owner (%04x)\n", addr, owner);
111 for (mp = dosmem; ; mp = Next(mp)) {
112 if ((int)(mp + 16) == addr)
119 debug(D_ALWAYS, "%05x: illegal block in change owner\n", addr);
130 int base, avail_memory;
135 avail_memory = MAX_AVAIL_SEG * 16 - base;
136 dosmem = core_alloc(&avail_memory);
138 if (!dosmem || dosmem != (char *)base)
139 fatal("internal memory error\n");
141 dosmem_size = avail_memory / 16;
143 debug(D_MEMORY, "dosmem = %p base = 0x%x avail = 0x%x (%dK)\n",
144 dosmem, base, dosmem_size, avail_memory / 1024);
148 Size(dosmem) = dosmem_size - 1;
152 mem_unsplit(char *mp, int size)
156 while (Mark(mp) == 'M' && Size(mp) < size) {
162 Size(mp) += Size(nmp) + 1;
163 Mark(mp) = Mark(nmp);
168 mem_split(char *mp, int size)
173 rest = Size(mp) - size;
176 Mark(nmp) = Mark(mp);
179 Size(nmp) = rest - 1;
183 mem_alloc(int size, int owner, int *biggestp)
189 for (mp = dosmem; ; mp = Next(mp)) {
190 if (Owner(mp) == 0) {
192 mem_unsplit(mp, size);
193 if (Size(mp) >= size)
196 if (Size(mp) > biggest)
204 debug(D_MEMORY, "%04x: alloc(%04x, owner %04x) failed -> %d\n",
205 0, size, owner, biggest);
215 debug(D_MEMORY, "%04x: alloc(%04x, owner %04x)\n",
216 (int)mp/16 + 1, size, owner);
220 return (int)mp/16 + 1;
224 mem_adjust(int addr, int size, int *availp)
228 debug(D_MEMORY, "%04x: adjust(%05x)\n", addr, size);
231 for (mp = dosmem; ; mp = Next(mp)) {
232 if ((int)(mp + 16) == addr)
239 debug(D_ALWAYS, "%05x: illegal block in adjust\n", addr);
245 mem_unsplit(mp, size);
246 if (Size(mp) >= size)
249 debug(D_MEMORY, "%04x: adjust(%04x) failed -> %d\n",
250 (int)mp/16 + 1, size, Size(mp));
259 debug(D_MEMORY, "%04x: adjust(%04x)\n",
260 (int)mp/16 + 1, size);
272 struct mem_block *mp;
273 for (mp = mem_blocks.next; mp != &mem_blocks; mp = mp->next) {
274 if (mp->addr + mp->size != mp->next->addr)
276 if (mp->inuse && mp->size == 0)
280 if (mp->next != &mem_blocks)
295 for (i = 0; i < 100000; i++) {
299 newsize = random () % 20;
300 if ((newsize & 1) == 0)
304 printf ("adjust %d %x %d\n",
305 n, blocks[n], newsize);
306 mem_adjust (blocks[n], newsize, NULL);
310 while ((newsize = random () % 20) == 0)
313 printf ("alloc %d %d\n", n, newsize);
314 blocks[n] = mem_alloc (newsize, NULL);
316 if (mem_check () < 0) {
317 printf ("==== %d\n", i);
324 #endif /* MEM_TEST */