From 359c47ffdc836cfd617f9de8f4cb17fe029bc198 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Tue, 2 Aug 2005 20:18:14 +0000 Subject: [PATCH] Another pass at C++ification. --- SConstruct | 2 + src/conn.c | 6 ++- src/ctx.c | 33 ++++++++++---- src/event.c | 3 +- src/stanza.c | 6 ++- strophe.h | 25 ++++++----- strophepp.h | 111 +++++++++++++++++++++++++++-------------------- tests/test_ctx.c | 6 +-- 8 files changed, 120 insertions(+), 72 deletions(-) diff --git a/SConstruct b/SConstruct index 28529db..2aa2e42 100644 --- a/SConstruct +++ b/SConstruct @@ -46,6 +46,8 @@ Sources = Split(""" md5.c util.c thread.c + oocontext.cpp + oostanza.cpp """) Headers = Split(""" diff --git a/src/conn.c b/src/conn.c index 62ec312..a5fbc39 100644 --- a/src/conn.c +++ b/src/conn.c @@ -110,13 +110,14 @@ xmpp_conn_t * xmpp_conn_clone(xmpp_conn_t * const conn) return conn; } -void xmpp_conn_release(xmpp_conn_t * const conn) +int xmpp_conn_release(xmpp_conn_t * const conn) { xmpp_ctx_t *ctx; xmpp_connlist_t *item, *prev; xmpp_handlist_t *hlitem, *thli; hash_iterator_t *iter; const char *key; + int released = 0; if (conn->ref > 1) conn->ref--; @@ -196,7 +197,10 @@ void xmpp_conn_release(xmpp_conn_t * const conn) if (conn->pass) xmpp_free(ctx, conn->pass); if (conn->stream_id) xmpp_free(ctx, conn->stream_id); xmpp_free(ctx, conn); + released = 1; } + + return released; } const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn) diff --git a/src/ctx.c b/src/ctx.c index 758e0c3..8b4da2b 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -57,10 +57,27 @@ int xmpp_version_check(int major, int minor) /* define the global default allocator, logger and context here */ +/* wrap stdlib routines to deal with userdata pointer */ +static void *_malloc(const size_t size, void * const userdata) +{ + return malloc(size); +} + +static void _free(void *p, void * const userdata) +{ + free(p); +} + +static void *_realloc(void *p, const size_t size, void * const userdata) +{ + return realloc(p, size); +} + static xmpp_mem_t xmpp_default_mem = { - malloc, /* use the stdlib routines by default */ - free, - realloc + _malloc, /* use the thinly wrapped stdlib routines by default */ + _free, + _realloc, + NULL }; static const char * const _xmpp_log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"}; @@ -103,18 +120,18 @@ static xmpp_log_t xmpp_default_log = { NULL, NULL }; void *xmpp_alloc(const xmpp_ctx_t * const ctx, const size_t size) { - return ctx->mem->alloc(size); + return ctx->mem->alloc(size, ctx->mem->userdata); } void xmpp_free(const xmpp_ctx_t * const ctx, void *p) { - ctx->mem->free(p); + ctx->mem->free(p, ctx->mem->userdata); } void *xmpp_realloc(const xmpp_ctx_t * const ctx, void *p, const size_t size) { - return ctx->mem->realloc(p, size); + return ctx->mem->realloc(p, size, ctx->mem->userdata); } /* logger */ @@ -190,9 +207,9 @@ xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem, xmpp_ctx_t *ctx = NULL; if (mem == NULL) - ctx = xmpp_default_mem.alloc(sizeof(xmpp_ctx_t)); + ctx = xmpp_default_mem.alloc(sizeof(xmpp_ctx_t), NULL); else - ctx = mem->alloc(sizeof(xmpp_ctx_t)); + ctx = mem->alloc(sizeof(xmpp_ctx_t), mem->userdata); if (ctx != NULL) { if (mem != NULL) diff --git a/src/event.c b/src/event.c index 8306720..3300b1e 100644 --- a/src/event.c +++ b/src/event.c @@ -139,7 +139,8 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout) /* select errored */ if (ret < 0) { - xmpp_error(ctx, "xmpp", "event watcher internal error"); + xmpp_error(ctx, "xmpp", "event watcher internal error %d", + sock_error()); return; } diff --git a/src/stanza.c b/src/stanza.c index 26b5aac..eaef72a 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -120,8 +120,9 @@ copy_error: } /* free a stanza object and it's contents */ -void xmpp_stanza_release(xmpp_stanza_t * const stanza) +int xmpp_stanza_release(xmpp_stanza_t * const stanza) { + int released = 0; xmpp_stanza_t *child, *tchild; /* release all children */ @@ -139,7 +140,10 @@ void xmpp_stanza_release(xmpp_stanza_t * const stanza) if (stanza->attributes) hash_release(stanza->attributes); if (stanza->data) xmpp_free(stanza->ctx, stanza->data); xmpp_free(stanza->ctx, stanza); + released = 1; } + + return released; } /* small helper function */ diff --git a/strophe.h b/strophe.h index 98185c1..c04e02c 100644 --- a/strophe.h +++ b/strophe.h @@ -57,16 +57,17 @@ xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem, void xmpp_ctx_free(xmpp_ctx_t * const ctx); struct _xmpp_mem_t { - void *(*alloc)(const size_t size); - void (*free)(void *p); - void *(*realloc)(void *p, const size_t size); + void *(*alloc)(const size_t size, void * const userdata); + void (*free)(void *p, void * const userdata); + void *(*realloc)(void *p, const size_t size, void * const userdata); + void *userdata; }; typedef enum { - XMPP_LEVEL_DEBUG, - XMPP_LEVEL_INFO, - XMPP_LEVEL_WARN, - XMPP_LEVEL_ERROR + XMPP_LEVEL_DEBUG, + XMPP_LEVEL_INFO, + XMPP_LEVEL_WARN, + XMPP_LEVEL_ERROR } xmpp_log_level_t; typedef enum { @@ -81,9 +82,9 @@ typedef void (*xmpp_log_handler)(void * const userdata, const char * const msg); struct _xmpp_log_t { - xmpp_log_handler handler; - void * userdata; - /* mutex_t lock; */ + xmpp_log_handler handler; + void *userdata; + /* mutex_t lock; */ }; /* return a default logger filtering at a given level */ @@ -143,7 +144,7 @@ typedef void (*xmpp_conn_handler)(xmpp_conn_t * const conn, xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx); xmpp_conn_t * xmpp_conn_clone(xmpp_conn_t * const conn); -void xmpp_conn_release(xmpp_conn_t * const conn); +int xmpp_conn_release(xmpp_conn_t * const conn); const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn); void xmpp_conn_set_jid(xmpp_conn_t * const conn, const char * const jid); @@ -216,7 +217,7 @@ xmpp_stanza_t *xmpp_stanza_clone(xmpp_stanza_t * const stanza); xmpp_stanza_t * xmpp_stanza_copy(const xmpp_stanza_t * const stanza); /** free a stanza object and it's contents */ -void xmpp_stanza_release(xmpp_stanza_t * const stanza); +int xmpp_stanza_release(xmpp_stanza_t * const stanza); /** marshall a stanza into text for transmission or display **/ int xmpp_stanza_to_text(xmpp_stanza_t *stanza, diff --git a/strophepp.h b/strophepp.h index 0b394d4..c72fdc7 100644 --- a/strophepp.h +++ b/strophepp.h @@ -6,29 +6,83 @@ namespace XMPP { class Context { private: - xmpp_ctx_t *ctx; + xmpp_mem_t m_mem; + xmpp_log_t m_log; + xmpp_ctx_t *m_ctx; public: Context(); virtual ~Context(); - virtual void *alloc(const size_t size) = 0; - virtual void *realloc(void *p, const size_t size) = 0; - virtual void free(void *p) = 0; - virtual void log(xmpp_log_handler handler, void * const userdata) = 0; + virtual void *alloc(const size_t size); + virtual void *realloc(void *p, const size_t size); + virtual void free(void *p); + virtual void log(const xmpp_log_level_t level, + const char * const area, + const char * const msg); + + xmpp_ctx_t *getContext(); private: - static void *callAlloc(const size_t size, void *userdata); - static void *callRealloc(void *p, const size_t size, void *userdata); - static void *callFree(void *p, void *userdata); - } + static void *callAlloc(const size_t size, void * const userdata); + static void *callRealloc(void *p, const size_t size, + void * const userdata); + static void callFree(void *p, void * const userdata); + static void callLog(void * const userdata, + const xmpp_log_level_t level, + const char * const area, + const char * const msg); + }; + + class Stanza { + private: + Context *m_ctx; + xmpp_stanza_t *m_stanza; + + void *operator new(size_t size, Context *ctx); + void operator delete(void *p, Context *ctx); + Stanza(Context *ctx); + virtual ~Stanza(); + + public: + void release(); + Stanza *clone(); + Stanza *copy(); + + int toText(const char ** const buf, size_t * const buflen); + Stanza *getChildren(); + Stanza *getChildByName(const char * const name); + Stanza *getNext(); + char *getAttribute(const char * const name); + char *getNamespace(); + char *getText(); + char *getName(); + void addChild(Stanza *child); + void setNamespace(const char * const ns); + void setAttribute(const char * const key, const char * const value); + void setName(const char * const name); + void setText(const char * const text); + void setText(const char * const text, const size_t size); + char *getType(); + char *getId(); + char *getTo(); + char *getFrom(); + void setType(const char * const type); + void setId(const char * const id); + void setTo(const char * const to); + void setFrom(const char * const from); + }; class Connection { private: + Context *m_ctx; xmpp_conn_t *conn; - public: + void *operator new(size_t size, Context *ctx); Connection(Context *ctx); + + public: + static Connection *create(Context *ctx); virtual ~Connection(); Connection *clone(); void operator delete(void *p); @@ -57,42 +111,7 @@ namespace XMPP { const char * const id, void * const userdata); void deleteIdHandler(xmpp_handler handler); - } - - class Stanza { - private: - xmpp_stanza_t *stanza; - - public: - Stanza(Context *ctx); - virtual ~Stanza(); - void operator delete(void *p); - Stanza *clone(); - Stanza *copy(); - - int toText(const char ** const buf, size_t * const buflen); - Stanza *getChildren(); - Stanza *getChildByName(const char * const name); - Stanza *getNext(); - char *getAttribute(const char * const name); - char *getNamespace(); - char *getText(); - char *getName(); - void addChild(Stanza *child); - void setNamespace(const char * const ns); - void setAttribute(const char * const key, const char * const value); - void setName(const char * const name); - void setText(const char * const text); - void setText(const char * const text, const size_t size); - char *getType(); - char *getId(); - char *getTo(); - char *getFrom(); - void setType(const char * const type); - void setId(const char * const id); - void setTo(const char * const to); - void setFrom(const char * const from); - } + }; } #endif /* __LIBSTROPHE_STROPHEPP_H__ */ diff --git a/tests/test_ctx.c b/tests/test_ctx.c index 489d406..3d9281b 100644 --- a/tests/test_ctx.c +++ b/tests/test_ctx.c @@ -24,19 +24,19 @@ static int mem_alloc_called = 0; static int mem_free_called = 0; static int mem_realloc_called = 0; -void *my_alloc(const size_t size) +void *my_alloc(const size_t size, void * const userdata) { mem_alloc_called++; return malloc(size); } -void my_free(void *p) +void my_free(void *p, void * const userdata) { mem_free_called++; return free(p); } -void *my_realloc(void *p, const size_t size) +void *my_realloc(void *p, const size_t size, void * const userdata) { mem_realloc_called++; return realloc(p, size);