Files
xen/tools/libxl/flexarray.c
T
Ian Campbell 4de237a42e libxl: Enable -Wshadow.
It was convenient to invent $(CFLAGS_LIBXL) to do this.

Various renamings to avoid shadowing standard functions:
  - index(3)
  - listen(2)
  - link(2)
  - abort(3)
  - abs(3)

Reduced the scope of some variables to avoid conflicts.

Change to libxc is due to the nested hypercall buf macros in
set_xen_guest_handle (used in libxl) using the same local private vars.

Build tested only.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Campbell <ian.campbell@citrix.com>
2012-09-17 11:17:00 +01:00

118 lines
2.7 KiB
C

/*
* Copyright (C) 2009 Citrix Ltd.
* Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
#include "libxl_internal.h"
#include <stdarg.h>
flexarray_t *flexarray_make(int size, int autogrow)
{
flexarray_t *array = malloc(sizeof(struct flexarray));
if (array) {
array->size = size;
array->autogrow = autogrow;
array->count = 0;
array->data = calloc(size, sizeof(void *));
}
return array;
}
void flexarray_free(flexarray_t *array)
{
free(array->data);
free(array);
}
int flexarray_grow(flexarray_t *array, int extents)
{
void **data;
int newsize;
newsize = array->size + extents;
data = realloc(array->data, sizeof(void *) * newsize);
if (!data)
return 1;
array->size += extents;
array->data = data;
return 0;
}
int flexarray_set(flexarray_t *array, unsigned int idx, void *ptr)
{
if (idx >= array->size) {
int newsize;
if (!array->autogrow)
return 1;
newsize = (array->size * 2 < idx) ? idx + 1 : array->size * 2;
if (flexarray_grow(array, newsize - array->size))
return 2;
}
if ( idx + 1 > array->count )
array->count = idx + 1;
array->data[idx] = ptr;
return 0;
}
int flexarray_append(flexarray_t *array, void *ptr)
{
return flexarray_set(array, array->count, ptr);
}
int flexarray_append_pair(flexarray_t *array, void *ptr1, void *ptr2)
{
int rc = flexarray_append(array, ptr1);
if (!rc)
rc = flexarray_append(array, ptr2);
return rc;
}
int flexarray_vappend(flexarray_t *array, ...)
{
va_list va;
void *ptr;
int ret;
va_start(va, array);
for(ret = 0; (ptr = va_arg(va, void *)); ret++) {
if ( flexarray_append(array, ptr) )
break;
}
va_end(va);
return ret;
}
int flexarray_get(flexarray_t *array, int idx, void **ptr)
{
if (idx >= array->size)
return 1;
*ptr = array->data[idx];
return 0;
}
void **flexarray_contents(flexarray_t *array)
{
void **data;
data = array->data;
free(array);
return data;
}
/*
* Local variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/