NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
util.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  ******************************************************************************/
27 
28 /******************************************************************************
29  *
30  * Code and text by Sean Baxter, NVIDIA Research
31  * See http://nvlabs.github.io/moderngpu for repository and documentation.
32  *
33  ******************************************************************************/
34 
35 #pragma once
36 
37 #include "static.h"
38 
39 namespace mgpu {
40 
41 extern int Rand(int min, int max);
42 extern int64 Rand(int64 min, int64 max);
43 extern uint Rand(uint min, uint max);
44 extern uint64 Rand(uint64 min, uint64 max);
45 extern float Rand(float min, float max);
46 extern double Rand(double min, double max);
47 
48 
50 // intrusive_ptr
51 
52 // boost::noncopyable, moved here so we don't have dependency on boost
53 class noncopyable {
54 protected:
57 private:
58  noncopyable(const noncopyable&) { }
59  const noncopyable& operator=(const noncopyable&) { return *this; }
60 };
61 
62 class CudaBase : public noncopyable {
63 public:
64  CudaBase() : _ref(0) { }
65  virtual ~CudaBase() { }
66  virtual long AddRef() {
67  // return BOOST_INTERLOCKED_INCREMENT(&_ref);
68  return ++_ref;
69  }
70  virtual void Release() {
71  // if(!BOOST_INTERLOCKED_DECREMENT(&_ref)) delete this;
72  if(!--_ref) delete this;
73  }
74 private:
75  long _ref;
76 };
77 
78 inline long intrusive_ptr_add_ref(CudaBase* base) {
79  return base->AddRef();
80 }
81 
82 inline void intrusive_ptr_release(CudaBase* base) {
83  base->Release();
84 }
85 
86 template<typename T>
88 public:
89  intrusive_ptr() : _p(0) { }
90  explicit intrusive_ptr(T* p) : _p(p) {
91  if(p) intrusive_ptr_add_ref(p);
92  }
93  intrusive_ptr(const intrusive_ptr<T>& rhs) : _p(rhs._p) {
94  if(_p) intrusive_ptr_add_ref(_p);
95  }
97  if(_p) intrusive_ptr_release(_p);
98  }
100  intrusive_ptr(rhs.get()).swap(*this);
101  return *this;
102  }
103 
104  void reset(T* p = 0) {
105  intrusive_ptr(p).swap(*this);
106  }
107  T* release() {
108  T* p = _p;
109  _p = 0;
110  return p;
111  }
112 
113  T* get() const { return _p; }
114  operator T*() const { return _p; }
115  T* operator->() const { return _p; }
116 
117  void swap(intrusive_ptr& rhs) {
118  std::swap(_p, rhs._p);
119  }
120 private:
121  T* _p;
122 };
123 
124 } // namespace mgpu