X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/4644480a22eda2f7350a8997bb2109f325b576fe..38ae783db781810a19b3103bb55c81ea70e7d952:/Pooling.hpp diff --git a/Pooling.hpp b/Pooling.hpp index 4fa5dae..bd37c5c 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -1,110 +1,301 @@ -/* Cycript - Remote Execution Server and Disassembler - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2013 Jay Freeman (saurik) */ -/* Modified BSD License {{{ */ +/* GNU General Public License, Version 3 {{{ */ /* - * Redistribution and use in source and binary - * forms, with or without modification, are permitted - * provided that the following conditions are met: + * 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. * - * 1. Redistributions of source code must retain the - * above copyright notice, this list of conditions - * and the following disclaimer. - * 2. Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions - * and the following disclaimer in the documentation - * and/or other materials provided with the - * distribution. - * 3. The name of the author may not be used to endorse - * or promote products derived from this software - * without specific prior written permission. + * Cycript 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. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * You should have received a copy of the GNU General Public License + * along with Cycript. If not, see . +**/ /* }}} */ -#ifndef CYPOOLING_HPP -#define CYPOOLING_HPP +#ifndef CYCRIPT_POOLING_HPP +#define CYCRIPT_POOLING_HPP -#include -#include +#include +#include +#include +#include -#include +#include -_finline void *operator new(size_t size, apr_pool_t *pool) { - return apr_palloc(pool, size); -} +#include -_finline void *operator new [](size_t size, apr_pool_t *pool) { - return apr_palloc(pool, size); -} +#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_; + + 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; + } + + CYPool(const CYPool &); public: - CYPool() { - _aprcall(apr_pool_create(&pool_, NULL)); + CYPool() : + data_(NULL), + size_(0), + cleaner_(NULL) + { } ~CYPool() { - apr_pool_destroy(pool_); + for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) { + Cleaner *next(cleaner->next_); + (*cleaner->code_)(cleaner->data_); + cleaner = next; + } } - void Clear() { - apr_pool_clear(pool_); + 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); + } + + char *strdup(const char *data) { + 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; } - char *operator ()(const char *data) const { - return apr_pstrdup(pool_, data); + char *strndup(const char *data, size_t size) const { + return strmemdup(data, strnlen(data, size)); } - char *operator ()(const char *data, size_t size) const { - return apr_pstrndup(pool_, data, size); + char *strmemdup(const char *data, size_t size) const { + char *copy(new char[size + 1]); + memcpy(copy, data, size); + copy[size] = '\0'; + return copy; } + + // 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; + } + + // XXX: most people using this might should use sprintf + char *itoa(long value) { + return sprintf(16, "%ld", value); + } + + __attribute__((__format__(__printf__, 3, 4))) + char *sprintf(size_t size, const char *format, ...) { + va_list args; + va_start(args, format); + char *copy(vsprintf(size, format, args)); + va_end(args); + return copy; + } + + 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); }; +_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); +} + struct CYData { - apr_pool_t *pool_; + CYPool *pool_; + unsigned count_; + + CYData() : + count_(1) + { + } + + 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.malloc(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 { + CYPool *pool_; + typedef Type_ value_type; + typedef value_type *pointer; + typedef const value_type *const_pointer; + typedef value_type &reference; + typedef const value_type &const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + CYPoolAllocator() : + pool_(NULL) + { + } + + template + CYPoolAllocator(const CYPoolAllocator &rhs) : + pool_(rhs.pool_) + { + } + + pointer allocate(size_type size, const void *hint = 0) { + return pool_->malloc(size); + } + + void deallocate(pointer data, size_type size) { + } + + void construct(pointer address, const Type_ &rhs) { + new(address) Type_(rhs); + } + + void destroy(pointer address) { + address->~Type_(); + } + + template + inline bool operator==(const CYPoolAllocator &rhs) { + return pool_ == rhs.pool_; + } + + template + inline bool operator!=(const CYPoolAllocator &rhs) { + return !operator==(rhs); + } + + template + struct rebind { + typedef CYPoolAllocator other; + }; +}; + +class CYLocalPool : + public CYPool +{ + private: + CYLocal local_; + + public: + CYLocalPool() : + CYPool(), + local_(this) + { + } }; -#endif/*CYPOOLING_HPP*/ +#define $pool \ + (*CYLocal::Get()) + +#endif/*CYCRIPT_POOLING_HPP*/