2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "MemFile.h" // Interface declarations
29 CMemFile::CMemFile(unsigned int growthRate
)
34 m_growthRate
= growthRate
;
41 CMemFile::CMemFile(byte
* buffer
, size_t bufferSize
)
43 MULE_VALIDATE_PARAMS(buffer
, wxT("CMemFile: Attempted to attach invalid buffer."));
46 m_BufferSize
= bufferSize
;
47 m_fileSize
= bufferSize
;
54 CMemFile::CMemFile(const byte
* buffer
, size_t bufferSize
)
56 MULE_VALIDATE_PARAMS(buffer
, wxT("CMemFile: Attempted to attach invalid buffer."));
58 m_buffer
= const_cast<byte
*>(buffer
);
59 m_BufferSize
= bufferSize
;
60 m_fileSize
= bufferSize
;
75 uint64
CMemFile::GetPosition() const
81 void CMemFile::SetLength(size_t newLen
)
83 MULE_VALIDATE_STATE(!m_readonly
, wxT("CMemFile: Attempted to change length on a read-only buffer."));
85 if (newLen
> m_BufferSize
) {
86 enlargeBuffer(newLen
);
89 if (newLen
< m_position
) {
97 uint64
CMemFile::GetLength() const
103 void CMemFile::enlargeBuffer(size_t size
)
105 MULE_VALIDATE_PARAMS(size
>= m_BufferSize
, wxT("CMemFile: Attempted to shrink buffer."));
106 MULE_VALIDATE_STATE(m_delete
, wxT("CMemFile: Attempted to grow an attached buffer."));
107 MULE_VALIDATE_STATE(!m_readonly
, wxT("CMemFile: Attempted to grow a read-only buffer."));
109 size_t newsize
= m_BufferSize
;
112 newsize
= ((size
+ m_growthRate
- 1) / m_growthRate
) * m_growthRate
;
114 // No growth-rate specified. Change to exactly the size specified.
118 byte
*tmp
= (byte
*)realloc(m_buffer
, newsize
);
121 m_BufferSize
= newsize
;
124 MULE_VALIDATE_STATE(tmp
, wxT("CMemFile: Failed to (re)allocate buffer"));
128 sint64
CMemFile::doRead(void* buffer
, size_t count
) const
130 MULE_VALIDATE_PARAMS(buffer
, wxT("CMemFile: Attempting to read to invalid buffer"));
132 // Handle reads past EOF
133 if (m_position
> m_fileSize
) {
135 } else if (m_position
+ count
> m_fileSize
) {
136 count
= m_fileSize
- m_position
;
140 memcpy(buffer
, m_buffer
+ m_position
, count
);
148 sint64
CMemFile::doWrite(const void* buffer
, size_t count
)
150 MULE_VALIDATE_PARAMS(buffer
, wxT("CMemFile: Attempting to write to invalid buffer"));
151 MULE_VALIDATE_STATE(!m_readonly
, wxT("CMemFile: Attempted to write to a read-only buffer."));
154 if (m_position
+ count
> m_BufferSize
) {
155 enlargeBuffer(m_position
+ count
);
158 MULE_VALIDATE_STATE(m_position
+ count
<= m_BufferSize
, wxT("CMemFile: Buffer not resized to needed size."));
160 memcpy(m_buffer
+ m_position
, buffer
, count
);
163 if (m_position
> m_fileSize
) {
164 m_fileSize
= m_position
;
171 sint64
CMemFile::doSeek(sint64 offset
) const
173 MULE_VALIDATE_PARAMS(offset
>= 0, wxT("CMemFile: Invalid seek, position, must be positive."));
175 return m_position
= offset
;
179 void CMemFile::ResetData()
181 wxCHECK_RET(!m_readonly
, wxT("Trying to reset read-only buffer"));
183 memset(m_buffer
, 0, m_BufferSize
);
188 // File_checked_for_headers