1 | /* |
---|
2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
---|
3 | |
---|
4 | This file is part of SINA. |
---|
5 | SINA is free software: you can redistribute it and/or modify it under |
---|
6 | the terms of the GNU General Public License as published by the Free |
---|
7 | Software Foundation, either version 3 of the License, or (at your |
---|
8 | option) any later version. |
---|
9 | |
---|
10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
13 | for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | Additional permission under GNU GPL version 3 section 7 |
---|
19 | |
---|
20 | If you modify SINA, or any covered work, by linking or combining it |
---|
21 | with components of ARB (or a modified version of that software), |
---|
22 | containing parts covered by the terms of the |
---|
23 | ARB-public-library-license, the licensors of SINA grant you additional |
---|
24 | permission to convey the resulting work. Corresponding Source for a |
---|
25 | non-source form of such a combination shall include the source code |
---|
26 | for the parts of ARB used as well as that of the covered work. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _BUFFER_H_ |
---|
30 | #define _BUFFER_H_ |
---|
31 | |
---|
32 | #include <cstddef> |
---|
33 | #include <cstdint> |
---|
34 | #include <new> |
---|
35 | |
---|
36 | #ifdef HAVE_TBB_MALLOC |
---|
37 | #include <tbb/scalable_allocator.h> |
---|
38 | #else |
---|
39 | #include <stdlib.h> |
---|
40 | #endif |
---|
41 | |
---|
42 | namespace sina { |
---|
43 | |
---|
44 | template<typename T> |
---|
45 | class buffer { |
---|
46 | public: |
---|
47 | using size_type = size_t; |
---|
48 | explicit buffer(size_type size) { |
---|
49 | #ifdef HAVE_TBB_MALLOC |
---|
50 | _start = (T*)scalable_malloc(sizeof(T) * size); |
---|
51 | #else |
---|
52 | _start = (T*)malloc(sizeof(T) * size); |
---|
53 | #endif |
---|
54 | } |
---|
55 | ~buffer() { |
---|
56 | #ifdef HAVE_TBB_MALLOC |
---|
57 | scalable_free(_start); |
---|
58 | #else |
---|
59 | free(_start); |
---|
60 | #endif |
---|
61 | } |
---|
62 | inline T& operator[](size_type idx) { |
---|
63 | return _start[idx]; |
---|
64 | } |
---|
65 | private: |
---|
66 | T* _start; |
---|
67 | }; |
---|
68 | |
---|
69 | template<typename T, size_t ALIGN=64> |
---|
70 | class aligned_buffer { |
---|
71 | public: |
---|
72 | using size_type = size_t; |
---|
73 | using offset_t = uint16_t; |
---|
74 | explicit aligned_buffer(size_type size) { |
---|
75 | size_type alloc = sizeof(T) * size + sizeof(offset_t) + ALIGN - 1; |
---|
76 | #ifdef HAVE_TBB_MALLOC |
---|
77 | void *ptr = scalable_malloc(alloc); |
---|
78 | #else |
---|
79 | void *ptr = malloc(alloc); |
---|
80 | #endif |
---|
81 | if (!ptr) { |
---|
82 | throw std::bad_alloc(); |
---|
83 | } |
---|
84 | size_t res = ((size_t)ptr + sizeof(offset_t) + ALIGN - 1) & ~(ALIGN -1); |
---|
85 | *((offset_t*)res - 1) = (offset_t)((size_t)res - (size_t)ptr); |
---|
86 | _start = (T*)res; |
---|
87 | } |
---|
88 | |
---|
89 | ~aligned_buffer() { |
---|
90 | offset_t offset = *((offset_t*)_start - 1); |
---|
91 | #ifdef HAVE_TBB_MALLOC |
---|
92 | scalable_free((char*)_start - offset); |
---|
93 | #else |
---|
94 | free((char*)_start - offset); |
---|
95 | #endif |
---|
96 | } |
---|
97 | |
---|
98 | inline T& operator[](size_type idx) { |
---|
99 | return _start[idx]; |
---|
100 | } |
---|
101 | private: |
---|
102 | T* _start; |
---|
103 | }; |
---|
104 | |
---|
105 | } // namespace sina |
---|
106 | |
---|
107 | #endif // _BUFFER_H_ |
---|
108 | |
---|
109 | /* |
---|
110 | Local Variables: |
---|
111 | mode:c++ |
---|
112 | c-file-style:"stroustrup" |
---|
113 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . 0)) |
---|
114 | indent-tabs-mode:nil |
---|
115 | fill-column:99 |
---|
116 | End: |
---|
117 | */ |
---|
118 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
---|
119 | |
---|