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
|
// MemDebug.cpp
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "Core/PrecompiledHeader.h"
#include "MemDebug.h"
#if defined( MEM_DEBUG_ENABLED )
// Core
#include "Core/Env/Assert.h"
// FillMem
//------------------------------------------------------------------------------
void MemDebug::FillMem( void * ptr, const size_t size, const uint32_t pattern )
{
// 该方法只适用于地址是32位对齐的
// this function assumes at least 32bit alignment
ASSERT( uintptr_t( ptr ) % sizeof( uint32_t ) == 0 );
// fill whole words
// 将能够为sizeof(unit32_t)整除的部分用pattern进行赋值
const size_t numWords = size / sizeof( uint32_t );
uint32_t * it = static_cast< uint32_t * >( ptr );
const uint32_t * end = it + numWords;
while ( it != end )
{
*it = pattern;
++it;
}
// fill remaining bytes
// 填充剩余的部分
const size_t remainder = size - ( numWords * sizeof( uint32_t ) );
if ( remainder )
{
// assuming little-endian format
// 此处假设内存是以小端结尾的格式
char bytes[ 3 ] = { (char)( ( pattern & 0x000000FF ) ),
(char)( ( pattern & 0x0000FF00 ) >> 8 ),
(char)( ( pattern & 0x00FF0000 ) >> 16 ) };
const char * b = bytes;
char * cit = static_cast< char * >( static_cast< void * >( it ) );
// 此处没有break,利用了switch case在没有break的情况下,
// 会继续运行下一个case的特性
switch( remainder )
{
case 3: *cit = *b; ++cit; ++b;
case 2: *cit = *b; ++cit; ++b;
case 1: *cit = *b;
}
}
}
//------------------------------------------------------------------------------
#endif // MEM_DEBUG_ENABLED
|