diff options
author | Quincey Koziol <koziol@hdfgroup.org> | 2003-12-11 18:26:56 (GMT) |
---|---|---|
committer | Quincey Koziol <koziol@hdfgroup.org> | 2003-12-11 18:26:56 (GMT) |
commit | cf591231155821ab65fdb7dfe6fdf23a7af0f0b4 (patch) | |
tree | b822cd66d8613e462642cc7b1d410d03bd6c8357 /src | |
parent | 69d2e3fd11e6b6530aa4044eae17ce8293b560fd (diff) | |
download | hdf5-cf591231155821ab65fdb7dfe6fdf23a7af0f0b4.zip hdf5-cf591231155821ab65fdb7dfe6fdf23a7af0f0b4.tar.gz hdf5-cf591231155821ab65fdb7dfe6fdf23a7af0f0b4.tar.bz2 |
[svn-r7933] Purpose:
Add new feature
Description:
Add new H5I{dec|get|inc}_ref() routines and tests for them.
Platforms tested:
FreeBSD 4.9 (sleipnir)
h5committest
Diffstat (limited to 'src')
-rw-r--r-- | src/H5I.c | 237 | ||||
-rw-r--r-- | src/H5Ipublic.h | 9 |
2 files changed, 212 insertions, 34 deletions
@@ -117,6 +117,7 @@ H5FL_DEFINE_STATIC(H5I_id_info_t); /*--------------------- Local function prototypes ---------------------------*/ static herr_t H5I_init_interface(void); static H5I_id_info_t *H5I_find_id(hid_t id); +static int H5I_get_ref(hid_t id); #ifdef H5I_DEBUG_OUTPUT static herr_t H5I_debug(H5I_type_t grp); #endif /* H5I_DEBUG_OUTPUT */ @@ -866,6 +867,44 @@ done: /*------------------------------------------------------------------------- + * Function: H5Idec_ref + * + * Purpose: Decrements the number of references outstanding for an ID. + * If the reference count for an ID reaches zero, the object + * will be closed. + * + * Return: Success: New reference count + * Failure: Negative + * + * Programmer: Quincey Koziol + * Dec 7, 2003 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +int +H5Idec_ref(hid_t id) +{ + int ret_value; /* Return value */ + + FUNC_ENTER_API(H5Idec_ref, FAIL); + H5TRACE1("Is","i",id); + + /* Check arguments */ + if (id<0) + HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "invalid ID"); + + /* Do actual decrement operation */ + if((ret_value = H5I_dec_ref(id))<0) + HGOTO_ERROR (H5E_ATOM, H5E_CANTDEC, FAIL, "can't decrement ID ref count"); + +done: + FUNC_LEAVE_API(ret_value); +} /* end H5Idec_ref() */ + + +/*------------------------------------------------------------------------- * Function: H5I_dec_ref * * Purpose: Decrements the number of references outstanding for an ID. @@ -906,41 +945,47 @@ done: int H5I_dec_ref(hid_t id) { - H5I_type_t grp = H5I_GROUP(id); /*group the object is in*/ - H5I_id_group_t *grp_ptr = NULL; /*ptr to the group */ - H5I_id_info_t *id_ptr = NULL; /*ptr to the new ID */ - int ret_value=FAIL; /*return value */ + H5I_type_t grp; /*group the object is in*/ + H5I_id_group_t *grp_ptr; /*ptr to the group */ + H5I_id_info_t *id_ptr; /*ptr to the new ID */ + int ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5I_dec_ref, FAIL); + /* Sanity check */ + assert(id>=0); + /* Check arguments */ + grp = H5I_GROUP(id); + if (grp <= H5I_BADID || grp >= H5I_NGROUPS) + HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "invalid group number"); grp_ptr = H5I_id_group_list_g[grp]; if (grp_ptr == NULL || grp_ptr->count <= 0) HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, FAIL, "invalid group number"); /* General lookup of the ID */ - if ((id_ptr=H5I_find_id(id))) { - /* - * If this is the last reference to the object then invoke the group's - * free method on the object. If the free method is undefined or - * successful then remove the object from the group; otherwise leave - * the object in the group without decrementing the reference - * count. If the reference count is more than one then decrement the - * reference count without calling the free method. - * - * Beware: the free method may call other H5I functions. - */ - if (1==id_ptr->count) { - if (!grp_ptr->free_func || - (grp_ptr->free_func)(id_ptr->obj_ptr)>=0) { - H5I_remove(id); - ret_value = 0; - } else { - ret_value = FAIL; - } - } else { - ret_value = --(id_ptr->count); - } + if ((id_ptr=H5I_find_id(id))==NULL) + HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "can't locate ID"); + + /* + * If this is the last reference to the object then invoke the group's + * free method on the object. If the free method is undefined or + * successful then remove the object from the group; otherwise leave + * the object in the group without decrementing the reference + * count. If the reference count is more than one then decrement the + * reference count without calling the free method. + * + * Beware: the free method may call other H5I functions. + */ + if (1==id_ptr->count) { + if (!grp_ptr->free_func || (grp_ptr->free_func)(id_ptr->obj_ptr)>=0) { + H5I_remove(id); + ret_value = 0; + } else { + ret_value = FAIL; + } + } else { + ret_value = --(id_ptr->count); } done: @@ -949,6 +994,42 @@ done: /*------------------------------------------------------------------------- + * Function: H5Iinc_ref + * + * Purpose: Increments the number of references outstanding for an ID. + * + * Return: Success: New reference count + * Failure: Negative + * + * Programmer: Quincey Koziol + * Dec 7, 2003 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +int +H5Iinc_ref(hid_t id) +{ + int ret_value; /* Return value */ + + FUNC_ENTER_API(H5Iinc_ref, FAIL); + H5TRACE1("Is","i",id); + + /* Check arguments */ + if (id<0) + HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "invalid ID"); + + /* Do actual increment operation */ + if((ret_value = H5I_inc_ref(id))<0) + HGOTO_ERROR (H5E_ATOM, H5E_CANTINC, FAIL, "can't increment ID ref count"); + +done: + FUNC_LEAVE_API(ret_value); +} /* end H5Iinc_ref() */ + + +/*------------------------------------------------------------------------- * Function: H5I_inc_ref * * Purpose: Increment the reference count for an object. @@ -967,16 +1048,105 @@ done: int H5I_inc_ref(hid_t id) { - H5I_type_t grp = H5I_GROUP(id); /*group the object is in*/ - H5I_id_group_t *grp_ptr = NULL; /*ptr to the group */ - H5I_id_info_t *id_ptr = NULL; /*ptr to the ID */ - int ret_value; /* Return value */ + H5I_type_t grp; /*group the object is in*/ + H5I_id_group_t *grp_ptr; /*ptr to the group */ + H5I_id_info_t *id_ptr; /*ptr to the ID */ + int ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5I_inc_ref, FAIL); + /* Sanity check */ + assert(id>=0); + + /* Check arguments */ + grp = H5I_GROUP(id); + if (grp <= H5I_BADID || grp >= H5I_NGROUPS) + HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "invalid group number"); + grp_ptr = H5I_id_group_list_g[grp]; + if (!grp_ptr || grp_ptr->count<=0) + HGOTO_ERROR(H5E_ATOM, H5E_BADGROUP, FAIL, "invalid group"); + + /* General lookup of the ID */ + if (NULL==(id_ptr=H5I_find_id(id))) + HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "can't locate ID"); + + /* Set return value */ + ret_value=++(id_ptr->count); + +done: + FUNC_LEAVE_NOAPI(ret_value); +} + + +/*------------------------------------------------------------------------- + * Function: H5Iget_ref + * + * Purpose: Retrieves the number of references outstanding for an ID. + * + * Return: Success: Reference count + * Failure: Negative + * + * Programmer: Quincey Koziol + * Dec 7, 2003 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +int +H5Iget_ref(hid_t id) +{ + int ret_value; /* Return value */ + + FUNC_ENTER_API(H5Iget_ref, FAIL); + H5TRACE1("Is","i",id); + /* Check arguments */ if (id<0) HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "invalid ID"); + + /* Do actual retrieve operation */ + if((ret_value = H5I_get_ref(id))<0) + HGOTO_ERROR (H5E_ATOM, H5E_CANTGET, FAIL, "can't get ID ref count"); + +done: + FUNC_LEAVE_API(ret_value); +} /* end H5Iget_ref() */ + + +/*------------------------------------------------------------------------- + * Function: H5I_get_ref + * + * Purpose: Retrieve the reference count for an object. + * + * Return: Success: The reference count. + * + * Failure: Negative + * + * Programmer: Quincey Koziol + * Saturday, Decemeber 6, 2003 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +static int +H5I_get_ref(hid_t id) +{ + H5I_type_t grp; /*group the object is in*/ + H5I_id_group_t *grp_ptr; /*ptr to the group */ + H5I_id_info_t *id_ptr; /*ptr to the ID */ + int ret_value; /* Return value */ + + FUNC_ENTER_NOAPI(H5I_get_ref, FAIL); + + /* Sanity check */ + assert(id>=0); + + /* Check arguments */ + grp = H5I_GROUP(id); + if (grp <= H5I_BADID || grp >= H5I_NGROUPS) + HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "invalid group number"); grp_ptr = H5I_id_group_list_g[grp]; if (!grp_ptr || grp_ptr->count<=0) HGOTO_ERROR(H5E_ATOM, H5E_BADGROUP, FAIL, "invalid group"); @@ -984,14 +1154,13 @@ H5I_inc_ref(hid_t id) /* General lookup of the ID */ if (NULL==(id_ptr=H5I_find_id(id))) HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "can't locate ID"); - id_ptr->count++; /* Set return value */ ret_value=id_ptr->count; done: FUNC_LEAVE_NOAPI(ret_value); -} +} /* end H5I_get_ref() */ /*------------------------------------------------------------------------- @@ -1024,6 +1193,7 @@ H5I_search(H5I_type_t grp, H5I_search_func_t func, void *key) { H5I_id_group_t *grp_ptr = NULL; /*ptr to the group */ H5I_id_info_t *id_ptr = NULL; /*ptr to the new ID */ + H5I_id_info_t *next_id = NULL; /*ptr to the next ID */ unsigned i; /*counter */ void *ret_value = NULL; /*return value */ @@ -1040,9 +1210,10 @@ H5I_search(H5I_type_t grp, H5I_search_func_t func, void *key) for (i=0; i<grp_ptr->hash_size; i++) { id_ptr = grp_ptr->id_list[i]; while (id_ptr) { + next_id= id_ptr->next; /* Protect against ID being deleted in callback */ if ((*func)(id_ptr->obj_ptr, id_ptr->id, key)) HGOTO_DONE(id_ptr->obj_ptr); /*found the item*/ - id_ptr = id_ptr->next; + id_ptr = next_id; } } diff --git a/src/H5Ipublic.h b/src/H5Ipublic.h index ca32b2a..7f67d62 100644 --- a/src/H5Ipublic.h +++ b/src/H5Ipublic.h @@ -27,6 +27,10 @@ * tracing output look better when hid_t values are large numbers. Change the * GROUP_BITS in H5I.c if the MAXID gets larger than 32 (an assertion will * fail otherwise). + * + * When adding groups here, add a section to the 'misc19' test in test/tmisc.c + * to verify that the H5I{inc|dec|get}_ref() routines work correctly with in. + * */ typedef enum { H5I_BADID = (-1), /*invalid Group */ @@ -58,7 +62,10 @@ extern "C" { /* Public API functions */ H5_DLL H5I_type_t H5Iget_type(hid_t id); -H5_DLL ssize_t H5Iget_name(hid_t object_id, char *name/*out*/, size_t size); +H5_DLL ssize_t H5Iget_name(hid_t id, char *name/*out*/, size_t size); +H5_DLL int H5Iinc_ref(hid_t id); +H5_DLL int H5Idec_ref(hid_t id); +H5_DLL int H5Iget_ref(hid_t id); #ifdef __cplusplus } |