RAII wrapper for OpenCL memory object. Image is moveable but not copyable.
| 28 | // |
| 29 | // Image is moveable but not copyable. |
| 30 | class CLMemory { |
| 31 | public: |
| 32 | // Creates invalid object. |
| 33 | CLMemory() : CLMemory(nullptr, false) {} |
| 34 | |
| 35 | CLMemory(cl_mem memory, bool has_ownership) |
| 36 | : memory_(memory), has_ownership_(has_ownership) {} |
| 37 | |
| 38 | // Move-only |
| 39 | CLMemory(const CLMemory&) = delete; |
| 40 | CLMemory& operator=(const CLMemory&) = delete; |
| 41 | CLMemory(CLMemory&& image) |
| 42 | : memory_(image.memory_), has_ownership_(image.has_ownership_) { |
| 43 | image.memory_ = nullptr; |
| 44 | } |
| 45 | |
| 46 | ~CLMemory() { Invalidate(); } |
| 47 | |
| 48 | CLMemory& operator=(CLMemory&& image) { |
| 49 | if (this != &image) { |
| 50 | Invalidate(); |
| 51 | std::swap(memory_, image.memory_); |
| 52 | has_ownership_ = image.has_ownership_; |
| 53 | } |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | cl_mem memory() const { return memory_; } |
| 58 | |
| 59 | bool is_valid() const { return memory_ != nullptr; } |
| 60 | |
| 61 | // @return true if this object actually owns corresponding CL memory |
| 62 | // and manages it's lifetime. |
| 63 | bool has_ownership() const { return has_ownership_; } |
| 64 | |
| 65 | cl_mem Release() { |
| 66 | cl_mem to_return = memory_; |
| 67 | memory_ = nullptr; |
| 68 | return to_return; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | void Invalidate() { |
| 73 | if (memory_ && has_ownership_) { |
| 74 | clReleaseMemObject(memory_); |
| 75 | } |
| 76 | memory_ = nullptr; |
| 77 | } |
| 78 | |
| 79 | cl_mem memory_ = nullptr; |
| 80 | bool has_ownership_ = false; |
| 81 | }; |
| 82 | |
| 83 | cl_mem_flags ToClMemFlags(AccessType access_type); |
| 84 |
no outgoing calls
no test coverage detected