buffer.h 2.4 KB

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