aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hybrid
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-05-11 12:20:11 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-05-11 12:20:11 +0200
commit2e501c68a8641a2b3c430b55f13491a9c1c5d0f5 (patch)
tree49c2748443fe3c1f01108756b647440e0647a11b /libxsde/xsde/cxx/hybrid
parent161beba6cdb0d91b15ad19fa8b3e51d986203915 (diff)
Add support for custom allocators
New example: examples/cxx/hybrid/allocator.
Diffstat (limited to 'libxsde/xsde/cxx/hybrid')
-rw-r--r--libxsde/xsde/cxx/hybrid/base.hxx27
-rw-r--r--libxsde/xsde/cxx/hybrid/cdr/istream.cxx13
-rw-r--r--libxsde/xsde/cxx/hybrid/cdr/istream.txx25
-rw-r--r--libxsde/xsde/cxx/hybrid/sequence.hxx14
-rw-r--r--libxsde/xsde/cxx/hybrid/sequence.ixx50
-rw-r--r--libxsde/xsde/cxx/hybrid/sequence.txx12
-rw-r--r--libxsde/xsde/cxx/hybrid/xdr/istream.cxx23
-rw-r--r--libxsde/xsde/cxx/hybrid/xdr/istream.txx24
8 files changed, 175 insertions, 13 deletions
diff --git a/libxsde/xsde/cxx/hybrid/base.hxx b/libxsde/xsde/cxx/hybrid/base.hxx
index 1a238a1..89431a2 100644
--- a/libxsde/xsde/cxx/hybrid/base.hxx
+++ b/libxsde/xsde/cxx/hybrid/base.hxx
@@ -12,6 +12,10 @@
# include <string.h> // strcmp
#endif
+#ifdef XSDE_CUSTOM_ALLOCATOR
+# include <xsde/cxx/allocator.hxx>
+#endif
+
namespace xsde
{
namespace cxx
@@ -566,17 +570,34 @@ namespace xsde
struct string_base
{
string_base () : x_ (0) {}
- ~string_base () {delete[] x_;}
+ ~string_base ()
+ {
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete[] x_;
+#else
+ cxx::free (x_);
+#endif
+ }
const char* base_value () const {return x_;}
char* base_value () {return x_;}
- void base_value (char* x) {delete[] x_; x_ = x;}
+
+ void base_value (char* x)
+ {
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete[] x_;
+#else
+ cxx::free (x_);
+#endif
+ x_ = x;
+ }
+
char* base_value_detach () {char* r = x_; x_ = 0; return r;}
operator const char* () const {return x_;}
operator char* () {return x_;}
- string_base& operator= (char* x) {delete[] x_; x_ = x; return *this;}
+ string_base& operator= (char* x) {base_value (x); return *this;}
protected:
char* x_;
diff --git a/libxsde/xsde/cxx/hybrid/cdr/istream.cxx b/libxsde/xsde/cxx/hybrid/cdr/istream.cxx
index 684df2d..06e26e6 100644
--- a/libxsde/xsde/cxx/hybrid/cdr/istream.cxx
+++ b/libxsde/xsde/cxx/hybrid/cdr/istream.cxx
@@ -17,7 +17,14 @@ namespace xsde
struct str_guard
{
str_guard (char* s) : s_ (s) {}
- ~str_guard () {delete[] s_;}
+ ~str_guard ()
+ {
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete[] s_;
+#else
+ cxx::free (s_);
+#endif
+ }
private:
char* s_;
@@ -70,7 +77,11 @@ namespace xsde
return false;
x = v;
+#ifndef XSDE_CUSTOM_ALLOCATOR
delete[] v;
+#else
+ cxx::free (v);
+#endif
return true;
}
#else
diff --git a/libxsde/xsde/cxx/hybrid/cdr/istream.txx b/libxsde/xsde/cxx/hybrid/cdr/istream.txx
index 58cd98b..f1d8d6c 100644
--- a/libxsde/xsde/cxx/hybrid/cdr/istream.txx
+++ b/libxsde/xsde/cxx/hybrid/cdr/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,28 @@ 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;
}
diff --git a/libxsde/xsde/cxx/hybrid/sequence.hxx b/libxsde/xsde/cxx/hybrid/sequence.hxx
index 451c76d..fa5c697 100644
--- a/libxsde/xsde/cxx/hybrid/sequence.hxx
+++ b/libxsde/xsde/cxx/hybrid/sequence.hxx
@@ -22,6 +22,10 @@
# include <xsde/cxx/string-sequence.hxx>
#endif
+#ifdef XSDE_CUSTOM_ALLOCATOR
+# include <xsde/cxx/allocator.hxx>
+#endif
+
namespace xsde
{
namespace cxx
@@ -831,7 +835,15 @@ namespace xsde
public:
struct guard
{
- ~guard () { delete p_; }
+ ~guard ()
+ {
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete p_;
+#else
+ cxx::free (p_);
+#endif
+ }
+
guard (T* p) : p_ (p) {}
void
diff --git a/libxsde/xsde/cxx/hybrid/sequence.ixx b/libxsde/xsde/cxx/hybrid/sequence.ixx
index 6029b76..1ec2a4c 100644
--- a/libxsde/xsde/cxx/hybrid/sequence.ixx
+++ b/libxsde/xsde/cxx/hybrid/sequence.ixx
@@ -551,7 +551,15 @@ namespace xsde
inline void var_sequence<T>::
pop_back ()
{
- delete static_cast<T**> (data_)[size_ - 1];
+ T* x = static_cast<T**> (data_)[size_ - 1];
+
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete x;
+#else
+ if (x)
+ x->~T ();
+ cxx::free (x);
+#endif
--size_;
}
@@ -559,7 +567,15 @@ namespace xsde
inline var_iterator<T> var_sequence<T>::
erase (iterator i)
{
- delete *i.i_;
+ T* x = *i.i_;
+
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete x;
+#else
+ if (x)
+ x->~T ();
+ cxx::free (x);
+#endif
if (i.i_ != static_cast<T**> (data_) + (size_ - 1))
erase_ (i.i_, sizeof (T*), 0);
@@ -582,7 +598,15 @@ namespace xsde
inline void var_sequence<T>::
attach (iterator i, T* x)
{
- delete *i.i_;
+ T* t = *i.i_;
+
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete t;
+#else
+ if (t)
+ t->~T ();
+ cxx::free (t);
+#endif
*i.i_ = x;
}
@@ -632,7 +656,15 @@ namespace xsde
if (r == error_none)
static_cast<T**> (data_)[size_++] = x;
else
+ {
+#ifndef XSDE_CUSTOM_ALLOCATOR
delete x;
+#else
+ if (x)
+ x->~T ();
+ cxx::free (x);
+#endif
+ }
return r;
}
@@ -650,7 +682,13 @@ namespace xsde
}
else
{
+#ifndef XSDE_CUSTOM_ALLOCATOR
delete x;
+#else
+ if (x)
+ x->~T ();
+ cxx::free (x);
+#endif
return error_no_memory;
}
}
@@ -669,7 +707,13 @@ namespace xsde
}
else
{
+#ifndef XSDE_CUSTOM_ALLOCATOR
delete x;
+#else
+ if (x)
+ x->~T ();
+ cxx::free (x);
+#endif
return error_no_memory;
}
}
diff --git a/libxsde/xsde/cxx/hybrid/sequence.txx b/libxsde/xsde/cxx/hybrid/sequence.txx
index 4fd3a85..b79e9d5 100644
--- a/libxsde/xsde/cxx/hybrid/sequence.txx
+++ b/libxsde/xsde/cxx/hybrid/sequence.txx
@@ -152,7 +152,17 @@ namespace xsde
clear ()
{
for (size_t i = 0; i < size_; ++i)
- delete static_cast<T**> (data_)[i];
+ {
+ T* x = static_cast<T**> (data_)[i];
+
+#ifndef XSDE_CUSTOM_ALLOCATOR
+ delete x;
+#else
+ if (x)
+ x->~T ();
+ cxx::free (x);
+#endif
+ }
size_ = 0;
}
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;
}