~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/core/alloc_type.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: alloc_type.hpp 15649 2009-03-09 09:38:00Z rubidium $ */
 
2
 
 
3
/** @file alloc_type.hpp Helper types related to the allocation of memory */
 
4
 
 
5
#ifndef ALLOC_TYPE_HPP
 
6
#define ALLOC_TYPE_HPP
 
7
 
 
8
#include "alloc_func.hpp"
 
9
 
 
10
/**
 
11
 * A small 'wrapper' for allocations that can be done on most OSes on the
 
12
 * stack, but are just too large to fit in the stack on devices with a small
 
13
 * stack such as the NDS.
 
14
 * So when it is possible a stack allocation is made, otherwise a heap
 
15
 * allocation is made and this is freed once the struct goes out of scope.
 
16
 * @param T      the type to make the allocation for
 
17
 * @param length the amount of items to allocate
 
18
 */
 
19
template <typename T, size_t length>
 
20
struct SmallStackSafeStackAlloc {
 
21
#if !defined(__NDS__)
 
22
        /** Storing the data on the stack */
 
23
        T data[length];
 
24
#else
 
25
        /** Storing it on the heap */
 
26
        T *data;
 
27
        /** The length (in elements) of data in this allocator. */
 
28
        size_t len;
 
29
 
 
30
        /** Allocating the memory */
 
31
        SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}
 
32
 
 
33
        /** And freeing when it goes out of scope */
 
34
        ~SmallStackSafeStackAlloc()
 
35
        {
 
36
                free(data);
 
37
        }
 
38
#endif
 
39
 
 
40
        /**
 
41
         * Gets a pointer to the data stored in this wrapper.
 
42
         * @return the pointer.
 
43
         */
 
44
        FORCEINLINE operator T *()
 
45
        {
 
46
                return data;
 
47
        }
 
48
 
 
49
        /**
 
50
         * Gets a pointer to the data stored in this wrapper.
 
51
         * @return the pointer.
 
52
         */
 
53
        FORCEINLINE T *operator -> ()
 
54
        {
 
55
                return data;
 
56
        }
 
57
 
 
58
        /**
 
59
         * Gets a pointer to the last data element stored in this wrapper.
 
60
         * @note needed because endof does not work properly for pointers.
 
61
         * @return the 'endof' pointer.
 
62
         */
 
63
        FORCEINLINE T *EndOf()
 
64
        {
 
65
#if !defined(__NDS__)
 
66
                return endof(data);
 
67
#else
 
68
                return &data[len];
 
69
#endif
 
70
        }
 
71
};
 
72
 
 
73
/**
 
74
 * A reusable buffer that can be used for places that temporary allocate
 
75
 * a bit of memory and do that very often, or for places where static
 
76
 * memory is allocated that might need to be reallocated sometimes.
 
77
 *
 
78
 * Every time Allocate or ZeroAllocate is called previous results of both
 
79
 * functions will become invalid.
 
80
 */
 
81
template <typename T>
 
82
class ReusableBuffer {
 
83
private:
 
84
        T *buffer;    ///< The real data buffer
 
85
        size_t count; ///< Number of T elements in the buffer
 
86
 
 
87
public:
 
88
        /** Create a new buffer */
 
89
        ReusableBuffer() : buffer(NULL), count(0) {}
 
90
        /** Clear the buffer */
 
91
        ~ReusableBuffer() { free(this->buffer); }
 
92
 
 
93
        /**
 
94
         * Get buffer of at least count times T.
 
95
         * @note the buffer might be bigger
 
96
         * @note calling this function invalidates any previous buffers given
 
97
         * @param count the minimum buffer size
 
98
         * @return the buffer
 
99
         */
 
100
        T *Allocate(size_t count)
 
101
        {
 
102
                if (this->count < count) {
 
103
                        free(this->buffer);
 
104
                        this->buffer = MallocT<T>(count);
 
105
                        this->count = count;
 
106
                }
 
107
                return this->buffer;
 
108
        }
 
109
 
 
110
        /**
 
111
         * Get buffer of at least count times T with zeroed memory.
 
112
         * @note the buffer might be bigger
 
113
         * @note calling this function invalidates any previous buffers given
 
114
         * @param count the minimum buffer size
 
115
         * @return the buffer
 
116
         */
 
117
        T *ZeroAllocate(size_t count)
 
118
        {
 
119
                if (this->count < count) {
 
120
                        free(this->buffer);
 
121
                        this->buffer = CallocT<T>(count);
 
122
                        this->count = count;
 
123
                } else {
 
124
                        memset(this->buffer, 0, sizeof(T) * count);
 
125
                }
 
126
                return this->buffer;
 
127
        }
 
128
 
 
129
        /**
 
130
         * Get the currently allocated buffer.
 
131
         * @return the buffer
 
132
         */
 
133
        FORCEINLINE const T *GetBuffer() const
 
134
        {
 
135
                return this->buffer;
 
136
        }
 
137
};
 
138
 
 
139
/**
 
140
 * Base class that provides memory initialization on dynamically created objects.
 
141
 * All allocated memory will be zeroed.
 
142
 */
 
143
class ZeroedMemoryAllocator
 
144
{
 
145
public:
 
146
        ZeroedMemoryAllocator() {}
 
147
        virtual ~ZeroedMemoryAllocator() {}
 
148
 
 
149
        /**
 
150
         * Memory allocator for a single class instance.
 
151
         * @param size the amount of bytes to allocate.
 
152
         * @return the given amounts of bytes zeroed.
 
153
         */
 
154
        FORCEINLINE void *operator new(size_t size) { return CallocT<byte>(size); }
 
155
 
 
156
        /**
 
157
         * Memory allocator for an array of class instances.
 
158
         * @param size the amount of bytes to allocate.
 
159
         * @return the given amounts of bytes zeroed.
 
160
         */
 
161
        FORCEINLINE void *operator new[](size_t size) { return CallocT<byte>(size); }
 
162
 
 
163
        /**
 
164
         * Memory release for a single class instance.
 
165
         * @param ptr  the memory to free.
 
166
         * @param size the amount of allocated memory (unused).
 
167
         *
 
168
         * @warning The value of the \a size parameter can only be trusted for
 
169
         *          classes that have their own (virtual) destructor method.
 
170
         */
 
171
        FORCEINLINE void operator delete(void *ptr, size_t size) { free(ptr); }
 
172
 
 
173
        /**
 
174
         * Memory release for an array of class instances.
 
175
         * @param ptr  the memory to free.
 
176
         * @param size the amount of allocated memory (unused).
 
177
         *
 
178
         * @warning The value of the \a size parameter can only be trusted for
 
179
         *          classes that have their own (virtual) destructor method.
 
180
         */
 
181
        FORCEINLINE void operator delete[](void *ptr, size_t size) { free(ptr); }
 
182
};
 
183
 
 
184
#endif /* ALLOC_TYPE_HPP */