1d722e3fbSopenharmony_ci/* 2d722e3fbSopenharmony_ci * GLX Hardware Device Driver common code 3d722e3fbSopenharmony_ci * Copyright (C) 1999 Wittawat Yamwong 4d722e3fbSopenharmony_ci * 5d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 6d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"), 7d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation 8d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 10d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions: 11d722e3fbSopenharmony_ci * 12d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included 13d722e3fbSopenharmony_ci * in all copies or substantial portions of the Software. 14d722e3fbSopenharmony_ci * 15d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16d722e3fbSopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18d722e3fbSopenharmony_ci * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM, 19d722e3fbSopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20d722e3fbSopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 21d722e3fbSopenharmony_ci * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22d722e3fbSopenharmony_ci * 23d722e3fbSopenharmony_ci */ 24d722e3fbSopenharmony_ci 25d722e3fbSopenharmony_ci#include <stdlib.h> 26d722e3fbSopenharmony_ci#include <assert.h> 27d722e3fbSopenharmony_ci 28d722e3fbSopenharmony_ci#include "xf86drm.h" 29d722e3fbSopenharmony_ci#include "libdrm_macros.h" 30d722e3fbSopenharmony_ci#include "mm.h" 31d722e3fbSopenharmony_ci 32d722e3fbSopenharmony_cidrm_private void mmDumpMemInfo(const struct mem_block *heap) 33d722e3fbSopenharmony_ci{ 34d722e3fbSopenharmony_ci drmMsg("Memory heap %p:\n", (void *)heap); 35d722e3fbSopenharmony_ci if (heap == 0) { 36d722e3fbSopenharmony_ci drmMsg(" heap == 0\n"); 37d722e3fbSopenharmony_ci } else { 38d722e3fbSopenharmony_ci const struct mem_block *p; 39d722e3fbSopenharmony_ci 40d722e3fbSopenharmony_ci for (p = heap->next; p != heap; p = p->next) { 41d722e3fbSopenharmony_ci drmMsg(" Offset:%08x, Size:%08x, %c%c\n", p->ofs, 42d722e3fbSopenharmony_ci p->size, p->free ? 'F' : '.', 43d722e3fbSopenharmony_ci p->reserved ? 'R' : '.'); 44d722e3fbSopenharmony_ci } 45d722e3fbSopenharmony_ci 46d722e3fbSopenharmony_ci drmMsg("\nFree list:\n"); 47d722e3fbSopenharmony_ci 48d722e3fbSopenharmony_ci for (p = heap->next_free; p != heap; p = p->next_free) { 49d722e3fbSopenharmony_ci drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n", p->ofs, 50d722e3fbSopenharmony_ci p->size, p->free ? 'F' : '.', 51d722e3fbSopenharmony_ci p->reserved ? 'R' : '.'); 52d722e3fbSopenharmony_ci } 53d722e3fbSopenharmony_ci 54d722e3fbSopenharmony_ci } 55d722e3fbSopenharmony_ci drmMsg("End of memory blocks\n"); 56d722e3fbSopenharmony_ci} 57d722e3fbSopenharmony_ci 58d722e3fbSopenharmony_cidrm_private struct mem_block *mmInit(int ofs, int size) 59d722e3fbSopenharmony_ci{ 60d722e3fbSopenharmony_ci struct mem_block *heap, *block; 61d722e3fbSopenharmony_ci 62d722e3fbSopenharmony_ci if (size <= 0) 63d722e3fbSopenharmony_ci return NULL; 64d722e3fbSopenharmony_ci 65d722e3fbSopenharmony_ci heap = (struct mem_block *)calloc(1, sizeof(struct mem_block)); 66d722e3fbSopenharmony_ci if (!heap) 67d722e3fbSopenharmony_ci return NULL; 68d722e3fbSopenharmony_ci 69d722e3fbSopenharmony_ci block = (struct mem_block *)calloc(1, sizeof(struct mem_block)); 70d722e3fbSopenharmony_ci if (!block) { 71d722e3fbSopenharmony_ci free(heap); 72d722e3fbSopenharmony_ci return NULL; 73d722e3fbSopenharmony_ci } 74d722e3fbSopenharmony_ci 75d722e3fbSopenharmony_ci heap->next = block; 76d722e3fbSopenharmony_ci heap->prev = block; 77d722e3fbSopenharmony_ci heap->next_free = block; 78d722e3fbSopenharmony_ci heap->prev_free = block; 79d722e3fbSopenharmony_ci 80d722e3fbSopenharmony_ci block->heap = heap; 81d722e3fbSopenharmony_ci block->next = heap; 82d722e3fbSopenharmony_ci block->prev = heap; 83d722e3fbSopenharmony_ci block->next_free = heap; 84d722e3fbSopenharmony_ci block->prev_free = heap; 85d722e3fbSopenharmony_ci 86d722e3fbSopenharmony_ci block->ofs = ofs; 87d722e3fbSopenharmony_ci block->size = size; 88d722e3fbSopenharmony_ci block->free = 1; 89d722e3fbSopenharmony_ci 90d722e3fbSopenharmony_ci return heap; 91d722e3fbSopenharmony_ci} 92d722e3fbSopenharmony_ci 93d722e3fbSopenharmony_cistatic struct mem_block *SliceBlock(struct mem_block *p, 94d722e3fbSopenharmony_ci int startofs, int size, 95d722e3fbSopenharmony_ci int reserved, int alignment) 96d722e3fbSopenharmony_ci{ 97d722e3fbSopenharmony_ci struct mem_block *newblock; 98d722e3fbSopenharmony_ci 99d722e3fbSopenharmony_ci /* break left [p, newblock, p->next], then p = newblock */ 100d722e3fbSopenharmony_ci if (startofs > p->ofs) { 101d722e3fbSopenharmony_ci newblock = 102d722e3fbSopenharmony_ci (struct mem_block *)calloc(1, sizeof(struct mem_block)); 103d722e3fbSopenharmony_ci if (!newblock) 104d722e3fbSopenharmony_ci return NULL; 105d722e3fbSopenharmony_ci newblock->ofs = startofs; 106d722e3fbSopenharmony_ci newblock->size = p->size - (startofs - p->ofs); 107d722e3fbSopenharmony_ci newblock->free = 1; 108d722e3fbSopenharmony_ci newblock->heap = p->heap; 109d722e3fbSopenharmony_ci 110d722e3fbSopenharmony_ci newblock->next = p->next; 111d722e3fbSopenharmony_ci newblock->prev = p; 112d722e3fbSopenharmony_ci p->next->prev = newblock; 113d722e3fbSopenharmony_ci p->next = newblock; 114d722e3fbSopenharmony_ci 115d722e3fbSopenharmony_ci newblock->next_free = p->next_free; 116d722e3fbSopenharmony_ci newblock->prev_free = p; 117d722e3fbSopenharmony_ci p->next_free->prev_free = newblock; 118d722e3fbSopenharmony_ci p->next_free = newblock; 119d722e3fbSopenharmony_ci 120d722e3fbSopenharmony_ci p->size -= newblock->size; 121d722e3fbSopenharmony_ci p = newblock; 122d722e3fbSopenharmony_ci } 123d722e3fbSopenharmony_ci 124d722e3fbSopenharmony_ci /* break right, also [p, newblock, p->next] */ 125d722e3fbSopenharmony_ci if (size < p->size) { 126d722e3fbSopenharmony_ci newblock = 127d722e3fbSopenharmony_ci (struct mem_block *)calloc(1, sizeof(struct mem_block)); 128d722e3fbSopenharmony_ci if (!newblock) 129d722e3fbSopenharmony_ci return NULL; 130d722e3fbSopenharmony_ci newblock->ofs = startofs + size; 131d722e3fbSopenharmony_ci newblock->size = p->size - size; 132d722e3fbSopenharmony_ci newblock->free = 1; 133d722e3fbSopenharmony_ci newblock->heap = p->heap; 134d722e3fbSopenharmony_ci 135d722e3fbSopenharmony_ci newblock->next = p->next; 136d722e3fbSopenharmony_ci newblock->prev = p; 137d722e3fbSopenharmony_ci p->next->prev = newblock; 138d722e3fbSopenharmony_ci p->next = newblock; 139d722e3fbSopenharmony_ci 140d722e3fbSopenharmony_ci newblock->next_free = p->next_free; 141d722e3fbSopenharmony_ci newblock->prev_free = p; 142d722e3fbSopenharmony_ci p->next_free->prev_free = newblock; 143d722e3fbSopenharmony_ci p->next_free = newblock; 144d722e3fbSopenharmony_ci 145d722e3fbSopenharmony_ci p->size = size; 146d722e3fbSopenharmony_ci } 147d722e3fbSopenharmony_ci 148d722e3fbSopenharmony_ci /* p = middle block */ 149d722e3fbSopenharmony_ci p->free = 0; 150d722e3fbSopenharmony_ci 151d722e3fbSopenharmony_ci /* Remove p from the free list: 152d722e3fbSopenharmony_ci */ 153d722e3fbSopenharmony_ci p->next_free->prev_free = p->prev_free; 154d722e3fbSopenharmony_ci p->prev_free->next_free = p->next_free; 155d722e3fbSopenharmony_ci 156d722e3fbSopenharmony_ci p->next_free = 0; 157d722e3fbSopenharmony_ci p->prev_free = 0; 158d722e3fbSopenharmony_ci 159d722e3fbSopenharmony_ci p->reserved = reserved; 160d722e3fbSopenharmony_ci return p; 161d722e3fbSopenharmony_ci} 162d722e3fbSopenharmony_ci 163d722e3fbSopenharmony_cidrm_private struct mem_block *mmAllocMem(struct mem_block *heap, int size, 164d722e3fbSopenharmony_ci int align2, int startSearch) 165d722e3fbSopenharmony_ci{ 166d722e3fbSopenharmony_ci struct mem_block *p; 167d722e3fbSopenharmony_ci const int mask = (1 << align2) - 1; 168d722e3fbSopenharmony_ci int startofs = 0; 169d722e3fbSopenharmony_ci int endofs; 170d722e3fbSopenharmony_ci 171d722e3fbSopenharmony_ci if (!heap || align2 < 0 || size <= 0) 172d722e3fbSopenharmony_ci return NULL; 173d722e3fbSopenharmony_ci 174d722e3fbSopenharmony_ci for (p = heap->next_free; p != heap; p = p->next_free) { 175d722e3fbSopenharmony_ci assert(p->free); 176d722e3fbSopenharmony_ci 177d722e3fbSopenharmony_ci startofs = (p->ofs + mask) & ~mask; 178d722e3fbSopenharmony_ci if (startofs < startSearch) { 179d722e3fbSopenharmony_ci startofs = startSearch; 180d722e3fbSopenharmony_ci } 181d722e3fbSopenharmony_ci endofs = startofs + size; 182d722e3fbSopenharmony_ci if (endofs <= (p->ofs + p->size)) 183d722e3fbSopenharmony_ci break; 184d722e3fbSopenharmony_ci } 185d722e3fbSopenharmony_ci 186d722e3fbSopenharmony_ci if (p == heap) 187d722e3fbSopenharmony_ci return NULL; 188d722e3fbSopenharmony_ci 189d722e3fbSopenharmony_ci assert(p->free); 190d722e3fbSopenharmony_ci p = SliceBlock(p, startofs, size, 0, mask + 1); 191d722e3fbSopenharmony_ci 192d722e3fbSopenharmony_ci return p; 193d722e3fbSopenharmony_ci} 194d722e3fbSopenharmony_ci 195d722e3fbSopenharmony_cistatic int Join2Blocks(struct mem_block *p) 196d722e3fbSopenharmony_ci{ 197d722e3fbSopenharmony_ci /* XXX there should be some assertions here */ 198d722e3fbSopenharmony_ci 199d722e3fbSopenharmony_ci /* NOTE: heap->free == 0 */ 200d722e3fbSopenharmony_ci 201d722e3fbSopenharmony_ci if (p->free && p->next->free) { 202d722e3fbSopenharmony_ci struct mem_block *q = p->next; 203d722e3fbSopenharmony_ci 204d722e3fbSopenharmony_ci assert(p->ofs + p->size == q->ofs); 205d722e3fbSopenharmony_ci p->size += q->size; 206d722e3fbSopenharmony_ci 207d722e3fbSopenharmony_ci p->next = q->next; 208d722e3fbSopenharmony_ci q->next->prev = p; 209d722e3fbSopenharmony_ci 210d722e3fbSopenharmony_ci q->next_free->prev_free = q->prev_free; 211d722e3fbSopenharmony_ci q->prev_free->next_free = q->next_free; 212d722e3fbSopenharmony_ci 213d722e3fbSopenharmony_ci free(q); 214d722e3fbSopenharmony_ci return 1; 215d722e3fbSopenharmony_ci } 216d722e3fbSopenharmony_ci return 0; 217d722e3fbSopenharmony_ci} 218d722e3fbSopenharmony_ci 219d722e3fbSopenharmony_cidrm_private int mmFreeMem(struct mem_block *b) 220d722e3fbSopenharmony_ci{ 221d722e3fbSopenharmony_ci if (!b) 222d722e3fbSopenharmony_ci return 0; 223d722e3fbSopenharmony_ci 224d722e3fbSopenharmony_ci if (b->free) { 225d722e3fbSopenharmony_ci drmMsg("block already free\n"); 226d722e3fbSopenharmony_ci return -1; 227d722e3fbSopenharmony_ci } 228d722e3fbSopenharmony_ci if (b->reserved) { 229d722e3fbSopenharmony_ci drmMsg("block is reserved\n"); 230d722e3fbSopenharmony_ci return -1; 231d722e3fbSopenharmony_ci } 232d722e3fbSopenharmony_ci 233d722e3fbSopenharmony_ci b->free = 1; 234d722e3fbSopenharmony_ci b->next_free = b->heap->next_free; 235d722e3fbSopenharmony_ci b->prev_free = b->heap; 236d722e3fbSopenharmony_ci b->next_free->prev_free = b; 237d722e3fbSopenharmony_ci b->prev_free->next_free = b; 238d722e3fbSopenharmony_ci 239d722e3fbSopenharmony_ci Join2Blocks(b); 240d722e3fbSopenharmony_ci if (b->prev != b->heap) 241d722e3fbSopenharmony_ci Join2Blocks(b->prev); 242d722e3fbSopenharmony_ci 243d722e3fbSopenharmony_ci return 0; 244d722e3fbSopenharmony_ci} 245d722e3fbSopenharmony_ci 246d722e3fbSopenharmony_cidrm_private void mmDestroy(struct mem_block *heap) 247d722e3fbSopenharmony_ci{ 248d722e3fbSopenharmony_ci struct mem_block *p; 249d722e3fbSopenharmony_ci 250d722e3fbSopenharmony_ci if (!heap) 251d722e3fbSopenharmony_ci return; 252d722e3fbSopenharmony_ci 253d722e3fbSopenharmony_ci for (p = heap->next; p != heap;) { 254d722e3fbSopenharmony_ci struct mem_block *next = p->next; 255d722e3fbSopenharmony_ci free(p); 256d722e3fbSopenharmony_ci p = next; 257d722e3fbSopenharmony_ci } 258d722e3fbSopenharmony_ci 259d722e3fbSopenharmony_ci free(heap); 260d722e3fbSopenharmony_ci} 261