tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
aligned_allocator.h
1/*
2 Copyright (c) 2016, Taiga Nomi
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27#pragma once
28#include <stdlib.h>
29#ifdef _WIN32
30#include <malloc.h>
31#endif
32#ifdef __MINGW32__
33#include <mm_malloc.h>
34#endif
35#include "nn_error.h"
36
37namespace tiny_dnn {
38
39template <typename T, std::size_t alignment>
41public:
42 typedef T value_type;
43 typedef T* pointer;
44 typedef std::size_t size_type;
45 typedef std::ptrdiff_t difference_type;
46 typedef T& reference;
47 typedef const T& const_reference;
48 typedef const T* const_pointer;
49
50 template <typename U>
51 struct rebind {
53 };
54
56
57 template <typename U>
59
60 const_pointer address(const_reference value) const {
61 return std::addressof(value);
62 }
63
64 pointer address(reference value) const {
65 return std::addressof(value);
66 }
67
68 pointer allocate(size_type size, const void* = nullptr) {
69 void* p = aligned_alloc(alignment, sizeof(T) * size);
70 if (!p && size > 0)
71 throw nn_error("failed to allocate");
72 return static_cast<pointer>(p);
73 }
74
75 size_type max_size() const {
76 return ~static_cast<std::size_t>(0) / sizeof(T);
77 }
78
79 void deallocate(pointer ptr, size_type) {
80 aligned_free(ptr);
81 }
82
83 template<class U, class V>
84 void construct(U* ptr, const V& value) {
85 void* p = ptr;
86 ::new(p) U(value);
87 }
88
89#if defined(_MSC_VER) && _MSC_VER <= 1800
90 // -vc2013 doesn't support variadic templates
91#else
92 template<class U, class... Args>
93 void construct(U* ptr, Args&&... args) {
94 void* p = ptr;
95 ::new(p) U(std::forward<Args>(args)...);
96 }
97#endif
98
99 template<class U>
100 void construct(U* ptr) {
101 void* p = ptr;
102 ::new(p) U();
103 }
104
105 template<class U>
106 void destroy(U* ptr) {
107 ptr->~U();
108 }
109
110private:
111 void* aligned_alloc(size_type align, size_type size) const {
112#if defined(_MSC_VER)
113 return ::_aligned_malloc(size, align);
114#elif defined (__ANDROID__)
115 return ::memalign(align, size);
116#elif defined (__MINGW32__)
117 return ::_mm_malloc(size, align);
118#else // posix assumed
119 void* p;
120 if (::posix_memalign(&p, align, size) != 0) {
121 p = 0;
122 }
123 return p;
124#endif
125 }
126
127 void aligned_free(pointer ptr) {
128#if defined(_MSC_VER)
129 ::_aligned_free(ptr);
130#elif defined(__MINGW32__)
131 ::_mm_free(ptr);
132#else
133 ::free(ptr);
134#endif
135 }
136};
137
138template<typename T1, typename T2, std::size_t alignment>
139inline bool operator==(const aligned_allocator<T1, alignment>&, const aligned_allocator<T2, alignment>&)
140{
141 return true;
142}
143
144template<typename T1, typename T2, std::size_t alignment>
145inline bool operator!=(const aligned_allocator<T1, alignment>&, const aligned_allocator<T2, alignment>&)
146{
147 return false;
148}
149}
Definition aligned_allocator.h:40
Simple image utility class.
Definition image.h:94
Definition aligned_allocator.h:51