| 1 | /*
|
|---|
| 2 | * Copyright (C) 2001-2004 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 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 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <mm/heap.h>
|
|---|
| 30 | #include <synch/spinlock.h>
|
|---|
| 31 | #include <func.h>
|
|---|
| 32 | #include <memstr.h>
|
|---|
| 33 | #include <panic.h>
|
|---|
| 34 | #include <arch/types.h>
|
|---|
| 35 | #include <arch/asm.h>
|
|---|
| 36 | #include <arch.h>
|
|---|
| 37 | #include <align.h>
|
|---|
| 38 |
|
|---|
| 39 | /*
|
|---|
| 40 | * First-fit algorithm.
|
|---|
| 41 | * Simple, but hopefully correct.
|
|---|
| 42 | * Chunks being freed are tested for mergability with their neighbours.
|
|---|
| 43 | */
|
|---|
| 44 |
|
|---|
| 45 | static chunk_t *chunk0;
|
|---|
| 46 | SPINLOCK_INITIALIZE(heaplock);
|
|---|
| 47 |
|
|---|
| 48 | void early_heap_init(__address heap, size_t size)
|
|---|
| 49 | {
|
|---|
| 50 | memsetb(heap, size, 0);
|
|---|
| 51 | chunk0 = (chunk_t *) heap;
|
|---|
| 52 | chunk0->used = 0;
|
|---|
| 53 | chunk0->size = size - sizeof(chunk_t);
|
|---|
| 54 | chunk0->next = NULL;
|
|---|
| 55 | chunk0->prev = NULL;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /*
|
|---|
| 59 | * Uses first-fit algorithm.
|
|---|
| 60 | */
|
|---|
| 61 | void *early_malloc(size_t size)
|
|---|
| 62 | {
|
|---|
| 63 | ipl_t ipl;
|
|---|
| 64 | chunk_t *x, *y, *z;
|
|---|
| 65 |
|
|---|
| 66 | if (size == 0)
|
|---|
| 67 | panic("zero-size allocation request");
|
|---|
| 68 |
|
|---|
| 69 | size = ALIGN_UP(size, sizeof(__native));
|
|---|
| 70 |
|
|---|
| 71 | x = chunk0;
|
|---|
| 72 | ipl = interrupts_disable();
|
|---|
| 73 | spinlock_lock(&heaplock);
|
|---|
| 74 | while (x) {
|
|---|
| 75 | if (x->used || x->size < size) {
|
|---|
| 76 | x = x->next;
|
|---|
| 77 | continue;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | x->used = 1;
|
|---|
| 81 |
|
|---|
| 82 | /*
|
|---|
| 83 | * If the chunk exactly matches required size or if truncating
|
|---|
| 84 | * it would not provide enough space for storing a new chunk
|
|---|
| 85 | * header plus at least one byte of data, we are finished.
|
|---|
| 86 | */
|
|---|
| 87 | if (x->size < size + sizeof(chunk_t) + 1) {
|
|---|
| 88 | spinlock_unlock(&heaplock);
|
|---|
| 89 | interrupts_restore(ipl);
|
|---|
| 90 | return &x->data[0];
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /*
|
|---|
| 94 | * Truncate x and create a new chunk.
|
|---|
| 95 | */
|
|---|
| 96 | y = (chunk_t *) (((__address) x) + size + sizeof(chunk_t));
|
|---|
| 97 | y->used = 0;
|
|---|
| 98 | y->size = x->size - size - sizeof(chunk_t);
|
|---|
| 99 | y->prev = x;
|
|---|
| 100 | y->next = NULL;
|
|---|
| 101 |
|
|---|
| 102 | z = x->next;
|
|---|
| 103 | if (z) {
|
|---|
| 104 | z->prev = y;
|
|---|
| 105 | y->next = z;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | x->size = size;
|
|---|
| 109 | x->next = y;
|
|---|
| 110 | spinlock_unlock(&heaplock);
|
|---|
| 111 | interrupts_restore(ipl);
|
|---|
| 112 |
|
|---|
| 113 | return &x->data[0];
|
|---|
| 114 | }
|
|---|
| 115 | spinlock_unlock(&heaplock);
|
|---|
| 116 | interrupts_restore(ipl);
|
|---|
| 117 | return NULL;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void early_free(void *ptr)
|
|---|
| 121 | {
|
|---|
| 122 | ipl_t ipl;
|
|---|
| 123 | chunk_t *x, *y, *z;
|
|---|
| 124 |
|
|---|
| 125 | if (!ptr)
|
|---|
| 126 | panic("free on NULL");
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | y = (chunk_t *) (((__u8 *) ptr) - sizeof(chunk_t));
|
|---|
| 130 | if (y->used != 1)
|
|---|
| 131 | panic("freeing unused/damaged chunk");
|
|---|
| 132 |
|
|---|
| 133 | ipl = interrupts_disable();
|
|---|
| 134 | spinlock_lock(&heaplock);
|
|---|
| 135 | x = y->prev;
|
|---|
| 136 | z = y->next;
|
|---|
| 137 | /* merge x and y */
|
|---|
| 138 | if (x && !x->used) {
|
|---|
| 139 | x->size += y->size + sizeof(chunk_t);
|
|---|
| 140 | x->next = z;
|
|---|
| 141 | if (z)
|
|---|
| 142 | z->prev = x;
|
|---|
| 143 | y = x;
|
|---|
| 144 | }
|
|---|
| 145 | /* merge y and z or merge (x merged with y) and z */
|
|---|
| 146 | if (z && !z->used) {
|
|---|
| 147 | y->size += z->size + sizeof(chunk_t);
|
|---|
| 148 | y->next = z->next;
|
|---|
| 149 | if (z->next) {
|
|---|
| 150 | /* y is either y or x */
|
|---|
| 151 | z->next->prev = y;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | y->used = 0;
|
|---|
| 155 | spinlock_unlock(&heaplock);
|
|---|
| 156 | interrupts_restore(ipl);
|
|---|
| 157 | }
|
|---|