X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/579301290fa12f6d54af8d840e8740c8aaefe954..aaa29c28ae45afad21be2891d38744d68d3c907e:/Pooling.hpp diff --git a/Pooling.hpp b/Pooling.hpp index e3116d9..b67f262 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -1,21 +1,21 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2013 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 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 . **/ /* }}} */ @@ -35,6 +35,14 @@ #include "Local.hpp" #include "Standard.hpp" +// XXX: std::aligned_storage and alignof +static const size_t CYAlignment(sizeof(void *)); + +template +static void CYAlign(Type_ &data, size_t size) { + data = reinterpret_cast((reinterpret_cast(data) + (size - 1)) & ~static_cast(size - 1)); +} + class CYPool; _finline void *operator new(size_t size, CYPool &pool); _finline void *operator new [](size_t size, CYPool &pool); @@ -43,6 +51,7 @@ class CYPool { private: uint8_t *data_; size_t size_; + size_t next_; struct Cleaner { Cleaner *next_; @@ -57,11 +66,6 @@ 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_(); @@ -70,9 +74,10 @@ class CYPool { CYPool(const CYPool &); public: - CYPool() : + CYPool(size_t next = 64) : data_(NULL), size_(0), + next_(next), cleaner_(NULL) { } @@ -86,41 +91,49 @@ class CYPool { } 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))); + Type_ *malloc(size_t size, size_t alignment = CYAlignment) { + uint8_t *end(data_); + CYAlign(end, alignment); + end += size; + + if (size_t(end - data_) > size_) { + size_t need(sizeof(Cleaner)); + CYAlign(need, alignment); + need += size; + size_ = std::max(next_, need); + next_ *= 2; data_ = reinterpret_cast(::malloc(size_)); atexit(free, data_); _assert(size <= size_); } - void *data(data_); - data_ += size; - size_ -= size; + uint8_t *data(data_); + CYAlign(data, alignment); + end = data + size; + size_ -= end - data_; + data_ = end; return reinterpret_cast(data); } char *strdup(const char *data) { if (data == NULL) return NULL; - return reinterpret_cast(memdup(data, strlen(data) + 1)); + return memdup(data, strlen(data) + 1, 1); } - void *memdup(const void *data, size_t size) { - void *copy(malloc(size)); + template + Type_ *memdup(const Type_ *data, size_t size, size_t alignment = CYAlignment) { + Type_ *copy(malloc(size, alignment)); memcpy(copy, data, size); 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, 1)); memcpy(copy, data, size); copy[size] = '\0'; return copy; @@ -139,7 +152,7 @@ class CYPool { va_end(args); } - char *copy(malloc(size + 1)); { + char *copy(malloc(size + 1, 1)); { va_list args; va_start(args, data); @@ -312,4 +325,7 @@ class CYLocalPool : #define $pool \ (*CYLocal::Get()) +template <> +::pthread_key_t CYLocal::key_; + #endif/*CYCRIPT_POOLING_HPP*/