1/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3#ident "$Id$"
4/*======
5This file is part of PerconaFT.
6
7
8Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35======= */
36
37#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#pragma once
40
41#include <string.h>
42
43namespace toku {
44
45 class scoped_malloc {
46 public:
47 // Memory is allocated from thread-local storage if available, otherwise from malloc(3).
48 scoped_malloc(const size_t size);
49
50 ~scoped_malloc();
51
52 void *get() const {
53 return m_buf;
54 }
55
56 private:
57 // Non-copyable
58 scoped_malloc();
59
60 const size_t m_size;
61 const bool m_local;
62 void *const m_buf;
63 };
64
65 class scoped_calloc : public scoped_malloc {
66 public:
67 // A scoped malloc whose bytes are initialized to zero, as in calloc(3)
68 scoped_calloc(const size_t size) :
69 scoped_malloc(size) {
70 memset(scoped_malloc::get(), 0, size);
71 }
72 };
73
74 class scoped_malloc_aligned : public scoped_malloc {
75 public:
76 scoped_malloc_aligned(const size_t size, const size_t alignment) :
77 scoped_malloc(size + alignment) {
78 invariant(size >= alignment);
79 invariant(alignment > 0);
80 const uintptr_t addr = reinterpret_cast<uintptr_t>(scoped_malloc::get());
81 const uintptr_t aligned_addr = (addr + alignment) - (addr % alignment);
82 invariant(aligned_addr < addr + size + alignment);
83 m_aligned_buf = reinterpret_cast<char *>(aligned_addr);
84 }
85
86 void *get() const {
87 return m_aligned_buf;
88 }
89
90 private:
91 void *m_aligned_buf;
92 };
93
94} // namespace toku
95
96void toku_scoped_malloc_init(void);
97
98void toku_scoped_malloc_destroy(void);
99
100void toku_scoped_malloc_destroy_set(void);
101
102void toku_scoped_malloc_destroy_key(void);
103
104