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