1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 // common.h : include file for standard system include files,
6 // or project specific include files that are used frequently, but
7 // are changed infrequently
8 //
9
10 #pragma once
11
12 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
13 // Windows Header Files:
14 // Licensed to the .NET Foundation under one or more agreements.
15 // The .NET Foundation licenses this file to you under the MIT license.
16 // See the LICENSE file in the project root for more information.
17
18 #include <windows.h>
19
20 #ifndef _CRT_SECURE_NO_WARNINGS
21 #define _CRT_SECURE_NO_WARNINGS
22 #endif // _CRT_SECURE_NO_WARNINGS
23
24 #include <stdint.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <wchar.h>
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <memory.h>
32
33 #include <new>
34
35 #ifdef PLATFORM_UNIX
36 #include <pthread.h>
37 #endif
38
39 using namespace std;
40
41 #include "..\Runtime\inc\CommonTypes.h"
42 #include "..\Runtime\inc\daccess.h"
43 #include "..\Runtime\inc\varint.h"
44 #include "..\Runtime\PalRedhawkCommon.h" // Fp128
45 #include "..\Runtime\regdisplay.h"
46 #include "..\Runtime\ICodemanager.h"
47
48 //
49 // This macro returns val rounded up as necessary to be a multiple of alignment; alignment must be a power of 2
50 //
ALIGN_UP(size_t val,size_t alignment)51 inline size_t ALIGN_UP(size_t val, size_t alignment)
52 {
53 // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
54 assert(0 == (alignment & (alignment - 1)));
55 size_t result = (val + (alignment - 1)) & ~(alignment - 1);
56 assert(result >= val); // check for overflow
57 return result;
58 }
ALIGN_UP(void * val,size_t alignment)59 inline void* ALIGN_UP(void* val, size_t alignment)
60 {
61 return (void*)ALIGN_UP((size_t)val, alignment);
62 }
63
ALIGN_DOWN(size_t val,size_t alignment)64 inline size_t ALIGN_DOWN(size_t val, size_t alignment)
65 {
66 // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
67 assert(0 == (alignment & (alignment - 1)));
68 size_t result = val & ~(alignment - 1);
69 return result;
70 }
ALIGN_DOWN(void * val,size_t alignment)71 inline void* ALIGN_DOWN(void* val, size_t alignment)
72 {
73 return (void*)ALIGN_DOWN((size_t)val, alignment);
74 }
75
IS_ALIGNED(size_t val,size_t alignment)76 inline BOOL IS_ALIGNED(size_t val, size_t alignment)
77 {
78 // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
79 assert(0 == (alignment & (alignment - 1)));
80 return 0 == (val & (alignment - 1));
81 }
IS_ALIGNED(const void * val,size_t alignment)82 inline BOOL IS_ALIGNED(const void* val, size_t alignment)
83 {
84 return IS_ALIGNED((size_t)val, alignment);
85 }
86
87 // Rounds a ULONG up to the nearest power of two number.
RoundUpToPower2(ULONG x)88 inline ULONG RoundUpToPower2(ULONG x)
89 {
90 if (x == 0) return 1;
91
92 x = x - 1;
93 x = x | (x >> 1);
94 x = x | (x >> 2);
95 x = x | (x >> 4);
96 x = x | (x >> 8);
97 x = x | (x >> 16);
98 return x + 1;
99 }
100
ALIGN_UP(DWORD val,int alignment)101 inline DWORD ALIGN_UP(DWORD val, int alignment)
102 {
103 // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
104 assert(0 == (alignment & (alignment - 1)));
105 DWORD result = (val + (alignment - 1)) & ~(alignment - 1);
106 assert(result >= val); // check for overflow
107 return result;
108 }
109
110
111 #ifdef _DEBUG
112 extern void TraceImpl(const char *fmt, ...);
113 extern void TraceImpl(const wchar_t *fmt, ...);
114 #define TRACE(fmt, ...) TraceImpl((fmt), __VA_ARGS__)
115 #else
116 #define TRACE(fmt, ...)
117 #endif
118