| 742 | } |
| 743 | |
| 744 | static Block* create(Device* device, bool useUSM, size_t bytesAllocate, size_t bytesReserve, Block* next, AllocationType atype) |
| 745 | { |
| 746 | /* We avoid using os_malloc for small blocks as this could |
| 747 | * cause a risk of fragmenting the virtual address space and |
| 748 | * reach the limit of vm.max_map_count = 65k under Linux. */ |
| 749 | if (atype == EMBREE_OS_MALLOC && bytesAllocate < maxAllocationSize) |
| 750 | atype = ALIGNED_MALLOC; |
| 751 | |
| 752 | /* we need to additionally allocate some header */ |
| 753 | const size_t sizeof_Header = offsetof(Block,data[0]); |
| 754 | bytesAllocate = sizeof_Header+bytesAllocate; |
| 755 | bytesReserve = sizeof_Header+bytesReserve; |
| 756 | |
| 757 | /* consume full 4k pages with using os_malloc */ |
| 758 | if (atype == EMBREE_OS_MALLOC) { |
| 759 | bytesAllocate = ((bytesAllocate+PAGE_SIZE-1) & ~(PAGE_SIZE-1)); |
| 760 | bytesReserve = ((bytesReserve +PAGE_SIZE-1) & ~(PAGE_SIZE-1)); |
| 761 | } |
| 762 | |
| 763 | /* either use alignedMalloc or os_malloc */ |
| 764 | void *ptr = nullptr; |
| 765 | if (atype == ALIGNED_MALLOC) |
| 766 | { |
| 767 | /* special handling for default block size */ |
| 768 | if (bytesAllocate == (2*PAGE_SIZE_2M)) |
| 769 | { |
| 770 | const size_t alignment = maxAlignment; |
| 771 | if (device) device->memoryMonitor(bytesAllocate+alignment,false); |
| 772 | ptr = blockAlignedMalloc(device,useUSM,bytesAllocate,alignment); |
| 773 | |
| 774 | /* give hint to transparently convert these pages to 2MB pages */ |
| 775 | const size_t ptr_aligned_begin = ((size_t)ptr) & ~size_t(PAGE_SIZE_2M-1); |
| 776 | os_advise((void*)(ptr_aligned_begin + 0),PAGE_SIZE_2M); // may fail if no memory mapped before block |
| 777 | os_advise((void*)(ptr_aligned_begin + 1*PAGE_SIZE_2M),PAGE_SIZE_2M); |
| 778 | os_advise((void*)(ptr_aligned_begin + 2*PAGE_SIZE_2M),PAGE_SIZE_2M); // may fail if no memory mapped after block |
| 779 | |
| 780 | return new (ptr) Block(ALIGNED_MALLOC,bytesAllocate-sizeof_Header,bytesAllocate-sizeof_Header,next,alignment); |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | const size_t alignment = maxAlignment; |
| 785 | if (device) device->memoryMonitor(bytesAllocate+alignment,false); |
| 786 | ptr = blockAlignedMalloc(device,useUSM,bytesAllocate,alignment); |
| 787 | return new (ptr) Block(ALIGNED_MALLOC,bytesAllocate-sizeof_Header,bytesAllocate-sizeof_Header,next,alignment); |
| 788 | } |
| 789 | } |
| 790 | else if (atype == EMBREE_OS_MALLOC) |
| 791 | { |
| 792 | if (device) device->memoryMonitor(bytesAllocate,false); |
| 793 | bool huge_pages; ptr = os_malloc(bytesReserve,huge_pages); |
| 794 | return new (ptr) Block(EMBREE_OS_MALLOC,bytesAllocate-sizeof_Header,bytesReserve-sizeof_Header,next,0,huge_pages); |
| 795 | } |
| 796 | else |
| 797 | assert(false); |
| 798 | |
| 799 | return NULL; |
| 800 | } |
| 801 |
nothing calls this directly
no test coverage detected