block_allocator.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 
00033 #pragma once
00034 
00035 
00036 #include "../api_core.h"
00037 #include "sharedptr.h"
00038 #include "system.h"
00039 
00040 class CL_BlockAllocator_Impl;
00041 
00053 class CL_API_CORE CL_BlockAllocator
00054 {
00057 
00058 public:
00060         CL_BlockAllocator();
00061 
00062 
00066 
00067 public:
00068 
00069 
00073 
00074 public:
00076 
00079         void *allocate(int size);
00080 
00082 
00083         void free();
00084 
00086 
00087         template<typename Type>
00088         Type *new_obj()
00089         {
00090                 Type *data = (Type *) allocate(sizeof(Type));
00091                 CL_System::call_constructor<Type>(data);
00092                 return data;
00093         }
00094 
00096 
00098         template<typename Type, typename P1>
00099         Type *new_obj(P1 p1)
00100         {
00101                 Type *data = (Type *) allocate(sizeof(Type));
00102                 CL_System::call_constructor<Type, P1>(data, p1);
00103                 return data;
00104         }
00105 
00107 
00110         template<typename Type, typename P1, typename P2>
00111         Type *new_obj(P1 p1, P2 p2)
00112         {
00113                 Type *data = (Type *) allocate(sizeof(Type));
00114                 CL_System::call_constructor<Type, P1, P2>(data, p1, p2);
00115                 return data;
00116         }
00117 
00119 
00123         template<typename Type, typename P1, typename P2, typename P3>
00124         Type *new_obj(P1 p1, P2 p2, P3 p3)
00125         {
00126                 Type *data = (Type *) allocate(sizeof(Type));
00127                 CL_System::call_constructor<Type, P1, P2, P3>(data, p1, p2, p3);
00128                 return data;
00129         }
00130 
00132 
00137         template<typename Type, typename P1, typename P2, typename P3, typename P4>
00138         Type *new_obj(P1 p1, P2 p2, P3 p3, P4 p4)
00139         {
00140                 Type *data = (Type *) allocate(sizeof(Type));
00141                 CL_System::call_constructor<Type, P1, P2, P3, P4>(data, p1, p2, p3, p4);
00142                 return data;
00143         }
00144 
00146 
00152         template<typename Type, typename P1, typename P2, typename P3, typename P4, typename P5>
00153         Type *new_obj(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
00154         {
00155                 Type *data = (Type *) allocate(sizeof(Type));
00156                 CL_System::call_constructor<Type, P1, P2, P3, P4, P5>(data, p1, p2, p3, p4, p5);
00157                 return data;
00158         }
00159 
00161 
00168         template<typename Type, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
00169         Type *new_obj(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
00170         {
00171                 Type *data = (Type *) allocate(sizeof(Type));
00172                 CL_System::call_constructor<Type, P1, P2, P3, P4, P5, P6>(data, p1, p2, p3, p4, p5, p6);
00173                 return data;
00174         }
00175 
00177 
00185         template<typename Type, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
00186         Type *new_obj(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7)
00187         {
00188                 Type *data = (Type *) allocate(sizeof(Type));
00189                 CL_System::call_constructor<Type, P1, P2, P3, P4, P5, P6, P7>(data, p1, p2, p3, p4, p5, p6, p7);
00190                 return data;
00191         }
00192 
00194 
00196         template<typename Type>
00197         void delete_obj(Type *obj)
00198         {
00199                 CL_System::call_destructor(obj);
00200         }
00201 
00202 
00206 
00207 private:
00208         CL_SharedPtr<CL_BlockAllocator_Impl> impl;
00210 };
00211 
00224 class CL_BlockAllocated
00225 {
00228 
00229 public:
00230         void *operator new(size_t size, CL_BlockAllocator *allocator);
00231 
00232         void operator delete(void *data, size_t size);
00233 
00234         void operator delete(void *data, CL_BlockAllocator *allocator);
00236 };
00237 
00238 
00239