PageRenderTime 14ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/opengles/src/codegen/heap.c

http://ftk.googlecode.com/
C | 133 lines | 64 code | 32 blank | 37 comment | 5 complexity | 7eceb1a3d14f45ed05e20e440f75b32b MD5 | raw file
  1/****************************************************************************/
  2/*																			*/
  3/* Copyright (c) 2004, Hans-Martin Will. All rights reserved.				*/
  4/*																			*/
  5/* Redistribution and use in source and binary forms, with or without		*/
  6/* modification, are permitted provided that the following conditions are   */
  7/* met:																		*/
  8/*																			*/
  9/* *  Redistributions of source code must retain the above copyright		*/
 10/*    notice, this list of conditions and the following disclaimer.			*/
 11/*																			*/
 12/* *  Redistributions in binary form must reproduce the above copyright		*/
 13/*    notice, this list of conditions and the following disclaimer in the   */
 14/*    documentation and/or other materials provided with the distribution.  */
 15/*																			*/
 16/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS		*/
 17/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT		*/
 18/* LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A   */
 19/* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER */
 20/* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
 21/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,		*/
 22/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR		*/
 23/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   */
 24/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING		*/
 25/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS		*/
 26/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.				*/
 27/*																			*/
 28/****************************************************************************/
 29
 30
 31#include "heap.h"
 32
 33
 34#define HEAP_ALIGNMENT	8
 35
 36
 37typedef struct block_t 
 38{
 39	struct block_t * next;
 40	char * base;
 41	size_t total;
 42	size_t current;
 43} block_t;
 44
 45
 46struct cg_heap_t 
 47{
 48	block_t *	blocks;
 49	size_t		default_block_size;
 50};
 51
 52
 53static block_t * create_block(size_t block_size) 
 54{
 55	block_t * result = (block_t *) malloc(sizeof(block_t));
 56
 57	result->base = (char *) malloc(block_size);
 58	result->total = block_size;
 59	result->current = 0;
 60	result->next = (block_t *) 0;
 61
 62	return result;
 63}
 64
 65
 66cg_heap_t * cg_heap_create(size_t default_block_size)
 67	/************************************************************************/
 68	/* Create a new heap object.											*/
 69	/************************************************************************/
 70{
 71	cg_heap_t * heap = (cg_heap_t *) malloc(sizeof(cg_heap_t));
 72
 73	heap->blocks = create_block(default_block_size);
 74	heap->default_block_size = default_block_size;
 75
 76	return heap;
 77}
 78
 79
 80void cg_heap_destroy(cg_heap_t * heap)
 81	/************************************************************************/
 82	/* Destroy a heap and deallocate any memory belonging to it				*/
 83	/************************************************************************/
 84{
 85	block_t * current, *next;
 86
 87	for (current = heap->blocks; current != (block_t *) 0; current = next) 
 88	{
 89		next = current->next;
 90		free(current->base);
 91		free(current);
 92	}
 93
 94	free(heap);
 95}
 96
 97
 98
 99
100void * cg_heap_allocate(cg_heap_t * heap, size_t amount)
101	/************************************************************************/
102	/* Allocate a block of memory of the given size on the heap.			*/
103	/************************************************************************/
104{
105	void * result;
106
107	amount = (amount + HEAP_ALIGNMENT - 1) & ~(HEAP_ALIGNMENT - 1);
108
109	if (heap->blocks->total - heap->blocks->current < amount) 
110	{
111		block_t * new_block;
112
113		if (amount > heap->default_block_size) 
114		{ 
115			new_block = create_block(amount);
116		} 
117		else 
118		{
119			new_block = create_block(heap->default_block_size);
120		}
121
122		new_block->next = heap->blocks;
123		heap->blocks = new_block;
124	}
125
126	result = heap->blocks->base + heap->blocks->current;
127	heap->blocks->current += amount;
128
129	memset(result, 0, amount);
130	
131	return result;
132}
133