-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.h
51 lines (40 loc) · 1.27 KB
/
Utils.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
#pragma once
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
/// <summary>
/// Pointer arithmetic on byte level on other object types. This shall be used if the offset is in bytes, but T is some other pointer type.
/// </summary>
template <typename T>
T* AddBytes(T* inPtr, int offset)
{
return (T*)(((byte*)inPtr) + offset);
}
template <typename T>
int ByteDifference(T* higher, T* lower)
{
return (byte*)higher - (byte*)lower;
}
/// <summary>
/// Relocates a pointer (to compute the same address relative to a new base)
/// </summary>
/// <param name="inBasePtr">Input base pointer</param>
/// <param name="inPtr">Input pointer. Must be > inBasePtr</param>
/// <param name="outBasePtr">New base pointer</param>
template <typename T>
T* Relocate(T* inBasePtr, T* inPtr, T* outBasePtr)
{
int offset = ByteDifference(inPtr, inBasePtr);
return AddBytes(outBasePtr, offset);
}
#ifndef _MSC_VER
// The ESP32 GCC variant doesn't support these, and changing the standard causes unclear side effects
typedef int32_t errno_t;
const size_t _TRUNCATE = -1;
errno_t strncpy_s(
char* strDest,
size_t numberOfElements,
const char* strSource,
size_t count
);
errno_t memcpy_s(void* dest, size_t destinationLength, void* source, size_t sourceLength);
#endif