-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryLibrary.h
454 lines (387 loc) · 8.81 KB
/
MemoryLibrary.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* Copyright (c) 1994-2004 Pongsak Suvanpong ([email protected]).
* All Rights Reserved.
*
* This computer software is owned by Pongsak Suvanpong, and is
* protected by U.S. copyright laws and other laws and by international
* treaties. This computer software is furnished by Pongsak Suvanpong
* pursuant to a written license agreement and may be used, copied,
* transmitted, and stored only in accordance with the terms of such
* license and with the inclusion of the above copyright notice. This
* computer software or any other copies thereof may not be provided or
* otherwise made available to any other person.
*/
#ifndef __BUFFER_HEADER__
#define __BUFFER_HEADER__
#include "dll.ix.h"
#ifdef OSX_MACH_O // bsd does not have malloc.h, everything in malloc.h is in stdlib.h
#define ON_FREEBSD
#endif
#ifdef __APPLE__ // bsd does not have malloc.h, everything in malloc.h is in stdlib.h
#define ON_FREEBSD
#endif
#ifndef ON_FREEBSD
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <memory.h>
#include "dll.ix.h"
namespace MemoryLibrary
{
template <typename T>
void Copy(T *destination, T *source, int iCnt)
{
::memcpy((void*)destination, (void*)source, iCnt * sizeof(T));
}
template <typename T>
void Zero(T *destination, int iCnt)
{
::memset((void*)destination, 0, iCnt * sizeof(T));
}
template <typename T>
void Set(T *destination, unsigned char v, int iCnt)
{
::memset((void*)destination, v, iCnt * sizeof(T));
}
class Buffer
{
protected:
void* m_pBuffer;
unsigned long m_uiSize;
public:
Buffer(void)
{
m_pBuffer = NULL;
m_uiSize = 0;
}
Buffer(void* p, unsigned long s)
{
Set(p, s);
}
virtual ~Buffer(void)
{
m_pBuffer = NULL;
m_uiSize = 0;
}
bool IsValid(void)
{
return m_pBuffer != NULL ? true : false;
}
bool IsEqualTo(MemoryLibrary::Buffer& p)
{
return this->Size() == p.Size() && ::memcmp(this->Ptr(), p.Ptr(), Size()) == 0;
}
void* Ptr(void)
{
return m_pBuffer;
}
void* PointerToThisBuffer()
{
return Ptr();
}
unsigned char* memoryLocationAsByte()
{
return (unsigned char*)Ptr();
}
unsigned long size(void)
{
return m_uiSize;
}
unsigned long Size(void)
{
return m_uiSize;
}
void Set(void* p, unsigned long s)
{
m_pBuffer = p;
m_uiSize = s;
}
void Zero(void)
{
::memset(m_pBuffer, 0, m_uiSize);
}
void CopyFrom(void* p)
{
::memcpy(m_pBuffer, p, m_uiSize);
}
void CopyFrom(void* p, unsigned int s)
{
::memcpy(m_pBuffer, p, s);
}
void CopyFrom(MemoryLibrary::Buffer& b)
{
CopyFrom(b.Ptr(), b.Size());
}
void CopyTo(void* p, unsigned long s)
{
::memcpy(p, m_pBuffer, s);
}
void CopyTo(MemoryLibrary::Buffer& b)
{
CopyTo(b.Ptr(), b.Size());
}
template <typename T>
void CopyFrom(T* p)
{
if(sizeof(T) == Size())
CopyFrom((void*)p, sizeof(T));
else
{
// need to pump error msg here
}
}
template <typename T>
void CopyTo(T* p)
{
if(Size() == sizeof(T))
CopyTo((void*)p, sizeof(T));
else
{
// need to pump error
}
}
/*
template <typename T>
void CopyFrom(T* p, unsigned long nBlocks)
{
if(nBlocks * sizeof(T) == Size())
CopyFrom((void*)p, nBlocks * sizeof(T));
else
{
// need to pump error msg here
}
} */
template <typename T>
void CopyTo(T* p, unsigned long nBlocks)
{
if(Size() == nBlocks * sizeof(T))
CopyTo((void*)p, nBlocks * sizeof(T));
else
{
// need to pump error
}
}
template <typename T>
T makeValueFromOffset(int offset)
{
T result = (T)0;
if((offset + sizeof(T)) < Size())
{
unsigned char *pStart = ((unsigned char*)m_pBuffer) + (unsigned char)offset;
::memcpy(&result, pStart, sizeof(T));
}
return result;
}
template <typename T>
T MakeValueFromOffset(int offset)
{
T result = (T)0;
if((offset + sizeof(T)) < Size())
{
unsigned char *pStart = ((unsigned char*)m_pBuffer) + (unsigned char)offset;
::memcpy(&result, pStart, sizeof(T));
}
return result;
}
unsigned char& byteValueAt(int i)
{
unsigned char* byte = (unsigned char*)m_pBuffer;
return byte[i]; // no range check;
}
unsigned char& operator[](int i)
{
return byteValueAt(i);
}
};
///////////////////////////////////////////////////
template<int BufferSizeInByte>
class StaticBuffer :public Buffer
{
unsigned char myBuffer[BufferSizeInByte];
public:
StaticBuffer() :Buffer((void*)myBuffer, BufferSizeInByte)
{
}
};
//////////////////////////////
template <typename T>
class DataTypedBuffer :public Buffer
{
T myType;
public:
DataTypedBuffer() :Buffer((void*)myType, sizeof(T))
{
}
};
////////////////////////////////////////
class DynamicBuffer :public Buffer
{
public:
DynamicBuffer() :Buffer(NULL, 0)
{
}
virtual ~DynamicBuffer(void)
{
Free();
}
virtual bool Allocate(unsigned long nByte)
{
// new size?
if(nByte != m_uiSize)
{
Free();
m_pBuffer = ::malloc(nByte);
if(NULL == m_pBuffer)
return false;
else
{
m_uiSize = nByte;
return true;
}
}
else
{
// buffer is already been allocated and
// same size with nByte, so just return true;
return true;
}
}
virtual void Free(void)
{
if(NULL != m_pBuffer)
{
::free(m_pBuffer);
m_pBuffer = NULL;
m_uiSize = 0;
}
}
bool ReadFromFile(const char* szFilename)
{
FILE *fp;
long len;
fp = ::fopen(szFilename,"rb");
if(NULL == fp)
return false;
::fseek(fp,0,SEEK_END);
len=ftell(fp);
fseek(fp,0,SEEK_SET);
Allocate(len);
::fread(memoryLocationAsByte(), len, 1, fp);
::fclose(fp);
return true;
}
};
///////////////////////////////////////////////////////////////////
template <typename T>
class IndexableBuffer // rename to IndexableBuffer
{
bool m_bFreeBuffer;
MemoryLibrary::DynamicBuffer m_buffer;
T* m_ptr;
unsigned int m_nBlock;
public:
IndexableBuffer(void)
{
m_bFreeBuffer = true;
m_nBlock = 0;
m_ptr = NULL;
}
IndexableBuffer(int iSize)
{
m_bFreeBuffer = true;
m_nBlock = 0;
m_ptr = NULL;
Allocate(iSize);
}
IndexableBuffer(T* ptrExtBuffer, int iSize)
{
m_bFreeBuffer = true;
m_nBlock = 0;
m_ptr = NULL;
SetExternalBuffer(ptrExtBuffer, iSize);
}
virtual ~IndexableBuffer(void)
{
Free();
}
void SetExternalBuffer(T* ptr, unsigned int size)
{
Free();
m_bFreeBuffer = false;
m_ptr = ptr;
m_buffer.Set((void*)ptr, size);
m_nBlock = size;
}
void Copy(MemoryLibrary::IndexableBuffer<T>& a)
{
Copy(a.RawBuffer(), a.Size());
}
void Copy(T* Ptr, unsigned int iSize)
{
Allocate(iSize);
m_buffer.CopyFrom((void*)Ptr, iSize * sizeof(T));
}
void Fill(T a)
{
for(unsigned int i = 0; i < this->Size(); i++)
At(i) = a;
}
virtual bool Allocate(unsigned int nBlock, bool bZeroOutBuffer=true)
{
m_bFreeBuffer = true;
m_nBlock = nBlock;
if(true == m_buffer.Allocate(sizeof(T) * nBlock))
{
if(true == bZeroOutBuffer)
m_buffer.Zero();
m_ptr = (T*)m_buffer.Ptr();
return true;
}
else
{
m_ptr = NULL;
return false;
}
}
virtual void Free(void)
{
if(true == m_bFreeBuffer)
{
m_nBlock = 0;
m_buffer.Free();
m_ptr = NULL;
}
else // this mean I am operating on external buffer, see SetExternalBuffer
{
m_buffer.Set(NULL, 0); // so DynamicBuffer won't delete the external ptr
m_nBlock = 0;
}
}
unsigned int Size(void)
{
return m_nBlock;
}
unsigned int RawSize(void)
{
return m_buffer.Size();
}
T* RawBuffer(void)
{
return m_ptr;
}
T* Memory(void)
{
return m_ptr;
}
T& At(unsigned int iIdx)
{
//Debug::CheckUpperBoundRange(Size() - 1, iIdx, "MemoryLibrary::IndexableBuffer::At");
return m_ptr[iIdx];
}
T &operator[](int i)
{
return At(i);
}
};
} // namespace MemoryLibrary
#endif