1 /*
2 	Copyright (C) 2005-2007 Feeling Software Inc.
3 	Portions of the code are:
4 	Copyright (C) 2005-2007 Sony Computer Entertainment America
5 
6 	MIT License: http://www.opensource.org/licenses/mit-license.php
7 */
8 
9 /**
10 	@file FMAllocator.h
11 	The file contains the simple_allocator class and some export memory management functions.
12  */
13 
14 #ifndef _FM_ALLOCATOR_H_
15 #define _FM_ALLOCATOR_H_
16 
17 #include <new>
18 
19 #ifdef new
20 #define _OLD_NEW new
21 #undef new
22 #endif // new
23 
24 namespace fm
25 {
26 	/** An allocation function type.
27 		@param size The size, in bytes, to allocate.
28 		@return The allocated buffer or NULL if not enough memory is available.*/
29 	typedef void* (*AllocateFunc)(size_t size);
30 
31 	/** A deallocation function type.
32 		@param buffer The memory buffer to deallocate.*/
33 	typedef void (*FreeFunc)(void* buffer);
34 
35 	/** Sets the FCollada memory allocation / deallocation functions.
36 		@param a The Allocation function. Defaults to malloc.
37 		@param f The Deallocation function. Defaults to free.*/
38 	FCOLLADA_EXPORT void SetAllocationFunctions(AllocateFunc a, FreeFunc f);
39 
40 	/** Allocates a requested amount of memory.
41 		@param byteCount The amount of memory to allocate, in bytes.
42 		@return A pointer to the memory address. This pointer will be NULL if there is not
43 			enough memory to allocate. */
44 	FCOLLADA_EXPORT void* Allocate(size_t byteCount);
45 
46 	/** Releases a memory buffer.
47 		@param buffer The memory buffer to release. */
48 	FCOLLADA_EXPORT void Release(void* buffer);
49 
50 	/** Construct the object at a given pointer.
51 		@param o A pointer to the object. */
52 	template <class Type1>
Construct(Type1 * o)53 	inline void Construct(Type1* o)
54 	{
55 		::new (o) Type1;
56 	}
57 
58 	/** Construct the object at a given pointer.
59 		@param o A pointer to the object.
60 		@param value The value to copy. */
61 	template <class Type1, class Type2>
Construct(Type1 * o,const Type2 & value)62 	inline void Construct(Type1* o, const Type2& value)
63 	{
64 		::new (o) Type1(value);
65 	}
66 };
67 
68 #ifdef _OLD_NEW
69 #define new _OLD_NEW
70 #undef _OLD_NEW
71 #endif // _OLD_NEW
72 
73 #endif // _FM_ALLOCATOR_H_
74