2
* GLX Hardware Device Driver common code
3
* Copyright (C) 1999 Keith Whitwell
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
* KEITH WHITWELL, 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.
24
/* $XFree86: xc/lib/GL/mesa/src/drv/common/mm.c,v 1.4 2002/10/30 12:51:27 alanh Exp $ */
32
/* KW: I don't know who the author of this code is, but it wasn't me
33
* despite what the copyright says...
36
void mmDumpMemInfo( memHeap_t *heap )
40
fprintf(stderr, "Memory heap %p:\n", heap);
42
fprintf(stderr, " heap == 0\n");
44
p = (TMemBlock *)heap;
46
fprintf(stderr, " Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
48
p->reserved ? 'R':'.');
52
fprintf(stderr, "End of memory blocks\n");
55
memHeap_t *mmInit(int ofs,
63
blocks = (TMemBlock *) calloc(1,sizeof(TMemBlock));
68
return (memHeap_t *)blocks;
74
static TMemBlock* SliceBlock(TMemBlock *p,
75
int startofs, int size,
76
int reserved, int alignment)
81
if (startofs > p->ofs) {
82
newblock = (TMemBlock*) calloc(1,sizeof(TMemBlock));
85
newblock->ofs = startofs;
86
newblock->size = p->size - (startofs - p->ofs);
88
newblock->next = p->next;
89
p->size -= newblock->size;
96
newblock = (TMemBlock*) calloc(1,sizeof(TMemBlock));
99
newblock->ofs = startofs + size;
100
newblock->size = p->size - size;
102
newblock->next = p->next;
107
/* p = middle block */
108
p->align = alignment;
110
p->reserved = reserved;
114
PMemBlock mmAllocMem( memHeap_t *heap, int size, int align2, int startSearch)
116
int mask,startofs,endofs;
119
if (!heap || align2 < 0 || size <= 0)
121
mask = (1 << align2)-1;
123
p = (TMemBlock *)heap;
126
startofs = (p->ofs + mask) & ~mask;
127
if ( startofs < startSearch ) {
128
startofs = startSearch;
130
endofs = startofs+size;
131
if (endofs <= (p->ofs+p->size))
138
p = SliceBlock(p,startofs,size,0,mask+1);
143
static __inline__ int Join2Blocks(TMemBlock *p)
145
if (p->free && p->next && p->next->free) {
146
TMemBlock *q = p->next;
155
int mmFreeMem(PMemBlock b)
162
fprintf(stderr, "no heap\n");
167
while (p && p != b) {
171
if (!p || p->free || p->reserved) {
173
fprintf(stderr, "block not found in heap\n");
175
fprintf(stderr, "block already free\n");
177
fprintf(stderr, "block is reserved\n");
188
void mmDestroy(memHeap_t *heap)
194
p = (TMemBlock *)heap;