Fix hipMalloc to return error code if allocation fails.

[ROCm/hip commit: 59df70662a]
This commit is contained in:
Ben Sander
2017-04-24 11:02:38 -05:00
parent 2b8fbf40a8
commit ba2f2f56b8
2 changed files with 12 additions and 8 deletions
@@ -863,7 +863,7 @@ hipError_t hipPointerGetAttributes(hipPointerAttribute_t *attributes, const void
* *
* If size is 0, no memory is allocated, *ptr returns nullptr, and hipSuccess is returned. * If size is 0, no memory is allocated, *ptr returns nullptr, and hipSuccess is returned.
* *
* @return #hipSuccess * @return #hipSuccess, #hipErrorMemoryAllocation, #hipErrorInvalidValue (bad context, null *ptr)
* *
* @see hipMallocPitch, hipFree, hipMallocArray, hipFreeArray, hipMalloc3D, hipMalloc3DArray, hipHostFree, hipHostMalloc * @see hipMallocPitch, hipFree, hipMallocArray, hipFreeArray, hipMalloc3D, hipMalloc3DArray, hipHostFree, hipHostMalloc
*/ */
+11 -7
View File
@@ -207,22 +207,26 @@ hipError_t hipMalloc(void** ptr, size_t sizeBytes)
HIP_INIT_API(ptr, sizeBytes); HIP_INIT_API(ptr, sizeBytes);
HIP_SET_DEVICE(); HIP_SET_DEVICE();
hipError_t hip_status = hipSuccess; hipError_t hip_status = hipSuccess;
auto ctx = ihipGetTlsDefaultCtx();
// return NULL pointer when malloc size is 0 // return NULL pointer when malloc size is 0
if (sizeBytes == 0) if (sizeBytes == 0)
{ {
*ptr = NULL; *ptr = NULL;
return ihipLogStatus(hipSuccess); hip_status = hipSuccess;
}
auto ctx = ihipGetTlsDefaultCtx(); } else if ((ctx==nullptr) || (ptr == nullptr)) {
hip_status = hipErrorInvalidValue;
if (ctx) { } else {
auto device = ctx->getWriteableDevice(); auto device = ctx->getWriteableDevice();
*ptr = hip_internal::allocAndSharePtr("device_mem", sizeBytes, ctx, 0/*amFlags*/, 0/*hipFlags*/); *ptr = hip_internal::allocAndSharePtr("device_mem", sizeBytes, ctx, 0/*amFlags*/, 0/*hipFlags*/);
} else { if(sizeBytes && (*ptr == NULL)){
hip_status = hipErrorMemoryAllocation; hip_status = hipErrorMemoryAllocation;
} }
}
return ihipLogStatus(hip_status); return ihipLogStatus(hip_status);