|
| 1 | +#define __FILE_TAG_DEBUG_UNTRACKED_JUL_11_2011__ "C:\\SD\\Reach\\Publishing\\Main\\shared\\engine\\source\\core\\corelib\\cseries\\cseries_memory.cpp" |
1 | 2 | /* ---------- headers */ |
2 | 3 |
|
3 | 4 | #include "core\corelib\cseries\cseries_memory.h" |
| 5 | +#include "core\corelib\cseries\cseries_asserts.h" |
| 6 | + |
| 7 | +#include "decomp.h" |
| 8 | +#include <xtl.h> |
4 | 9 |
|
5 | 10 | /* ---------- constants */ |
6 | 11 |
|
| 12 | +const size_t k_maximum_memcmp_size = 0x20000000; |
| 13 | +const size_t k_maximum_memcpy_size = 0x20000000; |
| 14 | +const size_t k_maximum_memmove_size = 0x20000000; |
| 15 | +const size_t k_maximum_memset_size = 0x20000000; |
| 16 | + |
7 | 17 | /* ---------- definitions */ |
8 | 18 |
|
| 19 | +struct s_memcmp_context |
| 20 | +{ |
| 21 | + unsigned char const* position; |
| 22 | + size_t remaining; |
| 23 | +}; |
| 24 | + |
9 | 25 | /* ---------- prototypes */ |
10 | 26 |
|
11 | 27 | /* ---------- globals */ |
12 | 28 |
|
13 | 29 | /* ---------- public code */ |
14 | 30 |
|
| 31 | +bool memory_is_filled_with(void const * source, size_t size, unsigned char c) |
| 32 | +{ |
| 33 | + mangled_ppc("?memory_is_filled_with@@YA_NPBXIE@Z"); |
| 34 | + |
| 35 | + unsigned char const* VAR_PREFIX(1, position) = reinterpret_cast<unsigned char const*>(source); |
| 36 | + size_t VAR_PREFIX(0, remaining_size) = size; |
| 37 | + |
| 38 | + while(VAR_PREFIX(0, remaining_size) && *VAR_PREFIX(1, position) == c) |
| 39 | + { |
| 40 | + VAR_PREFIX(1, position)++; |
| 41 | + VAR_PREFIX(0, remaining_size)--; |
| 42 | + } |
| 43 | + return VAR_PREFIX(0, remaining_size) == 0; |
| 44 | +}; |
| 45 | + |
| 46 | +bool memory_is_zeroed(const void* source, size_t size) |
| 47 | +{ |
| 48 | + mangled_ppc("?memory_is_zeroed@@YA_NPBXI@Z"); |
| 49 | + |
| 50 | + unsigned char const* VAR_PREFIX(1, position) = reinterpret_cast<unsigned char const*>(source); |
| 51 | + size_t VAR_PREFIX(0, remaining_size) = size; |
| 52 | + |
| 53 | + bool result = true; |
| 54 | + while(result && VAR_PREFIX(0, remaining_size) >= sizeof(unsigned long long)) |
| 55 | + { |
| 56 | + if(*reinterpret_cast<unsigned long long const*>(VAR_PREFIX(1, position))) |
| 57 | + { |
| 58 | + result = false; |
| 59 | + break; |
| 60 | + } |
| 61 | + VAR_PREFIX(1, position) += sizeof(unsigned long long); |
| 62 | + VAR_PREFIX(0, remaining_size) -= sizeof(unsigned long long); |
| 63 | + } |
| 64 | + |
| 65 | + if(result && VAR_PREFIX(0, remaining_size) >= sizeof(unsigned long)) |
| 66 | + { |
| 67 | + if(*reinterpret_cast<unsigned long const*>(VAR_PREFIX(1, position))) |
| 68 | + { |
| 69 | + result = false; |
| 70 | + } |
| 71 | + VAR_PREFIX(1, position) += sizeof(unsigned long); |
| 72 | + VAR_PREFIX(0, remaining_size) -= sizeof(unsigned long); |
| 73 | + } |
| 74 | + |
| 75 | + while(result && VAR_PREFIX(0, remaining_size) > 0) |
| 76 | + { |
| 77 | + if(*VAR_PREFIX(1, position)) |
| 78 | + { |
| 79 | + result = false; |
| 80 | + break; |
| 81 | + } |
| 82 | + VAR_PREFIX(1, position)++; |
| 83 | + VAR_PREFIX(0, remaining_size)--; |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | +}; |
| 88 | + |
| 89 | +void* csmemcpy(void* destination, const void* source, size_t size) |
| 90 | +{ |
| 91 | + mangled_ppc("csmemcpy"); |
| 92 | + |
| 93 | + assert_tag_debug_untracked_jul_11_2011(110, size==0 || (destination && source)); |
| 94 | + assert_tag_debug_untracked_jul_11_2011(111, size>=0 && size<k_maximum_memcpy_size); |
| 95 | + assert_tag_debug_untracked_jul_11_2011(112, offset_pointer(source, size)<=destination || offset_pointer(destination, size)<=source); |
| 96 | + |
| 97 | + return memcpy(destination, source, size); |
| 98 | +}; |
| 99 | + |
| 100 | +void* csmemmove(void* destination, const void* source, size_t size) |
| 101 | +{ |
| 102 | + mangled_ppc("csmemmove"); |
| 103 | + |
| 104 | + assert_tag_debug_untracked_jul_11_2011(122, size==0 || (destination && source)); |
| 105 | + assert_tag_debug_untracked_jul_11_2011(123, size>=0 && size<=k_maximum_memmove_size); |
| 106 | + |
| 107 | + return memmove(destination, source, size); |
| 108 | +}; |
| 109 | + |
| 110 | +void memmove_guarded(void* destination, const void* source, size_t size, const void* bounds_start, size_t bounds_size) |
| 111 | +{ |
| 112 | + mangled_ppc("?memmove_guarded@@YAXPAXPBXI1I@Z"); |
| 113 | + |
| 114 | + assert_tag_debug_untracked_jul_11_2011(135, size>=0 && size<=k_maximum_memmove_size); |
| 115 | + assert_tag_debug_untracked_jul_11_2011(136, bounds_size>=0); // $note this always evaluates to true because an unsigned value is always >= 0 |
| 116 | + if (size > 0) |
| 117 | + { |
| 118 | + // $note Yes this is toxic. It was still a 32bit cast even in the 64bit build. |
| 119 | + |
| 120 | + unsigned long write_start = (unsigned long)(static_cast<char const*>(destination)); |
| 121 | + unsigned long write_end = (unsigned long)(write_start + size - 1); |
| 122 | + unsigned long bounds_lower = (unsigned long)(static_cast<char const*>(bounds_start)); |
| 123 | + unsigned long bounds_upper = (unsigned long)(bounds_lower + bounds_size - 1); |
| 124 | + |
| 125 | + assert_tag_debug_untracked_jul_11_2011(146, bounds_upper>=bounds_lower); |
| 126 | + assert_tag_debug_untracked_jul_11_2011(147, bounds_size>0); |
| 127 | + assert_tag_debug_untracked_jul_11_2011(148, write_start>=bounds_lower && write_start<=bounds_upper); |
| 128 | + assert_tag_debug_untracked_jul_11_2011(149, write_end>=bounds_lower && write_end<=bounds_upper); |
| 129 | + |
| 130 | + memmove(destination, source, size); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +void* csmemset(void* buffer, int c, size_t size) |
| 135 | +{ |
| 136 | + mangled_ppc("csmemset"); |
| 137 | + |
| 138 | + assert_tag_debug_untracked_jul_11_2011(160, size==0 || buffer); |
| 139 | + assert_tag_debug_untracked_jul_11_2011(161, size>=0 && size<=k_maximum_memset_size); |
| 140 | + |
| 141 | + return memset(buffer, c, size); |
| 142 | +}; |
| 143 | + |
| 144 | +int csmemcmp(const void* p1, const void* p2, size_t size) |
| 145 | +{ |
| 146 | + mangled_ppc("csmemcmp"); |
| 147 | + |
| 148 | + assert_tag_debug_untracked_jul_11_2011(171, size==0 || (p1 && p2)); |
| 149 | + assert_tag_debug_untracked_jul_11_2011(172, size>=0 && size<=k_maximum_memcmp_size); |
| 150 | + |
| 151 | + return memcmp(p1, p2, size); |
| 152 | +}; |
| 153 | + |
| 154 | +void * memcpy_big_crossplatform_internal(void * destination, void const * source, size_t size) |
| 155 | +{ |
| 156 | + mangled_ppc("?memcpy_big_crossplatform_internal@@YAPAXPAXPBXI@Z"); |
| 157 | + |
| 158 | + return XMemCpy(destination, source, size); |
| 159 | +}; |
| 160 | + |
| 161 | +void * memset_big_crossplatform_internal(void * destination, int c, size_t size) |
| 162 | +{ |
| 163 | + mangled_ppc("?memset_big_crossplatform_internal@@YAPAXPAXHI@Z"); |
| 164 | + |
| 165 | + return XMemSet(destination, c, size); |
| 166 | +}; |
| 167 | + |
15 | 168 | /* ---------- private code */ |
16 | 169 |
|
17 | 170 | /* ---------- reverse engineering */ |
18 | | - |
19 | | -// bool memory_is_filled_with(void const *, unsigned int, unsigned char); |
20 | | -// bool memory_is_zeroed(void const *, unsigned int); |
21 | | -// csmemcpy; |
22 | | -// void const * offset_pointer(void const *, long); |
23 | | -// csmemmove; |
24 | | -// void memmove_guarded(void *, void const *, unsigned int, void const *, unsigned int); |
25 | | -// csmemset; |
26 | | -// csmemcmp; |
27 | | -// void * memcpy_big_crossplatform_internal(void *, void const *, unsigned int); |
28 | | -// void * memset_big_crossplatform_internal(void *, int, unsigned int); |
29 | | - |
30 | | -//bool memory_is_filled_with(void const *, unsigned int, unsigned char) |
31 | | -//{ |
32 | | -// mangled_ppc("?memory_is_filled_with@@YA_NPBXIE@Z"); |
33 | | -//}; |
34 | | - |
35 | | -//bool memory_is_zeroed(void const *, unsigned int) |
36 | | -//{ |
37 | | -// mangled_ppc("?memory_is_zeroed@@YA_NPBXI@Z"); |
38 | | -//}; |
39 | | - |
40 | | -//csmemcpy |
41 | | -//{ |
42 | | -// mangled_ppc("csmemcpy"); |
43 | | -//}; |
44 | | - |
45 | | -//void const * offset_pointer(void const *, long) |
46 | | -//{ |
47 | | -// mangled_ppc("?offset_pointer@@YAPBXPBXJ@Z"); |
48 | | -//}; |
49 | | - |
50 | | -//csmemmove |
51 | | -//{ |
52 | | -// mangled_ppc("csmemmove"); |
53 | | -//}; |
54 | | - |
55 | | -//void memmove_guarded(void *, void const *, unsigned int, void const *, unsigned int) |
56 | | -//{ |
57 | | -// mangled_ppc("?memmove_guarded@@YAXPAXPBXI1I@Z"); |
58 | | -//}; |
59 | | - |
60 | | -//csmemset |
61 | | -//{ |
62 | | -// mangled_ppc("csmemset"); |
63 | | -//}; |
64 | | - |
65 | | -//csmemcmp |
66 | | -//{ |
67 | | -// mangled_ppc("csmemcmp"); |
68 | | -//}; |
69 | | - |
70 | | -//void * memcpy_big_crossplatform_internal(void *, void const *, unsigned int) |
71 | | -//{ |
72 | | -// mangled_ppc("?memcpy_big_crossplatform_internal@@YAPAXPAXPBXI@Z"); |
73 | | -//}; |
74 | | - |
75 | | -//void * memset_big_crossplatform_internal(void *, int, unsigned int) |
76 | | -//{ |
77 | | -// mangled_ppc("?memset_big_crossplatform_internal@@YAPAXPAXHI@Z"); |
78 | | -//}; |
79 | | - |
|
0 commit comments