hdf初始化

NEOERR* hdf_init (HDF **hdf) 
{
NEOERR *err;
HDF *my_hdf;
*hdf = NULL;
err = nerr_init();
if (err != STATUS_OK)
return nerr_pass (err);
err = _alloc_hdf (&my_hdf, NULL, 0, NULL, 0, 0, NULL);
if (err != STATUS_OK)
return nerr_pass (err);
my_hdf->top = my_hdf;
*hdf = my_hdf;
return STATUS_OK;
}

下面是为hdf初始化的实际工作

首先分配一个HDF结构,然后设定此节点的top,使其指向这个树的顶端节点。 如果相应的name字段不为NULL的话,就分配空间来存储他。 
对于value字段采用这样的策略,如果说用户希望cs引擎分配空间来存储相应 value的副本,那么会设置dup为真,这样引擎分配相应的空间并且负责资源的释放 (通过设置alloc_value=1),如果dup为假的话,那么就按照用户说的去做吧 ,这样用户自己负责资源的分配和释放。 

一个HDF节点完成初始化后,如果说这个节点是作为整个树的 顶端节点,那么我们不需要设定其name,value值,所以你会注意到hdf_init中 相应的_alloc_hdf中相应参数(name,value)都为NULL。

static NEOERR *_alloc_hdf (HDF **hdf, const char *name, size_t nlen, 
const char *value, int dup, int wf, HDF *top)
{
*hdf = calloc (1, sizeof (HDF));
if (*hdf == NULL)
{
return nerr_raise (NERR_NOMEM, "Unable to allocate memory for hdf element");
}
(*hdf)->top = top;
if (name != NULL)
{
(*hdf)->name_len = nlen;
(*hdf)->name = (char *) malloc (nlen + 1);
if ((*hdf)->name == NULL)
{
free((*hdf));
(*hdf) = NULL;
return nerr_raise (NERR_NOMEM,
"Unable to allocate memory for hdf element: %s", name);
}
strncpy((*hdf)->name, name, nlen);
(*hdf)->name[nlen] = '\0';
}
if (value != NULL)
{
if (dup)
{
(*hdf)->alloc_value = 1;
(*hdf)->value = strdup(value);
if ((*hdf)->value == NULL)
{
free((*hdf)->name);
free((*hdf));
(*hdf) = NULL;
return nerr_raise (NERR_NOMEM,
"Unable to allocate memory for hdf element %s", name);
}
}
else
{
(*hdf)->alloc_value = wf;
/* We're overriding the const of value here for the set_buf case
* where we overrode the char * to const char * earlier, since
* alloc_value actually keeps track of the const-ness for us
*/
(*hdf)->value = (char *)value;
}
}
return STATUS_OK;
}


posted @ 2012-02-29 00:19  Lesterwang  阅读(395)  评论(0编辑  收藏  举报