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. --- xsde/cxx/hybrid/extraction-source.cxx | 189 ++++++++++++++++++++++++++-------- 1 file changed, 147 insertions(+), 42 deletions(-) (limited to 'xsde/cxx/hybrid/extraction-source.cxx') diff --git a/xsde/cxx/hybrid/extraction-source.cxx b/xsde/cxx/hybrid/extraction-source.cxx index 3b98100..1e2a602 100644 --- a/xsde/cxx/hybrid/extraction-source.cxx +++ b/xsde/cxx/hybrid/extraction-source.cxx @@ -239,15 +239,30 @@ namespace CXX { String fq (fq_name (t)); - if (exceptions) - os << "x." << name << " (new " << fq << ");"; + if (!custom_alloc) + os << fq << "* i = new " << fq << ";"; else - os << fq << "* i = new " << fq << ";" - << endl + os << fq << "* i = static_cast< " << fq << "* > (" << endl + << "::xsde::cxx::alloc (sizeof (" << fq << ")));"; + + if (!exceptions) + os << endl << "if (i == 0)" << endl << "return false;" - << endl - << "x." << name << " (i);"; + << endl; + + if (custom_alloc) + { + if (exceptions) + os << "::xsde::cxx::alloc_guard ig (i);"; + + os << "new (i) " << fq << ";"; + + if (exceptions) + os << "ig.release ();"; + } + + os << "x." << name << " (i);"; } if (exceptions) @@ -354,15 +369,30 @@ namespace CXX { String fq (fq_name (t)); - if (exceptions) - os << "x." << name << " (new " << fq << ");"; + if (!custom_alloc) + os << fq << "* i = new " << fq << ";"; else - os << fq << "* i = new " << fq << ";" - << endl + os << fq << "* i = static_cast< " << fq << "* > (" << endl + << "::xsde::cxx::alloc (sizeof (" << fq << ")));"; + + if (!exceptions) + os << endl << "if (i == 0)" << endl << "return false;" - << endl - << "x." << name << " (i);"; + << endl; + + if (custom_alloc) + { + if (exceptions) + os << "::xsde::cxx::alloc_guard ig (i);"; + + os << "new (i) " << fq << ";"; + + if (exceptions) + os << "ig.release ();"; + } + + os << "x." << name << " (i);"; } if (exceptions) @@ -423,15 +453,30 @@ namespace CXX { String fq (scope (a) + L"::" + etype (a)); - if (exceptions) - os << "x." << name << " (new " << fq << ");"; + if (!custom_alloc) + os << fq << "* i = new " << fq << ";"; else - os << fq << "* i = new " << fq << ";" - << endl + os << fq << "* i = static_cast< " << fq << "* > (" << endl + << "::xsde::cxx::alloc (sizeof (" << fq << ")));"; + + if (!exceptions) + os << endl << "if (i == 0)" << endl << "return false;" - << endl - << "x." << name << " (i);"; + << endl; + + if (custom_alloc) + { + if (exceptions) + os << "::xsde::cxx::alloc_guard ig (i);"; + + os << "new (i) " << fq << ";"; + + if (exceptions) + os << "ig.release ();"; + } + + os << "x." << name << " (i);"; } if (exceptions) @@ -529,15 +574,30 @@ namespace CXX { String fq (fq_name (t)); - if (exceptions) - os << "x." << name << " (new " << fq << ");"; + if (!custom_alloc) + os << fq << "* i = new " << fq << ";"; else - os << fq << "* i = new " << fq << ";" - << endl + os << fq << "* i = static_cast< " << fq << "* > (" << endl + << "::xsde::cxx::alloc (sizeof (" << fq << ")));"; + + if (!exceptions) + os << endl << "if (i == 0)" << endl << "return false;" - << endl - << "x." << name << " (i);"; + << endl; + + if (custom_alloc) + { + if (exceptions) + os << "::xsde::cxx::alloc_guard ig (i);"; + + os << "new (i) " << fq << ";"; + + if (exceptions) + os << "ig.release ();"; + } + + os << "x." << name << " (i);"; } if (exceptions) @@ -604,15 +664,30 @@ namespace CXX { String fq (scope (c) + L"::" + etype (c)); - if (exceptions) - os << "x." << name << " (new " << fq << ");"; + if (!custom_alloc) + os << fq << "* i = new " << fq << ";"; else - os << fq << "* i = new " << fq << ";" - << endl + os << fq << "* i = static_cast< " << fq << "* > (" << endl + << "::xsde::cxx::alloc (sizeof (" << fq << ")));"; + + if (!exceptions) + os << endl << "if (i == 0)" << endl << "return false;" - << endl - << "x." << name << " (i);"; + << endl; + + if (custom_alloc) + { + if (exceptions) + os << "::xsde::cxx::alloc_guard ig (i);"; + + os << "new (i) " << fq << ";"; + + if (exceptions) + os << "ig.release ();"; + } + + os << "x." << name << " (i);"; } if (exceptions) @@ -681,15 +756,30 @@ namespace CXX { String fq (scope (c) + L"::" + etype (c)); - if (exceptions) - os << "x." << name << " (new " << fq << ");"; + if (!custom_alloc) + os << fq << "* i = new " << fq << ";"; else - os << fq << "* i = new " << fq << ";" - << endl + os << fq << "* i = static_cast< " << fq << "* > (" << endl + << "::xsde::cxx::alloc (sizeof (" << fq << ")));"; + + if (!exceptions) + os << endl << "if (i == 0)" << endl << "return false;" - << endl - << "x." << name << " (i);"; + << endl; + + if (custom_alloc) + { + if (exceptions) + os << "::xsde::cxx::alloc_guard ig (i);"; + + os << "new (i) " << fq << ";"; + + if (exceptions) + os << "ig.release ();"; + } + + os << "x." << name << " (i);"; } if (exceptions) @@ -784,15 +874,30 @@ namespace CXX { String fq (scope (s) + L"::" + etype (s)); - if (exceptions) - os << "x." << name << " (new " << fq << ");"; + if (!custom_alloc) + os << fq << "* i = new " << fq << ";"; else - os << fq << "* i = new " << fq << ";" - << endl + os << fq << "* i = static_cast< " << fq << "* > (" << endl + << "::xsde::cxx::alloc (sizeof (" << fq << ")));"; + + if (!exceptions) + os << endl << "if (i == 0)" << endl << "return false;" - << endl - << "x." << name << " (i);"; + << endl; + + if (custom_alloc) + { + if (exceptions) + os << "::xsde::cxx::alloc_guard ig (i);"; + + os << "new (i) " << fq << ";"; + + if (exceptions) + os << "ig.release ();"; + } + + os << "x." << name << " (i);"; } if (exceptions) -- cgit v1.1