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
21#include <stddef.h>
22
23#include <new>
24#include <memory>
25
26namespace simics {
27
28template <class T>
30 public:
31 static void *allocate(std::size_t num) {
32 return MM_MALLOC(num, T);
33 }
34
35 static void deallocate(T * const addr) {
36 MM_FREE(addr);
37 }
38};
39
40template <class T, template <class> class Allocator = MmAllocator>
42 public:
43 virtual T *allocate(const size_t num_elements) const {
44 if (num_elements == 0) {
45 return NULL;
46 }
47 if (num_elements > max_size()) {
48 throw std::bad_alloc();
49 }
50
51 void *addr = Allocator<T>::allocate(num_elements);
52 if (!addr) {
53 throw std::bad_alloc();
54 }
55 return static_cast<T*>(addr);
56 }
57
58 T *allocate(const size_t num_elements,
59 std::allocator_traits<std::allocator<void> >
60 ::const_pointer hint) const {
61 return allocate(num_elements);
62 }
63
64 void deallocate(T * const addr, const size_t n) const {
65 Allocator<T>::deallocate(addr);
66 }
67
68 void construct(T * const uninitialized, const T &val) const {
69 void * const initialized = static_cast<void*>(uninitialized);
70 new(initialized) T(val);
71 }
72 void destroy(T * const p) const {
73 p->~T();
74 }
75
76 // Returns the maximum number of elements that may be allocated
77 size_t max_size() const {
78 return static_cast<size_t>(-1) / sizeof(T);
79 }
80
81 T *address(T &x) const { // NOLINT (runtime/references)
82 return &x;
83 }
84 const T *address(const T &x) const {
85 return &x;
86 }
87
88 // Always returns true since the allocator is stateless. Any memory
89 // allocated using this allocator may be freed by another allocator
90 bool operator==(const MallocAllocator &other) const {
91 return true;
92 }
93 bool operator!=(const MallocAllocator &other) const {
94 return !(*this == other);
95 }
96
100
101 template <typename U>
103
105 return this;
106 }
107
108 // Member type definitions required for all allocators:
109 typedef T *pointer;
110 typedef const T *const_pointer;
111
112 typedef T &reference;
113 typedef const T &const_reference;
114
115 typedef T value_type;
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118
119 template <typename U> struct rebind {
121 };
122};
123
124} // namespace simics
125
126#endif
#define NULL
Definition: _null.h:24
Definition: malloc_allocator.h:41
T * allocate(const size_t num_elements, std::allocator_traits< std::allocator< void > > ::const_pointer hint) const
Definition: malloc_allocator.h:58
const T & const_reference
Definition: malloc_allocator.h:113
T value_type
Definition: malloc_allocator.h:115
bool operator==(const MallocAllocator &other) const
Definition: malloc_allocator.h:90
void destroy(T *const p) const
Definition: malloc_allocator.h:72
const T * address(const T &x) const
Definition: malloc_allocator.h:84
size_t size_type
Definition: malloc_allocator.h:116
ptrdiff_t difference_type
Definition: malloc_allocator.h:117
void deallocate(T *const addr, const size_t n) const
Definition: malloc_allocator.h:64
~MallocAllocator()
Definition: malloc_allocator.h:99
T & reference
Definition: malloc_allocator.h:112
MallocAllocator & operator=(const MallocAllocator &other) const
Definition: malloc_allocator.h:104
MallocAllocator(const MallocAllocator< U, Allocator > &)
Definition: malloc_allocator.h:102
MallocAllocator()
Definition: malloc_allocator.h:97
const T * const_pointer
Definition: malloc_allocator.h:110
T * address(T &x) const
Definition: malloc_allocator.h:81
T * pointer
Definition: malloc_allocator.h:109
MallocAllocator(const MallocAllocator &)
Definition: malloc_allocator.h:98
virtual T * allocate(const size_t num_elements) const
Definition: malloc_allocator.h:43
void construct(T *const uninitialized, const T &val) const
Definition: malloc_allocator.h:68
bool operator!=(const MallocAllocator &other) const
Definition: malloc_allocator.h:93
size_t max_size() const
Definition: malloc_allocator.h:77
Definition: malloc_allocator.h:29
static void * allocate(std::size_t num)
Definition: malloc_allocator.h:31
static void deallocate(T *const addr)
Definition: malloc_allocator.h:35
Definition: attr-value.h:23
Definition: malloc_allocator.h:119
MallocAllocator< U, Allocator > other
Definition: malloc_allocator.h:120