From 2e501c68a8641a2b3c430b55f13491a9c1c5d0f5 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 11 May 2010 12:20:11 +0200 Subject: Add support for custom allocators New example: examples/cxx/hybrid/allocator. --- libxsde/xsde/cxx/hashmap.cxx | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'libxsde/xsde/cxx/hashmap.cxx') diff --git a/libxsde/xsde/cxx/hashmap.cxx b/libxsde/xsde/cxx/hashmap.cxx index b633cbd..f87ebbf 100644 --- a/libxsde/xsde/cxx/hashmap.cxx +++ b/libxsde/xsde/cxx/hashmap.cxx @@ -7,6 +7,10 @@ #include +#ifdef XSDE_CUSTOM_ALLOCATOR +# include +#endif + namespace xsde { namespace cxx @@ -76,10 +80,18 @@ namespace xsde for (size_t i = 0; i < bcount_; ++i) { if (buckets_[i]) +#ifndef XSDE_CUSTOM_ALLOCATOR operator delete (buckets_[i]); +#else + cxx::free (buckets_[i]); +#endif } +#ifndef XSDE_CUSTOM_ALLOCATOR delete[] buckets_; +#else + cxx::free (buckets_); // POD. +#endif #ifndef XSDE_EXCEPTIONS } @@ -95,7 +107,11 @@ namespace xsde error_ = error_none; #endif +#ifndef XSDE_CUSTOM_ALLOCATOR buckets_ = new bucket*[bcount_]; +#else + buckets_ = static_cast (alloc (bcount_ * sizeof (bucket*))); +#endif #ifndef XSDE_EXCEPTIONS if (buckets_ == 0) @@ -118,8 +134,13 @@ namespace xsde // No elements in this bucket yet. Start with capacity for 2 // elements. // +#ifndef XSDE_CUSTOM_ALLOCATOR p = static_cast ( operator new (sizeof (bucket) + 2 * (sizeof (element) + esize_))); +#else + p = static_cast ( + alloc (sizeof (bucket) + 2 * (sizeof (element) + esize_))); +#endif #ifndef XSDE_EXCEPTIONS if (p == 0) @@ -137,8 +158,14 @@ namespace xsde // No more space in this bucket. Create a bigger bucket. // size_t c = p->size_ * 2; + +#ifndef XSDE_CUSTOM_ALLOCATOR bucket* n = static_cast ( operator new (sizeof (bucket) + c * (sizeof (element) + esize_))); +#else + bucket* n = static_cast ( + alloc (sizeof (bucket) + c * (sizeof (element) + esize_))); +#endif #ifndef XSDE_EXCEPTIONS if (n == 0) @@ -155,7 +182,11 @@ namespace xsde memcpy (dst, src, p->size_ * (sizeof (element) + esize_)); +#ifndef XSDE_CUSTOM_ALLOCATOR operator delete (p); +#else + cxx::free (p); +#endif p = n; } -- cgit v1.1