1 /**************************************************************************
3 * Copyright (C) 1999 Wittawat Yamwong
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 **************************************************************************/
26 #include "pipe/p_compiler.h"
27 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 #include "util/u_mm.h"
34 u_mmDumpMemInfo(const struct mem_block
*heap
)
36 debug_printf("Memory heap %p:\n", (void *) heap
);
38 debug_printf(" heap == 0\n");
41 const struct mem_block
*p
;
43 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
44 debug_printf(" Offset:%08x, Size:%08x, %c%c\n", p
->ofs
, p
->size
,
46 p
->reserved
? 'R':'.');
49 debug_printf("\nFree list:\n");
51 for (p
= heap
->next_free
; p
!= heap
; p
= p
->next_free
) {
52 debug_printf(" FREE Offset:%08x, Size:%08x, %c%c\n", p
->ofs
, p
->size
,
54 p
->reserved
? 'R':'.');
58 debug_printf("End of memory blocks\n");
63 u_mmInit(int ofs
, int size
)
65 struct mem_block
*heap
, *block
;
70 heap
= CALLOC_STRUCT(mem_block
);
74 block
= CALLOC_STRUCT(mem_block
);
82 heap
->next_free
= block
;
83 heap
->prev_free
= block
;
88 block
->next_free
= heap
;
89 block
->prev_free
= heap
;
99 static struct mem_block
*
100 SliceBlock(struct mem_block
*p
,
101 int startofs
, int size
,
102 int reserved
, int alignment
)
104 struct mem_block
*newblock
;
106 /* break left [p, newblock, p->next], then p = newblock */
107 if (startofs
> p
->ofs
) {
108 newblock
= CALLOC_STRUCT(mem_block
);
111 newblock
->ofs
= startofs
;
112 newblock
->size
= p
->size
- (startofs
- p
->ofs
);
114 newblock
->heap
= p
->heap
;
116 newblock
->next
= p
->next
;
118 p
->next
->prev
= newblock
;
121 newblock
->next_free
= p
->next_free
;
122 newblock
->prev_free
= p
;
123 p
->next_free
->prev_free
= newblock
;
124 p
->next_free
= newblock
;
126 p
->size
-= newblock
->size
;
130 /* break right, also [p, newblock, p->next] */
131 if (size
< p
->size
) {
132 newblock
= CALLOC_STRUCT(mem_block
);
135 newblock
->ofs
= startofs
+ size
;
136 newblock
->size
= p
->size
- size
;
138 newblock
->heap
= p
->heap
;
140 newblock
->next
= p
->next
;
142 p
->next
->prev
= newblock
;
145 newblock
->next_free
= p
->next_free
;
146 newblock
->prev_free
= p
;
147 p
->next_free
->prev_free
= newblock
;
148 p
->next_free
= newblock
;
153 /* p = middle block */
156 /* Remove p from the free list:
158 p
->next_free
->prev_free
= p
->prev_free
;
159 p
->prev_free
->next_free
= p
->next_free
;
164 p
->reserved
= reserved
;
170 u_mmAllocMem(struct mem_block
*heap
, int size
, int align2
, int startSearch
)
173 const int mask
= (1 << align2
)-1;
179 assert(align2
<= 12); /* sanity check, 2^12 (4KB) enough? */
181 if (!heap
|| align2
< 0 || size
<= 0)
184 for (p
= heap
->next_free
; p
!= heap
; p
= p
->next_free
) {
187 startofs
= (p
->ofs
+ mask
) & ~mask
;
188 if ( startofs
< startSearch
) {
189 startofs
= startSearch
;
191 endofs
= startofs
+size
;
192 if (endofs
<= (p
->ofs
+p
->size
))
200 p
= SliceBlock(p
,startofs
,size
,0,mask
+1);
207 u_mmFindBlock(struct mem_block
*heap
, int start
)
211 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
221 Join2Blocks(struct mem_block
*p
)
223 /* XXX there should be some assertions here */
225 /* NOTE: heap->free == 0 */
227 if (p
->free
&& p
->next
->free
) {
228 struct mem_block
*q
= p
->next
;
230 assert(p
->ofs
+ p
->size
== q
->ofs
);
236 q
->next_free
->prev_free
= q
->prev_free
;
237 q
->prev_free
->next_free
= q
->next_free
;
246 u_mmFreeMem(struct mem_block
*b
)
252 debug_printf("block already free\n");
256 debug_printf("block is reserved\n");
261 b
->next_free
= b
->heap
->next_free
;
262 b
->prev_free
= b
->heap
;
263 b
->next_free
->prev_free
= b
;
264 b
->prev_free
->next_free
= b
;
267 if (b
->prev
!= b
->heap
)
268 Join2Blocks(b
->prev
);
275 u_mmDestroy(struct mem_block
*heap
)
282 for (p
= heap
->next
; p
!= heap
; ) {
283 struct mem_block
*next
= p
->next
;