src/libs/elektra/keyset.c
/**
 * @internal
 *
 * Resize keyset.
 *
 * For internal useage only.
 *
 * Don't use that function to be portable. You can give an hint
 * how large the keyset should be in ksNew().
 *
 * Subsequent is the description of the implementation with array.
 * ksResize() enlarge or shrink the internal array to wished
 * size alloc.
 *
 * If you resize it to n, you can be sure to fill in n-1 elements,
 * the n-th element will do an automatic resize to n*2. So give
 * some spare to avoid wasteful duplication.
 *
 * @param ks the keyset which should be resized
 * @param alloc the size to which the array will be resized
 * @retval 1 on success
 * @retval 0 on nothing done because keyset would be too small.
 * @retval -1 if alloc is smaller then current size of keyset.
 * @retval -1 on memory error or null ptr
 */
@internal
Resize keyset.

For internal useage only.

Don't use that function to be portable. You can give an hint
how large the keyset should be in ksNew().

Subsequent is the description of the implementation with array.
ksResize() enlarge or shrink the internal array to wished
size alloc.

If you resize it to n, you can be sure to fill in n-1 elements,
the n-th element will do an automatic resize to n*2. So give
some spare to avoid wasteful duplication.

@param ks the keyset which should be resized
@param alloc the size to which the array will be resized
@retval 1 on success
@retval 0 on nothing done because keyset would be too small.
@retval -1 if alloc is smaller then current size of keyset.
@retval -1 on memory error or null ptr
@internal Resize keyset . For internal useage only . Don ' t use that function to be portable . You can give an hint how large the keyset should be in ksNew () . Subsequent is the description of the implementation with array . ksResize () enlarge or shrink the internal array to wished size alloc . If you resize it to n you can be sure to fill in n - 1 elements the n - th element will do an automatic resize to n * 2 . So give some spare to avoid wasteful duplication . @param ks the keyset which should be resized @param alloc the size to which the array will be resized @retval 1 on success @retval 0 on nothing done because keyset would be too small . @retval - 1 if alloc is smaller then current size of keyset . @retval - 1 on memory error or null ptr
int ksResize (KeySet * ks, size_t alloc)
{
	if (!ks) return -1;
	alloc++; 
	if (alloc == ks->alloc) return 1;
	if (alloc < ks->size) return 0;
	if (alloc < KEYSET_SIZE)
	{
		if (ks->alloc != KEYSET_SIZE)
			alloc = KEYSET_SIZE;
		else
			return 0;
	}
	if (ks->array == NULL)
	{ 
		ks->alloc = alloc;
		ks->size = 0;
		ks->array = elektraMalloc (sizeof (struct _Key *) * ks->alloc);
		if (!ks->array)
		{
			return -1;
		}
	}
	ks->alloc = alloc;
	if (elektraRealloc ((void **)&ks->array, sizeof (struct _Key *) * ks->alloc) == -1)
	{
		elektraFree (ks->array);
		ks->array = 0;
		return -1;
	}
	return 1;
}
int ksResize ( KeySet * ks , size_t alloc ) { if ( ! ks ) return -1 ; alloc ++ ; if ( alloc == ks -> alloc ) return 1 ; if ( alloc < ks -> size ) return 0 ; if ( alloc < KEYSET_SIZE ) { if ( ks -> alloc != KEYSET_SIZE ) alloc = KEYSET_SIZE ; else return 0 ; } if ( ks -> array == NULL ) { ks -> alloc = alloc ; ks -> size = 0 ; ks -> array = elektraMalloc ( sizeof ( struct _Key * ) * ks -> alloc ) ; if ( ! ks -> array ) { return -1 ; } } ks -> alloc = alloc ; if ( elektraRealloc ( ( void * * ) & ks -> array , sizeof ( struct _Key * ) * ks -> alloc ) == -1 ) { elektraFree ( ks -> array ) ; ks -> array = 0 ; return -1 ; } return 1 ; }
@internal
 Resize keyset.
@internal Resize keyset .
/* for ending null byte */ /* Not allocated up to now */ /*errno = KDB_ERR_NOMEM;*/ /*errno = KDB_ERR_NOMEM;*/