/** * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes * and sets *oldCount to newCount. * * @param old address of the pointer to the array * *old may be NULL * @param elementSize the size of the elements of the array * @param oldCount address of the number of elements in the *old array * @param newCount number of elements in the new array, may be 0 * @param filename where in the code was the call to GNUNET_array_grow * @param linenumber where in the code was the call to GNUNET_array_grow */ void GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber) { void *tmp; size_t size; GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber); size = newCount * elementSize; if (size == 0) { tmp = NULL; } else { tmp = GNUNET_xmalloc_ (size, filename, linenumber); memset (tmp, 0, size); /* client code should not rely on this, though... */ if (*oldCount > newCount) *oldCount = newCount; /* shrink is also allowed! */ memcpy (tmp, *old, elementSize * (*oldCount)); } if (*old != NULL) { GNUNET_xfree_ (*old, filename, linenumber); } *old = tmp; *oldCount = newCount; }
/** * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes * and sets *oldCount to newCount. * * @param old address of the pointer to the array * *old may be NULL * @param elementSize the size of the elements of the array * @param oldCount address of the number of elements in the *old array * @param newCount number of elements in the new array, may be 0 * @param filename where in the code was the call to GNUNET_array_grow() * @param linenumber where in the code was the call to GNUNET_array_grow() */ void GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber) { void *tmp; size_t size; GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber); size = newCount * elementSize; if (0 == size) { tmp = NULL; } else { tmp = GNUNET_xmalloc_ (size, filename, linenumber); if (NULL != *old) { GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN(*oldCount, newCount)); } } if (NULL != *old) { GNUNET_xfree_ (*old, filename, linenumber); } *old = tmp; *oldCount = newCount; }
/** * Dup a string (same semantics as strdup). * * @param str the string to dup * @param filename where in the code was the call to GNUNET_strdup * @param linenumber where in the code was the call to GNUNET_strdup * @return strdup(str) */ char * GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber) { char *res; GNUNET_assert_at (str != NULL, filename, linenumber); res = GNUNET_xmalloc_ (strlen (str) + 1, filename, linenumber); memcpy (res, str, strlen (str) + 1); return res; }
/** * Dup partially a string (same semantics as strndup). * * @param str the string to dup * @param len the length of the string to dup * @param filename where in the code was the call to GNUNET_strndup * @param linenumber where in the code was the call to GNUNET_strndup * @return strndup(str,len) */ char * GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber) { char *res; GNUNET_assert_at (str != NULL, filename, linenumber); len = strnlen (str, len); res = GNUNET_xmalloc_ (len + 1, filename, linenumber); memcpy (res, str, len); /* res[len] = '\0'; 'malloc' zeros out anyway */ return res; }
/** * Dup a string (same semantics as strdup). * * @param str the string to dup * @param filename where in the code was the call to GNUNET_strdup() * @param linenumber where in the code was the call to GNUNET_strdup() * @return `strdup(@a str)` */ char * GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber) { char *res; size_t slen; GNUNET_assert_at (str != NULL, filename, linenumber); slen = strlen (str) + 1; res = GNUNET_xmalloc_ (slen, filename, linenumber); GNUNET_memcpy (res, str, slen); return res; }