1// ======================================================================== //
2// Copyright 2009-2019 Intel 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 "common.h"
20#include "device.h"
21
22namespace oidn {
23
24 class Device;
25
26 // Buffer which may or may not own its data
27 class Buffer : public RefCount
28 {
29 private:
30 char* ptr;
31 size_t byteSize;
32 bool shared;
33 Ref<Device> device;
34
35 public:
36 __forceinline Buffer(const Ref<Device>& device, size_t size)
37 : ptr((char*)alignedMalloc(size, 64)),
38 byteSize(size),
39 shared(false),
40 device(device) {}
41
42 __forceinline Buffer(const Ref<Device>& device, void* data, size_t size)
43 : ptr((char*)data),
44 byteSize(size),
45 shared(true),
46 device(device)
47 {
48 if (data == nullptr)
49 throw Exception(Error::InvalidArgument, "buffer pointer null");
50 }
51
52 __forceinline ~Buffer()
53 {
54 if (!shared)
55 alignedFree(ptr);
56 }
57
58 __forceinline char* data() { return ptr; }
59 __forceinline const char* data() const { return ptr; }
60 __forceinline size_t size() const { return byteSize; }
61
62 void* map(size_t offset, size_t size)
63 {
64 if (offset + size > byteSize)
65 throw Exception(Error::InvalidArgument, "buffer region out of range");
66
67 return ptr + offset;
68 }
69
70 void unmap(void* mappedPtr) {}
71
72 Device* getDevice() { return device.get(); }
73 };
74
75} // namespace oidn
76