X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/f95d2598051e347460e028286ff2d33e5825e548..bf998c10baabbd95f2d59d29f9edea0eb107b156:/Pooling.hpp diff --git a/Pooling.hpp b/Pooling.hpp index 0e5456f..78aa1b2 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -1,5 +1,5 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2014 Jay Freeman (saurik) + * Copyright (C) 2009-2015 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -43,6 +43,7 @@ class CYPool { private: uint8_t *data_; size_t size_; + size_t next_; struct Cleaner { Cleaner *next_; @@ -70,9 +71,10 @@ class CYPool { CYPool(const CYPool &); public: - CYPool() : + CYPool(size_t next = 64) : data_(NULL), size_(0), + next_(next), cleaner_(NULL) { } @@ -90,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_); @@ -109,18 +111,19 @@ class CYPool { return reinterpret_cast(memdup(data, strlen(data) + 1)); } - void *memdup(const void *data, size_t size) { - void *copy(malloc(size)); + template + Type_ *memdup(const Type_ *data, size_t size) { + Type_ *copy(malloc(size)); 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)); memcpy(copy, data, size); copy[size] = '\0'; return copy;