diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-05-11 12:20:11 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-05-11 12:20:11 +0200 |
commit | 2e501c68a8641a2b3c430b55f13491a9c1c5d0f5 (patch) | |
tree | 49c2748443fe3c1f01108756b647440e0647a11b /libxsde/xsde/cxx/hybrid/xdr | |
parent | 161beba6cdb0d91b15ad19fa8b3e51d986203915 (diff) |
Add support for custom allocators
New example: examples/cxx/hybrid/allocator.
Diffstat (limited to 'libxsde/xsde/cxx/hybrid/xdr')
-rw-r--r-- | libxsde/xsde/cxx/hybrid/xdr/istream.cxx | 23 | ||||
-rw-r--r-- | libxsde/xsde/cxx/hybrid/xdr/istream.txx | 24 |
2 files changed, 43 insertions, 4 deletions
diff --git a/libxsde/xsde/cxx/hybrid/xdr/istream.cxx b/libxsde/xsde/cxx/hybrid/xdr/istream.cxx index 2f870d0..e31fb7f 100644 --- a/libxsde/xsde/cxx/hybrid/xdr/istream.cxx +++ b/libxsde/xsde/cxx/hybrid/xdr/istream.cxx @@ -23,7 +23,7 @@ namespace xsde throw xdr_exception (); x.clear (); - + if (n != 0) { x.resize (n); @@ -42,11 +42,19 @@ namespace xsde if (!xdr_u_int (&xdr_, &n)) throw xdr_exception (); +#ifndef XSDE_CUSTOM_ALLOCATOR x = new char[n + 1]; +#else + x = static_cast<char*> (alloc (n + 1)); +#endif if (!xdr_opaque (&xdr_, x, n)) { +#ifndef XSDE_CUSTOM_ALLOCATOR delete[] x; +#else + cxx::free (x); +#endif throw xdr_exception (); } @@ -80,14 +88,14 @@ namespace xsde return false; x.clear (); - + if (n != 0) { x.resize (n); char* p = const_cast<char*> (x.c_str ()); return xdr_opaque (&xdr_, p, n); } - + return true; } #else @@ -99,14 +107,21 @@ namespace xsde if (!xdr_u_int (&xdr_, &n)) return false; +#ifndef XSDE_CUSTOM_ALLOCATOR x = new char[n + 1]; - +#else + x = static_cast<char*> (alloc (n + 1)); +#endif if (x == 0) return false; if (!xdr_opaque (&xdr_, x, n)) { +#ifndef XSDE_CUSTOM_ALLOCATOR delete[] x; +#else + cxx::free (x); +#endif return false; } diff --git a/libxsde/xsde/cxx/hybrid/xdr/istream.txx b/libxsde/xsde/cxx/hybrid/xdr/istream.txx index 8f1d646..876befd 100644 --- a/libxsde/xsde/cxx/hybrid/xdr/istream.txx +++ b/libxsde/xsde/cxx/hybrid/xdr/istream.txx @@ -3,6 +3,10 @@ // copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC // license : GNU GPL v2 + exceptions; see accompanying LICENSE file +#ifdef XSDE_CUSTOM_ALLOCATOR +# include <xsde/cxx/allocator.hxx> +#endif + namespace xsde { namespace cxx @@ -73,7 +77,14 @@ namespace xsde while (n--) { +#ifndef XSDE_CUSTOM_ALLOCATOR T* p = new T; +#else + T* p = static_cast<T*> (alloc (sizeof (T))); + alloc_guard pg (p); + new (p) T; + pg.release (); +#endif typename var_sequence<T>::guard g (p); s >> *p; g.release (); @@ -159,14 +170,27 @@ namespace xsde while (n--) { +#ifndef XSDE_CUSTOM_ALLOCATOR T* p = new T; if (p == 0) return false; +#else + void* v = alloc (sizeof (T)); + + if (v == 0) + return false; + T* p = new (v) T; // c-tor cannot fail +#endif if (!(s >> *p)) { +#ifndef XSDE_CUSTOM_ALLOCATOR delete p; +#else + p->~T (); + cxx::free (p); +#endif return false; } |