1 /* Cycript - Optimizing JavaScript Compiler/Runtime
 
   2  * Copyright (C) 2009-2015  Jay Freeman (saurik)
 
   5 /* GNU Affero General Public License, Version 3 {{{ */
 
   7  * This program is free software: you can redistribute it and/or modify
 
   8  * it under the terms of the GNU Affero General Public License as published by
 
   9  * the Free Software Foundation, either version 3 of the License, or
 
  10  * (at your option) any later version.
 
  12  * This program is distributed in the hope that it will be useful,
 
  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  15  * GNU Affero General Public License for more details.
 
  17  * You should have received a copy of the GNU Affero General Public License
 
  18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
  22 #ifndef CYCRIPT_POOLING_HPP
 
  23 #define CYCRIPT_POOLING_HPP
 
  34 #include "Exception.hpp"
 
  36 #include "Standard.hpp"
 
  38 // XXX: std::aligned_storage and alignof
 
  39 static const size_t CYAlignment(sizeof(void *));
 
  41 template <typename Type_>
 
  42 static void CYAlign(Type_ &data, size_t size) {
 
  43     data = reinterpret_cast<Type_>((reinterpret_cast<uintptr_t>(data) + (size - 1)) & ~static_cast<uintptr_t>(size - 1));
 
  47 _finline void *operator new(size_t size, CYPool &pool);
 
  48 _finline void *operator new [](size_t size, CYPool &pool);
 
  58         void (*code_)(void *);
 
  61         Cleaner(Cleaner *next, void (*code)(void *), void *data) :
 
  69     template <typename Type_>
 
  70     static void delete_(void *data) {
 
  71         reinterpret_cast<Type_ *>(data)->~Type_();
 
  74     CYPool(const CYPool &);
 
  77     CYPool(size_t next = 64) :
 
  86         for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
 
  87             Cleaner *next(cleaner->next_);
 
  88             (*cleaner->code_)(cleaner->data_);
 
  93     template <typename Type_>
 
  94     Type_ *malloc(size_t size, size_t alignment = CYAlignment) {
 
  96         CYAlign(end, alignment);
 
  99         if (size_t(end - data_) > size_) {
 
 100             size_t need(sizeof(Cleaner));
 
 101             CYAlign(need, alignment);
 
 103             size_ = std::max<size_t>(next_, need);
 
 105             data_ = reinterpret_cast<uint8_t *>(::malloc(size_));
 
 107             _assert(size <= size_);
 
 110         uint8_t *data(data_);
 
 111         CYAlign(data, alignment);
 
 113         size_ -= end - data_;
 
 115         return reinterpret_cast<Type_ *>(data);
 
 118     char *strdup(const char *data) {
 
 121         return reinterpret_cast<char *>(memdup(data, strlen(data) + 1));
 
 124     template <typename Type_>
 
 125     Type_ *memdup(const Type_ *data, size_t size, size_t alignment = CYAlignment) {
 
 126         Type_ *copy(malloc<Type_>(size, alignment));
 
 127         memcpy(copy, data, size);
 
 131     char *strndup(const char *data, size_t size) {
 
 132         return strmemdup(data, strnlen(data, size));
 
 135     char *strmemdup(const char *data, size_t size) {
 
 136         char *copy(malloc<char>(size + 1, 1));
 
 137         memcpy(copy, data, size);
 
 142     // XXX: this could be made much more efficient
 
 143     __attribute__((__sentinel__))
 
 144     char *strcat(const char *data, ...) {
 
 145         size_t size(strlen(data)); {
 
 147             va_start(args, data);
 
 149             while (const char *arg = va_arg(args, const char *))
 
 155         char *copy(malloc<char>(size + 1, 1)); {
 
 157             va_start(args, data);
 
 159             size_t offset(strlen(data));
 
 160             memcpy(copy, data, offset);
 
 162             while (const char *arg = va_arg(args, const char *)) {
 
 163                 size_t size(strlen(arg));
 
 164                 memcpy(copy + offset, arg, size);
 
 175     // XXX: most people using this might should use sprintf
 
 176     char *itoa(long value) {
 
 177         return sprintf(16, "%ld", value);
 
 180     __attribute__((__format__(__printf__, 3, 4)))
 
 181     char *sprintf(size_t size, const char *format, ...) {
 
 183         va_start(args, format);
 
 184         char *copy(vsprintf(size, format, args));
 
 189     char *vsprintf(size_t size, const char *format, va_list args) {
 
 193         int writ(vsnprintf(buffer, size, format, copy));
 
 197         if (size_t(writ) >= size)
 
 198             return vsprintf(writ + 1, format, args);
 
 199         return strmemdup(buffer, writ);
 
 202     void atexit(void (*code)(void *), void *data = NULL);
 
 204     template <typename Type_>
 
 206         Type_ *value(new(*this) Type_());
 
 207         atexit(&delete_<Type_>, value);
 
 212 _finline void *operator new(size_t size, CYPool &pool) {
 
 213     return pool.malloc<void>(size);
 
 216 _finline void *operator new [](size_t size, CYPool &pool) {
 
 217     return pool.malloc<void>(size);
 
 220 _finline void CYPool::atexit(void (*code)(void *), void *data) {
 
 221     cleaner_ = new(*this) Cleaner(cleaner_, code, data);
 
 233     CYData(CYPool &pool) :
 
 235         count_(_not(unsigned))
 
 242     static void *operator new(size_t size, CYPool &pool) {
 
 243         void *data(pool.malloc<void>(size));
 
 244         reinterpret_cast<CYData *>(data)->pool_ = &pool;
 
 248     static void *operator new(size_t size) {
 
 249         return operator new(size, *new CYPool());
 
 252     static void operator delete(void *data) {
 
 253         delete reinterpret_cast<CYData *>(data)->pool_;
 
 257 template <typename Type_>
 
 258 struct CYPoolAllocator {
 
 261     typedef Type_ value_type;
 
 262     typedef value_type *pointer;
 
 263     typedef const value_type *const_pointer;
 
 264     typedef value_type &reference;
 
 265     typedef const value_type &const_reference;
 
 266     typedef std::size_t size_type;
 
 267     typedef std::ptrdiff_t difference_type;
 
 274     template <typename Right_>
 
 275     CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
 
 280     pointer allocate(size_type size, const void *hint = 0) {
 
 281         return pool_->malloc<value_type>(size);
 
 284     void deallocate(pointer data, size_type size) {
 
 287     void construct(pointer address, const Type_ &rhs) {
 
 288         new(address) Type_(rhs);
 
 291     void destroy(pointer address) {
 
 295     template <typename Right_>
 
 296     inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
 
 297         return pool_ == rhs.pool_;
 
 300     template <typename Right_>
 
 301     inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
 
 302         return !operator==(rhs);
 
 305     template <typename Right_>
 
 307         typedef CYPoolAllocator<Right_> other;
 
 315     CYLocal<CYPool> local_;
 
 326     (*CYLocal<CYPool>::Get())
 
 329 ::pthread_key_t CYLocal<CYPool>::key_;
 
 331 #endif/*CYCRIPT_POOLING_HPP*/