1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
11 * @brief Memory allocation functions.
22 /** MEMALLOC declares the allocators for a class to allocate
23 * memory in the non-snapshotting heap. */
25 void * operator new(size_t size) { \
26 return model_malloc(size); \
28 void operator delete(void *p, size_t size) { \
31 void * operator new[](size_t size) { \
32 return model_malloc(size); \
34 void operator delete[](void *p, size_t size) { \
37 void * operator new(size_t size, void *p) { \
41 void *model_malloc(size_t size);
42 void *model_calloc(size_t count, size_t size);
43 void * model_realloc(void *ptr, size_t size);
44 void model_free(void *ptr);
46 /** @brief Provides a non-snapshotting allocator for use in STL classes.
48 * The code was adapted from a code example from the book The C++
49 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
50 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
51 * Permission to copy, use, modify, sell and distribute this software
52 * is granted provided this copyright notice appears in all copies.
53 * This software is provided "as is" without express or implied
54 * warranty, and with no claim as to its suitability for any purpose.
62 typedef const T* const_pointer;
64 typedef const T& const_reference;
65 typedef size_t size_type;
66 typedef size_t difference_type;
68 // rebind allocator to type U
71 typedef ModelAlloc<U> other;
74 // return address of values
75 pointer address(reference value) const {
78 const_pointer address(const_reference value) const {
82 /* constructors and destructor
83 * - nothing to do because the allocator has no state
85 ModelAlloc() throw() {
87 ModelAlloc(const ModelAlloc&) throw() {
90 ModelAlloc(const ModelAlloc<U>&) throw() {
92 ~ModelAlloc() throw() {
95 // return maximum number of elements that can be allocated
96 size_type max_size() const throw() {
97 return std::numeric_limits<size_t>::max() / sizeof(T);
100 // allocate but don't initialize num elements of type T
101 pointer allocate(size_type num, const void * = 0) {
102 pointer p = (pointer)model_malloc(num * sizeof(T));
106 // initialize elements of allocated storage p with value value
107 void construct(pointer p, const T& value) {
108 // initialize memory with placement new
109 new((void*)p)T(value);
112 // destroy elements of initialized storage p
113 void destroy(pointer p) {
114 // destroy objects by calling their destructor
118 // deallocate storage p of deleted elements
119 void deallocate(pointer p, size_type num) {
120 model_free((void*)p);
124 /** Return that all specializations of this allocator are interchangeable. */
125 template <class T1, class T2>
126 bool operator ==(const ModelAlloc<T1>&,
127 const ModelAlloc<T2>&) throw() {
131 /** Return that all specializations of this allocator are interchangeable. */
132 template <class T1, class T2>
133 bool operator!= (const ModelAlloc<T1>&,
134 const ModelAlloc<T2>&) throw() {
139 #endif/* _MY_MEMORY_H */