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 $
34 * Simple memory allocator for the bootstrap loader. Probably suffers
35 * a lot from fragmentation.
42 /* ``Nobody will ever need more than 640 KB of RAM.'' :-) */
43 #define MAXBRK (640 * 1024 * 1024)
55 static struct chunk *freelist;
60 struct chunk *p, *q, *oldp;
63 nelems = (len + sizeof(struct chunk) - 1) / sizeof(struct chunk) + 1;
66 * First, see if we can satisfy the request from the freelist.
68 for (p = freelist, oldp = 0;
69 p && p != (struct chunk *)brkval;
70 oldp = p, p = p->next) {
71 if (p->len > nelems) {
72 /* chunk is larger, shorten, and return the tail */
80 if (p->len == nelems) {
81 /* exact match, remove from freelist */
92 * Nothing found on freelist, try obtaining more space.
96 q = p = (struct chunk *)brkval;
97 if ((int)(p + NCHUNKS) > MAXBRK)
109 if (oldp + oldp->len == q) {
110 /* extend last chunk */
111 oldp->len += NCHUNKS;
123 struct chunk *p, *q, *oldp;
128 q = (struct chunk *)ptr;
131 printf("malloc error: botched ptr to free()\n");
136 * Walk the freelist, and insert in the correct sequence.
138 for (p = freelist, oldp = 0;
139 p && p != (struct chunk *)brkval;
140 oldp = p, p = p->next) {
141 if ((unsigned)p > (unsigned)q) {
142 if (q + q->len == p) {
143 /* aggregate with next chunk */
149 if (oldp + oldp->len == q) {
150 /* aggregate with previous chunk */
154 /* insert into chain */
166 if (oldp + oldp->len == q) {
167 /* aggregate with previous chunk */
176 /* we are alone on the freelist */