NSAPI_PUBLIC int PListDefProp(PList_t plist, int pindex, const char *pname, const int flags) { PListStruct_t *pl = (PListStruct_t *)plist; PLValueStruct_t *pv; if (!plist) return ERRPLUNDEF; /* Is pindex specified? */ if (pindex > 0) { /* Yes, is it in the reserved range? */ if (flags != PLFLG_IGN_RES && pindex > pl->pl_resvpi) { /* No, error */ return ERRPLINVPI; } PLValueStruct_t **ppval = (PLValueStruct_t **)(pl->pl_ppval); if (ppval[pindex - 1]) { /* Error - property already exists at specified index */ return ERRPLEXIST; } } else { /* Look for a free property index */ pindex = PListGetFreeIndex(pl); if (pindex < 1) { /* Error - no free property index */ return pindex; } } /* We have a property index. Create a new property value */ pv = (PLValueStruct_t *)pool_calloc(pl->pl_mempool, 1, sizeof(PLValueStruct_t)); if (!pv) { /* Error - insufficient memory */ return ERRPLNOMEM; } PLValueStruct_t **ppval = (PLValueStruct_t **)(pl->pl_ppval); pv->pv_pbentry.param = &pv->pv_pbparam; pv->pv_pi = pindex; pv->pv_mempool = pl->pl_mempool; ppval[pindex - 1] = pv; /* Name the property if the name was specified */ if (pname) { /* XXX Maybe should delete property if naming fails */ return PListNameProp(plist, pindex, pname); } /* Return the property index of the new property */ return pindex; }
NSAPI_PUBLIC int PListDefProp(PList_t plist, int pindex, const char *pname, const int flags) { PListStruct_t *pl = (PListStruct_t *)plist; PLValueStruct_t **ppval; PLValueStruct_t *pv; int cursize; int i; int wrapped; if (!plist) return ERRPLUNDEF; ppval = (PLValueStruct_t **)(pl->pl_ppval); /* Is pindex specified? */ if (pindex > 0) { /* Yes, is it in the reserved range? */ if (flags != PLFLG_IGN_RES && pindex > pl->pl_resvpi) { /* No, error */ return ERRPLINVPI; } i = pindex - 1; if (ppval[i]) { /* Error - property already exists at specified index */ return ERRPLEXIST; } } else { /* * Look for a free property index, starting at pl_lastpi + 1. * (Note that i is the property index - 1) */ for (wrapped = 0, i = pl->pl_lastpi; ;) { /* Are we in an initialized part of the array? */ if (i < pl->pl_initpi) { /* Yes, use this index if it's free */ if (ppval[i] == 0) break; /* Otherwise step to the next one */ ++i; } else { /* Have we reached the end yet? */ if (i < pl->pl_cursize) { /* * We are above the highest initialized index, but * still within the allocated size. An index in * this range can be used with no further checks. */ ppval[i] = 0; } else { /* * It's looking like time to grow the array, but * first go back and look for an unused, unreserved * index that might have been freed. */ if (!wrapped) { i = pl->pl_resvpi; wrapped = 1; continue; } /* * Grow the array unless there is a specified maximum * size and we've reached it. */ i = pl->pl_cursize; if (pl->pl_maxprop && (i > pl->pl_maxprop)) { /* Error - property list is full */ return ERRPLFULL; } /* Increase planned size of list */ cursize = i + PLIST_DEFGROW; /* Reallocate the array of property value pointers */ ppval = (PLValueStruct_t **)pool_realloc(pl->pl_mempool, (void *)ppval, (cursize * sizeof(PLValueStruct_t *))); if (!ppval) { /* Error - insufficient memory */ return ERRPLNOMEM; } /* Initialize the first new entry and select it */ ppval[i] = NULL; pl->pl_ppval = (pb_entry **)ppval; pl->pl_cursize = cursize; } /* Update the highest initialized index value */ pl->pl_initpi = i + 1; break; } } /* Set the starting point for the next allocation */ pl->pl_lastpi = i + 1; } /* We have a property index (i + 1). Create a new property value */ pv = (PLValueStruct_t *)pool_calloc(pl->pl_mempool, 1, sizeof(PLValueStruct_t)); if (!pv) { /* Error - insufficient memory */ return ERRPLNOMEM; } pv->pv_pbentry.param = &pv->pv_pbparam; pv->pv_pi = i + 1; ppval[i] = pv; /* Name the property if the name was specified */ if (pname) { /* XXX Maybe should delete property if naming fails */ return PListNameProp(plist, i + 1, pname); } /* Return the property index of the new property */ return i + 1; }