File: dmtcpalloc.h

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (231 lines) | stat: -rw-r--r-- 5,761 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
///////////////////////////////////////////////////////////////////////////////
//
// BASED ON:
//
//  DmtcpAlloc.h
//
//  Malloc-based allocator. Uses standard malloc and free.
//
//  Copyright � 2002 Pete Isensee (PKIsensee@msn.com).
//  All rights reserved worldwide.
//
//  Permission to copy, modify, reproduce or redistribute this source code is
//  granted provided the above copyright notice is retained in the resulting
//  source code.
//
//  This software is provided "as is" and without any express or implied
//  warranties.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef DMTCPALLOC_H
#define DMTCPALLOC_H

#include "../jalib/jalloc.h"
#include <memory>
#include <limits>
#include <vector>
#include <string>
#include <map>
#include <list>
#include <set>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstddef>
#include <utility>

namespace dmtcp
{


template <typename T>
class DmtcpAlloc
{
public:
    typedef size_t    size_type;
    typedef ptrdiff_t difference_type;
    typedef T*        pointer;
    typedef const T*  const_pointer;
    typedef T&        reference;
    typedef const T&  const_reference;
    typedef T         value_type;
#if __cplusplus >= 201103L
    typedef std::true_type propagate_on_container_move_assignment;
#endif

public:

    // Constructors
    DmtcpAlloc() throw() {}

    DmtcpAlloc( const DmtcpAlloc& ) throw() {}

    template <typename U>
    DmtcpAlloc( const DmtcpAlloc<U>& ) throw() {}

    DmtcpAlloc& operator=( const DmtcpAlloc& )
    {
        return *this;
    }

    // Destructor
    ~DmtcpAlloc() throw()
    {
    }

    // Utility functions
    pointer address( reference r ) const
    {
        return &r;
    }

    const_pointer address( const_reference c ) const
    {
        return &c;
    }

    size_type max_size() const
    {
        return std::numeric_limits<size_t>::max() / sizeof(T);
    }

#if __cplusplus >= 201103L
    // In-place construction
    template<typename X, typename... Args>
      void construct(X* p, Args&&... args)
      {
	::new((void *)p) X(std::forward<Args>(args)...);
      }

    // In-place destruction
    template<typename X>
      void destroy(X* p)
      {
	p->~X();
      }
#else
    // In-place construction
    void construct( pointer p, const_reference c )
    {
        // placement new operator
        new( reinterpret_cast<void*>(p) ) T(c);
    }

    // In-place construction
    template<typename X>
      void construct( X* p, const X& c )
      {
        // placement new operator
        new( reinterpret_cast<void*>(p) ) X(c);
      }

    // In-place destruction
    void destroy( pointer p )
    {
        // call destructor directly
        (p)->~T();
    }

# if __GNUC__ == 4 && __GNUC_MINOR__ == 0
    // In-place destruction
    //   This is a workaround for a bug in gcc-4.0.
    template<typename X>
    void destroy( X* p )
    {
        // call destructor directly
        (p)->~X();
    }
# endif
#endif

    // Rebind to allocators of other types
    template <typename U>
    struct rebind
    {
        typedef DmtcpAlloc<U> other;
    };

    // Allocate raw memory
    pointer allocate( size_type n, const void* = NULL )
    {
      //void* p = malloc( n * sizeof(T) );
      //if( p == NULL )
      //    throw std::bad_alloc();
      //return pointer(p);
      return pointer(jalib::JAllocDispatcher::allocate(n*sizeof(T)));
    }

    // Free raw memory.
    // Note that C++ standard defines this function as
    // deallocate( pointer p, size_type). Because Visual C++ 6.0
    // compiler doesn't support template rebind, Dinkumware uses
    // void* hack.
    void deallocate( void* p, size_type n )
    {
      //// assert( p != NULL );
      //// The standard states that p must not be NULL. However, some
      //// STL implementations fail this requirement, so the check must
      //// be made here.
      //if( p == NULL )
      //    return;
      //free( p );
      jalib::JAllocDispatcher::deallocate(p, n*sizeof(T));
    }

 // // Non-standard Dinkumware hack for Visual C++ 6.0 compiler.
 // // VC 6 doesn't support template rebind.
 // char* _Charalloc( size_type nBytes )
 // {
 //     char* p = reinterpret_cast<char*>( malloc( nBytes ) );
 //     if( p == NULL )
 //         throw dmtcp::bad_alloc();
 //     return p;
 // }

}; // end of DmtcpAlloc

// Comparison
template <typename T1, typename T2>
bool operator==( const DmtcpAlloc<T1>&,
                 const DmtcpAlloc<T2>& ) throw()
{
    return true;
}

template <typename T1, typename T2>
bool operator!=( const DmtcpAlloc<T1>&,
                 const DmtcpAlloc<T2>& ) throw()
{
    return false;
}

typedef std::basic_string< char, std::char_traits<char>, DmtcpAlloc<char> > string;
typedef std::basic_stringstream< char, std::char_traits<char>, DmtcpAlloc<char> > stringstream;
typedef std::basic_istringstream< char, std::char_traits<char>, DmtcpAlloc<char> > istringstream;
typedef std::basic_ostringstream< char, std::char_traits<char>, DmtcpAlloc<char> > ostringstream;
typedef std::ostream ostream;
typedef std::istream istream;
typedef std::iostream iostream;
typedef std::fstream fstream;
typedef std::ofstream ofstream;
typedef std::ifstream ifstream;

template < typename T >
class vector: public std::vector<T, DmtcpAlloc<T> > {
public:
  vector(size_t n, const T& v=T()) : std::vector<T, DmtcpAlloc<T> >(n, v) {}
  vector() : std::vector<T, DmtcpAlloc<T> >() {}
};

template < typename T >
class list: public std::list<T, DmtcpAlloc<T> > {};

template < typename K, typename V >
class map: public std::map<K, V, std::less<K>, DmtcpAlloc<std::pair<K, V> > > {};

template < typename K >
class set: public std::set<K, std::less<K>, DmtcpAlloc<K> > {};

}
#endif