gatelib  2.1
g_cont_HeapAbstract.h
1 #pragma once
2 
3 #include "g_cont_common.h"
4 
5 namespace g
6 {
7 namespace cont
8 {
9 
10 #define EQUAL_TO_CAPACITY ((size_t)-1)
11 #define EQUAL_TO_CURRENT ((size_t)-1)
12 
13 G_EXC_DEFINE_MSG ( HeapReservationFailException , g::Exception , "Reservation of heap memory failed!" );
14 
15 // Abstract class for implementation of a heap ( an allocator )
16 // a user can specify a custom Heap by subclassing HeapAbstract into its Heap class, instance it ( as a global variable or a new created object )
17 // and pass it through the heap constructor parameter.
18 // If the heap is not specified, heap standard is used(a call to HeapAbstract::get_FromStandardPolicy () is made).
19 // By default standard policy consists of returning the default Heap, that implements reserve/unreserve/realloc methods with malloc,realloc,free
20 class G_LIB_ITEM HeapAbstract
21 {
22 public:
23  virtual ~HeapAbstract(){}
24 
25  // Reserve data for a no size-mutable object (scalar)
26  virtual void* reserveScalar ( size_t ) = 0;
27  // Unreserve data (both allocated as scalar and array)
28  virtual void unreserve ( void* ) = 0;
29  // reserves data_capacity bytes,
30  // Using reserveArray rather than reserveScalar suggests to the Heap
31  // that the space is to be allocated for a resizable array, that could be extended. Implementation are free
32  // to reserve a real amount of memory larger than data capacity in order to perform reallocated
33  virtual void* reserveArray ( size_t data_capacity , size_t suggested_capacity = EQUAL_TO_CAPACITY ) = 0;
34  //to be used in conjunction with reserve multiple,
35  //change the amount of data bytes previously allocated by reserveArray
36  virtual void* reallocArray ( void* , size_t current_data_size , size_t data_capacity = EQUAL_TO_CURRENT , size_t suggested_data_capacity = EQUAL_TO_CAPACITY ) = 0;
37 
38  template < class T > T* reserveScalarT ( ) { return (T*)reserveScalar ( sizeof(T) ); }
39  template < class T > T* reallocArrayT ( T* , size_t current_item_num , size_t item_capacity = EQUAL_TO_CURRENT, size_t suggested_item_capacity = EQUAL_TO_CAPACITY );
40  template < class T > T* reserveArrayT ( size_t item_capacity , size_t suggested_item_capacity = EQUAL_TO_CAPACITY );
41 
42  virtual size_t getLocationSize ( void* /*location*/ ) { G_EXC_UNIMPLEMENTED ( "size_t HeapAbstract::getLocationSize ( void* location )" ); return 0; }
43  virtual void* searchLocation ( size_t /*size*/ ) { G_EXC_UNIMPLEMENTED ( "void* HeapAbstract::searchLocation ( size_t size )" ); return 0; }
44 
45  //Resets the default Heap as per Standard Heap Policy ( if the standard policy is overriden, it could be ignored ).
46  static HeapAbstract* set_Default ( HeapAbstract* );
47  //Returns the default Heap as per Standard Heap Policy ( if the standard policy is overriden, it could be ignored ).
48  static HeapAbstract* get_Default ( );
49 
50  static HeapAbstract* heap_SimpleP ( );
51 };
52 
53 class G_LIB_ITEM HeapSimple : public HeapAbstract
54 {
55  friend class HeapAbstract;
56 public:
57  virtual ~HeapSimple(){}
58  virtual void* reserveScalar ( size_t s );
59  virtual void unreserve ( void* );
60 
61  virtual void* reserveArray ( size_t data_capacity , size_t suggested_capacity = EQUAL_TO_CAPACITY );
62  virtual void* reallocArray ( void* , size_t current_data_size , size_t aDataCapacity = EQUAL_TO_CURRENT , size_t suggested_data_capacity = EQUAL_TO_CAPACITY );
63 };
64 
65 template<class T> T* HeapAbstract::reserveArrayT ( size_t item_capacity , size_t suggested_item_capacity )
66 {
67  size_t data_cap = (item_capacity == EQUAL_TO_CURRENT )?EQUAL_TO_CURRENT: (item_capacity * sizeof(T));
68  size_t suggested_data_cap = (suggested_item_capacity == EQUAL_TO_CAPACITY )?EQUAL_TO_CAPACITY: (suggested_item_capacity * sizeof(T));
69 
70  return reinterpret_cast<T*> (reserveArray ( data_cap , suggested_data_cap ) );
71 }
72 
73 template < class T > T* HeapAbstract::reallocArrayT ( T* aOldData , size_t current_item_num , size_t item_capacity , size_t suggested_item_capacity )
74 {
75  size_t data_cap = (item_capacity == EQUAL_TO_CURRENT )?EQUAL_TO_CURRENT: (item_capacity * sizeof(T));
76  size_t suggested_data_cap = (suggested_item_capacity == EQUAL_TO_CAPACITY )?EQUAL_TO_CAPACITY: (suggested_item_capacity * sizeof(T));
77 
78  return reinterpret_cast<T*> (reallocArray ( (void*)aOldData , sizeof(T) * current_item_num , data_cap , suggested_data_cap ) );
79 }
80 
81 }//namespace g
82 }//namespace cont
#define G_EXC_DEFINE_MSG(aexctypename, abaseexctypename, amsg)
Definition: g_exception_macros.h:43
Definition: g.mthread.ThreadSimpleEvent.h:5
Definition: g_cont_HeapAbstract.h:53
Definition: g_cont_HeapAbstract.h:20
#define G_EXC_UNIMPLEMENTED(acontextstr)
Sets a function as unimplemented ( raises UnimplemetedException ).
Definition: g_exception_macros.h:137
Anchestor Exception class for g::lib.
Definition: g_Exception.h:17