1 /*
2  *  Copyright (C) 2004-2021 Edward F. Valeev
3  *
4  *  This file is part of Libint.
5  *
6  *  Libint is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  Libint is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License
17  *  along with Libint.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef _libint2_src_lib_libint_libint2memory_h_
22 #define _libint2_src_lib_libint_libint2memory_h_
23 
24 #include <cstdlib>
25 #include <libint2/util/generated/libint2_params.h>
26 
27 namespace libint2 {
28 
29   /// Aligned version of malloc().
30 
31   /** Allocates a memory block aligned to LIBINT2_ALIGN_SIZE*sizeof(LIBINT2_REALTYPE).
32       If LIBINT2_ALIGN_SIZE, no alignment is assumed. Use free() to deallocate. */
malloc(size_t nbytes)33   inline void* malloc(size_t nbytes) {
34     void* result;
35 #if (LIBINT2_ALIGN_SIZE == 0)
36     result = ::malloc(nbytes);
37 #elif defined(HAVE_POSIX_MEMALIGN)
38     posix_memalign(&result, LIBINT2_ALIGN_SIZE*sizeof(LIBINT2_REALTYPE), nbytes);
39 #else
40 #   error "LIBINT2_ALIGN_SIZE!=0 but posix_memalign is not available"
41 #endif
42     return result;
43   }
44 
45   /// type-specific version of libint2::malloc()
46   template <typename T>
malloc(size_t n)47   inline T* malloc(size_t n) {
48     return reinterpret_cast<T*>(malloc(n * sizeof(T)));
49   }
50 
51 }
52 
53 #endif /* _libint2_src_lib_libint_libint2memory_h_ */
54