1233294Sstas/* $NetBSD: defs.h,v 1.4 2006/04/17 16:44:42 thorpej Exp $ */
2233294Sstas
3233294Sstas/*
490926Snectar * Copyright (c) 1999-2005 Alistair Crooks.  All rights reserved.
5233294Sstas *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8120945Snectar * are met:
9233294Sstas * 1. Redistributions of source code must retain the above copyright
10233294Sstas *    notice, this list of conditions and the following disclaimer.
11120945Snectar * 2. Redistributions in binary form must reproduce the above copyright
12233294Sstas *    notice, this list of conditions and the following disclaimer in the
13233294Sstas *    documentation and/or other materials provided with the distribution.
14233294Sstas * 3. The name of the author may not be used to endorse or promote
15120945Snectar *    products derived from this software without specific prior written
16233294Sstas *    permission.
17233294Sstas *
18233294Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19120945Snectar * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20233294Sstas * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22233294Sstas * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24233294Sstas * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25233294Sstas * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26233294Sstas * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27233294Sstas * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28233294Sstas * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29233294Sstas */
30233294Sstas#ifndef DEFS_H_
31120945Snectar#define DEFS_H_
32233294Sstas
33233294Sstas#include "config.h"
34120945Snectar
3590926Snectar#include <sys/types.h>
3690926Snectar#include <sys/param.h>
3790926Snectar
3890926Snectar#ifdef HAVE_STDINT_H
39233294Sstas#include <stdint.h>
4090926Snectar#endif
4190926Snectar
4290926Snectar#include <stdio.h>
4390926Snectar#include <stdlib.h>
4490926Snectar#include <string.h>
4590926Snectar
4690926Snectar#define NEWARRAY(type,ptr,size,where,action) do {			\
4790926Snectar	if ((ptr = (type *) calloc(sizeof(type), (unsigned)(size))) == NULL) { \
4890926Snectar		(void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \
4990926Snectar			where, (unsigned long)(size * sizeof(type)));	\
5090926Snectar		action;							\
5190926Snectar	}								\
5290926Snectar} while( /* CONSTCOND */ 0)
53102644Snectar
54233294Sstas#define RENEW(type,ptr,size,where,action) do {				\
5590926Snectar	type *_newptr;							\
56120945Snectar	if ((_newptr = (type *) realloc(ptr, sizeof(type) * (size))) == NULL) { \
57120945Snectar		(void) fprintf(stderr, "%s: can't realloc %lu bytes\n",	\
58120945Snectar			where, (unsigned long)(size * sizeof(type)));	\
5990926Snectar		action;							\
60102644Snectar	} else {							\
61233294Sstas		ptr = _newptr;						\
6290926Snectar	}								\
6390926Snectar} while( /* CONSTCOND */ 0)
6490926Snectar
6590926Snectar#define NEW(type, ptr, where, action)	NEWARRAY(type, ptr, 1, where, action)
66102644Snectar
67102644Snectar#define FREE(ptr)	(void) free(ptr)
6890926Snectar
6990926Snectar#define ALLOC(type, v, size, c, init, incr, where, action) do {		\
70102644Snectar	uint32_t	_newsize = size;				\
71233294Sstas	if (size == 0) {						\
7290926Snectar		_newsize = init;					\
7390926Snectar		NEWARRAY(type, v, _newsize, where ": new", action);	\
74120945Snectar	} else if (c == size) {						\
75120945Snectar		_newsize = size + incr;					\
76120945Snectar		RENEW(type, v, _newsize, where ": renew", action);	\
77120945Snectar	}								\
7890926Snectar	size = _newsize;						\
7990926Snectar} while( /* CONSTCOND */ 0)
80102644Snectar
81233294Sstas#define USE_ARG(x)  /*LINTED*/(void)&(x)
8290926Snectar
8390926Snectar#define DEFINE_ARRAY(name, type)					\
8490926Snectartypedef struct name {							\
85178825Sdfr	uint32_t	c;						\
8690926Snectar	uint32_t	size;						\
87102644Snectar	type	       *v;						\
88233294Sstas} name
8990926Snectar
9090926Snectar#ifndef ABS
9190926Snectar#define ABS(a)		(((a) < 0) ? -(a) : (a))
9290926Snectar#endif
9390926Snectar
9490926Snectar#endif /* !DEFS_H_ */
9590926Snectar