From 2b9e5b61a1b87e548bf2b193d8928962b91bda7c Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Tue, 2 Aug 2005 21:07:40 +0000 Subject: [PATCH] Stanza object creation/deletion now uses the context, but must be done with a factory function. --- src/oostanza.cpp | 32 +++++++++++++++++++++++++++----- strophepp.h | 3 ++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/oostanza.cpp b/src/oostanza.cpp index 4b85608..a4cf963 100644 --- a/src/oostanza.cpp +++ b/src/oostanza.cpp @@ -5,12 +5,30 @@ using namespace XMPP; void *Stanza::operator new(size_t size, Context *ctx) { - return ctx->alloc(size); + void *p; + + /* we must allocate extra room for the Context object so that the + destructor can access it to free the object. C++ does not allow + us to access normal members in the destructor, so we have to hide + it. This must be prepended as well, since C++ will add stuff to + the end in subclasses. */ + + p = ctx->alloc(size + sizeof(Context *)); + if (!p) return p; + + *reinterpret_cast(p) = ctx; + p = reinterpret_cast(reinterpret_cast(p) + + sizeof(Context *)); + + return p; } -void Stanza::operator delete(void *p, Context *ctx) +void Stanza::operator delete(void *p) { - ctx->free(p); + Context *ctx; + + ctx = *reinterpret_cast(reinterpret_cast(p) - 4); + ctx->free(reinterpret_cast(p) - 4); } Stanza::Stanza(Context *ctx) @@ -22,13 +40,17 @@ Stanza::Stanza(Context *ctx) Stanza::~Stanza() { - ::xmpp_stanza_release(m_stanza); +} + +Stanza *Stanza::create(Context *ctx) +{ + return new (ctx) Stanza(ctx); } void Stanza::release() { if (::xmpp_stanza_release(m_stanza)) - delete(m_ctx) this; + delete this; } Stanza *Stanza::clone() diff --git a/strophepp.h b/strophepp.h index c72fdc7..1d2b0d5 100644 --- a/strophepp.h +++ b/strophepp.h @@ -40,11 +40,12 @@ namespace XMPP { xmpp_stanza_t *m_stanza; void *operator new(size_t size, Context *ctx); - void operator delete(void *p, Context *ctx); + void operator delete(void *p); Stanza(Context *ctx); virtual ~Stanza(); public: + static Stanza *create(Context *ctx); void release(); Stanza *clone(); Stanza *copy();