From b799113bd4ec270504dd9f552142c1adfd6e583a Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Fri, 21 Jun 2013 07:44:46 -0700 Subject: [PATCH] Replace all apr_pool_t * usages with CYPool &. --- Console.cpp | 6 ++-- Error.hpp | 4 +-- Exception.hpp | 5 +-- Execute.cpp | 52 ++++++++++++++------------- Handler.mm | 14 ++++---- Internal.hpp | 33 +++++++++-------- JavaScript.hpp | 21 ++++++----- Library.cpp | 23 ++++++------ Mach/Inject.cpp | 2 +- ObjectiveC/Library.mm | 67 ++++++++++++++++++----------------- ObjectiveC/Replace.cpp | 2 +- Pooling.hpp | 80 +++++++++++++++++++++++++++++------------- Replace.cpp | 8 ++--- String.hpp | 4 +-- cycript.hpp | 6 ++-- sig/copy.cpp | 13 ++++--- sig/ffi_type.cpp | 22 ++++++------ sig/ffi_type.hpp | 13 ++++--- sig/parse.cpp | 43 +++++++++++------------ sig/parse.hpp | 17 +++++---- sig/types.hpp | 5 ++- 21 files changed, 234 insertions(+), 206 deletions(-) diff --git a/Console.cpp b/Console.cpp index 99421d8..32e4c5f 100644 --- a/Console.cpp +++ b/Console.cpp @@ -392,8 +392,8 @@ static void Console(CYOptions &options) { else passwd = getpwuid(getuid()); - const char *basedir(apr_psprintf(pool, "%s/.cycript", passwd->pw_dir)); - const char *histfile(apr_psprintf(pool, "%s/history", basedir)); + const char *basedir(pool.sprintf("%s/.cycript", passwd->pw_dir)); + const char *histfile(pool.sprintf("%s/history", basedir)); size_t histlines(0); rl_initialize(); @@ -678,7 +678,7 @@ int Main(int argc, char const * const argv[], char const * const envp[]) { pid = strtoul(arg, &end, 0); if (arg + size != end) { // XXX: arg needs to be escaped in some horrendous way of doom - const char *command(apr_psprintf(pool, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", arg)); + const char *command(pool.sprintf("ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", arg)); if (FILE *pids = popen(command, "r")) { char value[32]; diff --git a/Error.hpp b/Error.hpp index d46dbf8..f608e88 100644 --- a/Error.hpp +++ b/Error.hpp @@ -40,7 +40,7 @@ struct CYJSError : CYJSError(JSContextRef context, const char *format, ...); - virtual const char *PoolCString(apr_pool_t *pool) const; + virtual const char *PoolCString(CYPool &pool) const; virtual JSValueRef CastJSValue(JSContextRef context) const; }; #endif @@ -54,7 +54,7 @@ struct CYPoolError : CYPoolError(const char *format, ...); CYPoolError(const char *format, va_list args); - virtual const char *PoolCString(apr_pool_t *pool) const; + virtual const char *PoolCString(CYPool &pool) const; #ifdef CY_EXECUTE virtual JSValueRef CastJSValue(JSContextRef context) const; #endif diff --git a/Exception.hpp b/Exception.hpp index 97c629c..598548f 100644 --- a/Exception.hpp +++ b/Exception.hpp @@ -26,14 +26,15 @@ #include #endif -#include #include "Standard.hpp" +class CYPool; + struct CYException { virtual ~CYException() { } - virtual const char *PoolCString(apr_pool_t *pool) const = 0; + virtual const char *PoolCString(CYPool &pool) const = 0; #ifdef CY_EXECUTE virtual JSValueRef CastJSValue(JSContextRef context) const = 0; #endif diff --git a/Execute.cpp b/Execute.cpp index b2fe08a..7d20679 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -109,22 +109,22 @@ static CYUTF16String CYCastUTF16String(JSStringRef value) { return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); } -CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) { +CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) { return CYPoolUTF8String(pool, CYCastUTF16String(value)); } -const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value) { +const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value) { CYUTF8String utf8(CYPoolUTF8String(pool, context, value)); _assert(memchr(utf8.data, '\0', utf8.size) == NULL); return utf8.data; } -const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { +const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value) { return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value)); } /* }}} */ /* Index Offsets {{{ */ -size_t CYGetIndex(apr_pool_t *pool, JSContextRef context, JSStringRef value) { +size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) { return CYGetIndex(CYPoolUTF8String(pool, context, value)); } /* }}} */ @@ -154,11 +154,12 @@ static JSStringRef Result_; void CYFinalize(JSObjectRef object) { CYData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + _assert(internal->count_ != _not(unsigned)); if (--internal->count_ == 0) delete internal; } -void Structor_(apr_pool_t *pool, sig::Type *&type) { +void Structor_(CYPool &pool, sig::Type *&type) { if ( type->primitive == sig::pointer_P && type->data.data.type != NULL && @@ -219,7 +220,7 @@ struct Pointer : Pointer(void *value, JSContextRef context, JSObjectRef owner, size_t length, sig::Type *type) : CYOwned(value, context, owner), - type_(new(pool_) Type_privateData(type)), + type_(new(*pool_) Type_privateData(type)), length_(length) { } @@ -241,7 +242,7 @@ static TypeMap Types_; JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { Struct_privateData *internal(new Struct_privateData(context, owner)); - apr_pool_t *pool(internal->pool_); + CYPool &pool(*internal->pool_); Type_privateData *typical(new(pool) Type_privateData(type, ffi)); internal->type_ = typical; @@ -249,7 +250,7 @@ JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_ internal->value_ = data; else { size_t size(typical->GetFFI()->size); - void *copy(apr_palloc(internal->pool_, size)); + void *copy((*internal->pool_)(size)); memcpy(copy, data, size); internal->value_ = copy; } @@ -341,7 +342,7 @@ static size_t Nonce_(0); static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYPool pool; - const char *name(apr_psprintf(pool, "%s%"APR_SIZE_T_FMT"", CYPoolCString(pool, context, arguments[0]), Nonce_++)); + const char *name(pool.sprintf("%s%"APR_SIZE_T_FMT"", CYPoolCString(pool, context, arguments[0]), Nonce_++)); return CYCastJSValue(context, name); } @@ -350,7 +351,7 @@ static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef ob return CYJSUndefined(context); } -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry { +const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry { switch (JSType type = JSValueGetType(context, value)) { case kJSTypeUndefined: return "undefined"; @@ -363,7 +364,7 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value std::ostringstream str; CYNumerify(str, CYCastDouble(context, value)); std::string value(str.str()); - return apr_pstrmemdup(pool, value.c_str(), value.size()); + return pool.strmemdup(value.c_str(), value.size()); } break; case kJSTypeString: { @@ -371,7 +372,7 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value))); CYStringify(str, string.data, string.size); std::string value(str.str()); - return apr_pstrmemdup(pool, value.c_str(), value.size()); + return pool.strmemdup(value.c_str(), value.size()); } break; case kJSTypeObject: @@ -381,14 +382,14 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value } } CYCatch(NULL) } -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value) { +const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value) { JSValueRef exception(NULL); const char *cyon(CYPoolCCYON(pool, context, value, &exception)); CYThrow(context, exception); return cyon; } -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { +const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object) { JSValueRef toCYON(CYGetProperty(context, object, toCYON_s)); if (CYIsCallable(context, toCYON)) { JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL)); @@ -447,7 +448,7 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef obje JSPropertyNameArrayRelease(names); std::string string(str.str()); - return apr_pstrmemdup(pool, string.c_str(), string.size()); + return pool.strmemdup(string.c_str(), string.size()); } static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { @@ -518,7 +519,7 @@ static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const return JSObjectMake(context, Functor_, internal); } -static bool CYGetOffset(apr_pool_t *pool, JSContextRef context, JSStringRef value, ssize_t &index) { +static bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) { return CYGetOffset(CYPoolCString(pool, context, value), index); } @@ -546,7 +547,7 @@ void *CYCastPointer_(JSContextRef context, JSValueRef value) { } } -void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { +void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { switch (type->primitive) { case sig::boolean_P: *reinterpret_cast(data) = JSValueToBoolean(context, value); @@ -596,7 +597,8 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type break; case sig::string_P: - *reinterpret_cast(data) = CYPoolCString(pool, context, value); + _assert(pool != NULL); + *reinterpret_cast(data) = CYPoolCString(*pool, context, value); break; case sig::struct_P: { @@ -774,7 +776,7 @@ static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const c } } -static bool Index_(apr_pool_t *pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { +static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { Type_privateData *typical(internal->type_); sig::Type *type(typical->type_); if (type == NULL) @@ -930,7 +932,7 @@ static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JS } } -JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { CYTry { +JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { CYTry { if (setups + count != signature->count - 1) throw CYJSError(context, "incorrect number of arguments to ffi function"); @@ -943,7 +945,7 @@ JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, ffi_type *ffi(cif->arg_types[index]); // XXX: alignment? values[index] = new(pool) uint8_t[ffi->size]; - CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); + CYPoolFFI(&pool, context, element->type, ffi, values[index], arguments[index - setups]); } uint8_t value[cif->rtype->size]; @@ -1129,7 +1131,7 @@ static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, // XXX: alignment? uint8_t value[ffi->size]; CYPool pool; - CYPoolFFI(pool, context, type, ffi, value, arguments[0]); + CYPoolFFI(&pool, context, type, ffi, value, arguments[0]); return CYFromFFI(context, type, ffi, value); } CYCatch(NULL) } @@ -1293,7 +1295,7 @@ JSObjectRef CYGetGlobalObject(JSContextRef context) { return JSContextGetGlobalObject(context); } -const char *CYExecute(apr_pool_t *pool, CYUTF8String code) { +const char *CYExecute(CYPool &pool, CYUTF8String code) { JSContextRef context(CYGetJSContext()); JSValueRef exception(NULL), result; @@ -1424,7 +1426,7 @@ void CYThrow(JSContextRef context, JSValueRef value) { throw CYJSError(context, value); } -const char *CYJSError::PoolCString(apr_pool_t *pool) const { +const char *CYJSError::PoolCString(CYPool &pool) const { // XXX: this used to be CYPoolCString return CYPoolCCYON(pool, context_, value_); } @@ -1457,7 +1459,7 @@ CYJSError::CYJSError(JSContextRef context, const char *format, ...) { va_list args; va_start(args, format); - const char *message(apr_pvsprintf(pool, format, args)); + const char *message(pool.vsprintf(format, args)); va_end(args); value_ = CYCastJSError(context, message); diff --git a/Handler.mm b/Handler.mm index 6de5e9b..e460483 100644 --- a/Handler.mm +++ b/Handler.mm @@ -38,7 +38,7 @@ #include struct CYExecute_ { - apr_pool_t *pool_; + CYPool &pool_; const char * volatile data_; }; @@ -148,11 +148,11 @@ static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) { return NULL; } -extern "C" void CYHandleClient(apr_pool_t *pool, int socket) { +extern "C" void CYHandleClient(CYPool &pool, int socket) { CYClient *client(new(pool) CYClient(socket)); apr_threadattr_t *attr; - _aprcall(apr_threadattr_create(&attr, client->pool_)); - _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_)); + _aprcall(apr_threadattr_create(&attr, *client->pool_)); + _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, *client->pool_)); } extern "C" void CYHandleServer(pid_t pid) { @@ -166,10 +166,8 @@ extern "C" void CYHandleServer(pid_t pid) { _syscall(connect(socket, reinterpret_cast(&address), SUN_LEN(&address))); - apr_pool_t *pool; - apr_pool_create(&pool, NULL); - - CYHandleClient(pool, socket); + // XXX: this leaks memory... really? + CYHandleClient(*new CYPool(), socket); } catch (const CYException &error) { CYPool pool; fprintf(stderr, "%s\n", error.PoolCString(pool)); diff --git a/Internal.hpp b/Internal.hpp index e01f323..f7ad60d 100644 --- a/Internal.hpp +++ b/Internal.hpp @@ -22,18 +22,18 @@ #ifndef CYCRIPT_INTERNAL_HPP #define CYCRIPT_INTERNAL_HPP -#include "Pooling.hpp" +#include +#include #include #include #include #include -#include -#include +#include "Pooling.hpp" JSGlobalContextRef CYGetJSContext(JSContextRef context); -void Structor_(apr_pool_t *pool, sig::Type *&type); +void Structor_(CYPool &pool, sig::Type *&type); JSObjectRef CYMakeType(JSContextRef context, const char *type); JSObjectRef CYMakeType(JSContextRef context, sig::Type *type); @@ -49,17 +49,16 @@ struct Type_privateData : sig::Type *type_; void Set(sig::Type *type) { - type_ = new(pool_) sig::Type; - sig::Copy(pool_, *type_, *type); + type_ = new(*pool_) sig::Type; + sig::Copy(*pool_, *type_, *type); } - Type_privateData(apr_pool_t *pool, const char *type) : + Type_privateData(CYPool &pool, const char *type) : + CYData(pool), ffi_(NULL) { - _assert(pool != NULL); - pool_ = pool; sig::Signature signature; - sig::Parse(pool_, &signature, type, &Structor_); + sig::Parse(*pool_, &signature, type, &Structor_); type_ = signature.elements[0].type; } @@ -67,7 +66,7 @@ struct Type_privateData : ffi_(NULL) { sig::Signature signature; - sig::Parse(pool_, &signature, type, &Structor_); + sig::Parse(*pool_, &signature, type, &Structor_); type_ = signature.elements[0].type; } @@ -79,14 +78,14 @@ struct Type_privateData : } Type_privateData(sig::Type *type, ffi_type *ffi) { - ffi_ = new(pool_) ffi_type; - sig::Copy(pool_, *ffi_, *ffi); + ffi_ = new(*pool_) ffi_type; + sig::Copy(*pool_, *ffi_, *ffi); Set(type); } ffi_type *GetFFI() { if (ffi_ == NULL) { - ffi_ = new(pool_) ffi_type; + ffi_ = new(*pool_) ffi_type; sig::Element element; element.name = NULL; @@ -98,7 +97,7 @@ struct Type_privateData : signature.count = 1; ffi_cif cif; - sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif); + sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif); *ffi_ = *cif.rtype; } @@ -168,8 +167,8 @@ struct Functor : Functor(const char *type, void (*value)()) : CYValue(reinterpret_cast(value)) { - sig::Parse(pool_, &signature_, type, &Structor_); - sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); + sig::Parse(*pool_, &signature_, type, &Structor_); + sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_); } void (*GetValue() const)() { diff --git a/JavaScript.hpp b/JavaScript.hpp index 26f5d88..8f99cee 100644 --- a/JavaScript.hpp +++ b/JavaScript.hpp @@ -34,9 +34,8 @@ #include #endif -#include - -#include +#include "Pooling.hpp" +#include "String.hpp" extern JSStringRef Array_s; extern JSStringRef cy_s; @@ -57,15 +56,15 @@ JSGlobalContextRef CYGetJSContext(); JSObjectRef CYGetGlobalObject(JSContextRef context); extern "C" void CYSetupContext(JSGlobalContextRef context); -const char *CYExecute(apr_pool_t *pool, CYUTF8String code); +const char *CYExecute(CYPool &pool, CYUTF8String code); void CYSetArgs(int argc, const char *argv[]); bool CYCastBool(JSContextRef context, JSValueRef value); double CYCastDouble(JSContextRef context, JSValueRef value); -CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value); -const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value); +CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value); +const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value); JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index); JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name); @@ -99,15 +98,15 @@ _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { return reinterpret_cast(CYCastPointer_(context, value)); } -void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value); +void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value); JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL); -JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()); +JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()); bool CYIsCallable(JSContextRef context, JSValueRef value); JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]); -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object); +const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object); struct CYHooks { void *(*ExecuteStart)(JSContextRef); @@ -118,7 +117,7 @@ struct CYHooks { void (*Initialize)(); void (*SetupContext)(JSContextRef); - bool (*PoolFFI)(apr_pool_t *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef); + bool (*PoolFFI)(CYPool *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef); JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef); }; @@ -132,7 +131,7 @@ size_t CYArrayLength(JSContextRef context, JSObjectRef array); JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index); void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value); -const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value); +const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value); JSStringRef CYCopyJSString(const char *value); JSStringRef CYCopyJSString(JSStringRef value); diff --git a/Library.cpp b/Library.cpp index 0db5f42..37e2b3c 100644 --- a/Library.cpp +++ b/Library.cpp @@ -56,7 +56,7 @@ _finline size_t iconv_(size_t (*iconv)(iconv_t, Type_, size_t *, char **, size_t #define UCS_2_INTERNAL "UCS-2-INTERNAL" #endif -CYUTF8String CYPoolUTF8String(apr_pool_t *pool, CYUTF16String utf16) { +CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) { _assert(pool != NULL); const char *in(reinterpret_cast(utf16.data)); @@ -79,7 +79,7 @@ CYUTF8String CYPoolUTF8String(apr_pool_t *pool, CYUTF16String utf16) { return utf8; } -CYUTF16String CYPoolUTF16String(apr_pool_t *pool, CYUTF8String utf8) { +CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) { _assert(pool != NULL); const char *in(utf8.data); @@ -262,14 +262,13 @@ extern "C" void CydgetPoolParse(apr_pool_t *remote, const uint16_t **data, size_ out << *driver.program_; std::string code(str.str()); - CYUTF16String utf16(CYPoolUTF16String(remote, CYUTF8String(code.c_str(), code.size()))); + CYPool pool(remote); + CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(code.c_str(), code.size()))); *data = utf16.data; *size = utf16.size; } -static apr_pool_t *Pool_; - static bool initialized_; void CYInitializeStatic() { @@ -278,12 +277,12 @@ void CYInitializeStatic() { else return; _aprcall(apr_initialize()); - _aprcall(apr_pool_create(&Pool_, NULL)); } -apr_pool_t *CYGetGlobalPool() { +CYPool &CYGetGlobalPool() { CYInitializeStatic(); - return Pool_; + static CYPool pool; + return pool; } void CYThrow(const char *format, ...) { @@ -294,17 +293,17 @@ void CYThrow(const char *format, ...) { va_end(args); } -const char *CYPoolError::PoolCString(apr_pool_t *pool) const { - return apr_pstrdup(pool, message_); +const char *CYPoolError::PoolCString(CYPool &pool) const { + return pool.strdup(message_); } CYPoolError::CYPoolError(const char *format, ...) { va_list args; va_start(args, format); - message_ = apr_pvsprintf(pool_, format, args); + message_ = pool_.vsprintf(format, args); va_end(args); } CYPoolError::CYPoolError(const char *format, va_list args) { - message_ = apr_pvsprintf(pool_, format, args); + message_ = pool_.vsprintf(format, args); } diff --git a/Mach/Inject.cpp b/Mach/Inject.cpp index c2c735e..3f4923b 100644 --- a/Mach/Inject.cpp +++ b/Mach/Inject.cpp @@ -43,7 +43,7 @@ void InjectLibrary(pid_t pid) { depth = (depth + sizeof(uintptr_t) + 1) / sizeof(uintptr_t) * sizeof(uintptr_t); CYPool pool; - uint8_t *local(reinterpret_cast(apr_palloc(pool, depth))); + uint8_t *local(reinterpret_cast(pool(depth))); Baton *baton(reinterpret_cast(local)); baton->__pthread_set_self = &__pthread_set_self; diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 5873277..e430c0c 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -159,7 +159,7 @@ enum { BLOCK_HAS_SIGNATURE = 1 << 30, }; -JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception); +JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception); /* Objective-C Pool Release {{{ */ apr_status_t CYPoolRelease_(void *data) { @@ -168,24 +168,24 @@ apr_status_t CYPoolRelease_(void *data) { return APR_SUCCESS; } -id CYPoolRelease_(apr_pool_t *pool, id object) { +id CYPoolRelease_(CYPool *pool, id object) { if (object == nil) return nil; else if (pool == NULL) return [object autorelease]; else { - apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); + apr_pool_cleanup_register(*pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); return object; } } template -Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) { +Type_ CYPoolRelease(CYPool *pool, Type_ object) { return (Type_) CYPoolRelease_(pool, (id) object); } /* }}} */ /* Objective-C Strings {{{ */ -const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) { +const char *CYPoolCString(CYPool &pool, JSContextRef context, NSString *value) { if (pool == NULL) return [value UTF8String]; else { @@ -235,16 +235,16 @@ NSString *CYCopyNSString(JSContextRef context, JSValueRef value) { return CYCopyNSString(context, CYJSString(context, value)); } -NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) { +NSString *CYCastNSString(CYPool *pool, const CYUTF8String &value) { return CYPoolRelease(pool, CYCopyNSString(value)); } -NSString *CYCastNSString(apr_pool_t *pool, SEL sel) { +NSString *CYCastNSString(CYPool *pool, SEL sel) { const char *name(sel_getName(sel)); return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name)))); } -NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) { +NSString *CYCastNSString(CYPool *pool, JSContextRef context, JSStringRef value) { return CYPoolRelease(pool, CYCopyNSString(context, value)); } @@ -266,7 +266,7 @@ size_t CYGetIndex(NSString *value) { return CYGetIndex(CYCastUTF8String(value)); } -bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) { +bool CYGetOffset(CYPool &pool, JSContextRef context, NSString *value, ssize_t &index) { return CYGetOffset(CYPoolCString(pool, context, value), index); } @@ -519,7 +519,7 @@ struct PropertyAttributes { name = property_getName(property); const char *attributes(property_getAttributes(property)); - for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { + for (char *state, *token(apr_strtok(pool_.strdup(attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { switch (*token) { case 'R': readonly = true; break; case 'C': copy = true; break; @@ -544,7 +544,7 @@ struct PropertyAttributes { const char *Getter() { if (getter_ == NULL) - getter_ = apr_pstrdup(pool_, name); + getter_ = pool_.strdup(name); return getter_; } @@ -669,7 +669,7 @@ NSObject *CYMakeBlock(void (*invoke)(), sig::Signature &signature) { return reinterpret_cast(literal); } -NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { +NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSObjectRef object) { if (CYJSValueIsNSObject(context, object)) { Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); return internal->GetValue(); @@ -695,7 +695,7 @@ NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { @end #endif -id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { +id CYNSObject(CYPool *pool, JSContextRef context, JSValueRef value, bool cast) { id object; bool copy; @@ -748,12 +748,12 @@ id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cas return [object retain]; } -NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { +NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSValueRef value) { return CYNSObject(pool, context, value, true); } -NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { - return CYNSObject(pool, context, value, false); +NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) { + return CYNSObject(&pool, context, value, false); } /* Bridge: NSArray {{{ */ @@ -1182,8 +1182,8 @@ static bool CYIsClass(id self) { #endif } -Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) { - id self(CYCastNSObject(pool, context, value)); +Class CYCastClass(CYPool &pool, JSContextRef context, JSValueRef value) { + id self(CYCastNSObject(&pool, context, value)); if (CYIsClass(self)) return (Class) self; throw CYJSError(context, "got something that is not a Class"); @@ -1195,7 +1195,7 @@ NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) { size_t size(JSPropertyNameArrayGetCount(names)); NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); for (size_t index(0); index != size; ++index) - [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))]; + [array addObject:CYCastNSString(&pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))]; return array; } @@ -1466,12 +1466,13 @@ static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void ( ffi_call(cif, function, value, values); } CYSadCatch() } -static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry { +static bool CYObjectiveC_PoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry { switch (type->primitive) { // XXX: do something epic about blocks case sig::block_P: case sig::object_P: case sig::typename_P: + // XXX: this works for return values, but not for properties and fields *reinterpret_cast(data) = CYCastNSObject(pool, context, value); break; @@ -1531,7 +1532,7 @@ static bool CYImplements(id object, Class _class, SEL selector, bool devoid = fa return false; } -static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) { +static const char *CYPoolTypeEncoding(CYPool &pool, JSContextRef context, SEL sel, objc_method *method) { if (method != NULL) return method_getTypeEncoding(method); @@ -1688,7 +1689,7 @@ static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStr return true; CYPool pool; - NSString *name(CYCastNSString(pool, context, property)); + NSString *name(CYCastNSString(&pool, context, property)); if (CYInternal *internal = [CYInternal get:self]) if ([internal hasProperty:property inContext:context]) @@ -1726,7 +1727,7 @@ static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, return Internal::Make(context, self, object); CYPool pool; - NSString *name(CYCastNSString(pool, context, property)); + NSString *name(CYCastNSString(&pool, context, property)); if (CYInternal *internal = [CYInternal get:self]) if (JSValueRef value = [internal getProperty:property inContext:context]) @@ -1762,8 +1763,8 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr CYPool pool; - NSString *name(CYCastNSString(pool, context, property)); - NSObject *data(CYCastNSObject(pool, context, value)); + NSString *name(CYCastNSString(&pool, context, property)); + NSObject *data(CYCastNSObject(&pool, context, value)); CYPoolTry { if ([self cy$setProperty:name to:data]) @@ -1950,7 +1951,7 @@ static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef if (count == 0) throw CYJSError(context, "incorrect number of arguments to Instance"); CYPool pool; - id value(CYCastNSObject(pool, context, arguments[0])); + id value(CYCastNSObject(&pool, context, arguments[0])); if (value == nil) value = [NSNull null]; return CYCastJSValue(context, [value cy$box]); @@ -1994,7 +1995,7 @@ static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStr if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) { Type_privateData type(pool, ivar_getTypeEncoding(ivar)); - CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast(self) + ivar_getOffset(ivar), value); + CYPoolFFI(&pool, context, type.type_, type.GetFFI(), reinterpret_cast(self) + ivar_getOffset(ivar), value); return true; } @@ -2035,7 +2036,7 @@ static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { CYPool pool; - NSString *name(CYCastNSString(pool, context, property)); + NSString *name(CYCastNSString(&pool, context, property)); if (Class _class = NSClassFromString(name)) return CYMakeInstance(context, _class, true); return NULL; @@ -2182,7 +2183,7 @@ static bool stret(ffi_type *ffi_type) { } #endif -JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry { +JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry { const char *type; if (_class == NULL) @@ -2275,7 +2276,7 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje if (uninitialized) internal->value_ = nil; } else { - self = CYCastNSObject(pool, context, arguments[0]); + self = CYCastNSObject(&pool, context, arguments[0]); _class = nil; uninitialized = false; } @@ -2301,7 +2302,7 @@ static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef objec Message_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); // XXX: handle Instance::Uninitialized? - id self(CYCastNSObject(pool, context, _this)); + id self(CYCastNSObject(&pool, context, _this)); void *setup[2]; setup[0] = &self; @@ -2314,7 +2315,7 @@ static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t co if (count != 2) throw CYJSError(context, "incorrect number of arguments to Super constructor"); CYPool pool; - id self(CYCastNSObject(pool, context, arguments[0])); + id self(CYCastNSObject(&pool, context, arguments[0])); Class _class(CYCastClass(pool, context, arguments[1])); return cy::Super::Make(context, self, _class); } CYCatch(NULL) } @@ -2560,7 +2561,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { $objc_getAssociatedObject = reinterpret_cast(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject")); $objc_removeAssociatedObjects = reinterpret_cast(dlsym(RTLD_DEFAULT, "objc_removeAssociatedObjects")); - apr_pool_t *pool(CYGetGlobalPool()); + CYPool &pool(CYGetGlobalPool()); Object_type = new(pool) Type_privateData("@"); Selector_type = new(pool) Type_privateData(":"); diff --git a/ObjectiveC/Replace.cpp b/ObjectiveC/Replace.cpp index a7b6610..9197a26 100644 --- a/ObjectiveC/Replace.cpp +++ b/ObjectiveC/Replace.cpp @@ -191,7 +191,7 @@ CYString *CYSelectorPart::Replace(CYContext &context) { if (part->value_) str << ':'; } - return $S(apr_pstrdup($pool, str.str().c_str())); + return $S($pool.strdup(str.str().c_str())); } CYExpression *CYSendDirect::Replace(CYContext &context) { diff --git a/Pooling.hpp b/Pooling.hpp index ea9f995..807182e 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -22,6 +22,9 @@ #ifndef CYCRIPT_POOLING_HPP #define CYCRIPT_POOLING_HPP +#include +#include + #include #include @@ -29,16 +32,6 @@ #include "Local.hpp" #include "Standard.hpp" -#include - -_finline void *operator new(size_t size, apr_pool_t *pool) { - return apr_palloc(pool, size); -} - -_finline void *operator new [](size_t size, apr_pool_t *pool) { - return apr_palloc(pool, size); -} - class CYPool { private: apr_pool_t *pool_; @@ -48,6 +41,11 @@ class CYPool { _aprcall(apr_pool_create(&pool_, NULL)); } + CYPool(apr_pool_t *pool) : + pool_(pool) + { + } + ~CYPool() { apr_pool_destroy(pool_); } @@ -60,17 +58,45 @@ class CYPool { return pool_; } - char *operator ()(const char *data) const { + void *operator()(size_t size) const { + return apr_palloc(pool_, size); + } + + char *strdup(const char *data) const { return apr_pstrdup(pool_, data); } - char *operator ()(const char *data, size_t size) const { + char *strndup(const char *data, size_t size) const { return apr_pstrndup(pool_, data, size); } + + char *strmemdup(const char *data, size_t size) const { + return apr_pstrmemdup(pool_, data, size); + } + + char *sprintf(const char *format, ...) const { + va_list args; + va_start(args, format); + char *data(vsprintf(format, args)); + va_end(args); + return data; + } + + char *vsprintf(const char *format, va_list args) const { + return apr_pvsprintf(pool_, format, args); + } }; +_finline void *operator new(size_t size, CYPool &pool) { + return pool(size); +} + +_finline void *operator new [](size_t size, CYPool &pool) { + return pool(size); +} + struct CYData { - apr_pool_t *pool_; + CYPool *pool_; unsigned count_; CYData() : @@ -78,29 +104,33 @@ struct CYData { { } + CYData(CYPool &pool) : + pool_(&pool), + count_(_not(unsigned)) + { + } + virtual ~CYData() { } - static void *operator new(size_t size, apr_pool_t *pool) { - void *data(apr_palloc(pool, size)); - reinterpret_cast(data)->pool_ = pool; + static void *operator new(size_t size, CYPool &pool) { + void *data(pool(size)); + reinterpret_cast(data)->pool_ = &pool; return data; } static void *operator new(size_t size) { - apr_pool_t *pool; - _aprcall(apr_pool_create(&pool, NULL)); - return operator new(size, pool); + return operator new(size, *new CYPool()); } static void operator delete(void *data) { - apr_pool_destroy(reinterpret_cast(data)->pool_); + delete reinterpret_cast(data)->pool_; } }; template struct CYPoolAllocator { - apr_pool_t *pool_; + CYPool *pool_; typedef Type_ value_type; typedef value_type *pointer; @@ -122,7 +152,7 @@ struct CYPoolAllocator { } pointer allocate(size_type size, const void *hint = 0) { - return reinterpret_cast(apr_palloc(pool_, size)); + return reinterpret_cast((*pool_)(size)); } void deallocate(pointer data, size_type size) { @@ -156,17 +186,17 @@ class CYLocalPool : public CYPool { private: - CYLocal local_; + CYLocal local_; public: CYLocalPool() : CYPool(), - local_(operator apr_pool_t *()) + local_(this) { } }; #define $pool \ - CYLocal::Get() + (*CYLocal::Get()) #endif/*CYCRIPT_POOLING_HPP*/ diff --git a/Replace.cpp b/Replace.cpp index 98fd48a..1e0a5ca 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -210,7 +210,7 @@ void CYContext::NonLocal(CYStatement *&statements) { } CYIdentifier *CYContext::Unique() { - return $ CYIdentifier(apr_psprintf($pool, "$cy%u", unique_++)); + return $ CYIdentifier($pool.sprintf("$cy%u", unique_++)); } CYStatement *CYContinue::Replace(CYContext &context) { @@ -555,7 +555,7 @@ CYNumber *CYNumber::Number(CYContext &context) { CYString *CYNumber::String(CYContext &context) { // XXX: there is a precise algorithm for this - return $S(apr_psprintf($pool, "%.17g", Value())); + return $S($pool.sprintf("%.17g", Value())); } CYExpression *CYObject::Replace(CYContext &context) { @@ -619,7 +619,7 @@ void CYProgram::Replace(CYContext &context) { const char *name; if (context.options_.verbose_) - name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset); + name = $pool.sprintf("$%"APR_SIZE_T_FMT"", offset); else { char id[8]; id[7] = '\0'; @@ -638,7 +638,7 @@ void CYProgram::Replace(CYContext &context) { goto id; } - name = apr_pstrmemdup($pool, id + position, 7 - position); + name = $pool.strmemdup(id + position, 7 - position); // XXX: at some point, this could become a keyword } diff --git a/String.hpp b/String.hpp index e9a0dbe..12a2b23 100644 --- a/String.hpp +++ b/String.hpp @@ -69,7 +69,7 @@ size_t CYGetIndex(const CYUTF8String &value); bool CYIsKey(CYUTF8String value); bool CYGetOffset(const char *value, ssize_t &index); -CYUTF8String CYPoolUTF8String(apr_pool_t *pool, CYUTF16String utf16); -CYUTF16String CYPoolUTF16String(apr_pool_t *pool, CYUTF8String utf8); +CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16); +CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8); #endif/*CYCRIPT_STRING_HPP*/ diff --git a/cycript.hpp b/cycript.hpp index e9284e7..841cf16 100644 --- a/cycript.hpp +++ b/cycript.hpp @@ -22,10 +22,10 @@ #ifndef CYCRIPT_HPP #define CYCRIPT_HPP -#include #include #include +#include "Pooling.hpp" #include "String.hpp" void CYInitializeStatic(); @@ -39,7 +39,7 @@ void CYStringify(std::ostringstream &str, const char *data, size_t size); double CYCastDouble(const char *value, size_t size); double CYCastDouble(const char *value); -extern "C" void CYHandleClient(apr_pool_t *pool, int socket); +extern "C" void CYHandleClient(CYPool &pool, int socket); template bool CYRecvAll(int socket, Type_ *data, size_t size) { @@ -51,6 +51,6 @@ bool CYSendAll(int socket, const Type_ *data, size_t size) { return CYSendAll_(socket, reinterpret_cast(data), size); } -apr_pool_t *CYGetGlobalPool(); +CYPool &CYGetGlobalPool(); #endif/*CYCRIPT_HPP*/ diff --git a/sig/copy.cpp b/sig/copy.cpp index 7fd7fe2..b38405e 100644 --- a/sig/copy.cpp +++ b/sig/copy.cpp @@ -23,7 +23,6 @@ #define _GNU_SOURCE #endif -#include #include "Pooling.hpp" #include "sig/parse.hpp" @@ -35,8 +34,8 @@ namespace sig { -void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { - lhs.name = apr_pstrdup(pool, rhs.name); +void Copy(CYPool &pool, Element &lhs, Element &rhs) { + lhs.name = pool.strdup(rhs.name); if (rhs.type == NULL) lhs.type = NULL; else { @@ -46,7 +45,7 @@ void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { lhs.offset = rhs.offset; } -void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { +void Copy(CYPool &pool, Signature &lhs, Signature &rhs) { size_t count(rhs.count); lhs.count = count; lhs.elements = new(pool) Element[count]; @@ -54,9 +53,9 @@ void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { Copy(pool, lhs.elements[index], rhs.elements[index]); } -void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { +void Copy(CYPool &pool, Type &lhs, Type &rhs) { lhs.primitive = rhs.primitive; - lhs.name = apr_pstrdup(pool, rhs.name); + lhs.name = pool.strdup(rhs.name); lhs.flags = rhs.flags; if (sig::IsAggregate(rhs.primitive)) @@ -76,7 +75,7 @@ void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { } } -void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { +void Copy(CYPool &pool, ffi_type &lhs, ffi_type &rhs) { lhs.size = rhs.size; lhs.alignment = rhs.alignment; lhs.type = rhs.type; diff --git a/sig/ffi_type.cpp b/sig/ffi_type.cpp index 6a78f32..7d552ba 100644 --- a/sig/ffi_type.cpp +++ b/sig/ffi_type.cpp @@ -30,8 +30,8 @@ namespace sig { void sig_ffi_types( - apr_pool_t *pool, - ffi_type *(*sig_ffi_type)(apr_pool_t *, struct Type *), + CYPool &pool, + ffi_type *(*sig_ffi_type)(CYPool &, struct Type *), struct Signature *signature, ffi_type **types, size_t skip = 0, @@ -42,7 +42,7 @@ void sig_ffi_types( types[index - skip + offset] = (*sig_ffi_type)(pool, signature->elements[index].type); } -ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type) { +ffi_type *ObjectiveC(CYPool &pool, struct Type *type) { switch (type->primitive) { case typename_P: return &ffi_type_pointer; @@ -64,7 +64,7 @@ ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type) { case array_P: { // XXX: this is really lame - ffi_type *aggregate(reinterpret_cast(apr_palloc(pool, sizeof(ffi_type)))); + ffi_type *aggregate(reinterpret_cast(pool(sizeof(ffi_type)))); aggregate->size = 0; aggregate->alignment = 0; aggregate->type = FFI_TYPE_STRUCT; @@ -72,7 +72,7 @@ ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type) { ffi_type *element(ObjectiveC(pool, type->data.data.type)); size_t size(type->data.data.size); - aggregate->elements = reinterpret_cast(apr_palloc(pool, (size + 1) * sizeof(ffi_type *))); + aggregate->elements = reinterpret_cast(pool((size + 1) * sizeof(ffi_type *))); for (size_t i(0); i != size; ++i) aggregate->elements[i] = element; aggregate->elements[size] = NULL; @@ -98,12 +98,12 @@ ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type) { case void_P: return &ffi_type_void; case struct_P: { - ffi_type *aggregate(reinterpret_cast(apr_palloc(pool, sizeof(ffi_type)))); + ffi_type *aggregate(reinterpret_cast(pool(sizeof(ffi_type)))); aggregate->size = 0; aggregate->alignment = 0; aggregate->type = FFI_TYPE_STRUCT; - aggregate->elements = reinterpret_cast(apr_palloc(pool, (type->data.signature.count + 1) * sizeof(ffi_type *))); + aggregate->elements = reinterpret_cast(pool((type->data.signature.count + 1) * sizeof(ffi_type *))); sig_ffi_types(pool, &ObjectiveC, &type->data.signature, aggregate->elements); aggregate->elements[type->data.signature.count] = NULL; @@ -116,7 +116,7 @@ ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type) { } } -ffi_type *Java(apr_pool_t *pool, struct Type *type) { +ffi_type *Java(CYPool &pool, struct Type *type) { switch (type->primitive) { case typename_P: return &ffi_type_pointer; case union_P: return &ffi_type_pointer; @@ -153,8 +153,8 @@ ffi_type *Java(apr_pool_t *pool, struct Type *type) { } void sig_ffi_cif( - apr_pool_t *pool, - ffi_type *(*sig_ffi_type)(apr_pool_t *, struct Type *), + CYPool &pool, + ffi_type *(*sig_ffi_type)(CYPool &, struct Type *), struct Signature *signature, ffi_cif *cif, size_t skip, @@ -162,7 +162,7 @@ void sig_ffi_cif( size_t offset ) { if (types == NULL) - types = reinterpret_cast(apr_palloc(pool, (signature->count - 1) * sizeof(ffi_type *))); + types = reinterpret_cast(pool((signature->count - 1) * sizeof(ffi_type *))); ffi_type *type = (*sig_ffi_type)(pool, signature->elements[0].type); sig_ffi_types(pool, sig_ffi_type, signature, types, 1 + skip, offset); ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, signature->count - 1 - skip + offset, type, types); diff --git a/sig/ffi_type.hpp b/sig/ffi_type.hpp index 1294ebf..bcb4323 100644 --- a/sig/ffi_type.hpp +++ b/sig/ffi_type.hpp @@ -22,24 +22,23 @@ #ifndef SIG_FFI_TYPE_H #define SIG_FFI_TYPE_H -#include - #ifdef HAVE_FFI_FFI_H #include #else #include #endif +#include "Pooling.hpp" #include "sig/types.hpp" namespace sig { -ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type); -ffi_type *Java(apr_pool_t *pool, struct Type *type); +ffi_type *ObjectiveC(CYPool &pool, struct Type *type); +ffi_type *Java(CYPool &pool, struct Type *type); void sig_ffi_cif( - apr_pool_t *pool, - ffi_type *(*sig_ffi_type)(apr_pool_t *, struct Type *), + CYPool &pool, + ffi_type *(*sig_ffi_type)(CYPool &, struct Type *), struct Signature *signature, ffi_cif *cif, size_t skip = 0, @@ -47,7 +46,7 @@ void sig_ffi_cif( size_t offset = 0 ); -void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs); +void Copy(CYPool &pool, ffi_type &lhs, ffi_type &rhs); } diff --git a/sig/parse.cpp b/sig/parse.cpp index a0e1a96..8de5eb4 100644 --- a/sig/parse.cpp +++ b/sig/parse.cpp @@ -19,7 +19,6 @@ **/ /* }}} */ -#include #include "sig/parse.hpp" #include "Error.hpp" @@ -29,18 +28,18 @@ namespace sig { -void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback); -struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback); +void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback); +struct Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback); /* XXX: I really screwed up this time */ -void *prealloc_(apr_pool_t *pool, void *odata, size_t osize, size_t nsize) { - void *ndata = apr_palloc(pool, nsize); +void *prealloc_(CYPool &pool, void *odata, size_t osize, size_t nsize) { + void *ndata(pool(nsize)); memcpy(ndata, odata, osize); return ndata; } -void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback) { +void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback) { _assert(*name != NULL); // XXX: this is just a stupid check :( @@ -64,7 +63,7 @@ void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, ch element->name = NULL; else { const char *quote = strchr(++*name, '"'); - element->name = apr_pstrmemdup(pool, *name, quote - *name); + element->name = pool.strmemdup(*name, quote - *name); *name = quote + 1; } @@ -82,14 +81,14 @@ void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, ch } } -struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback) { +Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback) { char next = *(*name)++; if (next == '?') return NULL; - struct Type *type = (struct Type *) apr_palloc(pool, sizeof(struct Type)); + Type *type(new(pool) Type()); _assert(type != NULL); - memset(type, 0, sizeof(struct Type)); + memset(type, 0, sizeof(Type)); parse: switch (next) { @@ -121,7 +120,7 @@ struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, C printf("unterminated specific id type {%s}\n", *name - 10); _assert(false); } else if (!named || quote[1] == eos || quote[1] == '"') { - type->name = apr_pstrmemdup(pool, *name + 1, quote - *name - 1); + type->name = pool.strmemdup(*name + 1, quote - *name - 1); *name = quote + 1; } } @@ -188,7 +187,7 @@ struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, C ); size_t length = *name - begin - 1; if (strncmp(begin, "?", length) != 0) - type->name = (char *) apr_pstrmemdup(pool, begin, length); + type->name = (char *) pool.strmemdup(begin, length); else type->name = NULL; @@ -222,13 +221,13 @@ struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, C return type; } -void Parse(apr_pool_t *pool, struct Signature *signature, const char *name, Callback callback) { +void Parse(CYPool &pool, struct Signature *signature, const char *name, Callback callback) { const char *temp = name; Parse_(pool, signature, &temp, '\0', callback); _assert(temp[-1] == '\0'); } -const char *Unparse(apr_pool_t *pool, struct Signature *signature) { +const char *Unparse(CYPool &pool, struct Signature *signature) { const char *value = ""; size_t offset; @@ -240,14 +239,14 @@ const char *Unparse(apr_pool_t *pool, struct Signature *signature) { return value; } -const char *Unparse_(apr_pool_t *pool, struct Type *type) { +const char *Unparse_(CYPool &pool, struct Type *type) { switch (type->primitive) { case typename_P: return "#"; - case union_P: return apr_psprintf(pool, "(%s)", Unparse(pool, &type->data.signature)); + case union_P: return pool.sprintf("(%s)", Unparse(pool, &type->data.signature)); case string_P: return "*"; case selector_P: return ":"; case block_P: return "@?"; - case object_P: return type->name == NULL ? "@" : apr_psprintf(pool, "@\"%s\"", type->name); + case object_P: return type->name == NULL ? "@" : pool.sprintf("@\"%s\"", type->name); case boolean_P: return "B"; case uchar_P: return "C"; case uint_P: return "I"; @@ -257,11 +256,11 @@ const char *Unparse_(apr_pool_t *pool, struct Type *type) { case array_P: { const char *value = Unparse(pool, type->data.data.type); - return apr_psprintf(pool, "[%"APR_SIZE_T_FMT"%s]", type->data.data.size, value); + return pool.sprintf("[%"APR_SIZE_T_FMT"%s]", type->data.data.size, value); } break; - case pointer_P: return apr_psprintf(pool, "^%s", type->data.data.type == NULL ? "v" : Unparse(pool, type->data.data.type)); - case bit_P: return apr_psprintf(pool, "b%"APR_SIZE_T_FMT"", type->data.data.size); + case pointer_P: return pool.sprintf("^%s", type->data.data.type == NULL ? "v" : Unparse(pool, type->data.data.type)); + case bit_P: return pool.sprintf("b%"APR_SIZE_T_FMT"", type->data.data.size); case char_P: return "c"; case double_P: return "d"; case float_P: return "f"; @@ -270,14 +269,14 @@ const char *Unparse_(apr_pool_t *pool, struct Type *type) { case longlong_P: return "q"; case short_P: return "s"; case void_P: return "v"; - case struct_P: return apr_psprintf(pool, "{%s=%s}", type->name == NULL ? "?" : type->name, Unparse(pool, &type->data.signature)); + case struct_P: return pool.sprintf("{%s=%s}", type->name == NULL ? "?" : type->name, Unparse(pool, &type->data.signature)); } _assert(false); return NULL; } -const char *Unparse(apr_pool_t *pool, struct Type *type) { +const char *Unparse(CYPool &pool, struct Type *type) { if (type == NULL) return "?"; diff --git a/sig/parse.hpp b/sig/parse.hpp index 17afdd6..fb6db63 100644 --- a/sig/parse.hpp +++ b/sig/parse.hpp @@ -22,21 +22,20 @@ #ifndef SIG_PARSE_H #define SIG_PARSE_H +#include "Pooling.hpp" #include "sig/types.hpp" -#include - namespace sig { -typedef void (*Callback)(apr_pool_t *pool, Type *&type); -void Parse(apr_pool_t *pool, struct Signature *signature, const char *name, Callback callback); +typedef void (*Callback)(CYPool &pool, Type *&type); +void Parse(CYPool &pool, struct Signature *signature, const char *name, Callback callback); -const char *Unparse(apr_pool_t *pool, struct Signature *signature); -const char *Unparse(apr_pool_t *pool, struct Type *type); +const char *Unparse(CYPool &pool, struct Signature *signature); +const char *Unparse(CYPool &pool, struct Type *type); -void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); -void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs); -void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); +void Copy(CYPool &pool, Type &lhs, Type &rhs); +void Copy(CYPool &pool, Signature &lhs, Signature &rhs); +void Copy(CYPool &pool, Type &lhs, Type &rhs); } diff --git a/sig/types.hpp b/sig/types.hpp index 117c7df..d2367af 100644 --- a/sig/types.hpp +++ b/sig/types.hpp @@ -22,6 +22,9 @@ #ifndef SIG_TYPES_H #define SIG_TYPES_H +#include +#include + #include "Standard.hpp" namespace sig { @@ -54,7 +57,7 @@ enum Primitive { }; struct Element { - char *name; + const char *name; struct Type *type; size_t offset; }; -- 2.45.2