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
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 $
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)
48 static int dosmem_size
;
50 static char *next_p
= (char *)0;
51 static char *end_p
= (char *)0xB0000L
;
59 *size
= (*size
& ~0xfff) + 0x1000;
62 *size
= end_p
- next_p
;
65 if (next_p
+ *size
> end_p
) {
75 mem_free_owner(int owner
)
79 debug(D_MEMORY
, " : freeow(%04x)\n", owner
);
81 for (mp
= dosmem
; ; mp
= Next(mp
)) {
82 if (Owner(mp
) == owner
)
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
));
105 mem_change_owner(int addr
, int owner
)
109 debug(D_MEMORY
, "%04x: owner (%04x)\n", addr
, owner
);
112 for (mp
= dosmem
; ; mp
= Next(mp
)) {
113 if ((int)(mp
+ 16) == addr
)
120 debug(D_ALWAYS
, "%05x: illegal block in change owner\n", addr
);
131 int base
, avail_memory
;
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);
149 Size(dosmem
) = dosmem_size
- 1;
153 mem_unsplit(char *mp
, int size
)
157 while (Mark(mp
) == 'M' && Size(mp
) < size
) {
163 Size(mp
) += Size(nmp
) + 1;
164 Mark(mp
) = Mark(nmp
);
169 mem_split(char *mp
, int size
)
174 rest
= Size(mp
) - size
;
177 Mark(nmp
) = Mark(mp
);
180 Size(nmp
) = rest
- 1;
184 mem_alloc(int size
, int owner
, int *biggestp
)
190 for (mp
= dosmem
; ; mp
= Next(mp
)) {
191 if (Owner(mp
) == 0) {
193 mem_unsplit(mp
, size
);
194 if (Size(mp
) >= size
)
197 if (Size(mp
) > biggest
)
205 debug(D_MEMORY
, "%04x: alloc(%04x, owner %04x) failed -> %d\n",
206 0, size
, owner
, biggest
);
216 debug(D_MEMORY
, "%04x: alloc(%04x, owner %04x)\n",
217 (int)mp
/16 + 1, size
, owner
);
221 return (int)mp
/16 + 1;
225 mem_adjust(int addr
, int size
, int *availp
)
229 debug(D_MEMORY
, "%04x: adjust(%05x)\n", addr
, size
);
232 for (mp
= dosmem
; ; mp
= Next(mp
)) {
233 if ((int)(mp
+ 16) == addr
)
240 debug(D_ALWAYS
, "%05x: illegal block in adjust\n", addr
);
246 mem_unsplit(mp
, size
);
247 if (Size(mp
) >= size
)
250 debug(D_MEMORY
, "%04x: adjust(%04x) failed -> %d\n",
251 (int)mp
/16 + 1, size
, Size(mp
));
260 debug(D_MEMORY
, "%04x: adjust(%04x)\n",
261 (int)mp
/16 + 1, size
);
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
)
277 if (mp
->inuse
&& mp
->size
== 0)
281 if (mp
->next
!= &mem_blocks
)
296 for (i
= 0; i
< 100000; i
++) {
300 newsize
= random () % 20;
301 if ((newsize
& 1) == 0)
305 printf ("adjust %d %x %d\n",
306 n
, blocks
[n
], newsize
);
307 mem_adjust (blocks
[n
], newsize
, NULL
);
311 while ((newsize
= random () % 20) == 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
);
325 #endif /* MEM_TEST */