Switch to C for Zach
[satune.git] / src / mymemory.h
1 /*      Copyright (c) 2015 Regents of the University of California
2  *
3  *      Author: Brian Demsky <bdemsky@uci.edu>
4  *
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.
8  */
9
10 /** @file mymemory.h
11  *  @brief Memory allocation functions.
12  */
13
14 #ifndef _MY_MEMORY_H
15 #define _MY_MEMORY_H
16 #include <limits.h>
17 #include <stddef.h>
18 #include <stdlib.h>
19
20 #include "config.h"
21
22 /** MEMALLOC declares the allocators for a class to allocate
23  *      memory in the non-snapshotting heap. */
24 #define MEMALLOC \
25         void * operator new(size_t size) { \
26                 return model_malloc(size); \
27         } \
28         void operator delete(void *p, size_t size) { \
29                 model_free(p);                                   \
30         } \
31         void * operator new[](size_t size) { \
32                 return model_malloc(size); \
33         } \
34         void operator delete[](void *p, size_t size) { \
35                 model_free(p); \
36         } \
37         void * operator new(size_t size, void *p) {     \
38                 return p;                                                                                                                                                               \
39         }
40
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);
45
46 /** @brief Provides a non-snapshotting allocator for use in STL classes.
47  *
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.
55  */
56 template <class T>
57 class ModelAlloc {
58 public:
59         // type definitions
60         typedef T value_type;
61         typedef T*       pointer;
62         typedef const T* const_pointer;
63         typedef T&       reference;
64         typedef const T& const_reference;
65         typedef size_t size_type;
66         typedef size_t difference_type;
67
68         // rebind allocator to type U
69         template <class U>
70         struct rebind {
71                 typedef ModelAlloc<U> other;
72         };
73
74         // return address of values
75         pointer address(reference value) const {
76                 return &value;
77         }
78         const_pointer address(const_reference value) const {
79                 return &value;
80         }
81
82         /* constructors and destructor
83          * - nothing to do because the allocator has no state
84          */
85         ModelAlloc() throw() {
86         }
87         ModelAlloc(const ModelAlloc&) throw() {
88         }
89         template <class U>
90         ModelAlloc(const ModelAlloc<U>&) throw() {
91         }
92         ~ModelAlloc() throw() {
93         }
94
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);
98         }
99
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));
103                 return p;
104         }
105
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);
110         }
111
112         // destroy elements of initialized storage p
113         void destroy(pointer p) {
114                 // destroy objects by calling their destructor
115                 p->~T();
116         }
117
118         // deallocate storage p of deleted elements
119         void deallocate(pointer p, size_type num) {
120                 model_free((void*)p);
121         }
122 };
123
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() {
128         return true;
129 }
130
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() {
135         return false;
136 }
137
138
139 #endif/* _MY_MEMORY_H */