C++ Device API Reference Manual
Reference documentation for the Simics C++ Device API.
 
Loading...
Searching...
No Matches
malloc_allocator.h
Go to the documentation of this file.
1// -*- mode: C++; c-file-style: "virtutech-c++" -*-
2
3/*
4 © 2016 Intel Corporation
5
6 This software and the related documents are Intel copyrighted materials, and
7 your use of them is governed by the express license under which they were
8 provided to you ("License"). Unless the License provides otherwise, you may
9 not use, modify, copy, publish, distribute, disclose or transmit this software
10 or the related documents without Intel's prior written permission.
11
12 This software and the related documents are provided as is, with no express or
13 implied warranties, other than those that are expressly stated in the License.
14*/
15
16#ifndef SIMICS_MALLOC_ALLOCATOR_H
17#define SIMICS_MALLOC_ALLOCATOR_H
18
19#include <simics/util/alloc.h>
20#include <stddef.h>
21
22#include <new>
23#include <memory>
24
25namespace simics {
26
27template <class T>
29 public:
30 static void *allocate(std::size_t num) {
31 return MM_MALLOC(num, T);
32 }
33
34 static void deallocate(T * const addr) {
35 MM_FREE(addr);
36 }
37};
38
39template <class T, template <class> class Allocator = MmAllocator>
41 public:
42 virtual T *allocate(const size_t num_elements) const {
43 if (num_elements == 0) {
44 return nullptr;
45 }
46 if (num_elements > max_size()) {
47 throw std::bad_alloc();
48 }
49
50 void *addr = Allocator<T>::allocate(num_elements);
51 if (!addr) {
52 throw std::bad_alloc();
53 }
54 return static_cast<T*>(addr);
55 }
56
57 T *allocate(const size_t num_elements,
58 std::allocator_traits<std::allocator<void> >
59 ::const_pointer hint) const {
60 return allocate(num_elements);
61 }
62
63 void deallocate(T * const addr, const size_t n) const {
64 Allocator<T>::deallocate(addr);
65 }
66
67 void construct(T * const uninitialized, const T &val) const {
68 void * const initialized = static_cast<void*>(uninitialized);
69 new(initialized) T(val);
70 }
71 void destroy(T * const p) const {
72 p->~T();
73 }
74
75 // Returns the maximum number of elements that may be allocated
76 size_t max_size() const {
77 return static_cast<size_t>(-1) / sizeof(T);
78 }
79
80 T *address(T &x) const { // NOLINT (runtime/references)
81 return &x;
82 }
83 const T *address(const T &x) const {
84 return &x;
85 }
86
87 // Always returns true since the allocator is stateless. Any memory
88 // allocated using this allocator may be freed by another allocator
89 bool operator==(const MallocAllocator &other) const {
90 return true;
91 }
92 bool operator!=(const MallocAllocator &other) const {
93 return !(*this == other);
94 }
95
99
100 template <typename U>
102
104 return this;
105 }
106
107 // Member type definitions required for all allocators:
108 typedef T *pointer;
109 typedef const T *const_pointer;
110
111 typedef T &reference;
112 typedef const T &const_reference;
113
114 typedef T value_type;
115 typedef size_t size_type;
116 typedef ptrdiff_t difference_type;
117
118 template <typename U> struct rebind {
120 };
121};
122
123} // namespace simics
124
125#endif
Definition: malloc_allocator.h:40
T * allocate(const size_t num_elements, std::allocator_traits< std::allocator< void > > ::const_pointer hint) const
Definition: malloc_allocator.h:57
const T & const_reference
Definition: malloc_allocator.h:112
T value_type
Definition: malloc_allocator.h:114
bool operator==(const MallocAllocator &other) const
Definition: malloc_allocator.h:89
void destroy(T *const p) const
Definition: malloc_allocator.h:71
const T * address(const T &x) const
Definition: malloc_allocator.h:83
size_t size_type
Definition: malloc_allocator.h:115
ptrdiff_t difference_type
Definition: malloc_allocator.h:116
void deallocate(T *const addr, const size_t n) const
Definition: malloc_allocator.h:63
~MallocAllocator()
Definition: malloc_allocator.h:98
T & reference
Definition: malloc_allocator.h:111
MallocAllocator & operator=(const MallocAllocator &other) const
Definition: malloc_allocator.h:103
MallocAllocator(const MallocAllocator< U, Allocator > &)
Definition: malloc_allocator.h:101
MallocAllocator()
Definition: malloc_allocator.h:96
const T * const_pointer
Definition: malloc_allocator.h:109
T * address(T &x) const
Definition: malloc_allocator.h:80
T * pointer
Definition: malloc_allocator.h:108
MallocAllocator(const MallocAllocator &)
Definition: malloc_allocator.h:97
virtual T * allocate(const size_t num_elements) const
Definition: malloc_allocator.h:42
void construct(T *const uninitialized, const T &val) const
Definition: malloc_allocator.h:67
bool operator!=(const MallocAllocator &other) const
Definition: malloc_allocator.h:92
size_t max_size() const
Definition: malloc_allocator.h:76
Definition: malloc_allocator.h:28
static void * allocate(std::size_t num)
Definition: malloc_allocator.h:30
static void deallocate(T *const addr)
Definition: malloc_allocator.h:34
Definition: after-bank.h:33
Definition: malloc_allocator.h:118
MallocAllocator< U, Allocator > other
Definition: malloc_allocator.h:119