X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/1560b2c8f7d30fa6b96625ae0fd30c75d4354871..2447e531132913a88707e5d457fb2946fa3b0478:/Pooling.hpp
diff --git a/Pooling.hpp b/Pooling.hpp
index 854113e..c03dd1d 100644
--- a/Pooling.hpp
+++ b/Pooling.hpp
@@ -1,21 +1,21 @@
/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2013 Jay Freeman (saurik)
+ * Copyright (C) 2009-2015 Jay Freeman (saurik)
*/
-/* GNU General Public License, Version 3 {{{ */
+/* GNU Affero General Public License, Version 3 {{{ */
/*
- * Cycript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation, either version 3 of the License,
- * or (at your option) any later version.
- *
- * Cycript is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Cycript. If not, see .
+ * 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 .
**/
/* }}} */
@@ -23,18 +23,27 @@
#define CYCRIPT_POOLING_HPP
#include
+#include
#include
+#include
-#include
-#include
+#include
+
+#include
#include "Exception.hpp"
#include "Local.hpp"
#include "Standard.hpp"
+class CYPool;
+_finline void *operator new(size_t size, CYPool &pool);
+_finline void *operator new [](size_t size, CYPool &pool);
+
class CYPool {
private:
- apr_pool_t *pool_;
+ uint8_t *data_;
+ size_t size_;
+ size_t next_;
struct Cleaner {
Cleaner *next_;
@@ -49,64 +58,152 @@ class CYPool {
}
} *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() :
+ CYPool(size_t next = 64) :
+ data_(NULL),
+ size_(0),
+ next_(next),
cleaner_(NULL)
{
- _aprcall(apr_pool_create(&pool_, NULL));
}
~CYPool() {
- for (Cleaner *cleaner(cleaner_); cleaner_ != NULL; cleaner_ = cleaner_->next_)
+ for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
+ Cleaner *next(cleaner->next_);
(*cleaner->code_)(cleaner->data_);
- apr_pool_destroy(pool_);
+ cleaner = next;
+ }
+ }
+
+ template
+ Type_ *malloc(size_t size) {
+ size = align(size);
+
+ if (size > size_) {
+ size_ = std::max(next_, size + align(sizeof(Cleaner)));
+ next_ *= 2;
+ data_ = reinterpret_cast(::malloc(size_));
+ atexit(free, data_);
+ _assert(size <= size_);
+ }
+
+ void *data(data_);
+ data_ += size;
+ size_ -= size;
+ return reinterpret_cast(data);
}
- void Clear() {
- apr_pool_clear(pool_);
+ char *strdup(const char *data) {
+ if (data == NULL)
+ return NULL;
+ return reinterpret_cast(memdup(data, strlen(data) + 1));
}
- operator apr_pool_t *() const {
- return pool_;
+ void *memdup(const void *data, size_t size) {
+ void *copy(malloc(size));
+ memcpy(copy, data, size);
+ return copy;
}
- void *operator()(size_t size) const {
- return apr_palloc(pool_, size);
+ char *strndup(const char *data, size_t size) {
+ return strmemdup(data, strnlen(data, size));
}
- char *strdup(const char *data) const {
- return apr_pstrdup(pool_, data);
+ char *strmemdup(const char *data, size_t size) {
+ char *copy(malloc(size + 1));
+ memcpy(copy, data, size);
+ copy[size] = '\0';
+ return copy;
}
- char *strndup(const char *data, size_t size) const {
- return apr_pstrndup(pool_, data, size);
+ // XXX: this could be made much more efficient
+ __attribute__((__sentinel__))
+ char *strcat(const char *data, ...) {
+ size_t size(strlen(data)); {
+ va_list args;
+ va_start(args, data);
+
+ while (const char *arg = va_arg(args, const char *))
+ size += strlen(arg);
+
+ va_end(args);
+ }
+
+ char *copy(malloc(size + 1)); {
+ va_list args;
+ va_start(args, data);
+
+ size_t offset(strlen(data));
+ memcpy(copy, data, offset);
+
+ while (const char *arg = va_arg(args, const char *)) {
+ size_t size(strlen(arg));
+ memcpy(copy + offset, arg, size);
+ offset += size;
+ }
+
+ va_end(args);
+ }
+
+ copy[size] = '\0';
+ return copy;
}
- char *strmemdup(const char *data, size_t size) const {
- return apr_pstrmemdup(pool_, data, size);
+ // XXX: most people using this might should use sprintf
+ char *itoa(long value) {
+ return sprintf(16, "%ld", value);
}
- char *sprintf(const char *format, ...) const {
+ __attribute__((__format__(__printf__, 3, 4)))
+ char *sprintf(size_t size, const char *format, ...) {
va_list args;
va_start(args, format);
- char *data(vsprintf(format, args));
+ char *copy(vsprintf(size, format, args));
va_end(args);
- return data;
+ return copy;
}
- char *vsprintf(const char *format, va_list args) const {
- return apr_pvsprintf(pool_, format, args);
+ char *vsprintf(size_t size, const char *format, va_list args) {
+ va_list copy;
+ va_copy(copy, args);
+ char buffer[size];
+ int writ(vsnprintf(buffer, size, format, copy));
+ va_end(copy);
+ _assert(writ >= 0);
+
+ if (size_t(writ) >= size)
+ return vsprintf(writ + 1, format, args);
+ return strmemdup(buffer, writ);
}
void atexit(void (*code)(void *), void *data = NULL);
+
+ template
+ Type_ &object() {
+ Type_ *value(new(*this) Type_());
+ atexit(&delete_, value);
+ return *value;
+ }
};
_finline void *operator new(size_t size, CYPool &pool) {
- return pool(size);
+ return pool.malloc(size);
}
_finline void *operator new [](size_t size, CYPool &pool) {
- return pool(size);
+ return pool.malloc(size);
}
_finline void CYPool::atexit(void (*code)(void *), void *data) {
@@ -132,7 +229,7 @@ struct CYData {
}
static void *operator new(size_t size, CYPool &pool) {
- void *data(pool(size));
+ void *data(pool.malloc(size));
reinterpret_cast(data)->pool_ = &pool;
return data;
}
@@ -170,7 +267,7 @@ struct CYPoolAllocator {
}
pointer allocate(size_type size, const void *hint = 0) {
- return reinterpret_cast((*pool_)(size));
+ return pool_->malloc(size);
}
void deallocate(pointer data, size_type size) {
@@ -217,4 +314,7 @@ class CYLocalPool :
#define $pool \
(*CYLocal::Get())
+template <>
+::pthread_key_t CYLocal::key_;
+
#endif/*CYCRIPT_POOLING_HPP*/