OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / system / cuda / detail / malloc_and_free.h
1 /*
2  *  Copyright 2008-2013 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 #pragma once
18
19 #include <thrust/detail/config.h>
20 #include <thrust/system/cuda/detail/execution_policy.h>
21 #include <thrust/detail/raw_pointer_cast.h>
22 #include <thrust/system/cuda/detail/guarded_cuda_runtime_api.h>
23 #include <thrust/system/system_error.h>
24 #include <thrust/system/cuda/error.h>
25 #include <thrust/system/detail/bad_alloc.h>
26
27 namespace thrust
28 {
29 namespace system
30 {
31 namespace cuda
32 {
33 namespace detail
34 {
35
36
37 // note that malloc returns a raw pointer to avoid
38 // depending on the heavyweight thrust/system/cuda/memory.h header
39 template<typename DerivedPolicy>
40   void *malloc(execution_policy<DerivedPolicy> &, std::size_t n)
41 {
42   void *result = 0;
43
44   cudaError_t error = cudaMalloc(reinterpret_cast<void**>(&result), n);
45
46   if(error)
47   {
48     throw thrust::system::detail::bad_alloc(thrust::cuda_category().message(error).c_str());
49   } // end if
50
51   return result;
52 } // end malloc()
53
54
55 template<typename DerivedPolicy, typename Pointer>
56   void free(execution_policy<DerivedPolicy> &, Pointer ptr)
57 {
58   cudaError_t error = cudaFree(thrust::raw_pointer_cast(ptr));
59
60   if(error)
61   {
62     throw thrust::system_error(error, thrust::cuda_category());
63   } // end error
64 } // end free()
65
66
67 } // end detail
68 } // end cuda
69 } // end system
70 } // end thrust
71