]> granicus.if.org Git - graphviz/commitdiff
add new files: logic.h, arith.h
authorellson <devnull@localhost>
Tue, 18 Oct 2005 18:38:54 +0000 (18:38 +0000)
committerellson <devnull@localhost>
Tue, 18 Oct 2005 18:38:54 +0000 (18:38 +0000)
lib/common/arith.h [new file with mode: 0644]
lib/common/memory.c [new file with mode: 0644]
lib/common/memory.h [new file with mode: 0644]

diff --git a/lib/common/arith.h b/lib/common/arith.h
new file mode 100644 (file)
index 0000000..68f0cc1
--- /dev/null
@@ -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 (file)
index 0000000..b060e29
--- /dev/null
@@ -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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#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 (file)
index 0000000..cfd7717
--- /dev/null
@@ -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