]> git.sesse.net Git - casparcg/blob - common/page_locked_allocator.h
9cce6e8f87899fac36fdd19087dd349a2aba96fb
[casparcg] / common / page_locked_allocator.h
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #pragma once\r
23 \r
24 namespace caspar {\r
25 \r
26 namespace detail {\r
27 \r
28 void* alloc_page_locked(size_t size);\r
29 void free_page_locked(void* p);\r
30 \r
31 }\r
32         \r
33 template <class T>\r
34 class page_locked_allocator\r
35 {\r
36 public:\r
37         typedef size_t    size_type;\r
38         typedef ptrdiff_t difference_type;\r
39         typedef T*        pointer;\r
40         typedef const T*  const_pointer;\r
41         typedef T&        reference;\r
42         typedef const T&  const_reference;\r
43         typedef T         value_type;\r
44 \r
45         page_locked_allocator() {}\r
46         page_locked_allocator(const page_locked_allocator&) {}\r
47   \r
48         pointer allocate(size_type n, const void * = 0) \r
49         {\r
50                 return reinterpret_cast<T*>(detail::alloc_page_locked(n));\r
51         }\r
52   \r
53         void deallocate(void* p, size_type) \r
54         {\r
55                 detail::free_page_locked(p);            \r
56         }\r
57 \r
58         pointer           address(reference x) const { return &x; }\r
59         const_pointer     address(const_reference x) const { return &x; }\r
60         page_locked_allocator<T>&  operator=(const page_locked_allocator&) { return *this; }\r
61         void              construct(pointer p, const T& val) { new ((T*) p) T(val); }\r
62         void              destroy(pointer p) { p->~T(); }\r
63 \r
64         size_type         max_size() const { return size_t(-1); }\r
65 \r
66         template <class U>\r
67         struct rebind { typedef page_locked_allocator<U> other; };\r
68 \r
69         template <class U>\r
70         page_locked_allocator(const page_locked_allocator<U>&) {}\r
71 \r
72         template <class U>\r
73         page_locked_allocator& operator=(const page_locked_allocator<U>&) { return *this; }\r
74 };\r
75 }