[HIP CLR] Make hipMemPtrGetInfo consistent with malloc and hipMalloc (#1005)

hipMemPtrGetInfo was returning the error hipErrorInvalidValue if it
was called on a nullptr.  However, this does not match the malloc
convention where a nullptr has size zero;  for example,
malloc_usable_size() returns zero if called on a nullptr.

This commit changes hipMemPtrGetInfo to set the size to zero and
return hipSuccess when called with a nullptr.  (This also fits with
hipMalloc and hipFree usage, since hipMalloc of size zero results in a
nullptr, and hipFree of a nullptr is successful.)
Этот коммит содержится в:
JonathanLichtnerAMD authored and GitHub committed 2025-09-19 12:53:41 -06:00
1 родитель 1c10592be2
Коммит f31afe1d20
2 files changed
+21 -2

No files matched your search

+6 -1
Просмотреть файл
@@ -1,4 +1,4 @@
/* Copyright (c) 2015 - 2024 Advanced Micro Devices, Inc.
/* Copyright (c) 2015 - 2025 Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -829,6 +829,11 @@ hipError_t hipMemcpyWithStream(void* dst, const void* src, size_t sizeBytes, hip
hipError_t hipMemPtrGetInfo(void* ptr, size_t* size) {
HIP_INIT_API(hipMemPtrGetInfo, ptr, size);
if (ptr == nullptr) {
*size = 0;
HIP_RETURN(hipSuccess);
}
size_t offset = 0;
amd::Memory* svmMem = getMemoryObject(ptr, offset);
+15 -1
Просмотреть файл
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -56,3 +56,17 @@ TEST_CASE("Unit_hipMemPtrGetInfo_Basic") {
HIP_CHECK(hipFree(fPtr));
HIP_CHECK(hipFree(sPtr));
}
/*
This testcase verifies the scenario of
hipMemPtrGetInfo API being called on a zero-sized allocation.
*/
TEST_CASE("Unit_hipMemPtrGetInfo_SizeZeroAllocation") {
int* iPtr;
size_t sSetSize = 0, sGetSize;
HIP_CHECK(hipMalloc(&iPtr, sSetSize));
HIP_CHECK(hipMemPtrGetInfo(iPtr, &sGetSize));
REQUIRE(sGetSize == sSetSize);
HIP_CHECK(hipFree(iPtr));
}