Path: blob/master/thirdparty/embree/common/sys/alloc.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "platform.h"6#include <vector>7#include <set>89namespace embree10{11#define ALIGNED_STRUCT_(align) \12void* operator new(size_t size) { return alignedMalloc(size,align); } \13void operator delete(void* ptr) { alignedFree(ptr); } \14void* operator new[](size_t size) { return alignedMalloc(size,align); } \15void operator delete[](void* ptr) { alignedFree(ptr); }1617#define ALIGNED_CLASS_(align) \18public: \19ALIGNED_STRUCT_(align) \20private:2122/*! aligned allocation */23void* alignedMalloc(size_t size, size_t align);24void alignedFree(void* ptr);252627enum class EmbreeUSMMode {28DEFAULT = 0,29DEVICE_READ_WRITE = 0,30DEVICE_READ_ONLY = 131};3233enum class EmbreeMemoryType {34USM_HOST = 0,35USM_DEVICE = 1,36USM_SHARED = 2,37MALLOC = 338};3940#if defined(EMBREE_SYCL_SUPPORT)4142/*! aligned allocation using SYCL USM */43void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode);44void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode, EmbreeMemoryType type);45void alignedSYCLFree(sycl::context* context, void* ptr);4647#endif4849/*! allocator that performs aligned allocations */50template<typename T, size_t alignment>51struct aligned_allocator52{53typedef T value_type;54typedef T* pointer;55typedef const T* const_pointer;56typedef T& reference;57typedef const T& const_reference;58typedef std::size_t size_type;59typedef std::ptrdiff_t difference_type;6061__forceinline pointer allocate( size_type n ) {62return (pointer) alignedMalloc(n*sizeof(value_type),alignment);63}6465__forceinline void deallocate( pointer p, size_type n ) {66return alignedFree(p);67}6869__forceinline void construct( pointer p, const_reference val ) {70new (p) T(val);71}7273__forceinline void destroy( pointer p ) {74p->~T();75}76};7778/*! allocates pages directly from OS */79bool win_enable_selockmemoryprivilege(bool verbose);80bool os_init(bool hugepages, bool verbose);81void* os_malloc (size_t bytes, bool& hugepages);82size_t os_shrink (void* ptr, size_t bytesNew, size_t bytesOld, bool hugepages);83void os_free (void* ptr, size_t bytes, bool hugepages);84void os_advise (void* ptr, size_t bytes);8586/*! allocator that performs OS allocations */87template<typename T>88struct os_allocator89{90typedef T value_type;91typedef T* pointer;92typedef const T* const_pointer;93typedef T& reference;94typedef const T& const_reference;95typedef std::size_t size_type;96typedef std::ptrdiff_t difference_type;9798__forceinline os_allocator ()99: hugepages(false) {}100101__forceinline pointer allocate( size_type n ) {102return (pointer) os_malloc(n*sizeof(value_type),hugepages);103}104105__forceinline void deallocate( pointer p, size_type n ) {106return os_free(p,n*sizeof(value_type),hugepages);107}108109__forceinline void construct( pointer p, const_reference val ) {110new (p) T(val);111}112113__forceinline void destroy( pointer p ) {114p->~T();115}116117bool hugepages;118};119120/*! allocator that newer performs allocations */121template<typename T>122struct no_allocator123{124typedef T value_type;125typedef T* pointer;126typedef const T* const_pointer;127typedef T& reference;128typedef const T& const_reference;129typedef std::size_t size_type;130typedef std::ptrdiff_t difference_type;131132__forceinline pointer allocate( size_type n ) {133abort(); //throw std::runtime_error("no allocation supported");134}135136__forceinline void deallocate( pointer p, size_type n ) {137}138139__forceinline void construct( pointer p, const_reference val ) {140new (p) T(val);141}142143__forceinline void destroy( pointer p ) {144p->~T();145}146};147148/*! allocator for IDs */149template<typename T, size_t max_id>150struct IDPool151{152typedef T value_type;153154IDPool ()155: nextID(0) {}156157T allocate()158{159/* return ID from list */160if (!IDs.empty())161{162T id = *IDs.begin();163IDs.erase(IDs.begin());164return id;165}166167/* allocate new ID */168else169{170if (size_t(nextID)+1 > max_id)171return -1;172173return nextID++;174}175}176177/* adds an ID provided by the user */178bool add(T id)179{180if (id > max_id)181return false;182183/* check if ID should be in IDs set */184if (id < nextID) {185auto p = IDs.find(id);186if (p == IDs.end()) return false;187IDs.erase(p);188return true;189}190191/* otherwise increase ID set */192else193{194for (T i=nextID; i<id; i++) {195IDs.insert(i);196}197nextID = id+1;198return true;199}200}201202void deallocate( T id )203{204assert(id < nextID);205MAYBE_UNUSED auto done = IDs.insert(id).second;206assert(done);207}208209private:210std::set<T> IDs; //!< stores deallocated IDs to be reused211T nextID; //!< next ID to use when IDs vector is empty212};213}214215216217