Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GLX Hardware Device Driver common code |
| 3 | * Copyright (C) 1999 Wittawat Yamwong |
| 4 | * |
| 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: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included |
| 13 | * in all copies or substantial portions of the Software. |
| 14 | * |
| 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. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
| 27 | |
| 28 | #include "xf86drm.h" |
| 29 | #include "mm.h" |
| 30 | |
| 31 | void |
Eric Anholt | 57b4c4c | 2008-06-11 15:58:33 -0700 | [diff] [blame] | 32 | mmDumpMemInfo(const struct mem_block *heap) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 33 | { |
| 34 | drmMsg("Memory heap %p:\n", (void *)heap); |
| 35 | if (heap == 0) { |
| 36 | drmMsg(" heap == 0\n"); |
| 37 | } else { |
| 38 | const struct mem_block *p; |
| 39 | |
| 40 | for(p = heap->next; p != heap; p = p->next) { |
| 41 | drmMsg(" Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size, |
| 42 | p->free ? 'F':'.', |
| 43 | p->reserved ? 'R':'.'); |
| 44 | } |
| 45 | |
| 46 | drmMsg("\nFree list:\n"); |
| 47 | |
| 48 | for(p = heap->next_free; p != heap; p = p->next_free) { |
| 49 | drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size, |
| 50 | p->free ? 'F':'.', |
| 51 | p->reserved ? 'R':'.'); |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | drmMsg("End of memory blocks\n"); |
| 56 | } |
| 57 | |
| 58 | struct mem_block * |
Eric Anholt | 57b4c4c | 2008-06-11 15:58:33 -0700 | [diff] [blame] | 59 | mmInit(int ofs, int size) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 60 | { |
| 61 | struct mem_block *heap, *block; |
| 62 | |
| 63 | if (size <= 0) |
| 64 | return NULL; |
| 65 | |
| 66 | heap = (struct mem_block *) calloc(1, sizeof(struct mem_block)); |
| 67 | if (!heap) |
| 68 | return NULL; |
| 69 | |
| 70 | block = (struct mem_block *) calloc(1, sizeof(struct mem_block)); |
| 71 | if (!block) { |
| 72 | free(heap); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | heap->next = block; |
| 77 | heap->prev = block; |
| 78 | heap->next_free = block; |
| 79 | heap->prev_free = block; |
| 80 | |
| 81 | block->heap = heap; |
| 82 | block->next = heap; |
| 83 | block->prev = heap; |
| 84 | block->next_free = heap; |
| 85 | block->prev_free = heap; |
| 86 | |
| 87 | block->ofs = ofs; |
| 88 | block->size = size; |
| 89 | block->free = 1; |
| 90 | |
| 91 | return heap; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static struct mem_block * |
| 96 | SliceBlock(struct mem_block *p, |
| 97 | int startofs, int size, |
| 98 | int reserved, int alignment) |
| 99 | { |
| 100 | struct mem_block *newblock; |
| 101 | |
| 102 | /* break left [p, newblock, p->next], then p = newblock */ |
| 103 | if (startofs > p->ofs) { |
| 104 | newblock = (struct mem_block*) calloc(1, sizeof(struct mem_block)); |
| 105 | if (!newblock) |
| 106 | return NULL; |
| 107 | newblock->ofs = startofs; |
| 108 | newblock->size = p->size - (startofs - p->ofs); |
| 109 | newblock->free = 1; |
| 110 | newblock->heap = p->heap; |
| 111 | |
| 112 | newblock->next = p->next; |
| 113 | newblock->prev = p; |
| 114 | p->next->prev = newblock; |
| 115 | p->next = newblock; |
| 116 | |
| 117 | newblock->next_free = p->next_free; |
| 118 | newblock->prev_free = p; |
| 119 | p->next_free->prev_free = newblock; |
| 120 | p->next_free = newblock; |
| 121 | |
| 122 | p->size -= newblock->size; |
| 123 | p = newblock; |
| 124 | } |
| 125 | |
| 126 | /* break right, also [p, newblock, p->next] */ |
| 127 | if (size < p->size) { |
| 128 | newblock = (struct mem_block*) calloc(1, sizeof(struct mem_block)); |
| 129 | if (!newblock) |
| 130 | return NULL; |
| 131 | newblock->ofs = startofs + size; |
| 132 | newblock->size = p->size - size; |
| 133 | newblock->free = 1; |
| 134 | newblock->heap = p->heap; |
| 135 | |
| 136 | newblock->next = p->next; |
| 137 | newblock->prev = p; |
| 138 | p->next->prev = newblock; |
| 139 | p->next = newblock; |
| 140 | |
| 141 | newblock->next_free = p->next_free; |
| 142 | newblock->prev_free = p; |
| 143 | p->next_free->prev_free = newblock; |
| 144 | p->next_free = newblock; |
| 145 | |
| 146 | p->size = size; |
| 147 | } |
| 148 | |
| 149 | /* p = middle block */ |
| 150 | p->free = 0; |
| 151 | |
| 152 | /* Remove p from the free list: |
| 153 | */ |
| 154 | p->next_free->prev_free = p->prev_free; |
| 155 | p->prev_free->next_free = p->next_free; |
| 156 | |
| 157 | p->next_free = 0; |
| 158 | p->prev_free = 0; |
| 159 | |
| 160 | p->reserved = reserved; |
| 161 | return p; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | struct mem_block * |
Eric Anholt | 57b4c4c | 2008-06-11 15:58:33 -0700 | [diff] [blame] | 166 | mmAllocMem(struct mem_block *heap, int size, int align2, int startSearch) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 167 | { |
| 168 | struct mem_block *p; |
| 169 | const int mask = (1 << align2)-1; |
| 170 | int startofs = 0; |
| 171 | int endofs; |
| 172 | |
| 173 | if (!heap || align2 < 0 || size <= 0) |
| 174 | return NULL; |
| 175 | |
| 176 | for (p = heap->next_free; p != heap; p = p->next_free) { |
| 177 | assert(p->free); |
| 178 | |
| 179 | startofs = (p->ofs + mask) & ~mask; |
| 180 | if ( startofs < startSearch ) { |
| 181 | startofs = startSearch; |
| 182 | } |
| 183 | endofs = startofs+size; |
| 184 | if (endofs <= (p->ofs+p->size)) |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | if (p == heap) |
| 189 | return NULL; |
| 190 | |
| 191 | assert(p->free); |
| 192 | p = SliceBlock(p,startofs,size,0,mask+1); |
| 193 | |
| 194 | return p; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | struct mem_block * |
Eric Anholt | 57b4c4c | 2008-06-11 15:58:33 -0700 | [diff] [blame] | 199 | mmFindBlock(struct mem_block *heap, int start) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 200 | { |
| 201 | struct mem_block *p; |
| 202 | |
| 203 | for (p = heap->next; p != heap; p = p->next) { |
| 204 | if (p->ofs == start) |
| 205 | return p; |
| 206 | } |
| 207 | |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | static int |
| 213 | Join2Blocks(struct mem_block *p) |
| 214 | { |
| 215 | /* XXX there should be some assertions here */ |
| 216 | |
| 217 | /* NOTE: heap->free == 0 */ |
| 218 | |
| 219 | if (p->free && p->next->free) { |
| 220 | struct mem_block *q = p->next; |
| 221 | |
| 222 | assert(p->ofs + p->size == q->ofs); |
| 223 | p->size += q->size; |
| 224 | |
| 225 | p->next = q->next; |
| 226 | q->next->prev = p; |
| 227 | |
| 228 | q->next_free->prev_free = q->prev_free; |
| 229 | q->prev_free->next_free = q->next_free; |
| 230 | |
| 231 | free(q); |
| 232 | return 1; |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int |
Eric Anholt | 57b4c4c | 2008-06-11 15:58:33 -0700 | [diff] [blame] | 238 | mmFreeMem(struct mem_block *b) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 239 | { |
| 240 | if (!b) |
| 241 | return 0; |
| 242 | |
| 243 | if (b->free) { |
| 244 | drmMsg("block already free\n"); |
| 245 | return -1; |
| 246 | } |
| 247 | if (b->reserved) { |
| 248 | drmMsg("block is reserved\n"); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | b->free = 1; |
| 253 | b->next_free = b->heap->next_free; |
| 254 | b->prev_free = b->heap; |
| 255 | b->next_free->prev_free = b; |
| 256 | b->prev_free->next_free = b; |
| 257 | |
| 258 | Join2Blocks(b); |
| 259 | if (b->prev != b->heap) |
| 260 | Join2Blocks(b->prev); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | void |
Eric Anholt | 57b4c4c | 2008-06-11 15:58:33 -0700 | [diff] [blame] | 267 | mmDestroy(struct mem_block *heap) |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 268 | { |
| 269 | struct mem_block *p; |
| 270 | |
| 271 | if (!heap) |
| 272 | return; |
| 273 | |
| 274 | for (p = heap->next; p != heap; ) { |
| 275 | struct mem_block *next = p->next; |
| 276 | free(p); |
| 277 | p = next; |
| 278 | } |
| 279 | |
| 280 | free(heap); |
| 281 | } |