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