00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef POOL_H
00012 #define POOL_H
00013
00014
00015 #include <stddef.h>
00016 #ifndef __MWERKS__
00017 #include <memory.h>
00018 #endif
00019 #ifdef IPPL_STDSTL
00020 #include <vector>
00021 using std::vector;
00022 #else
00023 #include <vector.h>
00024 #endif
00025
00026 class Pool
00027 {
00028 private:
00029 struct Link { Link *next; };
00030 static inline size_t page() { return 4096-8; }
00031 static inline int blocks_in_page(size_t sz)
00032 {
00033 return (page()>sz)?(page()/sz):1;
00034 }
00035
00036 public:
00037
00038 static inline size_t log2_align() { return 3; }
00039 static inline size_t align() { return (1<<log2_align()); }
00040 static inline size_t align_mask() { return align()-1; }
00041
00042 static inline size_t round_to_align(size_t s)
00043 {
00044 if (s)
00045 s = (s & ~align_mask()) + ((s&align_mask())?align():0);
00046 else
00047 s = align();
00048 return s;
00049 }
00050
00051
00052
00053 Pool();
00054
00055
00056 Pool(size_t);
00057
00058
00059 ~Pool();
00060
00061
00062 inline void* alloc()
00063 {
00064 ++outstandingAllocs;
00065 if ( head==0 ) grow();
00066 Link *p = head;
00067
00068
00069
00070
00071
00072
00073 memcpy(&head, &p->next, sizeof(head));
00074
00075
00076 return p;
00077 }
00078
00079
00080 inline void free(void *b)
00081 {
00082 --outstandingAllocs;
00083 Link *p = (Link*)b;
00084 p->next = head;
00085 head = p;
00086 }
00087
00088 private:
00089 int outstandingAllocs;
00090 size_t bsize;
00091 size_t nblock;
00092 Link *head;
00093 vector<char*> chunks;
00094 void grow();
00095
00096 };
00097
00099
00100 #endif // POOL_H
00101
00102
00103
00104
00105
00106