193a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell/*
293a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * GLX Hardware Device Driver common code
3aae2b8b8eba46594d0915725c57f45d2965b15d6Keith Whitwell * Copyright (C) 1999 Wittawat Yamwong
493a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell *
593a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
693a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * copy of this software and associated documentation files (the "Software"),
793a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * to deal in the Software without restriction, including without limitation
893a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
993a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
1093a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * Software is furnished to do so, subject to the following conditions:
1193a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell *
1293a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * The above copyright notice and this permission notice shall be included
1393a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * in all copies or substantial portions of the Software.
1493a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell *
1593a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1693a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1793a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1893a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
1993a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2093a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
2193a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2293a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell */
2393a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell
241c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
251c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul/**
261c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul * Memory manager code.  Primarily used by device drivers to manage texture
271c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul * heaps, etc.
281c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul */
291c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
301c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
311c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul#ifndef MM_H
321c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul#define MM_H
331c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
341c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
352b8e66d210c333c1f9bdb4e2de079798f1c810f1Keith Whitwellstruct mem_block {
367e3fa7e837aaf687996abdd8511f6bf32e2c9097Keith Whitwell   struct mem_block *next, *prev;
377e3fa7e837aaf687996abdd8511f6bf32e2c9097Keith Whitwell   struct mem_block *next_free, *prev_free;
387e3fa7e837aaf687996abdd8511f6bf32e2c9097Keith Whitwell   struct mem_block *heap;
3960325331a8a76f3c783284c65b81cee471d7d64cJosé Fonseca   unsigned ofs;
4060325331a8a76f3c783284c65b81cee471d7d64cJosé Fonseca   unsigned size;
4160325331a8a76f3c783284c65b81cee471d7d64cJosé Fonseca   unsigned free:1;
4260325331a8a76f3c783284c65b81cee471d7d64cJosé Fonseca   unsigned reserved:1;
4393a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell};
441c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
451c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
461c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul
471c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul/**
4893a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * input: total size in bytes
4993a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * return: a heap pointer if OK, NULL if error
5093a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell */
5160325331a8a76f3c783284c65b81cee471d7d64cJosé Fonsecaextern struct mem_block *mmInit(unsigned ofs, unsigned size);
5293a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell
531c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul/**
5493a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * Allocate 'size' bytes with 2^align2 bytes alignment,
5593a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * restrict the search to free memory after 'startSearch'
5693a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * depth and back buffers should be in different 4mb banks
5793a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * to get better page hits if possible
5893a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * input:	size = size of block
5993a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell *       	align2 = 2^align2 bytes alignment
6093a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell *		startSearch = linear offset from start of heap to begin search
6193a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * return: pointer to the allocated block, 0 if error
6293a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell */
6360325331a8a76f3c783284c65b81cee471d7d64cJosé Fonsecaextern struct mem_block *mmAllocMem(struct mem_block *heap, unsigned size,
6460325331a8a76f3c783284c65b81cee471d7d64cJosé Fonseca                                    unsigned align2, unsigned startSearch);
6593a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell
661c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul/**
6793a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * Free block starts at offset
6893a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * input: pointer to a block
6993a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * return: 0 if OK, -1 if error
7093a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell */
712b8e66d210c333c1f9bdb4e2de079798f1c810f1Keith Whitwellextern int mmFreeMem(struct mem_block *b);
7293a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell
731c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul/**
74005469005df6ba5f80e382d5371c6d069c27738bKeith Whitwell * Free block starts at offset
75005469005df6ba5f80e382d5371c6d069c27738bKeith Whitwell * input: pointer to a heap, start offset
76005469005df6ba5f80e382d5371c6d069c27738bKeith Whitwell * return: pointer to a block
77005469005df6ba5f80e382d5371c6d069c27738bKeith Whitwell */
7860325331a8a76f3c783284c65b81cee471d7d64cJosé Fonsecaextern struct mem_block *mmFindBlock(struct mem_block *heap, unsigned start);
79005469005df6ba5f80e382d5371c6d069c27738bKeith Whitwell
801c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul/**
8193a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell * destroy MM
8293a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell */
832b8e66d210c333c1f9bdb4e2de079798f1c810f1Keith Whitwellextern void mmDestroy(struct mem_block *mmInit);
8493a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell
851c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul/**
861c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul * For debuging purpose.
871c236fd5c734b7388d7625ce8c3d4b2d6da9fcfbBrian Paul */
882b8e66d210c333c1f9bdb4e2de079798f1c810f1Keith Whitwellextern void mmDumpMemInfo(const struct mem_block *mmInit);
8993a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell
9093a458840c77b784fb74738f734ba2c5d22ca7a7Keith Whitwell#endif
91