OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [cxx/] [ustl/] [current/] [src/] [cmemlink.cpp] - Blame information for rev 819

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
#include "cmemlink.h"
7
#ifdef CYGCLS_USTL_FSTREAMS
8
#include "ofstream.h"
9
#else
10
#include "sostream.h"
11
#endif
12
#include "strmsize.h"
13
#include "ualgo.h"
14
#include "config.h"
15
 
16
namespace ustl {
17
 
18
/// \brief Attaches the object to pointer \p p of size \p n.
19
///
20
/// If \p p is NULL and \p n is non-zero, bad_alloc is thrown and current
21
/// state remains unchanged.
22
///
23
void cmemlink::link (const void* p, size_type n)
24
{
25
    if (!p && n)
26
        USTL_THROW(bad_alloc (n));
27
    unlink();
28
    relink (p, n);
29
}
30
 
31
void cmemlink::unlink (void) throw()
32
{
33
    m_Data = NULL; m_Size = 0;
34
}
35
 
36
/// Writes the object to stream \p os
37
void cmemlink::write (ostream& os) const
38
{
39
    const written_size_type sz (size());
40
    assert (sz == size() && "No support for writing memblocks larger than 4G");
41
    os << sz;
42
    os.write (cdata(), sz);
43
    os.align (alignof (sz));
44
}
45
 
46
/// Writes the object to stream \p os
47
void cmemlink::text_write (ostringstream& os) const
48
{
49
    os.write (begin(), readable_size());
50
}
51
 
52
/// Returns the number of bytes required to write this object to a stream.
53
cmemlink::size_type cmemlink::stream_size (void) const
54
{
55
    const written_size_type sz (size());
56
    return (Align (stream_size_of (sz) + sz, alignof(sz)));
57
}
58
 
59
#ifdef CYGCLS_USTL_FSTREAMS
60
/// Writes the data to file \p "filename".
61
void cmemlink::write_file (const char* filename, int mode) const
62
{
63
    fstream f;
64
    f.exceptions (fstream::allbadbits);
65
    f.open (filename, fstream::out | fstream::trunc, mode);
66
    f.write (cdata(), readable_size());
67
    f.close();
68
}
69
#endif
70
 
71
/// Compares to memory block pointed by l. Size is compared first.
72
bool cmemlink::operator== (const cmemlink& l) const
73
{
74
    return (l.m_Size == m_Size &&
75
            (l.m_Data == m_Data || 0 == memcmp (l.m_Data, m_Data, m_Size)));
76
}
77
 
78
} // namespace ustl

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.