| 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 | // -------------------------------------------------------------------------- |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> // strerror |
| 30 | #include <errno.h> |
| 31 | |
| 32 | #include "Base.h" |
| 33 | #include "Memory.h" |
| 34 | |
| 35 | |
| 36 | int MemAlloc(void ** ptr, size_t bytesToAlloc) |
| 37 | { |
| 38 | void * tmpPtr; |
| 39 | |
| 40 | tmpPtr = malloc(bytesToAlloc); |
| 41 | |
| 42 | if (tmpPtr == NULL) { // && bytesToAlloc != 0) { |
| 43 | Error("allocation of %lu bytes failed: %d - %s\n", bytesToAlloc, errno, strerror(errno)); |
| 44 | exit(1); |
| 45 | } |
| 46 | |
| 47 | *ptr = tmpPtr; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int MemAllocAligned(void ** ptr, size_t bytesToAlloc, size_t alignmentBytes) |
| 53 | { |
| 54 | int ret; |
| 55 | |
| 56 | ret = posix_memalign(ptr, alignmentBytes, bytesToAlloc); |
| 57 | |
| 58 | if (ret) { |
| 59 | Error("allocation of %lu bytes aligned to %lu bytes failed: %d - %s\n", bytesToAlloc, alignmentBytes, errno, strerror(errno)); |
| 60 | exit(1); |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | int MemFree(void ** ptr) |
| 68 | { |
| 69 | Assert(*ptr != NULL); |
| 70 | |
| 71 | free(*ptr); |
| 72 | |
| 73 | *ptr = NULL; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int MemZero(void * ptr, size_t bytesToZero) |
| 79 | { |
| 80 | Assert(ptr != NULL); |
| 81 | Assert(bytesToZero > 0); |
| 82 | |
| 83 | memset(ptr, 0, bytesToZero); |
| 84 | |
| 85 | return 0; |
| 86 | } |