X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/0cbeddf886b3e6d732c96a8f6a578a755e7ddf66..2447e531132913a88707e5d457fb2946fa3b0478:/Pooling.hpp diff --git a/Pooling.hpp b/Pooling.hpp index bd37c5c..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 . **/ /* }}} */ @@ -43,6 +43,7 @@ class CYPool { private: uint8_t *data_; size_t size_; + size_t next_; struct Cleaner { Cleaner *next_; @@ -62,12 +63,18 @@ class CYPool { 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) { } @@ -85,8 +92,8 @@ class CYPool { size = align(size); if (size > size_) { - // XXX: is this an optimal malloc size? - size_ = std::max(size, size + align(sizeof(Cleaner))); + size_ = std::max(next_, size + align(sizeof(Cleaner))); + next_ *= 2; data_ = reinterpret_cast(::malloc(size_)); atexit(free, data_); _assert(size <= size_); @@ -99,6 +106,8 @@ class CYPool { } char *strdup(const char *data) { + if (data == NULL) + return NULL; return reinterpret_cast(memdup(data, strlen(data) + 1)); } @@ -108,12 +117,12 @@ class CYPool { return copy; } - char *strndup(const char *data, size_t size) const { + char *strndup(const char *data, size_t size) { return strmemdup(data, strnlen(data, size)); } - char *strmemdup(const char *data, size_t size) const { - char *copy(new char[size + 1]); + char *strmemdup(const char *data, size_t size) { + char *copy(malloc(size + 1)); memcpy(copy, data, size); copy[size] = '\0'; return copy; @@ -180,6 +189,13 @@ class CYPool { } 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) { @@ -298,4 +314,7 @@ class CYLocalPool : #define $pool \ (*CYLocal::Get()) +template <> +::pthread_key_t CYLocal::key_; + #endif/*CYCRIPT_POOLING_HPP*/