From 64edd9dfbc798337d33d3b070a1174c9a1b09eb9 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Fri, 26 Jun 2015 23:02:32 -0700 Subject: Borrow Cycript's CYPool to avoid Depends: apr-lib. --- Menes/Menes.h | 1 + Menes/Pooling.hpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ MobileCydia.mm | 48 ++++++++++------------ makefile | 1 - sysroot.sh | 2 - 5 files changed, 139 insertions(+), 30 deletions(-) create mode 100644 Menes/Pooling.hpp diff --git a/Menes/Menes.h b/Menes/Menes.h index 5da16ba..0a421c7 100644 --- a/Menes/Menes.h +++ b/Menes/Menes.h @@ -24,6 +24,7 @@ #include "Menes/Function.h" #include "Menes/ObjectHandle.h" +#include "Menes/Pooling.hpp" #include "Menes/invocationWithSelector.h" #include "Menes/radixSortWithSelector.h" diff --git a/Menes/Pooling.hpp b/Menes/Pooling.hpp new file mode 100644 index 0000000..751f397 --- /dev/null +++ b/Menes/Pooling.hpp @@ -0,0 +1,117 @@ +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2014 Jay Freeman (saurik) +*/ + +/* GNU Affero General Public License, Version 3 {{{ */ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . +**/ +/* }}} */ + +#ifndef Menes_Pooling_HPP +#define Menes_Pooling_HPP + +#include +#include +#include +#include + +#include + +#include + +class CYPool; +_finline void *operator new(size_t size, CYPool &pool); +_finline void *operator new [](size_t size, CYPool &pool); + +class CYPool { + private: + uint8_t *data_; + size_t size_; + + struct Cleaner { + Cleaner *next_; + void (*code_)(void *); + void *data_; + + Cleaner(Cleaner *next, void (*code)(void *), void *data) : + next_(next), + code_(code), + data_(data) + { + } + } *cleaner_; + + static _finline size_t align(size_t size) { + // XXX: alignment is more complex than this + return (size + 7) & ~0x3; + } + + template + static void delete_(void *data) { + reinterpret_cast(data)->~Type_(); + } + + CYPool(const CYPool &); + + public: + CYPool() : + data_(NULL), + size_(0), + cleaner_(NULL) + { + } + + ~CYPool() { + for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) { + Cleaner *next(cleaner->next_); + (*cleaner->code_)(cleaner->data_); + cleaner = next; + } + } + + template + Type_ *malloc(size_t size) { + size = align(size); + + if (size > size_) { + // XXX: is this an optimal malloc size? + size_ = std::max(size, size + align(sizeof(Cleaner))); + data_ = reinterpret_cast(::malloc(size_)); + atexit(free, data_); + _assert(size <= size_); + } + + void *data(data_); + data_ += size; + size_ -= size; + return reinterpret_cast(data); + } + + void atexit(void (*code)(void *), void *data = NULL); +}; + +_finline void *operator new(size_t size, CYPool &pool) { + return pool.malloc(size); +} + +_finline void *operator new [](size_t size, CYPool &pool) { + return pool.malloc(size); +} + +_finline void CYPool::atexit(void (*code)(void *), void *data) { + cleaner_ = new(*this) Cleaner(cleaner_, code, data); +} + +#endif/*Menes_Pooling_HPP*/ diff --git a/MobileCydia.mm b/MobileCydia.mm index 0625b35..1968599 100644 --- a/MobileCydia.mm +++ b/MobileCydia.mm @@ -83,8 +83,6 @@ #include #include -#include - #include #include #include @@ -580,14 +578,14 @@ class CYString { cache_ = reinterpret_cast(CFRetain(rhs.cache_)); } - void copy(apr_pool_t *pool) { - char *temp(reinterpret_cast(apr_palloc(pool, size_ + 1))); + void copy(CYPool *pool) { + char *temp(pool->malloc(size_ + 1)); memcpy(temp, data_, size_); temp[size_] = '\0'; data_ = temp; } - void set(apr_pool_t *pool, const char *data, size_t size) { + void set(CYPool *pool, const char *data, size_t size) { if (size == 0) clear(); else { @@ -601,11 +599,11 @@ class CYString { } } - _finline void set(apr_pool_t *pool, const char *data) { + _finline void set(CYPool *pool, const char *data) { set(pool, data, data == NULL ? 0 : strlen(data)); } - _finline void set(apr_pool_t *pool, const std::string &rhs) { + _finline void set(CYPool *pool, const std::string &rhs) { set(pool, rhs.data(), rhs.size()); } @@ -1118,7 +1116,7 @@ typedef std::map< unsigned long, _H > SourceMap; @interface Database : NSObject { NSZone *zone_; - apr_pool_t *pool_; + CYPool pool_; unsigned era_; @@ -1556,7 +1554,7 @@ static void PackageImport(const void *key, const void *value, void *context) { _transient NSObject *delegate_; } -- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(apr_pool_t *)pool; +- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool; - (NSComparisonResult) compareByName:(Source *)source; @@ -1639,7 +1637,7 @@ static void PackageImport(const void *key, const void *value, void *context) { return index_; } -- (void) setMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { +- (void) setMetaIndex:(metaIndex *)index inPool:(CYPool *)pool { trusted_ = index->IsTrusted(); uri_.set(pool, index->GetURI()); @@ -1715,7 +1713,7 @@ static void PackageImport(const void *key, const void *value, void *context) { authority_ = [url path]; } -- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(apr_pool_t *)pool { +- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool { if ((self = [super init]) != nil) { era_ = [database era]; database_ = database; @@ -2110,7 +2108,7 @@ struct ParsedPackage { uint32_t ignored_ : 1; uint32_t pooled_ : 1; - apr_pool_t *pool_; + CYPool *pool_; uint32_t rank_; @@ -2139,8 +2137,8 @@ struct ParsedPackage { _H tags_; } -- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; -+ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; +- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; ++ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; - (pkgCache::PkgIterator) iterator; - (void) parse; @@ -2357,7 +2355,7 @@ struct PackageNameOrdering : - (void) dealloc { if (!pooled_) - apr_pool_destroy(pool_); + delete pool_; if (parsed_ != NULL) delete parsed_; [super dealloc]; @@ -2540,11 +2538,11 @@ struct PackageNameOrdering : _end } } -- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { +- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { if ((self = [super init]) != nil) { _profile(Package$initWithVersion) if (pool == NULL) - apr_pool_create(&pool_, NULL); + pool_ = new CYPool(); else { pool_ = pool; pooled_ = true; @@ -2620,7 +2618,7 @@ struct PackageNameOrdering : char *transform; _profile(Package$initWithVersion$Transliterate$apr_palloc) - transform = static_cast(apr_palloc(pool_, length)); + transform = pool_->malloc(length); _end _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$transform) u_strToUTF8WithSub(transform, length, NULL, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); @@ -2719,7 +2717,7 @@ struct PackageNameOrdering : _end } return self; } -+ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { ++ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { pkgCache::VerIterator version; _profile(Package$packageWithIterator$GetCandidateVer) @@ -3520,7 +3518,6 @@ class CydiaLogCleaner : // XXX: actually implement this thing _assert(false); [self releasePackages]; - apr_pool_destroy(pool_); NSRecycleZone(zone_); [super dealloc]; } @@ -3651,7 +3648,6 @@ class CydiaLogCleaner : lock_ = NULL; zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); - apr_pool_create(&pool_, NULL); size_t capacity(MetaFile_->active_); if (capacity == 0) @@ -3801,7 +3797,8 @@ class CydiaLogCleaner : cache_.Close(); - apr_pool_clear(pool_); + pool_.~CYPool(); + new (&pool_) CYPool(); NSRecycleZone(zone_); zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); @@ -3823,7 +3820,7 @@ class CydiaLogCleaner : _profile(reloadDataWithInvocation$Source$initWithMetaIndex) for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { - Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:pool_] autorelease]); + Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:&pool_] autorelease]); [sourceList_ addObject:object]; } _end @@ -3926,7 +3923,7 @@ class CydiaLogCleaner : _profile(reloadDataWithInvocation$packageWithIterator) for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) - if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:pool_ database:self]) + if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self]) //packages.push_back(package); CFArrayAppendValue(packages_, CFRetain(package)); _end @@ -10319,9 +10316,6 @@ int main(int argc, char *argv[]) { CollationStarts_ = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"ʒ",nil]; } /* }}} */ - - apr_app_initialize(&argc, const_cast(&argv), NULL); - /* Parse Arguments {{{ */ bool substrate(false); diff --git a/makefile b/makefile index 4ca2d6f..fbca76b 100644 --- a/makefile +++ b/makefile @@ -42,7 +42,6 @@ libs += -framework SystemConfiguration libs += -framework WebCore libs += -framework WebKit -libs += -lapr-1 libs += -lapt-pkg libs += -licucore diff --git a/sysroot.sh b/sysroot.sh index ec3ee2c..e17d8d9 100755 --- a/sysroot.sh +++ b/sysroot.sh @@ -62,8 +62,6 @@ function extract() { declare -A urls -urls[apr]=http://apt.saurik.com/debs/apr_1.3.3-4_iphoneos-arm.deb -urls[apr-lib]=http://apt.saurik.com/debs/apr-lib_1.3.3-2_iphoneos-arm.deb urls[apt7]=http://apt.saurik.com/debs/apt7_0.7.25.3-7_iphoneos-arm.deb urls[apt7-lib]=http://apt.saurik.com/debs/apt7-lib_0.7.25.3-12_iphoneos-arm.deb urls[coreutils]=http://apt.saurik.com/debs/coreutils_7.4-11_iphoneos-arm.deb -- cgit v1.2.3