view parray.h @ 109:4efd3873a457

Remove useless bits in _free()
author David Demelier <markand@malikania.fr>
date Tue, 07 Feb 2012 10:11:06 +0100
parents d5ea329ba28e
children bbe86bdb55da
line wrap: on
line source

/*
 * array.h -- manipulate dynamic pointer arrays
 *
 * Copyright (c) 2011, 2012, David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _PARRAY_H_
#define _PARRAY_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdarg.h>

#ifndef PARRAY_DEFAULT_BSIZE
#define PARRAY_DEFAULT_BSIZE	128
#endif

enum parray_type {
	PARRAY_AUTO	= 0,
	PARRAY_FIXED	= 1
};

struct parray {
	enum parray_type type;		/* array type (default FIXED) */
	void		**datas;	/* array of data */
	int		length;		/* number of element inside */
	size_t		size;		/* current buffer size (allocated memory) */
	int		bsize;		/* block size (used when growing array) */
	int		i;		/* only for PARRAY_FOREACH(_R) */

	/* Own allocation functions */
	void * (*malloc)(size_t);
	void * (*realloc)(void *, size_t);
};

typedef void (*parray_map_fn)(void *, void *);
typedef int (*parray_cmp_fn)(void *, void *);

int	parray_init(struct parray *);
void	parray_set(struct parray *, const char *, ...);
int	parray_push(struct parray *, void *);
int	parray_insert(struct parray *, void *, int);
int	parray_append(struct parray *, void *);
void	parray_pop(struct parray *);
void	parray_unqueue(struct parray *);
void	parray_remove(struct parray *, int);
void	parray_unref(struct parray *, const void *);
int	parray_iswap(struct parray *, int, int);
int	parray_pswap(struct parray *, const void *, const void *);
void	parray_map(const struct parray *, parray_map_fn, void *);
int	parray_find(const struct parray *, parray_cmp_fn, void *, void *);
void	parray_clear(struct parray *);
void	parray_free(struct parray *);

#define PARRAY_HEAD(a)								\
	(a->datas[0])
#define PARRAY_TAIL(a)								\
	(a->datas[(a->length == 0) ? 0 : a->length - 1])
#define PARRAY_INDEX(a, i)							\
	(((i) < 0 || (a)->length == 0) ? (PARRAY_HEAD((a)))	/* < 0 head */	\
	    : ((i) >= (a)->length) ? (PARRAY_TAIL((a)))		/* > l tail */	\
	    : ((a)->datas[i]))					/* correct */

#define PARRAY_FOREACH_R(a, var)						\
	for ((a)->i = 0, var = PARRAY_TAIL((a));				\
	    (a)->i < (a)->length; ++(a)->i, var = PARRAY_INDEX((a), (a)->i))

#define PARRAY_FOREACH(a, var)							\
	for ((a)->i = 0, var = PARRAY_INDEX((a), (a)->i);			\
	    (a)->i < (a)->length; ++(a)->i, var = PARRAY_INDEX((a), (a)->i))

/* Only for PARRAY_FIXED */
#define PARRAY_FULL(a)								\
	((a)->length == (a)->bsize)

#define PARRAY_FLUSH(a)								\
do {										\
	void *i;								\
										\
	PARRAY_FOREACH_R(a, i) {						\
		free(i);							\
		parray_unqueue((a));						\
	}									\
										\
	(a)->length = 0;							\
} while (/* CONSTCOND */ 0);

#ifdef __cplusplus
}
#endif

#endif /* _PARRAY_H_ */