/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/core/memory/allocscratch.cpp

*/

#include "sgx/core/core.hpp"

CSGXMemoryAllocatorScratch::CSGXMemoryAllocatorScratch(void *ptr, const tMEM_SIZE size, const CSGXMemoryAlignment &alignment) :
	CSGXMemoryAllocator(ptr, size, alignment) {
	
	m_pBlock = ptr; 
	m_iBlockSize = size; 
	m_bLocked = FALSE; 
}
 
void *CSGXMemoryAllocatorScratch::alloc(const tMEM_SIZE size) {
	if (m_bLocked) {
		sgxError("You can allocate from this scratch block, since it's already locked");
	}
 
	if (size > m_iBlockSize) {
		sgxError("You can allocate %ld bytes from this scratch block, since it's only %ld in size", size, m_iBlockSize);
	}
 
	m_bLocked = TRUE;
 
	return m_pBlock;
}
 
tBOOL CSGXMemoryAllocatorScratch::freePtr(void *ptr) {
	if (ptr == m_pBlock) {
		m_bLocked = FALSE;
		return TRUE;
	}
	return FALSE;
}
 
tBOOL CSGXMemoryAllocatorScratch::freeAll() {
	m_bLocked = FALSE;
	return TRUE;
}
 
