CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
67 lines
2.0 KiB
C++
67 lines
2.0 KiB
C++
/*
|
|
* Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
#include "os/alloc.hpp"
|
|
#include "os/os.hpp"
|
|
#include "utils/util.hpp"
|
|
|
|
#include <cstdlib>
|
|
|
|
namespace amd {
|
|
|
|
void* AlignedMemory::allocate(size_t size, size_t alignment) {
|
|
return Os::alignedMalloc(size, alignment);
|
|
}
|
|
|
|
void* GuardedMemory::allocate(size_t size, size_t alignment, size_t guardSize) {
|
|
size_t sizeToAllocate = guardSize + alignment;
|
|
sizeToAllocate += size + guardSize + Os::pageSize();
|
|
|
|
sizeToAllocate = amd::alignUp(sizeToAllocate, Os::pageSize());
|
|
address userHostMemGuarded = Os::reserveMemory(NULL, sizeToAllocate);
|
|
if (!userHostMemGuarded ||
|
|
!Os::commitMemory(userHostMemGuarded, sizeToAllocate, Os::MEM_PROT_RW)) {
|
|
return NULL;
|
|
}
|
|
|
|
address userHostMem = userHostMemGuarded + sizeToAllocate;
|
|
userHostMem = amd::alignDown(userHostMem - guardSize, Os::pageSize());
|
|
|
|
// Protect the guard pages after the end of the users's buffer.
|
|
if (!Os::protectMemory(userHostMem, guardSize, Os::MEM_PROT_NONE)) {
|
|
fatal("Protect memory (up) failed");
|
|
}
|
|
|
|
userHostMem = userHostMem - size;
|
|
userHostMem = amd::alignDown(userHostMem, alignment);
|
|
// Write the actual size allocated including all the guard pages,
|
|
// alignment, page file size... as well as the size of guarded byte
|
|
// count before the beginning of the user's buffer.
|
|
size_t* temp = reinterpret_cast<size_t*>(userHostMem);
|
|
*--temp = sizeToAllocate;
|
|
*--temp = userHostMem - userHostMemGuarded;
|
|
|
|
// Protect the guard pages before the beginning of the user's buffer.
|
|
if (!Os::protectMemory(userHostMemGuarded, guardSize, Os::MEM_PROT_NONE)) {
|
|
fatal("Protect memory (down) failed");
|
|
}
|
|
|
|
return userHostMem;
|
|
}
|
|
|
|
void AlignedMemory::deallocate(void* ptr) { Os::alignedFree(ptr); }
|
|
|
|
void GuardedMemory::deallocate(void* ptr) {
|
|
size_t* userHostMem = static_cast<size_t*>(ptr);
|
|
|
|
size_t size = *--userHostMem;
|
|
size_t offset = *--userHostMem;
|
|
|
|
Os::releaseMemory(static_cast<address>(ptr) - offset, size);
|
|
}
|
|
|
|
} // namespace amd
|