00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef SIMPLEALLOCATOR_H
00016 #define SIMPLEALLOCATOR_H
00017
00018 #include "pandabase.h"
00019 #include "linkedListNode.h"
00020 #include "pmutex.h"
00021 #include "mutexHolder.h"
00022
00023 class SimpleAllocatorBlock;
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class EXPCL_PANDA_GOBJ SimpleAllocator : public LinkedListNode {
00034 PUBLISHED:
00035 INLINE SimpleAllocator(size_t max_size, Mutex &lock);
00036 virtual ~SimpleAllocator();
00037
00038 INLINE SimpleAllocatorBlock *alloc(size_t size);
00039
00040 INLINE bool is_empty() const;
00041 INLINE size_t get_total_size() const;
00042 INLINE size_t get_max_size() const;
00043 INLINE void set_max_size(size_t max_size);
00044 INLINE size_t get_contiguous() const;
00045
00046 INLINE SimpleAllocatorBlock *get_first_block() const;
00047
00048 void output(ostream &out) const;
00049 void write(ostream &out) const;
00050
00051 protected:
00052 SimpleAllocatorBlock *do_alloc(size_t size);
00053 INLINE bool do_is_empty() const;
00054
00055 virtual SimpleAllocatorBlock *make_block(size_t start, size_t size);
00056 INLINE void mark_contiguous(const LinkedListNode *block);
00057 virtual void changed_contiguous();
00058
00059 protected:
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 size_t _total_size;
00070 size_t _max_size;
00071
00072
00073
00074
00075 size_t _contiguous;
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 Mutex &_lock;
00086
00087 friend class SimpleAllocatorBlock;
00088 };
00089
00090
00091
00092
00093
00094
00095 class EXPCL_PANDA_GOBJ SimpleAllocatorBlock : public LinkedListNode {
00096 protected:
00097 INLINE SimpleAllocatorBlock(SimpleAllocator *alloc,
00098 size_t start, size_t size);
00099
00100 PUBLISHED:
00101 INLINE ~SimpleAllocatorBlock();
00102 INLINE void free();
00103
00104 INLINE SimpleAllocator *get_allocator() const;
00105
00106 INLINE size_t get_start() const;
00107 INLINE size_t get_size() const;
00108 INLINE bool is_free() const;
00109
00110 INLINE size_t get_max_size() const;
00111 INLINE bool realloc(size_t size);
00112
00113 INLINE SimpleAllocatorBlock *get_next_block() const;
00114
00115 void output(ostream &out) const;
00116
00117 protected:
00118 INLINE void do_free();
00119 INLINE size_t do_get_max_size() const;
00120 INLINE bool do_realloc(size_t size);
00121
00122 private:
00123 SimpleAllocator *_allocator;
00124 size_t _start;
00125 size_t _size;
00126
00127 friend class SimpleAllocator;
00128 };
00129
00130 INLINE ostream &operator << (ostream &out, const SimpleAllocator &obj) {
00131 obj.output(out);
00132 return out;
00133 }
00134
00135 INLINE ostream &operator << (ostream &out, const SimpleAllocatorBlock &obj) {
00136 obj.output(out);
00137 return out;
00138 }
00139
00140 #include "simpleAllocator.I"
00141
00142 #endif