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 lenght 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 m_buffer
= (byte
*)realloc(m_buffer
, newsize
);
119 m_BufferSize
= newsize
;
121 MULE_VALIDATE_STATE(m_buffer
, wxT("CMemFile: Failed to (re)allocate buffer"));
125 sint64
CMemFile::doRead(void* buffer
, size_t count
) const
127 MULE_VALIDATE_PARAMS(buffer
, wxT("CMemFile: Attempting to read to invalid buffer"));
129 // Handle reads past EOF
130 if (m_position
> m_fileSize
) {
132 } else if (m_position
+ count
> m_fileSize
) {
133 count
= m_fileSize
- m_position
;
137 memcpy(buffer
, m_buffer
+ m_position
, count
);
145 sint64
CMemFile::doWrite(const void* buffer
, size_t count
)
147 MULE_VALIDATE_PARAMS(buffer
, wxT("CMemFile: Attempting to write to invalid buffer"));
148 MULE_VALIDATE_STATE(!m_readonly
, wxT("CMemFile: Attempted to write to a read-only buffer."));
151 if (m_position
+ count
> m_BufferSize
) {
152 enlargeBuffer(m_position
+ count
);
155 MULE_VALIDATE_STATE(m_position
+ count
<= m_BufferSize
, wxT("CMemFile: Buffer not resized to needed size."));
157 memcpy(m_buffer
+ m_position
, buffer
, count
);
160 if (m_position
> m_fileSize
) {
161 m_fileSize
= m_position
;
168 sint64
CMemFile::doSeek(sint64 offset
) const
170 MULE_VALIDATE_PARAMS(offset
>= 0, wxT("CMemFile: Invalid seek, position, must be positive."));
172 return m_position
= offset
;
176 void CMemFile::ResetData()
178 wxCHECK_RET(!m_readonly
, wxT("Trying to reset read-only buffer"));
180 memset(m_buffer
, 0, m_BufferSize
);
185 // File_checked_for_headers