Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_oct_alloc_h)
00024 #define octave_oct_alloc_h 1
00025
00026 #include <cstddef>
00027
00028 class
00029 OCTAVE_API
00030 octave_allocator
00031 {
00032 public:
00033
00034 octave_allocator (size_t item_sz, int grow_sz = 256)
00035 : head (0), grow_size (grow_sz),
00036 item_size (item_sz > sizeof (link *) ? item_sz : sizeof (link *))
00037 { }
00038
00039
00040
00041 void *alloc (size_t size);
00042
00043
00044 void free (void *p, size_t size);
00045
00046 private:
00047
00048
00049 struct link { link *next; };
00050
00051
00052 link *head;
00053
00054
00055 int grow_size;
00056
00057
00058
00059 size_t item_size;
00060
00061
00062 bool grow (void);
00063 };
00064
00065 #if defined (HAVE_PLACEMENT_DELETE)
00066 #define DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \
00067 void operator delete (void *p, void *) \
00068 { ::operator delete (p, static_cast<void*> (0)); }
00069 #else
00070 #define DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \
00071 void operator delete (void *p, void *) \
00072 { ::operator delete (p); }
00073 #endif
00074
00075 #if defined (USE_OCTAVE_ALLOCATOR)
00076
00077 #define DECLARE_OCTAVE_ALLOCATOR \
00078 public: \
00079 void *operator new (size_t size, void *p) \
00080 { return ::operator new (size, p); } \
00081 DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \
00082 void *operator new (size_t size) { return allocator.alloc (size); } \
00083 void operator delete (void *p, size_t size) { allocator.free (p, size); } \
00084 private: \
00085 static octave_allocator allocator;
00086
00087 #define DEFINE_OCTAVE_ALLOCATOR(t) \
00088 octave_allocator t::allocator (sizeof (t))
00089
00090 #define DEFINE_OCTAVE_ALLOCATOR2(t, s) \
00091 octave_allocator t::allocator (sizeof (t), s)
00092
00093 #else
00094
00095 #define DECLARE_OCTAVE_ALLOCATOR
00096 #define DEFINE_OCTAVE_ALLOCATOR(t)
00097 #define DEFINE_OCTAVE_ALLOCATOR2(t, s)
00098
00099 #endif
00100
00101 #endif