Example #1
0
_STACK *sk_deep_copy(const _STACK *sk, void *(*copy_func)(void *),
                     void (*free_func)(void *)) {
  _STACK *ret = sk_dup(sk);
  if (ret == NULL) {
    return NULL;
  }

  for (size_t i = 0; i < ret->num; i++) {
    if (ret->data[i] == NULL) {
      continue;
    }
    ret->data[i] = copy_func(ret->data[i]);
    if (ret->data[i] == NULL) {
      for (size_t j = 0; j < i; j++) {
        if (ret->data[j] != NULL) {
          free_func(ret->data[j]);
        }
      }
      sk_free(ret);
      return NULL;
    }
  }

  return ret;
}
Example #2
0
/*
 * Post 1.0.1 sk function "deep_copy".  For the moment we simply make
 * these take void * and use them directly without a glorious blob of
 * obfuscating macros of dubious value in front of them. All this in
 * preparation for a rototilling of safestack.h (likely inspired by
 * this).
 */
static void *
sk_deep_copy(void *sk_void, void *copy_func_void, void *free_func_void)
{
	_STACK *sk = sk_void;
	void *(*copy_func)(void *) = copy_func_void;
	void (*free_func)(void *) = free_func_void;
	_STACK *ret = sk_dup(sk);
	size_t i;

	if (ret == NULL)
		return NULL;

	for (i = 0; i < ret->num; i++) {
		if (ret->data[i] == NULL)
			continue;
		ret->data[i] = copy_func(ret->data[i]);
		if (ret->data[i] == NULL) {
			size_t j;
			for (j = 0; j < i; j++) {
				if (ret->data[j] != NULL)
					free_func(ret->data[j]);
			}
			sk_free(ret);
			return NULL;
		}
	}

	return ret;
}