Line data Source code
1 : /*
2 : * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG__binary_buffer_impl__
34 : #define __H__UG__binary_buffer_impl__
35 :
36 : #include <cassert>
37 : #include <cstring>
38 : #include "vector_util.h"
39 :
40 : namespace ug
41 : {
42 :
43 : inline size_t BinaryBuffer::capacity() const
44 : {
45 : return m_data.size();
46 : }
47 :
48 : inline size_t BinaryBuffer::read_pos() const
49 : {
50 : return m_readPos;
51 : }
52 :
53 : inline size_t BinaryBuffer::write_pos() const
54 : {
55 0 : return m_writePos;
56 : }
57 :
58 0 : inline void BinaryBuffer::read(char* buf, size_t size)
59 : {
60 : // make sure that we only read valid data
61 : assert(m_readPos + size <= m_data.size());
62 : assert(m_readPos + size <= m_writePos);
63 :
64 : // copy the data
65 0 : memcpy(buf, &m_data.front() + m_readPos, size);
66 :
67 : // adjust read-pos
68 0 : m_readPos += size;
69 0 : }
70 :
71 0 : inline void BinaryBuffer::write(const char* buf, size_t size)
72 : {
73 : // make sure that our data buffer is big enough
74 0 : if(m_writePos + size > m_data.size()){
75 : // if the size of the data to be written exceeds the current
76 : // data-buffers size, then we increase the data buffer by
77 : // the size of the now written data.
78 : // If not, then we'll increase the data-buffer by simply doubling
79 : // its memory, to minimize reallocation costs (this is just a
80 : // heuristic)
81 0 : if(size > m_data.size())
82 0 : m_data.resize(m_data.size() + size);
83 : else
84 0 : m_data.resize(m_data.size() * 2);
85 : }
86 :
87 : // copy the data
88 0 : memcpy(&m_data.front() + m_writePos, buf, size);
89 :
90 : // adjust write pos
91 0 : m_writePos += size;
92 0 : }
93 :
94 : inline char* BinaryBuffer::buffer()
95 : {
96 : return GetDataPtr(m_data);
97 : }
98 :
99 : inline bool BinaryBuffer::eof()
100 : {
101 0 : return m_readPos >= m_writePos;
102 : }
103 :
104 : }// end of namespace
105 :
106 : #endif
|