source: mainline/generic/src/mm/heap.c@ 3fc03fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3fc03fd was 5a95b25, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Cleanups to make it compile with -Wall. Did not catch everything yet.

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[f761f1eb]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>
[02a99d2]33#include <panic.h>
[f761f1eb]34#include <arch/types.h>
[18e0a6c]35#include <arch/asm.h>
[9c0a9b3]36#include <arch.h>
[7a8c866a]37#include <align.h>
[f761f1eb]38
39/*
40 * First-fit algorithm.
41 * Simple, but hopefully correct.
42 * Chunks being freed are tested for mergability with their neighbours.
43 */
44
45static chunk_t *chunk0;
46static spinlock_t heaplock;
47
[adecf496]48void early_heap_init(__address heap, size_t size)
[f761f1eb]49{
[2d93f1f9]50 spinlock_initialize(&heaplock, "heap_lock");
[6ba143d]51 memsetb(heap, size, 0);
[f761f1eb]52 chunk0 = (chunk_t *) heap;
53 chunk0->used = 0;
54 chunk0->size = size - sizeof(chunk_t);
55 chunk0->next = NULL;
56 chunk0->prev = NULL;
57}
58
59/*
60 * Uses first-fit algorithm.
61 */
[adecf496]62void *early_malloc(size_t size)
[f761f1eb]63{
[22f7769]64 ipl_t ipl;
[f761f1eb]65 chunk_t *x, *y, *z;
66
67 if (size == 0)
[2b50d7c]68 panic("zero-size allocation request");
[f761f1eb]69
[2312685]70 size = ALIGN_UP(size, sizeof(__native));
71
[f761f1eb]72 x = chunk0;
[22f7769]73 ipl = interrupts_disable();
[f761f1eb]74 spinlock_lock(&heaplock);
75 while (x) {
76 if (x->used || x->size < size) {
77 x = x->next;
78 continue;
79 }
80
81 x->used = 1;
82
83 /*
84 * If the chunk exactly matches required size or if truncating
85 * it would not provide enough space for storing a new chunk
86 * header plus at least one byte of data, we are finished.
87 */
88 if (x->size < size + sizeof(chunk_t) + 1) {
89 spinlock_unlock(&heaplock);
[22f7769]90 interrupts_restore(ipl);
[f761f1eb]91 return &x->data[0];
92 }
93
94 /*
95 * Truncate x and create a new chunk.
96 */
97 y = (chunk_t *) (((__address) x) + size + sizeof(chunk_t));
98 y->used = 0;
99 y->size = x->size - size - sizeof(chunk_t);
100 y->prev = x;
101 y->next = NULL;
102
[5a95b25]103 z = x->next;
104 if (z) {
[f761f1eb]105 z->prev = y;
106 y->next = z;
107 }
108
109 x->size = size;
110 x->next = y;
111 spinlock_unlock(&heaplock);
[22f7769]112 interrupts_restore(ipl);
[f761f1eb]113
114 return &x->data[0];
115 }
116 spinlock_unlock(&heaplock);
[22f7769]117 interrupts_restore(ipl);
[f761f1eb]118 return NULL;
119}
120
[adecf496]121void early_free(void *ptr)
[f761f1eb]122{
[22f7769]123 ipl_t ipl;
[f761f1eb]124 chunk_t *x, *y, *z;
125
126 if (!ptr)
127 panic("free on NULL");
128
129
130 y = (chunk_t *) (((__u8 *) ptr) - sizeof(chunk_t));
131 if (y->used != 1)
132 panic("freeing unused/damaged chunk");
133
[22f7769]134 ipl = interrupts_disable();
[f761f1eb]135 spinlock_lock(&heaplock);
136 x = y->prev;
137 z = y->next;
138 /* merge x and y */
139 if (x && !x->used) {
140 x->size += y->size + sizeof(chunk_t);
141 x->next = z;
142 if (z)
143 z->prev = x;
144 y = x;
145 }
146 /* merge y and z or merge (x merged with y) and z */
147 if (z && !z->used) {
148 y->size += z->size + sizeof(chunk_t);
149 y->next = z->next;
150 if (z->next) {
151 /* y is either y or x */
152 z->next->prev = y;
153 }
154 }
155 y->used = 0;
156 spinlock_unlock(&heaplock);
[22f7769]157 interrupts_restore(ipl);
[f761f1eb]158}
Note: See TracBrowser for help on using the repository browser.