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