summaryrefslogtreecommitdiff
path: root/plugin/handler_socket/libhsclient/allocator.hpp
blob: dd3a28ba7bdca3eb55e5d57445edbb01313af1a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

// vim:sw=2:ai

/*
 * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved.
 * See COPYRIGHT.txt for details.
 */

#ifndef DENA_ALLOCATOR_HPP
#define DENA_ALLOCATOR_HPP

#include <stdlib.h>
#include <string.h>

#if 0
extern "C" {
#include <tlsf.h>
};
#define DENA_MALLOC(x) tlsf_malloc(x)
#define DENA_REALLOC(x, y) tlsf_realloc(x, y)
#define DENA_FREE(x) tlsf_free(x)
#define DENA_NEWCHAR(x) static_cast<char *>(tlsf_malloc(x))
#define DENA_DELETE(x) tlsf_free(x)
typedef std::allocator<int> allocator_type;
#endif

#if 1
#define DENA_MALLOC(x) malloc(x)
#define DENA_REALLOC(x, y) realloc(x, y)
#define DENA_FREE(x) free(x)
#define DENA_NEWCHAR(x) (new char[x])
#define DENA_DELETE(x) (delete [] x)
typedef std::allocator<int> allocator_type;
#endif

#if 1
#define DENA_ALLOCA_ALLOCATE(typ, len) \
	(typ *) alloca((len) * sizeof(typ))
#define DENA_ALLOCA_FREE(x)
#else
#define DENA_ALLOCA_ALLOCATE(typ, len) \
	static_cast<typ *>(malloc((len) * sizeof(typ)))
#define DENA_ALLOCA_FREE(x) free(x)
#endif

namespace dena {

template <typename T> struct auto_alloca_free {
  auto_alloca_free(T *value) : value(value) { }
  ~auto_alloca_free() {
    /* no-op if alloca() is used */
    DENA_ALLOCA_FREE(value);
  }
 private:
  auto_alloca_free(const auto_alloca_free&);
  auto_alloca_free& operator =(const auto_alloca_free&);
 private:
  T *value;
};

};

#endif