| 1 | |
| 2 | // vim:sw=2:ai |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. |
| 6 | * See COPYRIGHT.txt for details. |
| 7 | */ |
| 8 | |
| 9 | #ifndef DENA_ALLOCATOR_HPP |
| 10 | #define DENA_ALLOCATOR_HPP |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #if 0 |
| 16 | extern "C" { |
| 17 | #include <tlsf.h> |
| 18 | }; |
| 19 | #define DENA_MALLOC(x) tlsf_malloc(x) |
| 20 | #define DENA_REALLOC(x, y) tlsf_realloc(x, y) |
| 21 | #define DENA_FREE(x) tlsf_free(x) |
| 22 | #define DENA_NEWCHAR(x) static_cast<char *>(tlsf_malloc(x)) |
| 23 | #define DENA_DELETE(x) tlsf_free(x) |
| 24 | typedef std::allocator<int> allocator_type; |
| 25 | #endif |
| 26 | |
| 27 | #if 1 |
| 28 | #define DENA_MALLOC(x) malloc(x) |
| 29 | #define DENA_REALLOC(x, y) realloc(x, y) |
| 30 | #define DENA_FREE(x) free(x) |
| 31 | #define DENA_NEWCHAR(x) (new char[x]) |
| 32 | #define DENA_DELETE(x) (delete [] x) |
| 33 | typedef std::allocator<int> allocator_type; |
| 34 | #endif |
| 35 | |
| 36 | #if 1 |
| 37 | #define DENA_ALLOCA_ALLOCATE(typ, len) \ |
| 38 | (typ *) alloca((len) * sizeof(typ)) |
| 39 | #define DENA_ALLOCA_FREE(x) |
| 40 | #else |
| 41 | #define DENA_ALLOCA_ALLOCATE(typ, len) \ |
| 42 | static_cast<typ *>(malloc((len) * sizeof(typ))) |
| 43 | #define DENA_ALLOCA_FREE(x) free(x) |
| 44 | #endif |
| 45 | |
| 46 | namespace dena { |
| 47 | |
| 48 | template <typename T> struct auto_alloca_free { |
| 49 | auto_alloca_free(T *value) : value(value) { } |
| 50 | ~auto_alloca_free() { |
| 51 | /* no-op if alloca() is used */ |
| 52 | DENA_ALLOCA_FREE(value); |
| 53 | } |
| 54 | private: |
| 55 | auto_alloca_free(const auto_alloca_free&); |
| 56 | auto_alloca_free& operator =(const auto_alloca_free&); |
| 57 | private: |
| 58 | T *value; |
| 59 | }; |
| 60 | |
| 61 | }; |
| 62 | |
| 63 | #endif |
| 64 | |
| 65 | |