#include "Local.hpp"
#include "Standard.hpp"
+// XXX: std::aligned_storage and alignof
+static const size_t CYAlignment(sizeof(void *));
+
+template <typename Type_>
+static void CYAlign(Type_ &data, size_t size) {
+ data = reinterpret_cast<Type_>((reinterpret_cast<uintptr_t>(data) + (size - 1)) & ~static_cast<uintptr_t>(size - 1));
+}
+
class CYPool;
_finline void *operator new(size_t size, CYPool &pool);
_finline void *operator new [](size_t size, CYPool &pool);
private:
uint8_t *data_;
size_t size_;
+ size_t next_;
struct Cleaner {
Cleaner *next_;
}
} *cleaner_;
- static _finline size_t align(size_t size) {
- // XXX: alignment is more complex than this
- return (size + 7) & ~0x3;
- }
-
template <typename Type_>
static void delete_(void *data) {
reinterpret_cast<Type_ *>(data)->~Type_();
CYPool(const CYPool &);
public:
- CYPool() :
+ CYPool(size_t next = 64) :
data_(NULL),
size_(0),
+ next_(next),
cleaner_(NULL)
{
}
}
template <typename Type_>
- Type_ *malloc(size_t size) {
- size = align(size);
-
- if (size > size_) {
- // XXX: is this an optimal malloc size?
- size_ = std::max<size_t>(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<size_t>(next_, need);
+ next_ *= 2;
data_ = reinterpret_cast<uint8_t *>(::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<Type_ *>(data);
}
return reinterpret_cast<char *>(memdup(data, strlen(data) + 1));
}
- void *memdup(const void *data, size_t size) {
- void *copy(malloc<void>(size));
+ template <typename Type_>
+ Type_ *memdup(const Type_ *data, size_t size, size_t alignment = CYAlignment) {
+ Type_ *copy(malloc<Type_>(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<char>(size + 1, 1));
memcpy(copy, data, size);
copy[size] = '\0';
return copy;
va_end(args);
}
- char *copy(malloc<char>(size + 1)); {
+ char *copy(malloc<char>(size + 1, 1)); {
va_list args;
va_start(args, data);