2 * Copyright © 1997 Pluto Technologies International, Inc. Boulder CO
3 * Copyright © 1997 interface business GmbH, Dresden.
6 * This code was written by Jörg Wunsch, Dresden.
7 * Direct comments to <joerg_wunsch@interface-business.de>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sys/i386/boot/cdboot/malloc.c,v 1.2 1999/08/28 00:43:18 peter Exp $
31 * $DragonFly: src/sys/platform/pc32/boot/cdboot/Attic/malloc.c,v 1.2 2003/06/17 04:28:34 dillon Exp $
35 * Simple memory allocator for the bootstrap loader. Probably suffers
36 * a lot from fragmentation.
43 /* ``Nobody will ever need more than 640 KB of RAM.'' :-) */
44 #define MAXBRK (640 * 1024 * 1024)
56 static struct chunk *freelist;
61 struct chunk *p, *q, *oldp;
64 nelems = (len + sizeof(struct chunk) - 1) / sizeof(struct chunk) + 1;
67 * First, see if we can satisfy the request from the freelist.
69 for (p = freelist, oldp = 0;
70 p && p != (struct chunk *)brkval;
71 oldp = p, p = p->next) {
72 if (p->len > nelems) {
73 /* chunk is larger, shorten, and return the tail */
81 if (p->len == nelems) {
82 /* exact match, remove from freelist */
93 * Nothing found on freelist, try obtaining more space.
97 q = p = (struct chunk *)brkval;
98 if ((int)(p + NCHUNKS) > MAXBRK)
110 if (oldp + oldp->len == q) {
111 /* extend last chunk */
112 oldp->len += NCHUNKS;
124 struct chunk *p, *q, *oldp;
129 q = (struct chunk *)ptr;
132 printf("malloc error: botched ptr to free()\n");
137 * Walk the freelist, and insert in the correct sequence.
139 for (p = freelist, oldp = 0;
140 p && p != (struct chunk *)brkval;
141 oldp = p, p = p->next) {
142 if ((unsigned)p > (unsigned)q) {
143 if (q + q->len == p) {
144 /* aggregate with next chunk */
150 if (oldp + oldp->len == q) {
151 /* aggregate with previous chunk */
155 /* insert into chain */
167 if (oldp + oldp->len == q) {
168 /* aggregate with previous chunk */
177 /* we are alone on the freelist */