Skip to content

Commit e05621a

Browse files
author
Hugh Delaney
committed
Put all calls to new in try catch
Make sure all calls of new in try catch which can catch a bad_alloc.
1 parent 708a0bc commit e05621a

File tree

6 files changed

+82
-67
lines changed

6 files changed

+82
-67
lines changed

source/adapters/cuda/memory.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
7171
MemObj = URMemObj.release();
7272
} catch (ur_result_t Err) {
7373
return Err;
74-
} catch (std::bad_alloc &Err) {
74+
} catch (std::bad_alloc &) {
7575
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
7676
} catch (...) {
7777
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
@@ -101,10 +101,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) {
101101
return UR_RESULT_SUCCESS;
102102
}
103103

104-
if (hMem->isSubBuffer()) {
105-
return UR_RESULT_SUCCESS;
106-
}
107-
108104
// Call destructor
109105
std::unique_ptr<ur_mem_handle_t_> MemObjPtr(hMem);
110106

@@ -247,7 +243,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate(
247243
*phMem = URMemObj.release();
248244
} catch (ur_result_t Err) {
249245
return Err;
250-
} catch (std::bad_alloc &Err) {
246+
} catch (std::bad_alloc &) {
251247
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
252248
} catch (...) {
253249
return UR_RESULT_ERROR_UNKNOWN;

source/adapters/cuda/physical_mem.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urPhysicalMemCreate(
3232
default:
3333
UR_CHECK_ERROR(Result);
3434
}
35-
*phPhysicalMem = new ur_physical_mem_handle_t_(ResHandle, hContext, hDevice);
36-
35+
try {
36+
*phPhysicalMem =
37+
new ur_physical_mem_handle_t_(ResHandle, hContext, hDevice);
38+
} catch (std::bad_alloc &) {
39+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
40+
} catch (...) {
41+
return UR_RESULT_ERROR_UNKNOWN;
42+
}
3743
return UR_RESULT_SUCCESS;
3844
}
3945

@@ -53,10 +59,10 @@ urPhysicalMemRelease(ur_physical_mem_handle_t hPhysicalMem) {
5359

5460
ScopedContext Active(hPhysicalMem->getDevice());
5561
UR_CHECK_ERROR(cuMemRelease(hPhysicalMem->get()));
56-
return UR_RESULT_SUCCESS;
5762
} catch (ur_result_t err) {
5863
return err;
5964
} catch (...) {
6065
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
6166
}
67+
return UR_RESULT_SUCCESS;
6268
}

source/adapters/cuda/program.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,30 @@ ur_result_t createProgram(ur_context_handle_t hContext,
187187
UR_RESULT_ERROR_INVALID_CONTEXT);
188188
UR_ASSERT(size, UR_RESULT_ERROR_INVALID_SIZE);
189189

190-
std::unique_ptr<ur_program_handle_t_> RetProgram{
191-
new ur_program_handle_t_{hContext, hDevice}};
192-
193-
if (pProperties) {
194-
if (pProperties->count > 0 && pProperties->pMetadatas == nullptr) {
195-
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
196-
} else if (pProperties->count == 0 && pProperties->pMetadatas != nullptr) {
197-
return UR_RESULT_ERROR_INVALID_SIZE;
190+
try {
191+
std::unique_ptr<ur_program_handle_t_> RetProgram{
192+
new ur_program_handle_t_{hContext, hDevice}};
193+
194+
if (pProperties) {
195+
if (pProperties->count > 0 && pProperties->pMetadatas == nullptr) {
196+
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
197+
} else if (pProperties->count == 0 &&
198+
pProperties->pMetadatas != nullptr) {
199+
return UR_RESULT_ERROR_INVALID_SIZE;
200+
}
201+
UR_CHECK_ERROR(
202+
RetProgram->setMetadata(pProperties->pMetadatas, pProperties->count));
198203
}
199-
UR_CHECK_ERROR(
200-
RetProgram->setMetadata(pProperties->pMetadatas, pProperties->count));
201-
}
202204

203-
auto pBinary_string = reinterpret_cast<const char *>(pBinary);
205+
auto pBinary_string = reinterpret_cast<const char *>(pBinary);
204206

205-
UR_CHECK_ERROR(RetProgram->setBinary(pBinary_string, size));
206-
*phProgram = RetProgram.release();
207+
UR_CHECK_ERROR(RetProgram->setBinary(pBinary_string, size));
208+
*phProgram = RetProgram.release();
209+
} catch (std::bad_alloc &) {
210+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
211+
} catch (...) {
212+
return UR_RESULT_ERROR_UNKNOWN;
213+
}
207214

208215
return UR_RESULT_SUCCESS;
209216
}
@@ -317,6 +324,8 @@ urProgramLink(ur_context_handle_t hContext, uint32_t count,
317324

318325
} catch (ur_result_t Err) {
319326
Result = Err;
327+
} catch (std::bad_alloc &) {
328+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
320329
}
321330
return Result;
322331
}

