+_finline void *operator new(size_t size, CYPool &pool) {
+ return pool.malloc<void>(size);
+}
+
+_finline void *operator new [](size_t size, CYPool &pool) {
+ return pool.malloc<void>(size);
+}
+
+_finline void CYPool::atexit(void (*code)(void *), void *data) {
+ cleaner_ = new(*this) Cleaner(cleaner_, code, data);
+}
+
+struct CYData {
+ CYPool *pool_;
+ unsigned count_;
+
+ CYData() :
+ count_(1)
+ {
+ }
+
+ CYData(CYPool &pool) :
+ pool_(&pool),
+ count_(_not(unsigned))
+ {
+ }
+
+ virtual ~CYData() {
+ }
+
+ static void *operator new(size_t size, CYPool &pool) {
+ void *data(pool.malloc<void>(size));
+ reinterpret_cast<CYData *>(data)->pool_ = &pool;
+ return data;
+ }
+
+ static void *operator new(size_t size) {
+ return operator new(size, *new CYPool());
+ }
+
+ static void operator delete(void *data) {
+ delete reinterpret_cast<CYData *>(data)->pool_;
+ }
+};
+
+template <typename Type_>
+struct CYPoolAllocator {
+ CYPool *pool_;
+
+ typedef Type_ value_type;
+ typedef value_type *pointer;
+ typedef const value_type *const_pointer;
+ typedef value_type &reference;
+ typedef const value_type &const_reference;
+ typedef std::size_t size_type;
+ typedef std::ptrdiff_t difference_type;
+
+ CYPoolAllocator() :
+ pool_(NULL)
+ {
+ }
+
+ template <typename Right_>
+ CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
+ pool_(rhs.pool_)
+ {
+ }
+
+ pointer allocate(size_type size, const void *hint = 0) {
+ return pool_->malloc<value_type>(size);
+ }
+
+ void deallocate(pointer data, size_type size) {
+ }
+
+ void construct(pointer address, const Type_ &rhs) {
+ new(address) Type_(rhs);
+ }
+
+ void destroy(pointer address) {
+ address->~Type_();
+ }
+
+ template <typename Right_>
+ inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
+ return pool_ == rhs.pool_;
+ }
+
+ template <typename Right_>
+ inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
+ return !operator==(rhs);
+ }
+
+ template <typename Right_>
+ struct rebind {
+ typedef CYPoolAllocator<Right_> other;
+ };
+};
+
+class CYLocalPool :
+ public CYPool
+{
+ private:
+ CYLocal<CYPool> local_;
+
+ public:
+ CYLocalPool() :
+ CYPool(),
+ local_(this)
+ {
+ }
+};
+
+#define $pool \
+ (*CYLocal<CYPool>::Get())
+
+template <>
+::pthread_key_t CYLocal<CYPool>::key_;
+
+#endif/*CYCRIPT_POOLING_HPP*/