-
Notifications
You must be signed in to change notification settings - Fork 30
/
tool.cpp
126 lines (94 loc) · 2.22 KB
/
tool.cpp
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
/*************************************************************************
> File Name: tool.cpp
> Author: gaopeng QQ:22389860 all right reserved
> Mail: [email protected]
> Created Time: Thu 28 Sep 2017 02:01:47 PM CST
************************************************************************/
#include<iostream>
#include <stdlib.h>
#include"myfine.h"
using namespace std;
void* aligned_malloc(size_t size, size_t alignment)
{
if(alignment & (alignment - 1))
{
return NULL;
}
else
{
void *praw = malloc(sizeof(void*) + size + alignment);
if(praw)
{
void *pbuf = reinterpret_cast<void*>(reinterpret_cast<size_t>(praw) + sizeof(void*));
void *palignedbuf = reinterpret_cast<void*>((reinterpret_cast<size_t>(pbuf) | (alignment - 1)) + 1);
static_cast<void**>(palignedbuf)[-1] = praw;
return palignedbuf;
}
else
{
return NULL;
}
}
}
void aligned_free(void *palignedmem)
{
free(reinterpret_cast<void*>((static_cast<void**>(palignedmem))[-1]));
}
ulint
mach_read_from_1(
/*=============*/
const byte* b) /*!< in: pointer to byte */
{
return((ulint)(b[0]));
}
ulint
mach_read_from_2(
/*=============*/
const byte* b) /*!< in: pointer to 2 bytes */
{
return(((ulint)(b[0]) << 8) | (ulint)(b[1]));
}
ulint
mach_read_from_3(
/*=============*/
const byte* b) /*!< in: pointer to 3 bytes */
{
return( ((ulint)(b[0]) << 16)
| ((ulint)(b[1]) << 8)
| (ulint)(b[2])
);
}
ulint
mach_read_from_4(
/*=============*/
const byte* b) /*!< in: pointer to four bytes */
{
return( ((ulint)(b[0]) << 24)
| ((ulint)(b[1]) << 16)
| ((ulint)(b[2]) << 8)
| (ulint)(b[3])
);
}
ib_uint64_t
mach_read_from_8(
/*=============*/
const byte* b) /*!< in: pointer to 8 bytes */
{
ib_uint64_t u64;
u64 = mach_read_from_4(b);
u64 <<= 32;
u64 |= mach_read_from_4(b + 4);
return(u64);
}
int16_t
mach_read_from_2_16(const byte* b)
{
return(((int16_t)(b[0]) << 8) | (ulint)(b[1]));
}
void*
ut_align_down(
const void* ptr, /*!< in: pointer */
ulint align_no) /*!< in: align by this number 16384*/
{
return((void*)((((ulint) ptr)) & ~(align_no - 1)));
}