Commit | Line | Data |
---|---|---|
10988083 MW |
1 | // -------------------------------------------------------------------------- |
2 | // | |
3 | // Copyright | |
4 | // Markus Wittmann, 2016-2017 | |
5 | // RRZE, University of Erlangen-Nuremberg, Germany | |
6 | // markus.wittmann -at- fau.de or hpc -at- rrze.fau.de | |
7 | // | |
8 | // Viktor Haag, 2016 | |
9 | // LSS, University of Erlangen-Nuremberg, Germany | |
10 | // | |
11 | // This file is part of the Lattice Boltzmann Benchmark Kernels (LbmBenchKernels). | |
12 | // | |
13 | // LbmBenchKernels is free software: you can redistribute it and/or modify | |
14 | // it under the terms of the GNU General Public License as published by | |
15 | // the Free Software Foundation, either version 3 of the License, or | |
16 | // (at your option) any later version. | |
17 | // | |
18 | // LbmBenchKernels is distributed in the hope that it will be useful, | |
19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | // GNU General Public License for more details. | |
22 | // | |
23 | // You should have received a copy of the GNU General Public License | |
24 | // along with LbmBenchKernels. If not, see <http://www.gnu.org/licenses/>. | |
25 | // | |
26 | // -------------------------------------------------------------------------- | |
e3f82424 MW |
27 | // TODO: make configurable |
28 | #define HAVE_HUGE_PAGES | |
29 | ||
30 | ||
31 | #ifdef HAVE_HUGE_PAGES | |
32 | #define _BSD_SOURCE | |
33 | #endif | |
34 | ||
10988083 MW |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
37 | #include <string.h> // strerror | |
38 | #include <errno.h> | |
39 | ||
e3f82424 MW |
40 | |
41 | #ifdef HAVE_HUGE_PAGES | |
42 | #include <sys/mman.h> // madvise | |
43 | #endif | |
44 | ||
10988083 MW |
45 | #include "Base.h" |
46 | #include "Memory.h" | |
47 | ||
48 | ||
49 | int MemAlloc(void ** ptr, size_t bytesToAlloc) | |
50 | { | |
51 | void * tmpPtr; | |
52 | ||
53 | tmpPtr = malloc(bytesToAlloc); | |
54 | ||
55 | if (tmpPtr == NULL) { // && bytesToAlloc != 0) { | |
56 | Error("allocation of %lu bytes failed: %d - %s\n", bytesToAlloc, errno, strerror(errno)); | |
57 | exit(1); | |
58 | } | |
59 | ||
60 | *ptr = tmpPtr; | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
65 | int MemAllocAligned(void ** ptr, size_t bytesToAlloc, size_t alignmentBytes) | |
66 | { | |
67 | int ret; | |
68 | ||
69 | ret = posix_memalign(ptr, alignmentBytes, bytesToAlloc); | |
70 | ||
71 | if (ret) { | |
72 | Error("allocation of %lu bytes aligned to %lu bytes failed: %d - %s\n", bytesToAlloc, alignmentBytes, errno, strerror(errno)); | |
73 | exit(1); | |
74 | } | |
75 | ||
e3f82424 MW |
76 | #ifdef HAVE_HUGE_PAGES |
77 | ||
78 | if (alignmentBytes % 4096 == 0) { | |
79 | ret = madvise(*ptr, bytesToAlloc, MADV_HUGEPAGE); | |
80 | ||
81 | if (ret != 0) { | |
82 | DEBUG_BREAK_POINT(); | |
83 | Error("madvise(%p, %lu, MADV_HUGEPAGE) failed: %d - %s.\n", *ptr, bytesToAlloc, errno, strerror(errno)); | |
84 | exit(1); | |
85 | } | |
86 | } | |
87 | #endif | |
88 | ||
10988083 MW |
89 | return 0; |
90 | } | |
91 | ||
92 | ||
93 | int MemFree(void ** ptr) | |
94 | { | |
95 | Assert(*ptr != NULL); | |
96 | ||
97 | free(*ptr); | |
98 | ||
99 | *ptr = NULL; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | int MemZero(void * ptr, size_t bytesToZero) | |
105 | { | |
106 | Assert(ptr != NULL); | |
107 | Assert(bytesToZero > 0); | |
108 | ||
109 | memset(ptr, 0, bytesToZero); | |
110 | ||
111 | return 0; | |
112 | } |