forked from rethinkdb/rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_utils.cc
More file actions
68 lines (60 loc) · 1.49 KB
/
Copy pathmemory_utils.cc
File metadata and controls
68 lines (60 loc) · 1.49 KB
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
#include "memory_utils.hpp"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef _WIN32
#include "windows.hpp"
#include <io.h> // NOLINT
#include <direct.h> // NOLINT
#ifndef __MINGW32__
#include <filesystem>
#endif
#endif // _WIN32
#include "errors.hpp"
void *raw_malloc_aligned(size_t size, size_t alignment) {
void *ptr = nullptr;
#ifdef _WIN32
ptr = _aligned_malloc(size, alignment);
if (UNLIKELY(ptr == nullptr)) {
crash_oom();
}
#else
int res = posix_memalign(&ptr, alignment, size); // NOLINT(runtime/rethinkdb_fn)
if (res != 0) {
if (res == EINVAL) {
crash_or_trap("posix_memalign with bad alignment: %zu.", alignment);
} else if (res == ENOMEM) {
crash_oom();
} else {
crash_or_trap("posix_memalign failed with unknown result: %d.", res);
}
}
#endif
return ptr;
}
#ifndef _WIN32
void *raw_malloc_page_aligned(size_t size) {
return raw_malloc_aligned(size, getpagesize());
}
#endif
void raw_free_aligned(void *ptr) {
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}
void *rmalloc(size_t size) {
void *res = malloc(size); // NOLINT(runtime/rethinkdb_fn)
if (UNLIKELY(res == nullptr && size != 0)) {
crash_oom();
}
return res;
}
void *rrealloc(void *ptr, size_t size) {
void *res = realloc(ptr, size); // NOLINT(runtime/rethinkdb_fn)
if (UNLIKELY(res == nullptr && size != 0)) {
crash_oom();
}
return res;
}