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