Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 15bd7d9

Browse files
authored
Merge pull request #1157 from KFilipek/task-remove_duplications
Add decoration to exceptions instead of calling method
2 parents a3064bc + e844778 commit 15bd7d9

File tree

9 files changed

+113
-186
lines changed

9 files changed

+113
-186
lines changed

include/libpmemobj++/allocator.hpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,14 @@ class standard_alloc_policy {
253253
detail::type_num<value_type>());
254254

255255
if (ptr == nullptr) {
256+
const char *msg =
257+
"Failed to allocate persistent memory object";
256258
if (errno == ENOMEM) {
257-
throw pmem::transaction_out_of_memory(
258-
"Failed to allocate persistent memory object")
259-
.with_pmemobj_errormsg();
259+
throw detail::exception_with_errormsg<
260+
pmem::transaction_out_of_memory>(msg);
260261
} else {
261-
throw pmem::transaction_alloc_error(
262-
"Failed to allocate persistent memory object")
263-
.with_pmemobj_errormsg();
262+
throw detail::exception_with_errormsg<
263+
pmem::transaction_alloc_error>(msg);
264264
}
265265
}
266266

@@ -282,9 +282,9 @@ class standard_alloc_policy {
282282
"refusing to free memory outside of transaction scope");
283283

284284
if (pmemobj_tx_free(*p.raw_ptr()) != 0)
285-
throw pmem::transaction_free_error(
286-
"failed to delete persistent memory object")
287-
.with_pmemobj_errormsg();
285+
throw detail::exception_with_errormsg<
286+
pmem::transaction_free_error>(
287+
"failed to delete persistent memory object");
288288
}
289289

290290
/**
@@ -368,14 +368,14 @@ class standard_alloc_policy<void> {
368368
pointer ptr = pmemobj_tx_alloc(1 /* void size */ * cnt, 0);
369369

370370
if (ptr == nullptr) {
371+
const char *msg =
372+
"Failed to allocate persistent memory object";
371373
if (errno == ENOMEM) {
372-
throw pmem::transaction_out_of_memory(
373-
"Failed to allocate persistent memory object")
374-
.with_pmemobj_errormsg();
374+
throw detail::exception_with_errormsg<
375+
pmem::transaction_out_of_memory>(msg);
375376
} else {
376-
throw pmem::transaction_alloc_error(
377-
"Failed to allocate persistent memory object")
378-
.with_pmemobj_errormsg();
377+
throw detail::exception_with_errormsg<
378+
pmem::transaction_alloc_error>(msg);
379379
}
380380
}
381381

@@ -397,9 +397,9 @@ class standard_alloc_policy<void> {
397397
"refusing to free memory outside of transaction scope");
398398

399399
if (pmemobj_tx_free(p.raw()) != 0)
400-
throw pmem::transaction_free_error(
401-
"failed to delete persistent memory object")
402-
.with_pmemobj_errormsg();
400+
throw detail::exception_with_errormsg<
401+
pmem::transaction_free_error>(
402+
"failed to delete persistent memory object");
403403
}
404404

