mm.c revision e6188e58
122944501Smrg/* 222944501Smrg * GLX Hardware Device Driver common code 322944501Smrg * Copyright (C) 1999 Wittawat Yamwong 422944501Smrg * 522944501Smrg * Permission is hereby granted, free of charge, to any person obtaining a 622944501Smrg * copy of this software and associated documentation files (the "Software"), 722944501Smrg * to deal in the Software without restriction, including without limitation 822944501Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 922944501Smrg * and/or sell copies of the Software, and to permit persons to whom the 1022944501Smrg * Software is furnished to do so, subject to the following conditions: 1122944501Smrg * 1222944501Smrg * The above copyright notice and this permission notice shall be included 1322944501Smrg * in all copies or substantial portions of the Software. 1422944501Smrg * 1522944501Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1622944501Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1722944501Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1822944501Smrg * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM, 1922944501Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 2022944501Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 2122944501Smrg * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2222944501Smrg * 2322944501Smrg */ 2422944501Smrg 25e6188e58Smrg#ifdef HAVE_CONFIG_H 26e6188e58Smrg#include "config.h" 27e6188e58Smrg#endif 28e6188e58Smrg 2922944501Smrg#include <stdlib.h> 3022944501Smrg#include <assert.h> 3122944501Smrg 3222944501Smrg#include "xf86drm.h" 33e6188e58Smrg#include "libdrm_macros.h" 3422944501Smrg#include "mm.h" 3522944501Smrg 36e6188e58Smrgdrm_private void mmDumpMemInfo(const struct mem_block *heap) 3722944501Smrg{ 3822944501Smrg drmMsg("Memory heap %p:\n", (void *)heap); 3922944501Smrg if (heap == 0) { 4022944501Smrg drmMsg(" heap == 0\n"); 4122944501Smrg } else { 4222944501Smrg const struct mem_block *p; 4322944501Smrg 4422944501Smrg for (p = heap->next; p != heap; p = p->next) { 4522944501Smrg drmMsg(" Offset:%08x, Size:%08x, %c%c\n", p->ofs, 4622944501Smrg p->size, p->free ? 'F' : '.', 4722944501Smrg p->reserved ? 'R' : '.'); 4822944501Smrg } 4922944501Smrg 5022944501Smrg drmMsg("\nFree list:\n"); 5122944501Smrg 5222944501Smrg for (p = heap->next_free; p != heap; p = p->next_free) { 5322944501Smrg drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n", p->ofs, 5422944501Smrg p->size, p->free ? 'F' : '.', 5522944501Smrg p->reserved ? 'R' : '.'); 5622944501Smrg } 5722944501Smrg 5822944501Smrg } 5922944501Smrg drmMsg("End of memory blocks\n"); 6022944501Smrg} 6122944501Smrg 62e6188e58Smrgdrm_private struct mem_block *mmInit(int ofs, int size) 6322944501Smrg{ 6422944501Smrg struct mem_block *heap, *block; 6522944501Smrg 6622944501Smrg if (size <= 0) 6722944501Smrg return NULL; 6822944501Smrg 6922944501Smrg heap = (struct mem_block *)calloc(1, sizeof(struct mem_block)); 7022944501Smrg if (!heap) 7122944501Smrg return NULL; 7222944501Smrg 7322944501Smrg block = (struct mem_block *)calloc(1, sizeof(struct mem_block)); 7422944501Smrg if (!block) { 7522944501Smrg free(heap); 7622944501Smrg return NULL; 7722944501Smrg } 7822944501Smrg 7922944501Smrg heap->next = block; 8022944501Smrg heap->prev = block; 8122944501Smrg heap->next_free = block; 8222944501Smrg heap->prev_free = block; 8322944501Smrg 8422944501Smrg block->heap = heap; 8522944501Smrg block->next = heap; 8622944501Smrg block->prev = heap; 8722944501Smrg block->next_free = heap; 8822944501Smrg block->prev_free = heap; 8922944501Smrg 9022944501Smrg block->ofs = ofs; 9122944501Smrg block->size = size; 9222944501Smrg block->free = 1; 9322944501Smrg 9422944501Smrg return heap; 9522944501Smrg} 9622944501Smrg 9722944501Smrgstatic struct mem_block *SliceBlock(struct mem_block *p, 9822944501Smrg int startofs, int size, 9922944501Smrg int reserved, int alignment) 10022944501Smrg{ 10122944501Smrg struct mem_block *newblock; 10222944501Smrg 10322944501Smrg /* break left [p, newblock, p->next], then p = newblock */ 10422944501Smrg if (startofs > p->ofs) { 10522944501Smrg newblock = 10622944501Smrg (struct mem_block *)calloc(1, sizeof(struct mem_block)); 10722944501Smrg if (!newblock) 10822944501Smrg return NULL; 10922944501Smrg newblock->ofs = startofs; 11022944501Smrg newblock->size = p->size - (startofs - p->ofs); 11122944501Smrg newblock->free = 1; 11222944501Smrg newblock->heap = p->heap; 11322944501Smrg 11422944501Smrg newblock->next = p->next; 11522944501Smrg newblock->prev = p; 11622944501Smrg p->next->prev = newblock; 11722944501Smrg p->next = newblock; 11822944501Smrg 11922944501Smrg newblock->next_free = p->next_free; 12022944501Smrg newblock->prev_free = p; 12122944501Smrg p->next_free->prev_free = newblock; 12222944501Smrg p->next_free = newblock; 12322944501Smrg 12422944501Smrg p->size -= newblock->size; 12522944501Smrg p = newblock; 12622944501Smrg } 12722944501Smrg 12822944501Smrg /* break right, also [p, newblock, p->next] */ 12922944501Smrg if (size < p->size) { 13022944501Smrg newblock = 13122944501Smrg (struct mem_block *)calloc(1, sizeof(struct mem_block)); 13222944501Smrg if (!newblock) 13322944501Smrg return NULL; 13422944501Smrg newblock->ofs = startofs + size; 13522944501Smrg newblock->size = p->size - size; 13622944501Smrg newblock->free = 1; 13722944501Smrg newblock->heap = p->heap; 13822944501Smrg 13922944501Smrg newblock->next = p->next; 14022944501Smrg newblock->prev = p; 14122944501Smrg p->next->prev = newblock; 14222944501Smrg p->next = newblock; 14322944501Smrg 14422944501Smrg newblock->next_free = p->next_free; 14522944501Smrg newblock->prev_free = p; 14622944501Smrg p->next_free->prev_free = newblock; 14722944501Smrg p->next_free = newblock; 14822944501Smrg 14922944501Smrg p->size = size; 15022944501Smrg } 15122944501Smrg 15222944501Smrg /* p = middle block */ 15322944501Smrg p->free = 0; 15422944501Smrg 15522944501Smrg /* Remove p from the free list: 15622944501Smrg */ 15722944501Smrg p->next_free->prev_free = p->prev_free; 15822944501Smrg p->prev_free->next_free = p->next_free; 15922944501Smrg 16022944501Smrg p->next_free = 0; 16122944501Smrg p->prev_free = 0; 16222944501Smrg 16322944501Smrg p->reserved = reserved; 16422944501Smrg return p; 16522944501Smrg} 16622944501Smrg 167e6188e58Smrgdrm_private struct mem_block *mmAllocMem(struct mem_block *heap, int size, 168e6188e58Smrg int align2, int startSearch) 16922944501Smrg{ 17022944501Smrg struct mem_block *p; 17122944501Smrg const int mask = (1 << align2) - 1; 17222944501Smrg int startofs = 0; 17322944501Smrg int endofs; 17422944501Smrg 17522944501Smrg if (!heap || align2 < 0 || size <= 0) 17622944501Smrg return NULL; 17722944501Smrg 17822944501Smrg for (p = heap->next_free; p != heap; p = p->next_free) { 17922944501Smrg assert(p->free); 18022944501Smrg 18122944501Smrg startofs = (p->ofs + mask) & ~mask; 18222944501Smrg if (startofs < startSearch) { 18322944501Smrg startofs = startSearch; 18422944501Smrg } 18522944501Smrg endofs = startofs + size; 18622944501Smrg if (endofs <= (p->ofs + p->size)) 18722944501Smrg break; 18822944501Smrg } 18922944501Smrg 19022944501Smrg if (p == heap) 19122944501Smrg return NULL; 19222944501Smrg 19322944501Smrg assert(p->free); 19422944501Smrg p = SliceBlock(p, startofs, size, 0, mask + 1); 19522944501Smrg 19622944501Smrg return p; 19722944501Smrg} 19822944501Smrg 19922944501Smrgstatic int Join2Blocks(struct mem_block *p) 20022944501Smrg{ 20122944501Smrg /* XXX there should be some assertions here */ 20222944501Smrg 20322944501Smrg /* NOTE: heap->free == 0 */ 20422944501Smrg 20522944501Smrg if (p->free && p->next->free) { 20622944501Smrg struct mem_block *q = p->next; 20722944501Smrg 20822944501Smrg assert(p->ofs + p->size == q->ofs); 20922944501Smrg p->size += q->size; 21022944501Smrg 21122944501Smrg p->next = q->next; 21222944501Smrg q->next->prev = p; 21322944501Smrg 21422944501Smrg q->next_free->prev_free = q->prev_free; 21522944501Smrg q->prev_free->next_free = q->next_free; 21622944501Smrg 21722944501Smrg free(q); 21822944501Smrg return 1; 21922944501Smrg } 22022944501Smrg return 0; 22122944501Smrg} 22222944501Smrg 223e6188e58Smrgdrm_private int mmFreeMem(struct mem_block *b) 22422944501Smrg{ 22522944501Smrg if (!b) 22622944501Smrg return 0; 22722944501Smrg 22822944501Smrg if (b->free) { 22922944501Smrg drmMsg("block already free\n"); 23022944501Smrg return -1; 23122944501Smrg } 23222944501Smrg if (b->reserved) { 23322944501Smrg drmMsg("block is reserved\n"); 23422944501Smrg return -1; 23522944501Smrg } 23622944501Smrg 23722944501Smrg b->free = 1; 23822944501Smrg b->next_free = b->heap->next_free; 23922944501Smrg b->prev_free = b->heap; 24022944501Smrg b->next_free->prev_free = b; 24122944501Smrg b->prev_free->next_free = b; 24222944501Smrg 24322944501Smrg Join2Blocks(b); 24422944501Smrg if (b->prev != b->heap) 24522944501Smrg Join2Blocks(b->prev); 24622944501Smrg 24722944501Smrg return 0; 24822944501Smrg} 24922944501Smrg 250e6188e58Smrgdrm_private void mmDestroy(struct mem_block *heap) 25122944501Smrg{ 25222944501Smrg struct mem_block *p; 25322944501Smrg 25422944501Smrg if (!heap) 25522944501Smrg return; 25622944501Smrg 25722944501Smrg for (p = heap->next; p != heap;) { 25822944501Smrg struct mem_block *next = p->next; 25922944501Smrg free(p); 26022944501Smrg p = next; 26122944501Smrg } 26222944501Smrg 26322944501Smrg free(heap); 26422944501Smrg} 265