From b83414016ed6bd2e8c9a498385feeac3263eb7a9 Mon Sep 17 00:00:00 2001 From: ellson Date: Tue, 18 Oct 2005 18:38:54 +0000 Subject: [PATCH] add new files: logic.h, arith.h --- lib/common/arith.h | 72 ++++++++++++++++++++++++++++++++++++++++++++ lib/common/memory.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ lib/common/memory.h | 52 ++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 lib/common/arith.h create mode 100644 lib/common/memory.c create mode 100644 lib/common/memory.h diff --git a/lib/common/arith.h b/lib/common/arith.h new file mode 100644 index 000000000..68f0cc111 --- /dev/null +++ b/lib/common/arith.h @@ -0,0 +1,72 @@ +/* $Id$ $Revision$ */ +/* vim:set shiftwidth=4 ts=8: */ + +/********************************************************** +* This software is part of the graphviz package * +* http://www.graphviz.org/ * +* * +* Copyright (c) 1994-2004 AT&T Corp. * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Corp. * +* * +* Information and Software Systems Research * +* AT&T Research, Florham Park NJ * +**********************************************************/ + +/* geometric functions (e.g. on points and boxes) with application to, but + * no specific dependance on graphs */ + +#ifndef GV_ARITH_H +#define GV_ARITH_H + +#include "logic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef MIN +#undef MIN +#endif +#define MIN(a,b) ((a)<(b)?(a):(b)) + +#ifdef MAX +#undef MAX +#endif +#define MAX(a,b) ((a)>(b)?(a):(b)) + +#ifdef ABS +#undef ABS +#endif +#define ABS(a) ((a) >= 0 ? (a) : -(a)) + +#ifndef MAXINT +#define MAXINT ((int)(~(unsigned)0 >> 1)) +#endif +#ifndef MAXSHORT +#define MAXSHORT (0x7fff) +#endif +#ifndef MAXDOUBLE +#define MAXDOUBLE 1.7976931348623157e+308 +#endif +#ifndef MAXFLOAT +#define MAXFLOAT ((float)3.40282347e+38) +#endif + +#ifdef BETWEEN +#undef BETWEEN +#endif +#define BETWEEN(a,b,c) (((a) <= (b)) && ((b) <= (c))) + +#define ROUND(f) ((f>=0)?(int)(f + .5):(int)(f - .5)) +#define RADIANS(deg) ((deg)/180.0 * PI) +#define DEGREES(rad) ((rad)/PI * 180.0) + +#define SQR(a) ((a) * (a)) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/common/memory.c b/lib/common/memory.c new file mode 100644 index 000000000..b060e2941 --- /dev/null +++ b/lib/common/memory.c @@ -0,0 +1,73 @@ +/* $Id$ $Revision$ */ +/* vim:set shiftwidth=4 ts=8: */ + +/********************************************************** +* This software is part of the graphviz package * +* http://www.graphviz.org/ * +* * +* Copyright (c) 1994-2004 AT&T Corp. * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Corp. * +* * +* Information and Software Systems Research * +* AT&T Research, Florham Park NJ * +**********************************************************/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "memory.h" + +void *zmalloc(size_t nbytes) +{ + char *rv = malloc(nbytes); + if (nbytes == 0) + return 0; + if (rv == NULL) { + fprintf(stderr, "out of memory\n"); + abort(); + } + memset(rv, 0, nbytes); + return rv; +} + +void *zrealloc(void *ptr, size_t size, size_t elt, size_t osize) +{ + void *p = realloc(ptr, size * elt); + if (p == NULL && size) { + fprintf(stderr, "out of memory\n"); + abort(); + } + if (osize < size) + memset((char *) p + (osize * elt), '\0', (size - osize) * elt); + return p; +} + +void *gmalloc(size_t nbytes) +{ + char *rv; + if (nbytes == 0) + return (char *)1; /* NB Return an invalid pointer - since nobody seems to check for NULL */ + rv = malloc(nbytes); + if (rv == NULL) { + fprintf(stderr, "out of memory\n"); + abort(); + } + return rv; +} + +void *grealloc(void *ptr, size_t size) +{ + void *p = realloc(ptr, size); + if (p == NULL && size) { + fprintf(stderr, "out of memory\n"); + abort(); + } + return p; +} diff --git a/lib/common/memory.h b/lib/common/memory.h new file mode 100644 index 000000000..cfd771761 --- /dev/null +++ b/lib/common/memory.h @@ -0,0 +1,52 @@ +/* $Id$ $Revision$ */ +/* vim:set shiftwidth=4 ts=8: */ + +/********************************************************** +* This software is part of the graphviz package * +* http://www.graphviz.org/ * +* * +* Copyright (c) 1994-2004 AT&T Corp. * +* and is licensed under the * +* Common Public License, Version 1.0 * +* by AT&T Corp. * +* * +* Information and Software Systems Research * +* AT&T Research, Florham Park NJ * +**********************************************************/ + +#ifndef GV_MEMORY_H +#define GV_MEMORY_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DMALLOC +#define NEW(t) (t*)calloc(1,sizeof(t)) +#define N_NEW(n,t) (t*)calloc((n),sizeof(t)) +#define GNEW(t) (t*)malloc(sizeof(t)) +#define N_GNEW(n,t) (t*)malloc((n)*sizeof(t)) +#define ALLOC(size,ptr,type) (ptr? (type*)realloc(ptr,(size)*sizeof(type)):(type*)malloc((size)*sizeof(type))) +#define RALLOC(size,ptr,type) ((type*)realloc(ptr,(size)*sizeof(type))) +#define ZALLOC(size,ptr,type,osize) (ptr? (type*)recalloc(ptr,(size)*sizeof(type)):(type*)calloc((size),sizeof(type))) +#else +#define NEW(t) (t*)zmalloc(sizeof(t)) +#define N_NEW(n,t) (t*)zmalloc((n)*sizeof(t)) +#define GNEW(t) (t*)gmalloc(sizeof(t)) +#define N_GNEW(n,t) (t*)gmalloc((n)*sizeof(t)) +#define ALLOC(size,ptr,type) (ptr? (type*)grealloc(ptr,(size)*sizeof(type)):(type*)gmalloc((size)*sizeof(type))) +#define RALLOC(size,ptr,type) ((type*)grealloc(ptr,(size)*sizeof(type))) +#define ZALLOC(size,ptr,type,osize) (ptr? (type*)zrealloc(ptr,size,sizeof(type),osize):(type*)zmalloc((size)*sizeof(type))) +#endif + + + extern void *zmalloc(size_t); + extern void *zrealloc(void *, size_t, size_t, size_t); + extern void *gmalloc(size_t); + extern void *grealloc(void *, size_t); + +#ifdef __cplusplus +} +#endif + +#endif -- 2.40.0