405405
/**

include/libpmemobj++/container/vector.hpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,16 +2055,14 @@ vector<T>::alloc(size_type capacity_new)
20552055
detail::type_num<value_type>());
20562056

20572057
if (res == nullptr) {
2058+
const char *msg = "Failed to allocate persistent memory object";
20582059
if (errno == ENOMEM)
2059-
throw pmem::transaction_out_of_memory(
2060-
"Failed to allocate persistent memory object")
2061-
.with_pmemobj_errormsg();
2060+
throw detail::exception_with_errormsg<
2061+
pmem::transaction_out_of_memory>(msg);
20622062
else
2063-
throw pmem::transaction_alloc_error(
2064-
"Failed to allocate persistent memory object")
2065-
.with_pmemobj_errormsg();
2063+
throw detail::exception_with_errormsg<
2064+
pmem::transaction_alloc_error>(msg);
20662065
}
2067-
20682066
_data = res;
20692067
}
20702068

@@ -2196,9 +2194,9 @@ vector<T>::dealloc()
21962194
if (_data != nullptr) {
21972195
shrink(0);
21982196
if (pmemobj_tx_free(*_data.raw_ptr()) != 0)
2199-
throw pmem::transaction_free_error(
2200-
"failed to delete persistent memory object")
2201-
.with_pmemobj_errormsg();
2197+
throw detail::exception_with_errormsg<
2198+
pmem::transaction_free_error>(
2199+
"failed to delete persistent memory object");
22022200
_data = nullptr;
22032201
_capacity = 0;
22042202
}
@@ -2345,9 +2343,9 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23452343
detail::destroy<value_type>(
23462344
old_data[static_cast<difference_type>(i)]);
23472345
if (pmemobj_tx_free(old_data.raw()) != 0)
2348-
throw pmem::transaction_free_error(
2349-
"failed to delete persistent memory object")
2350-
.with_pmemobj_errormsg();
2346+
throw detail::exception_with_errormsg<
2347+
pmem::transaction_free_error>(
2348+
"failed to delete persistent memory object");
23512349
}
23522350
}
23532351

@@ -2408,9 +2406,9 @@ vector<T>::realloc(size_type capacity_new)
24082406
detail::destroy<value_type>(
24092407
old_data[static_cast<difference_type>(i)]);
24102408
if (pmemobj_tx_free(old_data.raw()) != 0)
2411-
throw pmem::transaction_free_error(
2412-
"failed to delete persistent memory object")
2413-
.with_pmemobj_errormsg();
2409+
throw detail::exception_with_errormsg<
2410+
pmem::transaction_free_error>(
2411+
"failed to delete persistent memory object");
24142412
}
24152413

24162414
/**

include/libpmemobj++/detail/common.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,17 @@ static constexpr size_t CACHELINE_SIZE = 128ULL;
159159
#error unable to recognize architecture at compile time
160160
#endif
161161

162-
/*
162+
/**
163+
* Generic error message decorator for pmemobj-based exceptions.
164+
*/
165+
template <class ExcT, typename MsgT>
166+
ExcT
167+
exception_with_errormsg(const MsgT &msg)
168+
{
169+
return ExcT(msg + std::string(": ") + detail::errormsg());
170+
}
171+
172+
/**
163173
* Conditionally add 'count' objects to a transaction.
164174
*
165175
* Adds count objects starting from `that` to the transaction if '*that' is
@@ -186,18 +196,17 @@ conditional_add_to_tx(const T *that, std::size_t count = 1, uint64_t flags = 0)
186196
return;
187197

188198
if (pmemobj_tx_xadd_range_direct(that, sizeof(*that) * count, flags)) {
199+
const char *msg = "Could not add object(s) to the transaction.";
189200
if (errno == ENOMEM)
190-
throw pmem::transaction_out_of_memory(
191-
"Could not add object(s) to the transaction.")
192-
.with_pmemobj_errormsg();
201+
throw exception_with_errormsg<
202+
pmem::transaction_out_of_memory>(msg);
193203
else
194-
throw pmem::transaction_error(
195-
"Could not add object(s) to the transaction.")
196-
.with_pmemobj_errormsg();
204+
throw exception_with_errormsg<pmem::transaction_error>(
205+
msg);
197206
}
198207
}
199208

200-
/*
209+
/**
201210
* Return type number for given type.
202211
*/
203212
template <typename T>

include/libpmemobj++/detail/ctl.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ ctl_get_detail(PMEMobjpool *pool, const std::string &name)
3535
int ret = pmemobj_ctl_get(pool, name.c_str(), &tmp);
3636
#endif
3737
if (ret)
38-
throw pmem::ctl_error("ctl_get failed").with_pmemobj_errormsg();
38+
throw detail::exception_with_errormsg<pmem::ctl_error>(
39+
"ctl_get failed");
3940

4041
return tmp;
4142
}
@@ -50,7 +51,8 @@ ctl_set_detail(PMEMobjpool *pool, const std::string &name, T arg)
5051
int ret = pmemobj_ctl_set(pool, name.c_str(), &arg);
5152
#endif
5253
if (ret)
53-
throw pmem::ctl_error("ctl_set failed").with_pmemobj_errormsg();
54+
throw detail::exception_with_errormsg<pmem::ctl_error>(
55+
"ctl_set failed");
5456

5557
return arg;
5658
}
@@ -65,9 +67,8 @@ ctl_exec_detail(PMEMobjpool *pool, const std::string &name, T arg)
6567
int ret = pmemobj_ctl_exec(pool, name.c_str(), &arg);
6668
#endif
6769
if (ret)
68-
throw pmem::ctl_error("ctl_exec failed")
69-
.with_pmemobj_errormsg();
70-
70+
throw detail::exception_with_errormsg<pmem::ctl_error>(
71+
"ctl_exec failed");
7172
return arg;
7273
}
7374

@@ -80,7 +81,8 @@ ctl_get_detail(PMEMobjpool *pool, const std::wstring &name)
8081

8182
int ret = pmemobj_ctl_getW(pool, name.c_str(), &tmp);
8283
if (ret)
83-
throw pmem::ctl_error("ctl_get failed").with_pmemobj_errormsg();
84+
throw detail::exception_with_errormsg<pmem::ctl_error>(
85+
"ctl_get failed");
8486

