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 // memlink.cc 7 // 8 // A pointer to a sized block of memory. 9 // 10 11 #include "mistream.h" 12 #include "uassert.h" 13 #include "ustdxept.h" 14 15 #if PLATFORM_ANDROID 16 #include <stdio.h> 17 #endif 18 19 namespace ustl { 20 21 /// Reads the object from stream \p s 22 void memlink::read (istream& is) 23 { 24 written_size_type n; 25 is >> n; 26 is.verify_remaining ("read", "ustl::memlink", n); 27 if (n > size()) 28 #if PLATFORM_ANDROID 29 printf("length error\n"); 30 #else 31 throw length_error ("memlink can not increase the size of the linked storage for reading"); 32 #endif 33 resize (n); 34 is.read (data(), n); 35 is.align (alignof (n)); 36 } 37 38 /// Copies data from \p p, \p n to the linked block starting at \p start. 39 void memlink::copy (iterator start, const void* p, size_type n) 40 { 41 assert (data() || !n); 42 assert (p || !n); 43 assert (start >= begin() && start + n <= end()); 44 if (p) 45 copy_n (const_iterator(p), n, start); 46 } 47 48 /// Fills the linked block with the given pattern. 49 /// \arg start Offset at which to start filling the linked block 50 /// \arg p Pointer to the pattern. 51 /// \arg elSize Size of the pattern. 52 /// \arg elCount Number of times to write the pattern. 53 /// Total number of bytes written is \p elSize * \p elCount. 54 /// 55 void memlink::fill (iterator start, const void* p, size_type elSize, size_type elCount) 56 { 57 assert (data() || !elCount || !elSize); 58 assert (start >= begin() && start + elSize * elCount <= end()); 59 if (elSize == 1) 60 fill_n (start, elCount, *reinterpret_cast<const uint8_t*>(p)); 61 else while (elCount--) 62 start = copy_n (const_iterator(p), elSize, start); 63 } 64 65 } // namespace ustl 66 67