1cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson/**************************************************************************
2cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson *
3cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * Copyright (C) 1999 Wittawat Yamwong
4cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson *
5cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * Permission is hereby granted, free of charge, to any person obtaining a
6cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * copy of this software and associated documentation files (the "Software"),
7cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * to deal in the Software without restriction, including without limitation
8cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * and/or sell copies of the Software, and to permit persons to whom the
10cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * Software is furnished to do so, subject to the following conditions:
11cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson *
12cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * The above copyright notice and this permission notice shall be included
13cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * in all copies or substantial portions of the Software.
14cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson *
15cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson *
23cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson **************************************************************************/
24cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
25cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
26cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson/**
27cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * @file
28cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * Memory manager code.  Primarily used by device drivers to manage texture
29cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * heaps, etc.
30cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson */
31cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
32cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
33cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson#ifndef _U_MM_H_
34cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson#define _U_MM_H_
35cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
36cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
37f434e07a6ac849cd127d70eee8b3349da7cfb360Ian Romanickstruct mem_block {
38cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson   struct mem_block *next, *prev;
39cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson   struct mem_block *next_free, *prev_free;
40cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson   struct mem_block *heap;
41cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson   int ofs,size;
42cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson   unsigned int free:1;
4364d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf   unsigned int reserved:1;
4464d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf};
45cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
4664d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf
4764d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf
4864d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf/**
4964d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * input: total size in bytes
5064d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * return: a heap pointer if OK, NULL if error
5164d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf */
5264d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristófextern struct mem_block *u_mmInit(int ofs, int size);
5364d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf
5464d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf/**
5564d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * Allocate 'size' bytes with 2^align2 bytes alignment,
5664d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * restrict the search to free memory after 'startSearch'
5764d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * depth and back buffers should be in different 4mb banks
5864d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * to get better page hits if possible
5964d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * input:	size = size of block
6064d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf *       	align2 = 2^align2 bytes alignment
6164d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf *		startSearch = linear offset from start of heap to begin search
6264d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * return: pointer to the allocated block, 0 if error
6364d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf */
6464d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristófextern struct mem_block *u_mmAllocMem(struct mem_block *heap, int size, int align2,
6564d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf                            int startSearch);
6664d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf
6764d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf/**
68cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * Free block starts at offset
69cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * input: pointer to a block
70cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * return: 0 if OK, -1 if error
71cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson */
72cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jacksonextern int u_mmFreeMem(struct mem_block *b);
73cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson
74cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson/**
75cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson * Free block starts at offset
7664d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * input: pointer to a heap, start offset
7764d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * return: pointer to a block
7864d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf */
7964d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristófextern struct mem_block *u_mmFindBlock(struct mem_block *heap, int start);
8064d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf
81cb3610e37c4c0a40520441b8515d044dabcc8854Adam Jackson/**
8264d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * destroy MM
8364d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf */
8464d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristófextern void u_mmDestroy(struct mem_block *mmInit);
8564d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf
8664d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf/**
8764d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf * For debugging purposes.
8864d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf */
8964d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristófextern void u_mmDumpMemInfo(const struct mem_block *mmInit);
9064d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf
9164d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf#endif
9264d1c10e6c471b81da383b9900d2fc98a7e873d0RALOVICH, Kristóf