8587
return tmp;
8688
}
@@ -91,7 +93,8 @@ ctl_set_detail(PMEMobjpool *pool, const std::wstring &name, T arg)
9193
{
9294
int ret = pmemobj_ctl_setW(pool, name.c_str(), &arg);
9395
if (ret)
94-
throw pmem::ctl_error("ctl_set failed").with_pmemobj_errormsg();
96+
throw detail::exception_with_errormsg<pmem::ctl_error>(
97+
"ctl_set failed");
9598

9699
return arg;
97100
}
@@ -102,9 +105,8 @@ ctl_exec_detail(PMEMobjpool *pool, const std::wstring &name, T arg)
102105
{
103106
int ret = pmemobj_ctl_execW(pool, name.c_str(), &arg);
104107
if (ret)
105-
throw pmem::ctl_error("ctl_exec failed")
106-
.with_pmemobj_errormsg();
107-
108+
throw detail::exception_with_errormsg<pmem::ctl_error>(
109+
"ctl_exec failed");
108110
return arg;
109111
}
110112
#endif

include/libpmemobj++/make_persistent.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ make_persistent(allocation_flag flag, Args &&... args)
5858
pmemobj_tx_xalloc(sizeof(T), detail::type_num<T>(), flag.value);
5959

6060
if (ptr == nullptr) {
61+
const char *msg = "Failed to allocate persistent memory object";
6162
if (errno == ENOMEM)
62-
throw pmem::transaction_out_of_memory(
63-
"Failed to allocate persistent memory object")
64-
.with_pmemobj_errormsg();
63+
throw detail::exception_with_errormsg<
64+
pmem::transaction_out_of_memory>(msg);
6565
else
66-
throw pmem::transaction_alloc_error(
67-
"Failed to allocate persistent memory object")
68-
.with_pmemobj_errormsg();
66+
throw detail::exception_with_errormsg<
67+
pmem::transaction_alloc_error>(msg);
6968
}
7069

7170
detail::create<T, Args...>(ptr.get(), std::forward<Args>(args)...);
@@ -135,9 +134,9 @@ delete_persistent(typename detail::pp_if_not_array<T>::type ptr)
135134
detail::destroy<T>(*ptr);
136135

137136
if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
138-
throw pmem::transaction_free_error(
139-
"failed to delete persistent memory object")
140-
.with_pmemobj_errormsg();
137+
throw detail::exception_with_errormsg<
138+
pmem::transaction_free_error>(
139+
"failed to delete persistent memory object");
141140
}
142141

143142
} /* namespace obj */

include/libpmemobj++/make_persistent_array.hpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,13 @@ make_persistent(std::size_t N, allocation_flag flag = allocation_flag::none())
6868
sizeof(I) * N, detail::type_num<I>(), flag.value);
6969

7070
if (ptr == nullptr) {
71+
const char *msg = "Failed to allocate persistent memory array";
7172
if (errno == ENOMEM)
72-
throw pmem::transaction_out_of_memory(
73-
"Failed to allocate persistent memory array")
74-
.with_pmemobj_errormsg();
73+
throw detail::exception_with_errormsg<
74+
pmem::transaction_out_of_memory>(msg);
7575
else
76-
throw pmem::transaction_alloc_error(
77-
"Failed to allocate persistent memory array")
78-
.with_pmemobj_errormsg();
76+
throw detail::exception_with_errormsg<
77+
pmem::transaction_alloc_error>(msg);
7978
}
8079

8180
/*
@@ -129,14 +128,13 @@ make_persistent(allocation_flag flag = allocation_flag::none())
129128
sizeof(I) * N, detail::type_num<I>(), flag.value);
130129

131130
if (ptr == nullptr) {
131+
const char *msg = "Failed to allocate persistent memory array";
132132
if (errno == ENOMEM)
133-
throw pmem::transaction_out_of_memory(
134-
"Failed to allocate persistent memory array")
135-
.with_pmemobj_errormsg();
133+
throw detail::exception_with_errormsg<
134+
pmem::transaction_out_of_memory>(msg);
136135
else
137-
throw pmem::transaction_alloc_error(
138-
"Failed to allocate persistent memory array")
139-
.with_pmemobj_errormsg();
136+
throw detail::exception_with_errormsg<
137+
pmem::transaction_alloc_error>(msg);
140138
}
141139

142140
/*
@@ -201,9 +199,9 @@ delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
201199
data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
202200

203201
if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
204-
throw pmem::transaction_free_error(
205-
"failed to delete persistent memory object")
206-
.with_pmemobj_errormsg();
202+
throw detail::exception_with_errormsg<
203+
pmem::transaction_free_error>(
204+
"failed to delete persistent memory object");
207205
}
208206

209207
/**
@@ -248,9 +246,9 @@ delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
248246
data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
249247

250248
if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
251-
throw pmem::transaction_free_error(
252-
"failed to delete persistent memory object")
253-
.with_pmemobj_errormsg();
249+
throw detail::exception_with_errormsg<
250+
pmem::transaction_free_error>(
251+
"failed to delete persistent memory object");
254252
}
255253

256254
} /* namespace obj */

0 commit comments

Comments
 (0)