GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
secmem++.h
1 /* STL allocator for secmem
2  * Copyright (C) 2008 Marc Mutz <marc@kdab.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <https://www.gnu.org/licenses/>.
16  * SPDX-License-Identifier: GPL-2.0+
17  */
18 
19 #ifndef __SECMEM_SECMEMPP_H__
20 #define __SECMEM_SECMEMPP_H__
21 
22 #include "../secmem/secmem.h"
23 #include <cstddef>
24 
25 namespace secmem {
26 
27  template <typename T>
28  class alloc {
29  public:
30  // type definitions:
31  typedef size_t size_type;
32  typedef ptrdiff_t difference_type;
33  typedef T* pointer;
34  typedef const T* const_pointer;
35  typedef T& reference;
36  typedef const T& const_reference;
37  typedef T value_type;
38 
39  // rebind
40  template <typename U>
41  struct rebind {
42  typedef alloc<U> other;
43  };
44 
45  // address
46  pointer address( reference value ) const {
47  return &value;
48  }
49  const_pointer address( const_reference value ) const {
50  return &value;
51  }
52 
53  // (trivial) ctors and dtors
54  alloc() {}
55  alloc( const alloc & ) {}
56  template <typename U> alloc( const alloc<U> & ) {}
57  // copy ctor is ok
58  ~alloc() {}
59 
60  // de/allocation
61  size_type max_size() const {
62  return secmem_get_max_size();
63  }
64 
65  pointer allocate( size_type n, void * =0 ) {
66  return static_cast<pointer>( secmem_malloc( n * sizeof(T) ) );
67  }
68 
69  void deallocate( pointer p, size_type ) {
70  secmem_free( p );
71  }
72 
73  // de/construct
74  void construct( pointer p, const T & value ) {
75  void * loc = p;
76  new (loc)T(value);
77  }
78  void destruct( pointer p ) {
79  p->~T();
80  }
81  };
82 
83  // equality comparison
84  template <typename T1,typename T2>
85  bool operator==( const alloc<T1> &, const alloc<T2> & ) { return true; }
86  template <typename T1, typename T2>
87  bool operator!=( const alloc<T1> &, const alloc<T2> & ) { return false; }
88 
89 }
90 
91 #endif /* __SECMEM_SECMEMPP_H__ */
Definition: secmem++.h:28
Definition: secmem++.h:41