ktrdump: print ktr data using vfprintf()
[dragonfly.git] / usr.bin / doscmd / mem.c
blobbd39a2834403b234faf81dcd40e68b643cfff541
1 /*
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
7 * are met:
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
16 * Design, Inc.
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
28 * SUCH DAMAGE.
30 * BSDI mem.c,v 2.2 1996/04/08 19:32:57 bostic Exp
32 * $FreeBSD: src/usr.bin/doscmd/mem.c,v 1.2.2.1 2002/04/25 11:04:51 tg Exp $
33 * $DragonFly: src/usr.bin/doscmd/mem.c,v 1.3 2003/10/04 20:36:43 hmp Exp $
36 #include <stdio.h>
37 #include "doscmd.h"
39 #define Mark(x) (*(char *) (x))
40 #define Owner(x) (*(u_short *) ((char *)(x)+1))
41 #define Size(x) (*(u_short *) ((char *)(x)+3))
42 #define Next(x) ((char *)(x) + (Size(x)+1)*16)
44 /* exports */
45 char *dosmem;
47 /* locals */
48 static int dosmem_size;
50 static char *next_p = (char *)0;
51 static char *end_p = (char *)0xB0000L;
53 static char *
54 core_alloc(int *size)
56 char *ret;
57 if (*size) {
58 if (*size & 0xfff) {
59 *size = (*size & ~0xfff) + 0x1000;
61 } else {
62 *size = end_p - next_p;
65 if (next_p + *size > end_p) {
66 return NULL;
69 ret = next_p;
70 next_p += *size;
71 return ret;
74 void
75 mem_free_owner(int owner)
77 char *mp;
79 debug(D_MEMORY, " : freeow(%04x)\n", owner);
81 for (mp = dosmem; ; mp = Next(mp)) {
82 if (Owner(mp) == owner)
83 Owner(mp) = 0;
85 if (Mark(mp) != 'M')
86 break;
90 static void
91 mem_print(void)
93 char *mp;
95 for (mp = dosmem; ; mp = Next(mp)) {
96 debug(D_ALWAYS, "%8p: mark %c owner %04x size %04x\n",
97 mp, Mark(mp), Owner(mp), Size(mp));
99 if (Mark(mp) != 'M')
100 break;
104 void
105 mem_change_owner(int addr, int owner)
107 char *mp;
109 debug(D_MEMORY, "%04x: owner (%04x)\n", addr, owner);
110 addr <<= 4;
112 for (mp = dosmem; ; mp = Next(mp)) {
113 if ((int)(mp + 16) == addr)
114 goto found;
116 if (Mark(mp) != 'M')
117 break;
120 debug(D_ALWAYS, "%05x: illegal block in change owner\n", addr);
121 mem_print();
122 return;
124 found:
125 Owner(mp) = owner;
128 void
129 mem_init(void)
131 int base, avail_memory;
133 base = 0x600;
134 core_alloc(&base);
136 avail_memory = MAX_AVAIL_SEG * 16 - base;
137 dosmem = core_alloc(&avail_memory);
139 if (!dosmem || dosmem != (char *)base)
140 fatal("internal memory error\n");
142 dosmem_size = avail_memory / 16;
144 debug(D_MEMORY, "dosmem = %p base = 0x%x avail = 0x%x (%dK)\n",
145 dosmem, base, dosmem_size, avail_memory / 1024);
147 Mark(dosmem) = 'Z';
148 Owner(dosmem) = 0;
149 Size(dosmem) = dosmem_size - 1;
152 static void
153 mem_unsplit(char *mp, int size)
155 char *nmp;
157 while (Mark(mp) == 'M' && Size(mp) < size) {
158 nmp = Next(mp);
160 if (Owner(nmp) != 0)
161 break;
163 Size(mp) += Size(nmp) + 1;
164 Mark(mp) = Mark(nmp);
168 static void
169 mem_split(char *mp, int size)
171 char *nmp;
172 int rest;
174 rest = Size(mp) - size;
175 Size(mp) = size;
176 nmp = Next(mp);
177 Mark(nmp) = Mark(mp);
178 Mark(mp) = 'M';
179 Owner(nmp) = 0;
180 Size(nmp) = rest - 1;
184 mem_alloc(int size, int owner, int *biggestp)
186 char *mp;
187 int biggest;
189 biggest = 0;
190 for (mp = dosmem; ; mp = Next(mp)) {
191 if (Owner(mp) == 0) {
192 if (Size(mp) < size)
193 mem_unsplit(mp, size);
194 if (Size(mp) >= size)
195 goto got;
197 if (Size(mp) > biggest)
198 biggest = Size(mp);
201 if (Mark(mp) != 'M')
202 break;
205 debug(D_MEMORY, "%04x: alloc(%04x, owner %04x) failed -> %d\n",
206 0, size, owner, biggest);
208 if (biggestp)
209 *biggestp = biggest;
210 return 0;
212 got:
213 if (Size(mp) > size)
214 mem_split(mp, size);
215 Owner(mp) = owner;
216 debug(D_MEMORY, "%04x: alloc(%04x, owner %04x)\n",
217 (int)mp/16 + 1, size, owner);
219 if (biggestp)
220 *biggestp = size;
221 return (int)mp/16 + 1;
225 mem_adjust(int addr, int size, int *availp)
227 char *mp;
229 debug(D_MEMORY, "%04x: adjust(%05x)\n", addr, size);
230 addr <<= 4;
232 for (mp = dosmem; ; mp = Next(mp)) {
233 if ((int)(mp + 16) == addr)
234 goto found;
236 if (Mark(mp) != 'M')
237 break;
240 debug(D_ALWAYS, "%05x: illegal block in adjust\n", addr);
241 mem_print();
242 return -2;
244 found:
245 if (Size(mp) < size)
246 mem_unsplit(mp, size);
247 if (Size(mp) >= size)
248 goto got;
250 debug(D_MEMORY, "%04x: adjust(%04x) failed -> %d\n",
251 (int)mp/16 + 1, size, Size(mp));
253 if (availp)
254 *availp = Size(mp);
255 return -1;
257 got:
258 if (Size(mp) > size)
259 mem_split(mp, size);
260 debug(D_MEMORY, "%04x: adjust(%04x)\n",
261 (int)mp/16 + 1, size);
263 if (availp)
264 *availp = size;
265 return 0;
270 #ifdef MEM_TEST
271 mem_check (void)
273 struct mem_block *mp;
274 for (mp = mem_blocks.next; mp != &mem_blocks; mp = mp->next) {
275 if (mp->addr + mp->size != mp->next->addr)
276 break;
277 if (mp->inuse && mp->size == 0)
278 return (-1);
281 if (mp->next != &mem_blocks)
282 return (-1);
283 return (0);
286 char *blocks[10];
288 main (void)
290 int i;
291 int n;
292 int newsize;
294 mem_init (0, 300);
296 for (i = 0; i < 100000; i++) {
297 n = random () % 10;
299 if (blocks[n]) {
300 newsize = random () % 20;
301 if ((newsize & 1) == 0)
302 newsize = 0;
304 if (0)
305 printf ("adjust %d %x %d\n",
306 n, blocks[n], newsize);
307 mem_adjust (blocks[n], newsize, NULL);
308 if (newsize == 0)
309 blocks[n] = NULL;
310 } else {
311 while ((newsize = random () % 20) == 0)
313 if (0)
314 printf ("alloc %d %d\n", n, newsize);
315 blocks[n] = mem_alloc (newsize, NULL);
317 if (mem_check () < 0) {
318 printf ("==== %d\n", i);
319 mem_print ();
323 mem_print ();
325 #endif /* MEM_TEST */