2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file blob.hpp Support for storing random binary data. */
13 #include "../core/alloc_func.hpp"
16 * Base class for simple binary blobs.
18 * The word 'simple' means:
19 * - no configurable allocator type (always made from heap)
20 * - no smart deallocation - deallocation must be called from the same
21 * module (DLL) where the blob was allocated
22 * - no configurable allocation policy (how big blocks should be allocated)
23 * - no extra ownership policy (i.e. 'copy on write') when blob is copied
24 * - no thread synchronization at all
26 * Internal member layout:
27 * 1. The only class member is pointer to the first item (see union).
28 * 2. Allocated block contains the blob header (see BlobHeader) followed by the raw byte data.
29 * Always, when it allocates memory the allocated size is:
30 * sizeof(BlobHeader) + <data capacity>
31 * 3. Two 'virtual' members (items and capacity) are stored in the BlobHeader at beginning
32 * of the allocated block.
33 * 4. The pointer of the union pobsize_ts behind the header (to the first data byte).
34 * When memory block is allocated, the sizeof(BlobHeader) it added to it.
35 * 5. Benefits of this layout:
36 * - items are accessed in the simplest possible way - just dereferencing the pointer,
37 * which is good for performance (assuming that data are accessed most often).
38 * - sizeof(blob) is the same as the size of any other pointer
39 * 6. Drawbacks of this layout:
40 * - the fact that a pointer to the allocated block is adjusted by sizeof(BlobHeader) before
41 * it is stored can lead to several confusions:
42 * - it is not a common pattern so the implementation code is bit harder to read.
43 * - valgrind may generate a warning that the allocated block is lost (not accessible).
47 /** header of the allocated memory block */
49 size_t items
; ///< actual blob size in bytes
50 size_t capacity
; ///< maximum (allocated) size in bytes
53 /** type used as class member */
55 byte
*data
; ///< ptr to the first byte of data
56 BlobHeader
*header
; ///< ptr just after the BlobHeader holding items and capacity
61 * Just to silence an unsilencable GCC 4.4+ warning
62 * Note: This cannot be 'const' as we do a lot of 'hdrEmpty[0]->items += 0;' and 'hdrEmpty[0]->capacity += 0;'
63 * after const_casting.
65 static BlobHeader hdrEmpty
[];
68 static const size_t tail_reserve
= 4; ///< four extra bytes will be always allocated and zeroed at the end
69 static const size_t header_size
= sizeof(BlobHeader
);
71 /** default constructor - initializes empty blob */
77 /** copy constructor */
78 inline ByteBlob(const ByteBlob
&src
)
84 /** move constructor - take ownership of blob data */
85 inline ByteBlob(BlobHeader
* const & src
)
87 assert(src
!= nullptr);
89 *const_cast<BlobHeader
**>(&src
) = nullptr;
99 /** all allocation should happen here */
100 static inline BlobHeader
*RawAlloc(size_t num_bytes
)
102 return (BlobHeader
*)MallocT
<byte
>(num_bytes
);
106 * Return header pointer to the static BlobHeader with
107 * both items and capacity containing zero
109 static inline BlobHeader
*Zero()
111 return const_cast<BlobHeader
*>(&ByteBlob::hdrEmpty
[1]);
114 /** simple allocation policy - can be optimized later */
115 static inline size_t AllocPolicy(size_t min_alloc
)
117 if (min_alloc
< (1 << 9)) {
118 if (min_alloc
< (1 << 5)) return (1 << 5);
119 return (min_alloc
< (1 << 7)) ? (1 << 7) : (1 << 9);
121 if (min_alloc
< (1 << 15)) {
122 if (min_alloc
< (1 << 11)) return (1 << 11);
123 return (min_alloc
< (1 << 13)) ? (1 << 13) : (1 << 15);
125 if (min_alloc
< (1 << 20)) {
126 if (min_alloc
< (1 << 17)) return (1 << 17);
127 return (min_alloc
< (1 << 19)) ? (1 << 19) : (1 << 20);
129 min_alloc
= (min_alloc
| ((1 << 20) - 1)) + 1;
133 /** all deallocations should happen here */
134 static inline void RawFree(BlobHeader
*p
)
136 /* Just to silence an unsilencable GCC 4.4+ warning. */
137 assert(p
!= ByteBlob::hdrEmpty
);
139 /* In case GCC warns about the following, see GCC's PR38509 why it is bogus. */
143 /** initialize the empty blob */
144 inline void InitEmpty()
149 /** initialize blob by attaching it to the given header followed by data */
150 inline void Init(BlobHeader
*src
)
155 /** blob header accessor - use it rather than using the pointer arithmetic directly - non-const version */
156 inline BlobHeader
& Hdr()
158 return *(header
- 1);
161 /** blob header accessor - use it rather than using the pointer arithmetic directly - const version */
162 inline const BlobHeader
& Hdr() const
164 return *(header
- 1);
167 /** return reference to the actual blob size - used when the size needs to be modified */
168 inline size_t& LengthRef()
174 /** return true if blob doesn't contain valid data */
175 inline bool IsEmpty() const
177 return Length() == 0;
180 /** return the number of valid data bytes in the blob */
181 inline size_t Length() const
186 /** return the current blob capacity in bytes */
187 inline size_t Capacity() const
189 return Hdr().capacity
;
192 /** return pointer to the first byte of data - non-const version */
198 /** return pointer to the first byte of data - const version */
199 inline const byte
*Begin() const
204 /** invalidate blob's data - doesn't free buffer */
210 /** free the blob's memory */
213 if (Capacity() > 0) {
219 /** append new bytes at the end of existing data bytes - reallocates if necessary */
220 inline void AppendRaw(const void *p
, size_t num_bytes
)
222 assert(p
!= nullptr);
224 memcpy(Append(num_bytes
), p
, num_bytes
);
228 /** append bytes from given source blob to the end of existing data bytes - reallocates if necessary */
229 inline void AppendRaw(const ByteBlob
& src
)
231 if (!src
.IsEmpty()) {
232 memcpy(Append(src
.Length()), src
.Begin(), src
.Length());
237 * Reallocate if there is no free space for num_bytes bytes.
238 * @return pointer to the new data to be added
240 inline byte
*Prepare(size_t num_bytes
)
242 size_t new_size
= Length() + num_bytes
;
243 if (new_size
> Capacity()) SmartAlloc(new_size
);
244 return data
+ Length();
248 * Increase Length() by num_bytes.
249 * @return pointer to the new data added
251 inline byte
*Append(size_t num_bytes
)
253 byte
*pNewData
= Prepare(num_bytes
);
254 LengthRef() += num_bytes
;
258 /** reallocate blob data if needed */
259 void SmartAlloc(size_t new_size
)
261 if (Capacity() >= new_size
) return;
262 /* calculate minimum block size we need to allocate
263 * and ask allocation policy for some reasonable block size */
264 assert(new_size
< SIZE_MAX
- header_size
- tail_reserve
);
265 new_size
= AllocPolicy(header_size
+ new_size
+ tail_reserve
);
267 /* allocate new block and setup header */
268 BlobHeader
*tmp
= RawAlloc(new_size
);
269 tmp
->items
= Length();
270 tmp
->capacity
= new_size
- (header_size
+ tail_reserve
);
272 /* copy existing data */
273 if (tmp
->items
!= 0) {
274 memcpy(tmp
+ 1, data
, tmp
->items
);
277 /* replace our block with new one */
278 if (Capacity() > 0) {
284 /** fixing the four bytes at the end of blob data - useful when blob is used to hold string */
285 inline void FixTail() const
287 if (Capacity() > 0) {
288 byte
*p
= &data
[Length()];
289 for (uint i
= 0; i
< tail_reserve
; i
++) {
297 * Blob - simple dynamic T array. T (template argument) is a placeholder for any type.
298 * T can be any integral type, pointer, or structure. Using Blob instead of just plain C array
299 * simplifies the resource management in several ways:
300 * 1. When adding new item(s) it automatically grows capacity if needed.
301 * 2. When variable of type Blob comes out of scope it automatically frees the data buffer.
302 * 3. Takes care about the actual data size (number of used items).
303 * 4. Dynamically constructs only used items (as opposite of static array which constructs all items)
305 template <typename T
>
306 class CBlobT
: public ByteBlob
{
307 /* make template arguments public: */
309 typedef ByteBlob base
;
311 static const size_t type_size
= sizeof(T
);
314 typename
base::BlobHeader
*header
;
316 OnTransfer(const OnTransfer
& src
) : header(src
.header
)
318 assert(src
.header
!= nullptr);
319 *const_cast<typename
base::BlobHeader
**>(&src
.header
) = nullptr;
322 OnTransfer(CBlobT
& src
) : header(src
.header
)
329 assert(header
== nullptr);
333 /** Default constructor - makes new Blob ready to accept any data */
338 /** Take ownership constructor */
339 inline CBlobT(const OnTransfer
& ot
)
343 /** Destructor - ensures that allocated memory (if any) is freed */
349 /** Check the validity of item index (only in debug mode) */
350 inline void CheckIdx(size_t index
) const
352 assert(index
< Size());
355 /** Return pointer to the first data item - non-const version */
358 return (T
*)base::Begin();
361 /** Return pointer to the first data item - const version */
362 inline const T
*Data() const
364 return (const T
*)base::Begin();
367 /** Return pointer to the index-th data item - non-const version */
368 inline T
*Data(size_t index
)
371 return (Data() + index
);
374 /** Return pointer to the index-th data item - const version */
375 inline const T
*Data(size_t index
) const
378 return (Data() + index
);
381 /** Return number of items in the Blob */
382 inline size_t Size() const
384 return (base::Length() / type_size
);
387 /** Return total number of items that can fit in the Blob without buffer reallocation */
388 inline size_t MaxSize() const
390 return (base::Capacity() / type_size
);
393 /** Return number of additional items that can fit in the Blob without buffer reallocation */
394 inline size_t GetReserve() const
396 return ((base::Capacity() - base::Length()) / type_size
);
399 /** Grow number of data items in Blob by given number - doesn't construct items */
400 inline T
*GrowSizeNC(size_t num_items
)
402 return (T
*)base::Append(num_items
* type_size
);
406 * Ensures that given number of items can be added to the end of Blob. Returns pointer to the
407 * first free (unused) item
409 inline T
*MakeFreeSpace(size_t num_items
)
411 return (T
*)base::Prepare(num_items
* type_size
);
414 inline OnTransfer
Transfer()
416 return OnTransfer(*this);
421 #endif /* BLOB_HPP */