1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef MEMBLOCK_H_7ED63A891164CC43578E63664D52A196
|
7 |
|
|
#define MEMBLOCK_H_7ED63A891164CC43578E63664D52A196
|
8 |
|
|
|
9 |
|
|
#include "memlink.h"
|
10 |
|
|
#include "config.h"
|
11 |
|
|
|
12 |
|
|
namespace ustl {
|
13 |
|
|
|
14 |
|
|
/// \class memblock memblock.h ustl.h
|
15 |
|
|
/// \ingroup MemoryManagement
|
16 |
|
|
///
|
17 |
|
|
/// \brief Allocated memory block.
|
18 |
|
|
///
|
19 |
|
|
/// Adds memory management capabilities to memlink. Uses malloc and realloc to
|
20 |
|
|
/// maintain the internal pointer, but only if allocated using members of this class,
|
21 |
|
|
/// or if linked to using the Manage() member function. Managed memory is automatically
|
22 |
|
|
/// freed in the destructor.
|
23 |
|
|
///
|
24 |
|
|
class memblock : public memlink {
|
25 |
|
|
public:
|
26 |
|
|
memblock (void);
|
27 |
|
|
memblock (const void* p, size_type n);
|
28 |
|
|
explicit memblock (size_type n);
|
29 |
|
|
explicit memblock (const cmemlink& b);
|
30 |
|
|
explicit memblock (const memlink& b);
|
31 |
|
|
memblock (const memblock& b);
|
32 |
|
|
virtual ~memblock (void) throw();
|
33 |
|
|
virtual void unlink (void) throw();
|
34 |
|
|
inline void assign (const cmemlink& l) { assign (l.cdata(), l.readable_size()); }
|
35 |
|
|
inline const memblock& operator= (const cmemlink& l) { assign (l); return (*this); }
|
36 |
|
|
inline const memblock& operator= (const memlink& l) { assign (l); return (*this); }
|
37 |
|
|
inline const memblock& operator= (const memblock& l) { assign (l); return (*this); }
|
38 |
|
|
inline void swap (memblock& l) { memlink::swap (l); ::ustl::swap (m_Capacity, l.m_Capacity); }
|
39 |
|
|
void assign (const void* p, size_type n);
|
40 |
|
|
void reserve (size_type newSize, bool bExact = true);
|
41 |
|
|
void resize (size_type newSize, bool bExact = true);
|
42 |
|
|
iterator insert (iterator start, size_type size);
|
43 |
|
|
iterator erase (iterator start, size_type size);
|
44 |
|
|
inline void clear (void) { resize (0); }
|
45 |
|
|
inline size_type capacity (void) const { return (m_Capacity); }
|
46 |
|
|
inline bool is_linked (void) const { return (!capacity()); }
|
47 |
|
|
inline size_type max_size (void) const { return (is_linked() ? memlink::max_size() : SIZE_MAX); }
|
48 |
|
|
inline void manage (memlink& l) { manage (l.begin(), l.size()); }
|
49 |
|
|
void deallocate (void) throw();
|
50 |
|
|
void manage (void* p, size_type n);
|
51 |
|
|
void copy_link (void);
|
52 |
|
|
void read (istream& is);
|
53 |
|
|
#ifdef CYGCLS_USTL_FSTREAMS
|
54 |
|
|
void read_file (const char* filename);
|
55 |
|
|
#endif
|
56 |
|
|
protected:
|
57 |
|
|
virtual size_type minimumFreeCapacity (void) const throw() __attribute__((const));
|
58 |
|
|
private:
|
59 |
|
|
size_type m_Capacity; ///< Number of bytes allocated by Resize.
|
60 |
|
|
};
|
61 |
|
|
|
62 |
|
|
} // namespace ustl
|
63 |
|
|
|
64 |
|
|
#endif
|