language: C++11 (gcc-4.7.2)
date: 98 days 14 hours ago
link:
visibility: public
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
/** Trying this again. Simpler approach, maybe...
**/
 
#include <cstddef>
#include <limits>
#include <new>
#include <stdexcept>
#include <iostream>
#include <cstdlib>
 
template <typename T> class MemoryManager
{
    public:
    /** Typedefs **/
        typedef T * pointer;
        typedef const T * const_pointer;
        typedef T& reference;
        typedef const T& const_reference;
        typedef T value_type;
        typedef size_t size_type;
        typedef ptrdiff_t difference_type;
 
        T* allocate(size_t n) const;
        void deallocate(T* const buffer, size_t) const;
        void construct(T* const ptr, const T& value) const;
        size_t max_size() const;
        MemoryManager() {};
 
        template <typename U> struct rebind
        {
            typedef MemoryManager<U> other;
        };
};
 
template <typename T>
T* MemoryManager<T>::allocate(size_t n) const
{
    if(n == 0)
        return NULL;
 
    if(n > max_size())
    {
        throw std::length_error("Integer overflow at MemoryManager::allocate()");
    }
 
    T* buffer = static_cast<T*>(malloc(n * sizeof(T)));
 
    if(buffer == NULL)
    {
        throw std::bad_alloc();
    }
 
    std::cout << (n * sizeof(T)) << " bytes allocated.\n";
    return buffer;
}
 
template <typename T>
void MemoryManager<T>::deallocate(T* const buffer, size_t) const
{
    free(buffer);
    std::cout << "Buffer deallocated.\n";
}
 
template <typename T>
void MemoryManager<T>::construct(T* const ptr, const T& value) const
{
    void* const placement = static_cast<void*>(ptr);
    new (placement) T(value);
}
 
template <typename T>
size_t MemoryManager<T>::max_size() const
{
    return std::numeric_limits<size_t>::max();
}
 
#include <vector>
#include <iostream>
int main()
{
    auto f = [&](int i){std::cout<< i << "\t";};
 
        std::vector<int, MemoryManager<int> > v = {1,2,3,4,5};
 
        for(int i : v)
        {
                f(i);
        }
}