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.
8 // See cmemlink.h for documentation.
18 /// \brief Attaches the object to pointer \p p of size \p n.
20 /// If \p p is NULL and \p n is non-zero, bad_alloc is thrown and current
21 /// state remains unchanged.
23 void cmemlink::link (const void* p
, size_type n
)
31 /// Writes the object to stream \p os
32 void cmemlink::write (ostream
& os
) const
34 const written_size_type
sz (size());
35 assert (sz
== size() && "No support for writing memblocks larger than 4G");
37 os
.write (cdata(), sz
);
38 os
.align (alignof (sz
));
41 /// Writes the object to stream \p os
42 void cmemlink::text_write (ostringstream
& os
) const
44 os
.write (begin(), readable_size());
47 /// Returns the number of bytes required to write this object to a stream.
48 cmemlink::size_type
cmemlink::stream_size (void) const
50 const written_size_type
sz (size());
51 return (Align (stream_size_of (sz
) + sz
, alignof(sz
)));
54 /// Writes the data to file \p "filename".
55 void cmemlink::write_file (const char* filename
, int mode
) const
58 f
.exceptions (fstream::allbadbits
);
59 f
.open (filename
, fstream::out
| fstream::trunc
, mode
);
60 f
.write (cdata(), readable_size());
64 /// swaps the contents with \p l
65 void cmemlink::swap (cmemlink
& l
)
67 #if CPU_HAS_MMX && SIZE_OF_POINTER == 4
73 : "=m"(m_Data
), "=m"(m_Size
), "=m"(l
.m_Data
), "=m"(l
.m_Size
)
75 : "mm0", "mm1", "st", "st(1)");
77 #elif CPU_HAS_SSE && SIZE_OF_POINTER == 8
79 "movups %0, %%xmm0\n\t"
80 "movups %2, %%xmm1\n\t"
81 "movups %%xmm0, %2\n\t"
83 : "=m"(m_Data
), "=m"(m_Size
), "=m"(l
.m_Data
), "=m"(l
.m_Size
)
87 ::ustl::swap (m_Data
, l
.m_Data
);
88 ::ustl::swap (m_Size
, l
.m_Size
);
92 /// Compares to memory block pointed by l. Size is compared first.
93 bool cmemlink::operator== (const cmemlink
& l
) const
95 return (l
.m_Size
== m_Size
&&
96 (l
.m_Data
== m_Data
|| 0 == memcmp (l
.m_Data
, m_Data
, m_Size
)));