Skip to content

Commit

Permalink
Refactor to reduce code duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Quincey Koziol <[email protected]>
  • Loading branch information
qkoziol committed Apr 9, 2024
1 parent 414f4e3 commit 70701f3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 88 deletions.
71 changes: 13 additions & 58 deletions src/H5E.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ H5E__register_class(const char *cls_name, const char *lib_name, const char *vers
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");

/* Duplicate string information */
if (NULL == (cls->cls_name = H5MM_xstrdup(cls_name)))
if (NULL == (cls->cls_name = strdup(cls_name)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
if (NULL == (cls->lib_name = H5MM_xstrdup(lib_name)))
if (NULL == (cls->lib_name = strdup(lib_name)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
if (NULL == (cls->lib_vers = H5MM_xstrdup(version)))
if (NULL == (cls->lib_vers = strdup(version)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");

/* Set the return value */
Expand Down Expand Up @@ -737,7 +737,7 @@ H5E__create_msg(H5E_cls_t *cls, H5E_type_t msg_type, const char *msg_str)
/* Fill new message object */
msg->cls = cls;
msg->type = msg_type;
if (NULL == (msg->msg = H5MM_xstrdup(msg_str)))
if (NULL == (msg->msg = strdup(msg_str)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");

/* Set return value */
Expand Down Expand Up @@ -886,24 +886,9 @@ H5E__get_current_stack(void)
current_error = &(current_stack->slot[u]);
new_error = &(estack_copy->slot[u]);

/* Increment the IDs to indicate that they are used in this stack */
if (H5I_inc_ref(current_error->cls_id, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, NULL, "unable to increment ref count on error class");
new_error->cls_id = current_error->cls_id;
if (H5I_inc_ref(current_error->maj_num, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, NULL, "unable to increment ref count on error message");
new_error->maj_num = current_error->maj_num;
if (H5I_inc_ref(current_error->min_num, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, NULL, "unable to increment ref count on error message");
new_error->min_num = current_error->min_num;
/* The 'func' & 'file' strings are statically allocated (by the compiler)
* there's no need to duplicate them.
*/
new_error->func_name = current_error->func_name;
new_error->file_name = current_error->file_name;
new_error->line = current_error->line;
if (NULL == (new_error->desc = H5MM_xstrdup(current_error->desc)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
/* Set error stack entry */
if (H5E__set_stack_entry(new_error, current_error->file_name, current_error->func_name, current_error->line, current_error->cls_id, current_error->maj_num, current_error->min_num, current_error->desc) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTSET, NULL, "can't set error entry");
} /* end for */

/* Copy the "automatic" error reporting information */
Expand Down Expand Up @@ -1000,24 +985,9 @@ H5E__set_current_stack(H5E_t *estack)
current_error = &(current_stack->slot[u]);
new_error = &(estack->slot[u]);

/* Increment the IDs to indicate that they are used in this stack */
if (H5I_inc_ref(new_error->cls_id, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, FAIL, "unable to increment ref count on error class");
current_error->cls_id = new_error->cls_id;
if (H5I_inc_ref(new_error->maj_num, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, FAIL, "unable to increment ref count on error class");
current_error->maj_num = new_error->maj_num;
if (H5I_inc_ref(new_error->min_num, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, FAIL, "unable to increment ref count on error class");
current_error->min_num = new_error->min_num;
/* The 'func' & 'file' strings are statically allocated (by the compiler)
* there's no need to duplicate them.
*/
current_error->func_name = new_error->func_name;
current_error->file_name = new_error->file_name;
current_error->line = new_error->line;
if (NULL == (current_error->desc = H5MM_xstrdup(new_error->desc)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
/* Set error stack entry */
if (H5E__set_stack_entry(current_error, new_error->file_name, new_error->func_name, new_error->line, new_error->cls_id, new_error->maj_num, new_error->min_num, new_error->desc) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTSET, FAIL, "can't set error entry");
} /* end for */

done:
Expand Down Expand Up @@ -1649,24 +1619,9 @@ H5E__append_stack(H5E_t *dst_stack, const H5E_t *src_stack)
src_error = &(src_stack->slot[u]);
dst_error = &(dst_stack->slot[dst_stack->nused]);

/* Increment the IDs to indicate that they are used in this stack */
if (H5I_inc_ref(src_error->cls_id, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, FAIL, "unable to increment ref count on error class");
dst_error->cls_id = src_error->cls_id;
if (H5I_inc_ref(src_error->maj_num, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, FAIL, "unable to increment ref count on error message");
dst_error->maj_num = src_error->maj_num;
if (H5I_inc_ref(src_error->min_num, false) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINC, FAIL, "unable to increment ref count on error message");
dst_error->min_num = src_error->min_num;
/* The 'func' & 'file' strings are statically allocated (by the compiler)
* there's no need to duplicate them.
*/
dst_error->func_name = src_error->func_name;
dst_error->file_name = src_error->file_name;
dst_error->line = src_error->line;
if (NULL == (dst_error->desc = H5MM_xstrdup(src_error->desc)))
HGOTO_ERROR(H5E_ERROR, H5E_CANTALLOC, FAIL, "memory allocation failed");
/* Set error stack entry */
if (H5E__set_stack_entry(dst_error, src_error->file_name, src_error->func_name, src_error->line, src_error->cls_id, src_error->maj_num, src_error->min_num, src_error->desc) < 0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTSET, FAIL, "can't set error entry");

/* Increment # of errors in destination stack */
dst_stack->nused++;
Expand Down
95 changes: 65 additions & 30 deletions src/H5Eint.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,49 @@ H5E__push_stack(H5E_t *estack, const char *file, const char *func, unsigned line
if (NULL == (estack = H5E__get_my_stack()))
HGOTO_DONE(FAIL);

/*
* Push the error if there's room. Otherwise just forget it.
*/
if (estack->nused < H5E_NSLOTS) {
if (H5E__set_stack_entry(&estack->slot[estack->nused], file, func, line, cls_id, maj_id, min_id, desc) < 0)
HGOTO_DONE(FAIL);
estack->nused++;
} /* end if */

done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5E__push_stack() */

/*-------------------------------------------------------------------------
* Function: H5E__set_stack_entry
*
* Purpose: Sets the information for a given stack entry.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5E__set_stack_entry(H5E_error2_t *err_entry, const char *file, const char *func,
unsigned line, hid_t cls_id, hid_t maj_id, hid_t min_id, const char *desc)
{
herr_t ret_value = SUCCEED; /* Return value */

/*
* WARNING: We cannot call HERROR() from within this function or else we
* could enter infinite recursion. Furthermore, we also cannot
* call any other HDF5 macro or function which might call
* HERROR(). HERROR() is called by HRETURN_ERROR() which could
* be called by FUNC_ENTER().
*/
FUNC_ENTER_PACKAGE_NOERR

/* Sanity check */
assert(err_entry);
assert(cls_id > 0);
assert(maj_id > 0);
assert(min_id > 0);

/*
* Don't fail if arguments are bad. Instead, substitute some default
* value.
Expand All @@ -727,40 +770,32 @@ H5E__push_stack(H5E_t *estack, const char *file, const char *func, unsigned line
if (!desc)
desc = "No description given";

/*
* Push the error if there's room. Otherwise just forget it.
*/
assert(estack);

if (estack->nused < H5E_NSLOTS) {
/* Increment the IDs to indicate that they are used in this stack */
/* Note: don't waste time incrementing library internal error IDs */
if (cls_id != H5E_ERR_CLS_g)
if (H5I_inc_ref_noherr(cls_id, false) < 0)
HGOTO_DONE(FAIL);
estack->slot[estack->nused].cls_id = cls_id;
if (maj_id < H5E_first_maj_id_g || maj_id > H5E_last_maj_id_g)
if (H5I_inc_ref_noherr(maj_id, false) < 0)
HGOTO_DONE(FAIL);
estack->slot[estack->nused].maj_num = maj_id;
if (min_id < H5E_first_min_id_g || min_id > H5E_last_min_id_g)
if (H5I_inc_ref_noherr(min_id, false) < 0)
HGOTO_DONE(FAIL);
estack->slot[estack->nused].min_num = min_id;
/* The 'func' & 'file' strings are statically allocated (by the compiler)
* there's no need to duplicate them.
*/
estack->slot[estack->nused].func_name = func;
estack->slot[estack->nused].file_name = file;
estack->slot[estack->nused].line = line;
if (NULL == (estack->slot[estack->nused].desc = strdup(desc)))
/* Increment the IDs to indicate that they are used in this stack */
/* Note: don't waste time incrementing library internal error IDs */
if (cls_id != H5E_ERR_CLS_g)
if (H5I_inc_ref_noherr(cls_id, false) < 0)
HGOTO_DONE(FAIL);
estack->nused++;
} /* end if */
err_entry->cls_id = cls_id;
if (maj_id < H5E_first_maj_id_g || maj_id > H5E_last_maj_id_g)
if (H5I_inc_ref_noherr(maj_id, false) < 0)
HGOTO_DONE(FAIL);
err_entry->maj_num = maj_id;
if (min_id < H5E_first_min_id_g || min_id > H5E_last_min_id_g)
if (H5I_inc_ref_noherr(min_id, false) < 0)
HGOTO_DONE(FAIL);
err_entry->min_num = min_id;
/* The 'func' & 'file' strings are statically allocated (by the compiler)
* there's no need to duplicate them.
*/
err_entry->func_name = func;
err_entry->file_name = file;
err_entry->line = line;
if (NULL == (err_entry->desc = strdup(desc)))
HGOTO_DONE(FAIL);

done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5E__push_stack() */
} /* end H5E__set_stack_entry() */

/*-------------------------------------------------------------------------
* Function: H5E__clear_entries
Expand Down
2 changes: 2 additions & 0 deletions src/H5Epkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ H5_DLL herr_t H5E__term_deprec_interface(void);
H5_DLL void H5E__set_default_auto(H5E_t *stk);
H5_DLL herr_t H5E__push_stack(H5E_t *estack, const char *file, const char *func, unsigned line, hid_t cls_id,
hid_t maj_id, hid_t min_id, const char *desc);
H5_DLL herr_t H5E__set_stack_entry(H5E_error2_t *err_entry, const char *file, const char *func,
unsigned line, hid_t cls_id, hid_t maj_id, hid_t min_id, const char *desc);
H5_DLL ssize_t H5E__get_msg(const H5E_msg_t *msg_ptr, H5E_type_t *type, char *msg, size_t size);
H5_DLL herr_t H5E__print(const H5E_t *estack, FILE *stream, bool bk_compat);
H5_DLL herr_t H5E__walk(const H5E_t *estack, H5E_direction_t direction, const H5E_walk_op_t *op,
Expand Down

0 comments on commit 70701f3

Please sign in to comment.