1 /*
2 * HLLib
3 * Copyright (C) 2006-2010 Ryan Gregg
4
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later
9 * version.
10 */
11
12 #include "HLLib.h"
13 #include "MemoryMapping.h"
14
15 using namespace HLLib;
16 using namespace HLLib::Mapping;
17
CMemoryMapping(hlVoid * lpData,hlULongLong uiBufferSize)18 CMemoryMapping::CMemoryMapping(hlVoid *lpData, hlULongLong uiBufferSize) : bOpened(hlFalse), uiMode(HL_MODE_INVALID), lpData(lpData), uiBufferSize(uiBufferSize)
19 {
20
21 }
22
~CMemoryMapping()23 CMemoryMapping::~CMemoryMapping()
24 {
25 this->Close();
26 }
27
GetType() const28 HLMappingType CMemoryMapping::GetType() const
29 {
30 return HL_MAPPING_MEMORY;
31 }
32
33
GetBuffer() const34 const hlVoid *CMemoryMapping::GetBuffer() const
35 {
36 return this->lpData;
37 }
38
GetBufferSize() const39 hlULongLong CMemoryMapping::GetBufferSize() const
40 {
41 return this->uiBufferSize;
42 }
43
GetOpened() const44 hlBool CMemoryMapping::GetOpened() const
45 {
46 return this->bOpened;
47 }
48
GetMode() const49 hlUInt CMemoryMapping::GetMode() const
50 {
51 return this->uiMode;
52 }
53
OpenInternal(hlUInt uiMode)54 hlBool CMemoryMapping::OpenInternal(hlUInt uiMode)
55 {
56 assert(!this->GetOpened());
57
58 if(this->uiBufferSize != 0 && this->lpData == 0)
59 {
60 LastError.SetErrorMessage("Memory stream is null.");
61 return hlFalse;
62 }
63
64 if((uiMode & HL_MODE_READ) == 0 || (uiMode & HL_MODE_WRITE) != 0)
65 {
66 LastError.SetErrorMessageFormated("Invalid open mode (%#.8x).", uiMode);
67 return hlFalse;
68 }
69
70 this->bOpened = hlTrue;
71 this->uiMode = uiMode;
72
73 return hlTrue;
74 }
75
CloseInternal()76 hlVoid CMemoryMapping::CloseInternal()
77 {
78 this->bOpened = hlFalse;
79 this->uiMode = HL_MODE_INVALID;
80 }
81
GetMappingSize() const82 hlULongLong CMemoryMapping::GetMappingSize() const
83 {
84 return this->bOpened ? this->uiBufferSize : 0;
85 }
86
MapInternal(CView * & pView,hlULongLong uiOffset,hlULongLong uiLength)87 hlBool CMemoryMapping::MapInternal(CView *&pView, hlULongLong uiOffset, hlULongLong uiLength)
88 {
89 assert(this->GetOpened());
90
91 if(uiOffset + uiLength > this->uiBufferSize)
92 {
93 #ifdef _WIN32
94 LastError.SetErrorMessageFormated("Requested view (%I64u, %I64u) does not fit inside mapping, (%I64u, %I64u).", uiOffset, uiLength, 0, this->uiBufferSize);
95 #else
96 LastError.SetErrorMessageFormated("Requested view (%llu, %llu) does not fit inside mapping, (%llu, %llu).", uiOffset, uiLength, 0, this->uiBufferSize);
97 #endif
98 return hlFalse;
99 }
100
101 pView = new CView(this, this->lpData, 0, this->uiBufferSize, uiOffset, uiLength);
102
103 return hlTrue;
104 }
105