source/adapters/cuda/queue.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,11 @@ urQueueCreate(ur_context_handle_t hContext, ur_device_handle_t hDevice,
167167

168168
return UR_RESULT_SUCCESS;
169169
} catch (ur_result_t Err) {
170-
171170
return Err;
172-
171+
} catch (std::bad_alloc &) {
172+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
173173
} catch (...) {
174-
175-
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
174+
return UR_RESULT_ERROR_UNKNOWN;
176175
}
177176
}
178177

source/adapters/cuda/sampler.cpp

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,55 @@
1414
UR_APIEXPORT ur_result_t UR_APICALL
1515
urSamplerCreate(ur_context_handle_t hContext, const ur_sampler_desc_t *pDesc,
1616
ur_sampler_handle_t *phSampler) {
17-
std::unique_ptr<ur_sampler_handle_t_> Sampler{
18-
new ur_sampler_handle_t_(hContext)};
17+
try {
18+
std::unique_ptr<ur_sampler_handle_t_> Sampler{
19+
new ur_sampler_handle_t_(hContext)};
1920

20-
if (pDesc->stype == UR_STRUCTURE_TYPE_SAMPLER_DESC) {
21-
Sampler->Props |= static_cast<uint32_t>(pDesc->normalizedCoords);
22-
Sampler->Props |= pDesc->filterMode << 1;
23-
Sampler->Props |= pDesc->addressingMode << 2;
24-
} else {
25-
// Set default values
26-
Sampler->Props |= true; // Normalized Coords
27-
Sampler->Props |= UR_SAMPLER_ADDRESSING_MODE_CLAMP << 2;
28-
}
21+
if (pDesc->stype == UR_STRUCTURE_TYPE_SAMPLER_DESC) {
22+
Sampler->Props |= static_cast<uint32_t>(pDesc->normalizedCoords);
23+
Sampler->Props |= pDesc->filterMode << 1;
24+
Sampler->Props |= pDesc->addressingMode << 2;
25+
} else {
26+
// Set default values
27+
Sampler->Props |= true; // Normalized Coords
28+
Sampler->Props |= UR_SAMPLER_ADDRESSING_MODE_CLAMP << 2;
29+
}
2930

30-
void *pNext = const_cast<void *>(pDesc->pNext);
31-
while (pNext != nullptr) {
32-
const ur_base_desc_t *BaseDesc =
33-
reinterpret_cast<const ur_base_desc_t *>(pNext);
34-
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_SAMPLER_MIP_PROPERTIES) {
35-
const ur_exp_sampler_mip_properties_t *SamplerMipProperties =
36-
reinterpret_cast<const ur_exp_sampler_mip_properties_t *>(pNext);
37-
Sampler->MaxMipmapLevelClamp = SamplerMipProperties->maxMipmapLevelClamp;
38-
Sampler->MinMipmapLevelClamp = SamplerMipProperties->minMipmapLevelClamp;
39-
Sampler->MaxAnisotropy = SamplerMipProperties->maxAnisotropy;
40-
Sampler->Props |= SamplerMipProperties->mipFilterMode << 11;
41-
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES) {
42-
const ur_exp_sampler_addr_modes_t *SamplerAddrModes =
43-
reinterpret_cast<const ur_exp_sampler_addr_modes_t *>(pNext);
44-
Sampler->Props |= SamplerAddrModes->addrModes[0] << 2;
45-
Sampler->Props |= SamplerAddrModes->addrModes[1] << 5;
46-
Sampler->Props |= SamplerAddrModes->addrModes[2] << 8;
47-
} else if (BaseDesc->stype ==
48-
UR_STRUCTURE_TYPE_EXP_SAMPLER_CUBEMAP_PROPERTIES) {
49-
const ur_exp_sampler_cubemap_properties_t *SamplerCubemapProperties =
50-
reinterpret_cast<const ur_exp_sampler_cubemap_properties_t *>(pNext);
51-
Sampler->Props |= SamplerCubemapProperties->cubemapFilterMode << 12;
31+
void *pNext = const_cast<void *>(pDesc->pNext);
32+
while (pNext != nullptr) {
33+
const ur_base_desc_t *BaseDesc =
34+
reinterpret_cast<const ur_base_desc_t *>(pNext);
35+
if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_SAMPLER_MIP_PROPERTIES) {
36+
const ur_exp_sampler_mip_properties_t *SamplerMipProperties =
37+
reinterpret_cast<const ur_exp_sampler_mip_properties_t *>(pNext);
38+
Sampler->MaxMipmapLevelClamp =
39+
SamplerMipProperties->maxMipmapLevelClamp;
40+
Sampler->MinMipmapLevelClamp =
41+
SamplerMipProperties->minMipmapLevelClamp;
42+
Sampler->MaxAnisotropy = SamplerMipProperties->maxAnisotropy;
43+
Sampler->Props |= SamplerMipProperties->mipFilterMode << 11;
44+
} else if (BaseDesc->stype == UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES) {
45+
const ur_exp_sampler_addr_modes_t *SamplerAddrModes =
46+
reinterpret_cast<const ur_exp_sampler_addr_modes_t *>(pNext);
47+
Sampler->Props |= SamplerAddrModes->addrModes[0] << 2;
48+
Sampler->Props |= SamplerAddrModes->addrModes[1] << 5;
49+
Sampler->Props |= SamplerAddrModes->addrModes[2] << 8;
50+
} else if (BaseDesc->stype ==
51+
UR_STRUCTURE_TYPE_EXP_SAMPLER_CUBEMAP_PROPERTIES) {
52+
const ur_exp_sampler_cubemap_properties_t *SamplerCubemapProperties =
53+
reinterpret_cast<const ur_exp_sampler_cubemap_properties_t *>(
54+
pNext);
55+
Sampler->Props |= SamplerCubemapProperties->cubemapFilterMode << 12;
56+
}
57+
pNext = const_cast<void *>(BaseDesc->pNext);
5258
}
53-
pNext = const_cast<void *>(BaseDesc->pNext);
54-
}
5559

56-
*phSampler = Sampler.release();
60+
*phSampler = Sampler.release();
61+
} catch (std::bad_alloc &) {
62+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
63+
} catch (...) {
64+
return UR_RESULT_ERROR_UNKNOWN;
65+
}
5766
return UR_RESULT_SUCCESS;
5867
}
5968

source/adapters/hip/memory.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) {
6868
return UR_RESULT_SUCCESS;
6969
}
7070

71-
if (hMem->isSubBuffer()) {
72-
return UR_RESULT_SUCCESS;
73-
}
74-
7571
// Call destructor
7672
std::unique_ptr<ur_mem_handle_t_> uniqueMemObj(hMem);
7773

0 commit comments

Comments
 (0)