vector.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "default.h"
  4. namespace embree
  5. {
  6. /*! invokes the memory monitor callback */
  7. struct MemoryMonitorInterface {
  8. virtual void memoryMonitor(ssize_t bytes, bool post) = 0;
  9. };
  10. /*! allocator that performs aligned monitored allocations */
  11. template<typename T, size_t alignment = 64>
  12. struct aligned_monitored_allocator
  13. {
  14. typedef T value_type;
  15. typedef T* pointer;
  16. typedef const T* const_pointer;
  17. typedef T& reference;
  18. typedef const T& const_reference;
  19. typedef std::size_t size_type;
  20. typedef std::ptrdiff_t difference_type;
  21. __forceinline aligned_monitored_allocator(MemoryMonitorInterface* device)
  22. : device(device), hugepages(false) {}
  23. __forceinline pointer allocate( size_type n )
  24. {
  25. if (n) {
  26. assert(device);
  27. device->memoryMonitor(n*sizeof(T),false);
  28. }
  29. if (n*sizeof(value_type) >= 14 * PAGE_SIZE_2M)
  30. {
  31. pointer p = (pointer) os_malloc(n*sizeof(value_type),hugepages);
  32. assert(p);
  33. return p;
  34. }
  35. return (pointer) alignedMalloc(n*sizeof(value_type),alignment);
  36. }
  37. __forceinline void deallocate( pointer p, size_type n )
  38. {
  39. if (p)
  40. {
  41. if (n*sizeof(value_type) >= 14 * PAGE_SIZE_2M)
  42. os_free(p,n*sizeof(value_type),hugepages);
  43. else
  44. alignedFree(p);
  45. }
  46. else assert(n == 0);
  47. if (n) {
  48. assert(device);
  49. device->memoryMonitor(-ssize_t(n)*sizeof(T),true);
  50. }
  51. }
  52. __forceinline void construct( pointer p, const_reference val ) {
  53. new (p) T(val);
  54. }
  55. __forceinline void destroy( pointer p ) {
  56. p->~T();
  57. }
  58. private:
  59. MemoryMonitorInterface* device;
  60. bool hugepages;
  61. };
  62. /*! monitored vector */
  63. template<typename T>
  64. using mvector = vector_t<T,aligned_monitored_allocator<T,std::alignment_of<T>::value> >;
  65. }