1/*
2 *  Copyright 2008-2018 NVIDIA Corporation
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 */
16
17#include <thrust/detail/config.h>
18#include <thrust/system/cpp/detail/execution_policy.h>
19#include <thrust/system/tbb/memory.h>
20#include <thrust/system/cpp/memory.h>
21#include <limits>
22
23namespace thrust
24{
25namespace system
26{
27namespace tbb
28{
29
30
31namespace detail
32{
33
34// XXX circular #inclusion problems cause the compiler to believe that cpp::malloc
35//     is not defined
36//     WAR the problem by using adl to call cpp::malloc, which requires it to depend
37//     on a template parameter
38template<typename Tag>
39  pointer<void> malloc_workaround(Tag t, std::size_t n)
40{
41  return pointer<void>(malloc(t, n));
42} // end malloc_workaround()
43
44// XXX circular #inclusion problems cause the compiler to believe that cpp::free
45//     is not defined
46//     WAR the problem by using adl to call cpp::free, which requires it to depend
47//     on a template parameter
48template<typename Tag>
49  void free_workaround(Tag t, pointer<void> ptr)
50{
51  free(t, ptr.get());
52} // end free_workaround()
53
54} // end detail
55
56inline pointer<void> malloc(std::size_t n)
57{
58  // XXX this is how we'd like to implement this function,
59  //     if not for circular #inclusion problems:
60  //
61  // return pointer<void>(thrust::system::cpp::malloc(n))
62  //
63  return detail::malloc_workaround(cpp::tag(), n);
64} // end malloc()
65
66template<typename T>
67pointer<T> malloc(std::size_t n)
68{
69  pointer<void> raw_ptr = thrust::system::tbb::malloc(sizeof(T) * n);
70  return pointer<T>(reinterpret_cast<T*>(raw_ptr.get()));
71} // end malloc()
72
73inline void free(pointer<void> ptr)
74{
75  // XXX this is how we'd like to implement this function,
76  //     if not for circular #inclusion problems:
77  //
78  // thrust::system::cpp::free(ptr)
79  //
80  detail::free_workaround(cpp::tag(), ptr);
81} // end free()
82
83} // end tbb
84} // end system
85} // end thrust
86
87