00001
00002
00003 namespace dynamic
00004 {
00006 namespace internal
00007 {
00008 DYNAMIC_API void *apartment_malloc(std::size_t bytes);
00009 DYNAMIC_API void apartment_free(void *p);
00010
00012
00013 class DYNAMIC_API leave_apartment : cg::not_copyable, cg::not_assignable
00014 {
00015 public:
00016 leave_apartment();
00017 ~leave_apartment();
00018 private:
00019 leave_apartment(const leave_apartment&);
00020 leave_apartment & operator=(const leave_apartment&);
00021 };
00022
00023
00025
00026 class DYNAMIC_API use_new_apartment : cg::not_copyable, cg::not_assignable
00027 {
00028 public:
00029 use_new_apartment();
00030 ~use_new_apartment();
00031 private:
00032 apartment * m_previous;
00033 };
00034 }
00035
00036
00038
00040 class DYNAMIC_API kingsley_heap
00041 {
00042 public:
00043 kingsley_heap();
00044 ~kingsley_heap();
00045 void * malloc(std::size_t bytes);
00046 void free(void * p);
00047 void defrag();
00048
00049 private:
00050 static const int num_blocks = 32;
00051 void **m_blocks[num_blocks];
00052 void ** m_allocated;
00053 };
00054
00055
00057
00058 template<typename T>
00059 struct allocator : public std::allocator<T>
00060 {
00061 typedef std::size_t size_type;
00062 typedef std::ptrdiff_t difference_type;
00063 typedef T * pointer;
00064 typedef const T * const_pointer;
00065 typedef T & reference;
00066 typedef const T & const_reference;
00067 typedef T value_type;
00068 template<typename U> struct rebind { typedef allocator<U> other; };
00069
00070
00071 allocator() throw() { }
00072 allocator(const allocator&) throw() { }
00073 template<typename U> allocator(const allocator<U>&) throw() { }
00074 ~allocator() throw() { }
00075
00076 pointer address(reference x) { return &x; }
00077 const_pointer address(const_reference x) { return &x; }
00078
00079 pointer allocate(size_type s, void* = 0)
00080 {
00081 return (pointer)internal::apartment_malloc(s * sizeof(T));
00082 }
00083
00084 void deallocate(pointer p, size_type)
00085 {
00086 internal::apartment_free(p);
00087 }
00088
00089 void construct(pointer p, const_reference val) { new(p) T(val); }
00090 void destroy(pointer p) { p->~T(); }
00091 };
00092 }
00093