1 // This file is part of the ustl library, an STL implementation. 2 // 3 // Copyright (C) 2005 by Mike Sharov <msharov (at) users.sourceforge.net> 4 // This file is free software, distributed under the MIT License. 5 // 6 // memblock.h 7 // 8 9 #ifndef MEMBLOCK_H_7ED63A891164CC43578E63664D52A196 10 #define MEMBLOCK_H_7ED63A891164CC43578E63664D52A196 11 12 #include "memlink.h" 13 14 namespace ustl { 15 16 /// \class memblock memblock.h ustl.h 17 /// \ingroup MemoryManagement 18 /// 19 /// \brief Allocated memory block. 20 /// 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. 25 /// 26 class memblock : public memlink { 27 public: 28 static const size_type c_PageSize = 64; ///< The default minimum allocation unit. 29 public: 30 memblock (void); 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); 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 bool is_linked (void) const { return (!m_Capacity && cdata()); } 50 inline size_type max_size (void) const { return (is_linked() ? memlink::max_size() : SIZE_MAX); } 51 inline size_type capacity (void) const { return (m_Capacity); } 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); 58 protected: 59 inline virtual size_type minimumFreeCapacity (void) const { return (0); } 60 private: 61 size_type m_Capacity; ///< Number of bytes allocated by Resize. 62 }; 63 64 /// Reads object \p l from stream \p is 65 inline istream& operator>> (istream& is, memblock& l) 66 { 67 l.read (is); 68 return (is); 69 } 70 71 } // namespace ustl 72 73 #endif 74 75