Block-Structured AMR Software Framework
AlignedAllocator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017, UChicago Argonne, LLC
3  * All Rights Reserved
4  *
5  * Hardware/Hybrid Cosmology Code (HACC), Version 1.0
6  *
7  * Salman Habib, Adrian Pope, Hal Finkel, Nicholas Frontiere, Katrin Heitmann,
8  * Vitali Morozov, Jeffrey Emberson, Thomas Uram, Esteban Rangel
9  * (Argonne National Laboratory)
10  *
11  * David Daniel, Patricia Fasel, Chung-Hsing Hsu, Zarija Lukic, James Ahrens
12  * (Los Alamos National Laboratory)
13  *
14  * George Zagaris
15  * (Kitware)
16  *
17  * OPEN SOURCE LICENSE
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are met:
21  *
22  * 1. Redistributions of source code must retain the above copyright notice,
23  * this list of conditions and the following disclaimer. Software changes,
24  * modifications, or derivative works, should be noted with comments and
25  * the author and organization's name.
26  *
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  *
31  * 3. Neither the names of UChicago Argonne, LLC or the Department of Energy
32  * nor the names of its contributors may be used to endorse or promote
33  * products derived from this software without specific prior written
34  * permission.
35  *
36  * 4. The software and the end-user documentation included with the
37  * redistribution, if any, must include the following acknowledgment:
38  *
39  * "This product includes software produced by UChicago Argonne, LLC under
40  * Contract No. DE-AC02-06CH11357 with the Department of Energy."
41  *
42  * *****************************************************************************
43  * DISCLAIMER
44  * THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND. NEITHER THE
45  * UNITED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR
46  * UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY,
47  * EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE
48  * ACCURARY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, APPARATUS,
49  * PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE
50  * PRIVATELY OWNED RIGHTS.
51  *
52  * *****************************************************************************
53  */
54 
55 #ifndef HACC_ALIGNEDALLOCATOR_H
56 #define HACC_ALIGNEDALLOCATOR_H
57 
58 #include <stddef.h>
59 #include <stdlib.h>
60 
61 #include <new>
62 
63 namespace hacc {
64 template <typename T, size_t N>
66 {
67 public:
68  typedef T value_type;
69  typedef T *pointer;
70  typedef T &reference;
71  typedef const T *const_pointer;
72  typedef const T &const_reference;
73  typedef size_t size_type;
74  typedef ptrdiff_t difference_type;
75 
76  template <typename U>
77  struct rebind {
79  };
80 
81 public:
82  AlignedAllocator() throw() {};
83  AlignedAllocator(const AlignedAllocator&) throw() {};
84 
85  template <typename U, size_t M>
87 
88 public:
89  ~AlignedAllocator() throw () {};
90 
91 public:
92  pointer address(reference x) const { return &x; }
93  const_pointer address (const_reference x) const { return &x; }
94 
95  size_type max_size() const throw() { return size_t(-1) / sizeof(T); }
96 
97  void construct(pointer p, const_reference val) { ::new ((void*)p) T(val); }
98  void destroy(pointer p) { ((T*)p)->~T(); }
99 
100 public:
102  const void * /*hint*/ = 0)
103  {
104  pointer p;
105  if (posix_memalign((void **) &p, N, n*sizeof(T)) != 0) {
106  throw std::bad_alloc();
107  }
108 
109  return p;
110  }
111 
113  {
114  free((void *) p);
115  }
116 };
117 } // namespace hacc
118 
119 #endif // HACC_ALIGNEDALLOCATOR_H
120 
void free(void *)
Definition: AlignedAllocator.h:66
AlignedAllocator(const AlignedAllocator &)
Definition: AlignedAllocator.h:83
pointer address(reference x) const
Definition: AlignedAllocator.h:92
size_type max_size() const
Definition: AlignedAllocator.h:95
~AlignedAllocator()
Definition: AlignedAllocator.h:89
const_pointer address(const_reference x) const
Definition: AlignedAllocator.h:93
T & reference
Definition: AlignedAllocator.h:70
void destroy(pointer p)
Definition: AlignedAllocator.h:98
AlignedAllocator()
Definition: AlignedAllocator.h:82
const T * const_pointer
Definition: AlignedAllocator.h:71
T * pointer
Definition: AlignedAllocator.h:69
const T & const_reference
Definition: AlignedAllocator.h:72
pointer allocate(size_type n, const void *=0)
Definition: AlignedAllocator.h:101
AlignedAllocator(const AlignedAllocator< U, M > &)
Definition: AlignedAllocator.h:86
ptrdiff_t difference_type
Definition: AlignedAllocator.h:74
T value_type
Definition: AlignedAllocator.h:68
void deallocate(pointer p, size_type n)
Definition: AlignedAllocator.h:112
size_t size_type
Definition: AlignedAllocator.h:73
void construct(pointer p, const_reference val)
Definition: AlignedAllocator.h:97
Definition: AlignedAllocator.h:63
Definition: AlignedAllocator.h:77
AlignedAllocator< U, N > other
Definition: AlignedAllocator.h:78