/****************************************************************************** libx42 - skinned vertex animation library Copyright (C) 2007 HermitWorks Entertainment Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA. ******************************************************************************/ #include "local.h" #ifndef LIBX42_MINIMIZE_CRT #include typedef struct tmpMemInt_t { byte *pBase; byte *pCurr; byte *pTop; } tmpMemInt_t; void* X42_CALL tmpMem_alloc( size_t cb, void *baton ) { byte *ret; tmpMemInt_t *tmpi = (tmpMemInt_t*)baton; cb = align( cb, sizeof( byte* ) ); #ifdef DEBUG cb += sizeof( byte* ); #endif if( tmpi->pCurr + cb >= tmpi->pTop ) return NULL; ret = tmpi->pCurr; tmpi->pCurr += cb; #ifdef DEBUG *(byte**)(tmpi->pCurr - sizeof( byte* )) = ret; #endif return ret; } void X42_CALL tmpMem_free( void *p, void *baton ) { tmpMemInt_t *tmpi = (tmpMemInt_t*)baton; if( !p ) return; #ifdef DEBUG assert( *(byte**)(tmpi->pCurr - sizeof( byte* )) == (byte*)p, X42_ERR_BADPTR, "Mismatched alloc/free pair." ); #endif tmpi->pCurr = (byte*)p; } //create a temp memory allocator, set max_size to zero for the default (512K) X42_EXPORT x42tempMem_t* X42_CALL x42_CreateTempMem( size_t max_size ) { size_t mem_size; x42tempMem_t *ret; tmpMemInt_t *tmpi; if( !max_size ) max_size = 512 * 1024; mem_size = sizeof( x42tempMem_t ) + sizeof( tmpMemInt_t ) + max_size; ret = (x42tempMem_t*)malloc( mem_size ); demand_rn( ret != NULL, X42_ERR_OUTOFMEMORY, "can't allocate on the heap" ); tmpi = (tmpMemInt_t*)(ret + 1); tmpi->pBase = (byte*)(tmpi + 1); tmpi->pCurr = tmpi->pBase; tmpi->pTop = tmpi->pBase + mem_size; ret->alloc = tmpMem_alloc; ret->free = tmpMem_free; ret->baton = (void*)tmpi; return ret; } X42_EXPORT void X42_CALL x42_FreeTempMem( x42tempMem_t *mem ) { free( mem ); } #else #ifdef _MSC_VER #pragma warning( disable : 4206 ) //nonstandard extension: empty tanslation unit #endif #endif