source: trunk/GDE/SINA/builddir/src/buffer.h

Last change on this file was 19170, checked in by westram, 2 years ago
  • sina source
    • unpack + remove tarball
    • no longer ignore sina builddir.
File size: 3.0 KB
Line 
1/*
2Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu>
3
4This file is part of SINA.
5SINA is free software: you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free
7Software Foundation, either version 3 of the License, or (at your
8option) any later version.
9
10SINA is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or
12FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13for more details.
14
15You should have received a copy of the GNU General Public License
16along with SINA.  If not, see <http://www.gnu.org/licenses/>.
17
18Additional permission under GNU GPL version 3 section 7
19
20If you modify SINA, or any covered work, by linking or combining it
21with components of ARB (or a modified version of that software),
22containing parts covered by the terms of the
23ARB-public-library-license, the licensors of SINA grant you additional
24permission to convey the resulting work. Corresponding Source for a
25non-source form of such a combination shall include the source code
26for 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
42namespace sina {
43
44template<typename T>
45class buffer {
46public:
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    }
65private:
66    T* _start;
67};
68
69template<typename T, size_t ALIGN=64>
70class aligned_buffer {
71public:
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    }
101private:
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
Note: See TracBrowser for help on using the repository browser.