forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memblock.h
60 lines (56 loc) · 2.72 KB
/
memblock.h
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
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "memlink.h"
namespace ustl {
/// \class memblock memblock.h ustl.h
/// \ingroup MemoryManagement
///
/// \brief Allocated memory block.
///
/// Adds memory management capabilities to memlink. Uses malloc and realloc to
/// maintain the internal pointer, but only if allocated using members of this class,
/// or if linked to using the Manage() member function. Managed memory is automatically
/// freed in the destructor.
///
class memblock : public memlink {
public:
constexpr memblock (void) noexcept : memlink(), _capacity (0) {}
memblock (const void* p, size_type n);
explicit memblock (size_type n);
explicit memblock (const cmemlink& b);
explicit memblock (const memlink& b);
memblock (const memblock& b);
constexpr memblock (memblock&& b) : memlink(), _capacity(0) { swap (b); }
constexpr memblock& operator= (memblock&& b) { swap (b); return *this; }
virtual ~memblock (void) noexcept;
virtual void unlink (void) noexcept override;
inline void assign (const cmemlink& l) { assign (l.cdata(), l.readable_size()); }
inline const memblock& operator= (const cmemlink& l) { assign (l); return *this; }
inline const memblock& operator= (const memlink& l) { assign (l); return *this; }
inline const memblock& operator= (const memblock& l) { assign (l); return *this; }
constexpr void swap (memblock& l) noexcept { memlink::swap (l); ::ustl::swap (_capacity, l._capacity); }
void assign (const void* p, size_type n);
void reserve (size_type newSize, bool bExact = false);
void resize (size_type newSize, bool bExact = true);
iterator insert (const_iterator start, size_type size);
iterator erase (const_iterator start, size_type size);
inline void clear (void) noexcept { resize (0); }
constexpr size_type capacity (void) const { return _capacity; }
constexpr bool is_linked (void) const { return !capacity(); }
constexpr size_type max_size (void) const { return is_linked() ? memlink::max_size() : SIZE_MAX; }
inline void manage (memlink& l) { manage (l.begin(), l.size()); }
void deallocate (void) noexcept;
void shrink_to_fit (void);
void manage (void* p, size_type n) noexcept;
void copy_link (void);
void read (istream& is);
void read_file (const char* filename);
protected:
virtual size_type minimumFreeCapacity (void) const noexcept __attribute__((const)) { return 0; }
private:
size_type _capacity; ///< Number of bytes allocated by Resize.
};
} // namespace ustl