home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-12-23 | 39.3 KB | 1,411 lines |
- Newsgroups: alt.sources
- Path: sparky!uunet!spool.mu.edu!news.cs.indiana.edu!umn.edu!csus.edu!netcom.com!thinman
- From: thinman@netcom.com (Technically Sweet)
- Subject: COOL: C Object-Oriented Library: part 3 of 4
- Message-ID: <1992Dec23.191553.10523@netcom.com>
- Organization: International Foundation for Internal Freedom
- Date: Wed, 23 Dec 1992 19:15:53 GMT
- Lines: 1401
-
- #!/bin/sh
- # This is part 03 of a multipart archive
- # ============= coolint.h ==============
- if test -f 'coolint.h' -a X"$1" != X"-c"; then
- echo 'x - skipping coolint.h (File already exists)'
- else
- echo 'x - extracting coolint.h (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'coolint.h' &&
- X/*
- X * COOL: C Object-Oriented Library
- X *
- X * Internal data structures
- X *
- X * Copyright 1991 by Lance Norskog
- X *
- X * Permission to use, copy, modify, and distribute this software and its
- X * documentation for any purpose and without fee is hereby granted, provided
- X * that the above copyright notice appear in all copies and that both that
- X * copyright notice and this permission notice appear in supporting
- X * documentation. This software is provided "as is" without express or
- X * implied warranty.
- X */
- X
- X/* Table sizes */
- X#define MAXOBJECTS 300 /* Possible total number of objects */
- X#ifdef i386
- X#define MAXMETHODS 64 /* Possible number of methods classes */
- X#else
- X#define MAXMETHODS 32 /* Possible number of methods classes */
- X#endif
- X#define MAXPARENTS 7 /* Possible number of parent classes */
- X#define MYMETHODS MAXPARENTS /* List of class's methods */
- X#define ALLCLASSES (MAXPARENTS+1) /* Total set of class method lists */
- X#define MAXCOMP 10 /* Possible number of components */
- X
- X#define MAXARGS 9 /* Total arguments to a method */
- X
- X#define MAXEXCEPTIONS 50 /* Possible size of exception stack */
- X
- Xtypedef unsigned int cint;
- Xtypedef unsigned short sint;
- X
- X/* Table of methods for an object */
- X/* DO i need separate handling for arguments? */
- Xtypedef struct cool_methtab {
- X char *m_name; /* Method Name */
- X ret_t (*m_func)(); /* function handler */
- X char *m_rettype; /* Method return type */
- X char *m_argtypes[MAXARGS]; /* Method argument types */
- X short m_nargs; /* Number of arguments */
- X short m_gap;
- X} methtab_t;
- X
- X/* If type is one of known types, the string is replaced with this code: */
- X#define CVOID ((char *) 1)
- X#define CINT ((char *) 2)
- X#define CBOOL ((char *) 3)
- X#define CCHAR ((char *) 4)
- X#define CSTR ((char *) 5)
- X#define COBJ ((char *) 6)
- X#define CMSG ((char *) 7)
- X#define CDBL ((char *) 8)
- X#define CIVECT ((char *) 9)
- X#define CDVECT ((char *) 10)
- X#define CANY ((char *) 11)
- X/* VR add-ons: */
- X#define CDPOINT ((char *) 12)
- X#define CDHOMOG ((char *) 13)
- X/* table of strings is in lookup.c */
- X
- X/* Table of registered objects */
- Xstruct cool_objtab {
- X char *ob_name; /* Object Name */
- X/* int ob_methods; /* Number of methods */
- X sint ob_next; /* Next in alloc or free list */
- X sint ob_ambiguous; /* Parent conflict counter */
- X sint ob_generation; /* Parent conflict counter */
- X methtab_t *ob_methtab[ALLCLASSES];/* Tables of methods */
- X cool_priv_t *ob_private[ALLCLASSES];/* Private data structures */
- X /* start with a cool_priv_t */
- X object_t ob_components[MAXCOMP]; /* Component list */
- X};
- X
- Xtypedef struct cool_objtab objtab_t;
- Xobjtab_t cool_objtab[MAXOBJECTS];
- Xint cool_numobs; /* bogus */
- Xsint cool_freelist;
- X
- X#ifdef __STDC__
- X#define P(a) a
- X#define P2(a, b) a, b
- X#else
- X#define P(a)
- X#define P2(a, b)
- X#endif
- X
- X/* Malloc front-ends */
- Xchar *cool_malloc(P(int));
- Xvoid cool_free(P(char *));
- Xchar *cool_realloc(P2(char *, int));
- X
- X/* String front-ends */
- Xchar *cool_strdup(P(char *));
- Xvoid cool_strfree(P(char *));
- Xchar *cool_makename();
- Xint cool_streq(P2(char *, char *));
- X
- X#ifdef i386
- X#define RAM_INC 4096 /* table expansion increment */
- X/* Objects are indices into a table */
- X#define MKOBJECT(o,gen) ((object_t) (o | (gen << 9)))
- X#define MAXIMUMGEN 0x3fff
- X#define BADGEN 0xffff
- X#define GENOF(o) (((cint) o >> 9) & MAXIMUMGEN)
- X#define OBJECTOF(o) (((cint) o & 0x1ff))
- X#define ISOBJECT(o) ((cint) o < 0x100000)
- X#define GOODGEN(o) (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
- X/* Object "Class" is object #0 */
- X#define ISCLASS(o) (((cint) o < MAXOBJECTS) && \
- X (cool_objtab[(cint)o].ob_parent == 0))
- X/* Methods are indices into the method table of an object */
- X#define ISMETHOD(m) ((cint) m < 512) /* table & parent bits */
- X
- X#define OBJECTERR ((object_t) -1)
- X#define METHODERR ((method_t) -2)
- X
- X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
- X#define PARENTOF(m) (((cint) m) >> 6)
- X#define METHODOF(m) (((cint) m) & 63)
- X#define MKMETHOD(p,m) ((method_t) ((((cint) p & 7) << 6) | ((cint) m & 63)))
- X#endif
- X
- X/* Nicolas addition */
- X#ifdef sparc
- X#define RAM_INC 4096 /* table expansion increment */
- X/* Objects are indices into a table */
- X#define MKOBJECT(o,gen) ((object_t) (o | (gen << 9)))
- X#define MAXIMUMGEN 0x3fff
- X#define BADGEN 0xffff
- X#define GENOF(o) (((cint) o >> 9) & MAXIMUMGEN)
- X#define OBJECTOF(o) (((cint) o & 0x1ff))
- X#define ISOBJECT(o) ((cint) o < 0x6000)
- X#define GOODGEN(o) (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
- X/* Object "Class" is object #0 */
- X#define ISCLASS(o) (((cint) o < MAXOBJECTS) && \
- X (cool_objtab[(cint)o].ob_parent == 0))
- X/* Methods are indices into the method table of an object */
- X#define ISMETHOD(m) ((cint) m < 256)
- X
- X#define OBJECTERR ((object_t) -1)
- X#define METHODERR ((method_t) -2)
- X
- X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
- X#define PARENTOF(m) (((cint) m) >> 5)
- X#define METHODOF(m) (((cint) m) & 31)
- X#define MKMETHOD(p,m) ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
- X#endif
- X
- X/* nicolas addition */
- X#ifdef mc68020
- X#define RAM_INC 4096 /* table expansion increment */
- X/* Objects are indices into a table */
- X#define MKOBJECT(o,gen) ((object_t) (o | (gen << 9)))
- X#define MAXIMUMGEN 0x3fff
- X#define BADGEN 0xffff
- X#define GENOF(o) (((cint) o >> 9) & MAXIMUMGEN)
- X#define OBJECTOF(o) (((cint) o & 0x1ff))
- X#define ISOBJECT(o) ((cint) o < 0x6000)
- X#define GOODGEN(o) (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
- X/* Object "Class" is object #0 */
- X#define ISCLASS(o) (((cint) o < MAXOBJECTS) && \
- X (cool_objtab[(cint)o].ob_parent == 0))
- X/* Methods are indices into the method table of an object */
- X#define ISMETHOD(m) ((cint) m < 256)
- X
- X#define OBJECTERR ((object_t) -1)
- X#define METHODERR ((method_t) -2)
- X
- X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
- X#define PARENTOF(m) (((cint) m) >> 5)
- X#define METHODOF(m) (((cint) m) & 31)
- X#define MKMETHOD(p,m) ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
- X#endif
- X
- X/* Henk Davids' version of SPARC support. */
- X#ifdef SUN4
- X#define RAM_INC 4096 /* table expansion increment */
- X/* Objects are indices into a table */
- X/* Use the same method as for VMS (see below) to identify object ID's
- X * Observation: constant strings have addresses from 0x4000 and above;
- X * memory allocated by malloc is above that, but on my machine the last
- X * was at 0x5a43c50 - this obviously will depend on your system, but
- X * I'm fairly confident that the 0x80000000 will not be reached for normal
- X * programs...
- X */
- X#define OBJ_FLAG (0x80000000)
- X#define MKOBJECT(o,gen) ((object_t) (o | (gen << 9) | OBJ_FLAG))
- X#define MAXIMUMGEN 0x3fff
- X#define BADGEN 0xffff
- X#define GENOF(o) (((cint) o >> 9) & MAXIMUMGEN)
- X#define OBJECTOF(o) (((cint) o & 0x1ff))
- X#define ISOBJECT(o) ((cint) (((cint)o & OBJ_FLAG) == OBJ_FLAG))
- X#define GOODGEN(o) (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
- X/* Object "Class" is object #0 */
- X/* Not used either. Need to strip OBJ_FLAG before use? */
- X#define ISCLASS(o) (((cint) o < MAXOBJECTS) && \
- X (cool_objtab[(cint)o].ob_parent == 0))
- X/* Methods are indices into the method table of an object */
- X/* Sun should have no problem here. String pointers assumed to be always
- X higher than the 256 below
- X */
- X#define ISMETHOD(m) ((cint) m < 256)
- X
- X#define OBJECTERR ((object_t) -1)
- X#define METHODERR ((method_t) -2)
- X
- X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
- X#define PARENTOF(m) (((cint) m) >> 5)
- X#define METHODOF(m) (((cint) m) & 31)
- X#define MKMETHOD(p,m) ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
- X#endif /* SUN4 */
- X
- X#ifdef VMS
- X#define RAM_INC 4096 /* table expansion increment */
- X/* Objects are indices into a table */
- X/* On VMS, we could mark objects by adding the OBJ (high) bit. String pointers
- X can then be identified by the macro's below.
- X The original method relies on strings in high memory - that does
- X not work on VMS. Adding the high bit will produce addresses in S0/S1
- X space, which cannot occur in user-mode programs.
- X Alternatives:
- X - we could move the $CHAR_STRINGS_CONSTANTS psect into high memory?
- X */
- X#define OBJ_FLAG (0x80000000)
- X#define MKOBJECT(o,gen) ((object_t) (o | (gen << 9) | OBJ_FLAG))
- X#define MAXIMUMGEN 0x3fff
- X#define BADGEN 0xffff
- X#define GENOF(o) (((cint) o >> 9) & MAXIMUMGEN)
- X#define OBJECTOF(o) (((cint) o & 0x1ff))
- X#define ISOBJECT(o) ((cint) (((cint)o & OBJ_FLAG) == OBJ_FLAG))
- X/* Not used (and incorrect): */
- X/* #define GOODGEN(o) (cool_objtab[OBJOF(o)].ob_generation == GENOF(o)) */
- X/* should be: */
- X#define GOODGEN(o) (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
- X/* Object "Class" is object #0 */
- X/* Not used either. Need to strip OBJ_FLAG before use? */
- X#define ISCLASS(o) (((cint) o < MAXOBJECTS) && \
- X (cool_objtab[(cint)o].ob_parent == 0))
- X/* Methods are indices into the method table of an object */
- X/* VMS should have no problem here. String pointers assumed to be always
- X higher than the 256 below
- X */
- X#define ISMETHOD(m) ((cint) m < 256)
- X
- X#define OBJECTERR ((object_t) -1)
- X#define METHODERR ((method_t) -2)
- X
- X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
- X#define PARENTOF(m) (((cint) m) >> 5)
- X#define METHODOF(m) (((cint) m) & 31)
- X#define MKMETHOD(p,m) ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
- X#endif /* VMS */
- X
- X/* Standard methods for every class. Fixed places for fast ob build/nuke. */
- X/* Create, Destroy, Method, Inherit, Component */
- X#define METHOD_CREATE MKMETHOD(MYMETHODS, 0)
- X#define METHOD_CREATE2 MKMETHOD(MYMETHODS, 1)
- X#define METHOD_DESTROY MKMETHOD(MYMETHODS, 2)
- X#define METHOD_METHOD MKMETHOD(MYMETHODS, 3)
- X#define METHOD_INHERIT MKMETHOD(MYMETHODS, 4)
- X#define METHOD_COMPONENT MKMETHOD(MYMETHODS, 5)
- X#define METHOD_CLONE MKMETHOD(MYMETHODS, 6)
- X#define CLASSMETHODS 7
- X#define METHOD_INIT MKMETHOD(MYMETHODS, 7)
- X#define METHOD_EXIT MKMETHOD(MYMETHODS, 8)
- X#define METHOD_COPY MKMETHOD(MYMETHODS, 9)
- X#define STDMETHODS 10
- X/* More? */
- X
- Xobject_t cool_object(P(object_t)), cool_getobject(P(object_t));
- Xmethod_t cool_method(P2(object_t, method_t));
- Xmethod_t cool_getmethod(P2(method_t, methtab_t *));
- X
- X#if defined(USG) || defined(VMS)
- X#define bcopy(from, to, count) memcpy(to, from, count)
- X#define bzero(to, count) memset(to, 0, count)
- X#endif
- X
- X/* Strangely enough, this is reentrant */
- Xint __cool_parent;
- Xobjtab_t *__cool_obj;
- Xmethtab_t *__cool_mt;
- X
- X/* Dangerous but fast. Don't use until you're fully debugged. */
- X/* type/void, with arguments. Don't call these with strings! */
- X#define coolt(type, o, m) \
- X((type) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
- X __cool_parent = PARENTOF(m), \
- X __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
- X (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1)))
- X
- X#define coolv(o, m) \
- X((void) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
- X __cool_parent = PARENTOF(m), \
- X __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
- X (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1)))
- X
- X#define coolta(type, o, m, arg) \
- X((type) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
- X __cool_parent = PARENTOF(m), \
- X __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
- X (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg)))
- X
- X#define coolta2(type, o, m, arg, arg2) \
- X((type) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
- X __cool_parent = PARENTOF(m), \
- X __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
- X (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg, arg2)))
- X
- X#define coolva(o, m, arg) \
- X((void) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
- X __cool_parent = PARENTOF(m), \
- X __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
- X (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg)))
- X
- X#define coolva2(o, m, arg, arg2) \
- X((void) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
- X __cool_parent = PARENTOF(m), \
- X __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
- X (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg, arg2)))
- X
- X/* Pathetic attempt at speed */
- X#define cool_vars register int __cool_parent; register objtab_t *__cool_obj; register methtab_t *__cool_mt;
- SHAR_EOF
- chmod 0644 coolint.h ||
- echo 'restore of coolint.h failed'
- Wc_c="`wc -c < 'coolint.h'`"
- test 12130 -eq "$Wc_c" ||
- echo 'coolint.h: original size 12130, current size' "$Wc_c"
- fi
- # ============= cool.spec ==============
- if test -f 'cool.spec' -a X"$1" != X"-c"; then
- echo 'x - skipping cool.spec (File already exists)'
- else
- echo 'x - extracting cool.spec (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'cool.spec' &&
- XCopyright 1991 by Lance Norskog
- X
- XThe COOL (C Object-Oriented Library) is a research project to add
- Xdynamic classing to C with no language-mangling.
- X
- XImplementation:
- XThere is an object table.
- XClasses are objects.
- XEvery object has a table of methods.
- XEvery object and every method has a string name.
- XObject names are in the form [a-zA-Z]([a-zA-Z0-9_])*.
- XMethod names are in the form
- X[a-zA-Z][a-zA-Z0-9_]*(:[a-zA-Z][a-zA-Z0-9_](,[a-zA-Z][a-zA-Z0-9_])*)
- XThis syntax is not enforced.
- X
- XThe index in the object table may always be used in place of the object's
- Xname, and a method's index in the method table of an object may always
- Xbe used in place of a method string name. For example, these might be
- Xsynonymous:
- X cool_msg("Point3", (method_t) 4, 5.0, 7.0, 9.0);
- X cool_msg((object_t) 59, "Draw", 5.0, 7.0, 9.0);
- X coolv((object_t) 59, (method_t) 4, 5.0, 7.0, 9.0);
- X
- XThe object and method number spaces are divided.
- XThe object number space is in two parts: object table index and
- Xobject generation number. If you use an object number for
- Xan object which has been destroyed, the object generation number will
- Xbe wrong. The method number space is divided into the parent
- Xclass of the method, and the index into that parent's method table.
- X
- X
- SHAR_EOF
- chmod 0644 cool.spec ||
- echo 'restore of cool.spec failed'
- Wc_c="`wc -c < 'cool.spec'`"
- test 1236 -eq "$Wc_c" ||
- echo 'cool.spec: original size 1236, current size' "$Wc_c"
- fi
- # ============= tst1.c ==============
- if test -f 'tst1.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst1.c (File already exists)'
- else
- echo 'x - extracting tst1.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst1.c' &&
- X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
- X
- X/* Class creation test */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include "cool.h"
- X
- X/* Counter class: local storage for class is an integer. */
- X
- Xvoid increment(ip) int *ip; { (*ip)++; }
- Xvoid decrement(ip) int *ip; { (*ip)--; }
- Xint value(ip) int *ip; { return *ip; }
- Xvoid reset(ip) int *ip; { *ip = 0; }
- X
- Xcounter_init() {
- X /* No parent classes */
- X cool_msg("Class", "Create", "Counter", sizeof(int), NULL);
- X cool_msg("Counter", "Method", "Increment", increment, 0);
- X cool_msg("Counter", "Method", "Decrement", decrement, 0);
- X cool_msg("Counter", "Method", "Value", value, 0);
- X cool_msg("Counter", "Method", "Reset", reset, 0);
- X}
- X
- Xmain() {
- X object_t c;
- X
- X cool_init(); /* Must be called first */
- X counter_init(); /* Set up Counter class */
- X
- X /* Class usage */
- X
- X c = cool_msg("Counter", "Create", NULL);
- X cool_msg(c, "Reset");
- X cool_msg(c, "Increment");
- X cool_msg(c, "Increment");
- X cool_msg(c, "Decrement");
- X printf("This better be one: %d\n", cool(int, (c, "Value", NULL)));
- X cool_msg("Counter", "Destroy", c);
- X}
- X
- X
- SHAR_EOF
- chmod 0644 tst1.c ||
- echo 'restore of tst1.c failed'
- Wc_c="`wc -c < 'tst1.c'`"
- test 1086 -eq "$Wc_c" ||
- echo 'tst1.c: original size 1086, current size' "$Wc_c"
- fi
- # ============= tst2.c ==============
- if test -f 'tst2.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst2.c (File already exists)'
- else
- echo 'x - extracting tst2.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst2.c' &&
- X/* Copyright 1991 by Lance Norskog */
- X
- X/* Class inheritance test */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include "cool.h"
- X
- X/* Number abstract superclass: local storage for class is not used. */
- X/*
- X * Superclass of integer-like things: ints, shorts, 256-degree angles.
- X */
- X
- Xvoid print(junk)
- Xint *junk;
- X{
- X printf("%d", cool(int, (SELFOF(junk), "Get", NULL)));
- X}
- X
- Xvoid scan(junk, numstr)
- Xchar *junk;
- Xchar *numstr;
- X{
- X int local;
- X
- X if (sscanf(numstr, "%d", &local) == 1)
- X cool_msg(SELFOF(junk), "Set", local);
- X else
- X cool_raise("NotANumber", "%s", numstr);
- X}
- X
- Xvoid number_init() {
- X cool_msg("Class", "Create", "Number", sizeof(char), NULL);
- X cool_msg("Number", "Method", "Print", print, 0);
- X cool_msg("Number", "Method", "Scan", scan, 1);
- X}
- X
- X/* Integer class: local storage for class is an integer. */
- X
- Xint get(ip) int *ip; {
- Xreturn *ip;
- X}
- Xvoid set(ip, i2) int *ip, i2; {
- X*ip = i2;
- X}
- Xvoid add(ip, i2) int *ip, i2; {
- X*ip += i2;
- X}
- Xvoid add2(ip, i2, i3) int *ip, i2, i3; {
- X*ip = i2 + i3;
- X}
- X
- Xvoid integer_init() {
- X
- X /* Inherit Abstract Superclass Number */
- X cool_msg("Class", "Create", "Integer", sizeof(int), "Number", NULL);
- X cool_msg("Integer", "Method", "Get", get, 0);
- X cool_msg("Integer", "Method", "Set", set, 1);
- X cool_msg("Integer", "Method", "Add:integer", add, 1);
- X cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
- X}
- X
- Xmain() {
- X object_t i;
- X
- X cool_init(); /* Must be called first */
- X number_init();
- X integer_init();
- X
- X /* Class usage */
- X
- X i = cool_msg("Integer", "Create", NULL);
- X cool_msg(i, "Scan", "15");
- X cool_msg(i, "Add:integer", 20);
- X cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 8);
- X printf("This better be 43: ");
- X cool_msg(i, "Print");
- X printf("\n");
- X cool_msg("Integer", "Destroy", i);
- X}
- X
- X
- SHAR_EOF
- chmod 0644 tst2.c ||
- echo 'restore of tst2.c failed'
- Wc_c="`wc -c < 'tst2.c'`"
- test 1762 -eq "$Wc_c" ||
- echo 'tst2.c: original size 1762, current size' "$Wc_c"
- fi
- # ============= tst3.c ==============
- if test -f 'tst3.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst3.c (File already exists)'
- else
- echo 'x - extracting tst3.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst3.c' &&
- X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
- X
- X/* Class multiple inheritance test */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include "cool.h"
- X
- X/* Number abstract superclass: local storage for class is not used. */
- X/*
- X * Superclass of integer-like things: ints, shorts, 256-degree angles.
- X */
- X
- Xvoid print(junk)
- Xint *junk;
- X{
- X printf("%d", cool(int, (SELFOF(junk), "Get", NULL)));
- X}
- X
- Xvoid scan(junk, numstr)
- Xchar *junk;
- Xchar *numstr;
- X{
- X int local;
- X
- X if (sscanf(numstr, "%d", &local) == 1)
- X cool_msg(SELFOF(junk), "Set", local);
- X else
- X cool_raise("NotANumber", "%s", numstr);
- X}
- X
- Xvoid number_init() {
- X cool_msg("Class", "Create", "Number", sizeof(char), NULL);
- X cool_msg("Number", "Method", "Print", print, 0);
- X cool_msg("Number", "Method", "Scan", scan, 1);
- X}
- X
- X/* Subtraction abstract superclass: local storage for class is not used. */
- X/*
- X * Superclass that does subtract.
- X */
- X
- Xvoid sub(junk, i)
- Xint *junk;
- Xint i;
- X{
- X int local;
- X
- X local = (int) cool_msg(SELFOF(junk), "Get");
- X local -= i;
- X cool_msg(SELFOF(junk), "Set", local);
- X}
- X
- X
- Xvoid subtractor_init() {
- X cool_msg("Class", "Create", "Subtractor", sizeof(char), NULL);
- X cool_msg("Subtractor", "Method", "Subtract:integer", sub, 1);
- X}
- X
- X/* Integer class: local storage for class is an integer. */
- X
- Xint get(ip) int *ip; { return *ip; }
- Xvoid set(ip, i2) int *ip, i2; { *ip = i2; }
- Xvoid add(ip, i2) int *ip, i2; { *ip += i2; }
- Xvoid add2(ip, i2, i3) int *ip, i2, i3; { *ip = i2 + i3; }
- X
- Xvoid integer_init() {
- X
- X /* Inherit Abstract Superclass Number */
- X cool_msg("Class", "Create", "Integer", sizeof(int),
- X "Number", "Subtractor", NULL);
- X cool_msg("Integer", "Method", "Get", get, 0);
- X cool_msg("Integer", "Method", "Set", set, 1);
- X cool_msg("Integer", "Method", "Add:integer", add, 1);
- X cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
- X}
- X
- Xmain() {
- X object_t i;
- X
- X cool_init(); /* Must be called first */
- X number_init();
- X subtractor_init();
- X integer_init();
- X
- X /* Class usage */
- X
- X i = cool_msg("Integer", "Create", NULL);
- X cool_msg(i, "Scan", "15");
- X cool_msg(i, "Add:integer", 20);
- X cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 10);
- X cool_msg(i, "Subtract:integer", 2);
- X printf("This better be 43: ");
- X cool_msg(i, "Print");
- X printf("\n");
- X cool_msg("Integer", "Destroy", i);
- X}
- X
- X
- SHAR_EOF
- chmod 0644 tst3.c ||
- echo 'restore of tst3.c failed'
- Wc_c="`wc -c < 'tst3.c'`"
- test 2298 -eq "$Wc_c" ||
- echo 'tst3.c: original size 2298, current size' "$Wc_c"
- fi
- # ============= tst4.c ==============
- if test -f 'tst4.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst4.c (File already exists)'
- else
- echo 'x - extracting tst4.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst4.c' &&
- X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
- X
- X/* Exception handling test. */
- X
- X#include <setjmp.h>
- X#include "cool.h"
- X
- Xmain() {
- X
- X cool_exception("NastyBad",
- X { /* code */
- X cool_raise("NastyBad", "Exceptions don\'t %s!", "work");
- X },{ /* handler */
- X printf("Exceptions do indeed work.\n");
- X })
- X cool_raise("ThisIsAnException", "Remove the core file");
- X}
- SHAR_EOF
- chmod 0644 tst4.c ||
- echo 'restore of tst4.c failed'
- Wc_c="`wc -c < 'tst4.c'`"
- test 375 -eq "$Wc_c" ||
- echo 'tst4.c: original size 375, current size' "$Wc_c"
- fi
- # ============= tst5.c ==============
- if test -f 'tst5.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst5.c (File already exists)'
- else
- echo 'x - extracting tst5.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst5.c' &&
- X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
- X
- X/* Class multiple inheritance clashing methods "Inherit" test */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include "cool.h"
- X
- X/* Number abstract superclass: local storage for class is not used. */
- X/*
- X * Superclass of integer-like things: ints, shorts, 256-degree angles.
- X */
- X
- Xvoid print(junk)
- Xint *junk;
- X{
- X printf("%d", cool(int, (SELFOF(junk), "Get", NULL)));
- X}
- X
- Xvoid scan(junk, numstr)
- Xchar *junk;
- Xchar *numstr;
- X{
- X int local;
- X
- X if (sscanf(numstr, "%d", &local) == 1)
- X cool_msg(SELFOF(junk), "Set", local);
- X else
- X cool_raise("NotANumber", "%s", numstr);
- X}
- X
- Xvoid number_init() {
- X cool_msg("Class", "Create", "Number", sizeof(char), NULL);
- X cool_msg("Number", "Method", "Print", print, 0);
- X cool_msg("Number", "Method", "Scan", scan, 1);
- X}
- X
- X/* Subtraction abstract superclass: local storage for class is not used. */
- X/*
- X * Superclass that does subtract and printing.
- X */
- X
- Xvoid sub(junk, i)
- Xint *junk;
- Xint i;
- X{
- X int local;
- X
- X local = (int) cool_msg(SELFOF(junk), "Get");
- X local -= i;
- X cool_msg(SELFOF(junk), "Set", local);
- X}
- X
- X/* Another Print method. */
- Xvoid sub_print(junk, i)
- Xint *junk;
- X{
- X int local;
- X
- X printf("%d", cool(int, (SELFOF(junk), "Get", NULL)));
- X}
- X
- X
- Xvoid subtractor_init() {
- X cool_msg("Class", "Create", "Subtractor", sizeof(char), NULL);
- X cool_msg("Subtractor", "Method", "Subtract:integer", sub, 1);
- X cool_msg("Subtractor", "Method", "Print", sub_print, 1);
- X}
- X
- X/* Integer class: local storage for class is an integer. */
- X
- Xint get(ip) int *ip; { return *ip; }
- Xvoid set(ip, i2) int *ip, i2; { *ip = i2; }
- Xvoid add(ip, i2) int *ip, i2; { *ip += i2; }
- Xvoid add2(ip, i2, i3) int *ip, i2, i3; { *ip = i2 + i3; }
- X
- Xvoid integer_init() {
- X
- X /* Inherit Abstract Superclass Number */
- X cool_msg("Class", "Create", "Integer", sizeof(int),
- X "Number", "Subtractor", NULL);
- X cool_msg("Integer", "Method", "Get", get, 0);
- X cool_msg("Integer", "Method", "Set", set, 1);
- X cool_msg("Integer", "Method", "Add:integer", add, 1);
- X cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
- X cool_msg("Integer", "Inherit", "Print", "Subtractor");
- X}
- X
- Xmain() {
- X object_t i;
- X
- X cool_init(); /* Must be called first */
- X number_init();
- X subtractor_init();
- X integer_init();
- X
- X /* Class usage */
- X
- X i = cool_msg("Integer", "Create", NULL);
- X cool_msg(i, "Scan", "15");
- X cool_msg(i, "Add:integer", 20);
- X cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 10);
- X cool_msg(i, "Subtract:integer", 2);
- X printf("This better be 43: ");
- X cool_msg(i, "Print");
- X printf("\n");
- X cool_msg("Integer", "Destroy", i);
- X}
- X
- X
- SHAR_EOF
- chmod 0644 tst5.c ||
- echo 'restore of tst5.c failed'
- Wc_c="`wc -c < 'tst5.c'`"
- test 2592 -eq "$Wc_c" ||
- echo 'tst5.c: original size 2592, current size' "$Wc_c"
- fi
- # ============= tst6.c ==============
- if test -f 'tst6.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst6.c (File already exists)'
- else
- echo 'x - extracting tst6.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst6.c' &&
- X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
- X
- X/* Class creation test with init/exit/clone. */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include "cool.h"
- X
- X/* Counter class: local storage for class is an integer. */
- X
- Xvoid cinit(ip) int *ip; { printf("Counter created\n"); }
- Xvoid ccopy(ip,ip2) int *ip, *ip2; { printf("Counter cloned\n"); }
- Xvoid cexit(ip) int *ip; { printf("Counter destroyed\n"); }
- Xvoid increment(ip) int *ip; { (*ip)++; }
- Xvoid decrement(ip) int *ip; { (*ip)--; }
- Xint value(ip) int *ip; { return *ip; }
- Xvoid reset(ip) int *ip; { *ip = 0; }
- X
- Xmain() {
- X object_t c, clone;
- X
- X cool_init(); /* Must be called first */
- X
- X /* No parent classes */
- X cool_msg("Class", "Create", "Counter", sizeof(int), NULL);
- X cool_msg("Counter", "Method", "Init", cinit, 0);
- X cool_msg("Counter", "Method", "Copy", ccopy, 0);
- X cool_msg("Counter", "Method", "Exit", cexit, 0);
- X cool_msg("Counter", "Method", "Increment", increment, 0);
- X cool_msg("Counter", "Method", "Decrement", decrement, 0);
- X cool_msg("Counter", "Method", "Value", value, 0);
- X cool_msg("Counter", "Method", "Reset", reset, 0);
- X
- X /* Class usage */
- X
- X c = cool_msg("Counter", "Create", NULL);
- X cool_msg(c, "Reset");
- X cool_msg(c, "Increment");
- X cool_msg(c, "Increment");
- X cool_msg(c, "Decrement");
- X printf("This better be one: %d\n", cool(int, (c, "Value", NULL)));
- X clone = cool_msg("Counter", "Clone", c, NULL);
- X printf("This also better be one: %d\n",cool(int,(clone,"Value",NULL)));
- X cool_msg(clone, "Increment");
- X printf("This better be one: %d\n", cool(int, (c, "Value", NULL)));
- X printf("This better be two: %d\n", cool(int, (clone, "Value", NULL)));
- X cool_msg("Counter", "Destroy", c);
- X printf("This still better be two: %d\n", cool(int, (clone, "Value", NULL)));
- X}
- X
- X
- SHAR_EOF
- chmod 0644 tst6.c ||
- echo 'restore of tst6.c failed'
- Wc_c="`wc -c < 'tst6.c'`"
- test 1753 -eq "$Wc_c" ||
- echo 'tst6.c: original size 1753, current size' "$Wc_c"
- fi
- # ============= tst7.c ==============
- if test -f 'tst7.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst7.c (File already exists)'
- else
- echo 'x - extracting tst7.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst7.c' &&
- X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
- X
- X/* Class 3rd-level inheritance test */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include "cool.h"
- X
- X/* Number abstract superclass: local storage for class is not used. */
- X/*
- X * Superclass of integer-like things: ints, shorts, 256-degree angles.
- X */
- X
- Xvoid print(junk)
- Xint *junk;
- X{
- X printf("%d", cool(int, (SELFOF(junk), "Get", NULL)));
- X}
- X
- Xvoid scan(junk, numstr)
- Xchar *junk;
- Xchar *numstr;
- X{
- X int local;
- X
- X if (sscanf(numstr, "%d", &local) == 1)
- X cool_msg(SELFOF(junk), "Set", local);
- X else
- X cool_raise("NotANumber", "%s", numstr);
- X}
- X
- Xvoid number_init() {
- X cool_msg("Class", "Create", "Number", sizeof(char), NULL);
- X cool_msg("Number", "Method", "Print", print, 0);
- X cool_msg("Number", "Method", "Scan", scan, 1);
- X}
- X
- X/* Subtraction abstract superclass: local storage for class is not used. */
- X/*
- X * Superclass that does subtract.
- X */
- X
- Xvoid sub(junk, i)
- Xint *junk;
- Xint i;
- X{
- X int local;
- X
- X local = (int) cool_msg(SELFOF(junk), "Get");
- X local -= i;
- X cool_msg(SELFOF(junk), "Set", local);
- X}
- X
- X
- Xvoid subtractor_init() {
- X cool_msg("Class", "Create", "Subtractor", sizeof(char), NULL);
- X cool_msg("Subtractor", "Method", "Subtract:integer", sub, 1);
- X}
- X
- X/* Integer class: local storage for class is an integer. */
- X
- Xint get(ip) int *ip; { return *ip; }
- Xvoid set(ip, i2) int *ip, i2; { *ip = i2; }
- Xvoid add(ip, i2) int *ip, i2; { *ip += i2; }
- Xvoid add2(ip, i2, i3) int *ip, i2, i3; { *ip = i2 + i3; }
- X
- Xvoid integer_init() {
- X
- X /* Inherit Abstract Superclass Number */
- X cool_msg("Class", "Create", "Integer", sizeof(int),
- X "Number", "Subtractor", NULL);
- X cool_msg("Integer", "Method", "Get", get, 0);
- X cool_msg("Integer", "Method", "Set", set, 1);
- X cool_msg("Integer", "Method", "Add:integer", add, 1);
- X cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
- X}
- X
- Xint ratget(ip) int *ip; { return *ip; }
- Xvoid ratset(ip, i2) int *ip, i2; { *ip = i2; }
- X
- Xvoid rational_init() {
- X
- X /* Inherit Abstract Superclass Number */
- X cool_msg("Class", "Create", "Rational", sizeof(int),
- X "Integer", /* "Number", "Subtractor", */ NULL);
- X cool_msg("Rational", "Method", "RatGet", ratget, 0);
- X cool_msg("Rational", "Method", "RatSet", ratset, 1);
- X}
- X
- Xmain() {
- X object_t i;
- X
- X cool_init(); /* Must be called first */
- X number_init();
- X subtractor_init();
- X integer_init();
- X rational_init();
- X
- X /* Class usage */
- X
- X i = cool_msg("Rational", "Create", "KingRat");
- X cool_msg(i, "Scan", "15");
- X cool_msg(i, "Add:integer", 20);
- X cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 10);
- X cool_msg(i, "Subtract:integer", 2);
- X printf("This better be 43: ");
- X cool_msg(i, "Print");
- X printf("\n");
- X cool_msg(i, "RatSet", 86);
- X printf("This better be 86: %d\n", cool_msg(i, "RatGet"));
- X cool_msg("Rational", "Destroy", i);
- X}
- X
- X
- SHAR_EOF
- chmod 0644 tst7.c ||
- echo 'restore of tst7.c failed'
- Wc_c="`wc -c < 'tst7.c'`"
- test 2788 -eq "$Wc_c" ||
- echo 'tst7.c: original size 2788, current size' "$Wc_c"
- fi
- # ============= tst8.c ==============
- if test -f 'tst8.c' -a X"$1" != X"-c"; then
- echo 'x - skipping tst8.c (File already exists)'
- else
- echo 'x - extracting tst8.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'tst8.c' &&
- X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
- X
- X/* Class component test */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include "cool.h"
- X
- X/* Printer class. */
- X
- Xprinter_print(junk, n) int n; { printf("%d\n", n); }
- X
- Xprinter_init() {
- X /* No parent classes */
- X cool_msg("Class", "Create", "Printer", 4, NULL);
- X cool_msg("Printer", "Method", "Print", printer_print, 1);
- X}
- X
- X/* Counter class: local storage for class is an integer. */
- X
- Xvoid increment(ip) int *ip; { (*ip)++; }
- Xvoid decrement(ip) int *ip; { (*ip)--; }
- Xint value(ip) int *ip; { return *ip; }
- Xvoid reset(ip) int *ip; { *ip = 0; }
- X
- Xcounter_init() {
- X /* No parent classes */
- X cool_msg("Class", "Create", "Counter", sizeof(int), NULL);
- X cool_msg("Counter", "Method", "Increment", increment, 0);
- X cool_msg("Counter", "Method", "Decrement", decrement, 0);
- X cool_msg("Counter", "Method", "Value", value, 0);
- X cool_msg("Counter", "Method", "Reset", reset, 0);
- X cool_msg("Counter", "Component", "Printer", "P");
- X}
- X
- Xmain() {
- X object_t c, p, p2;
- X
- X cool_init(); /* Must be called first */
- X printer_init(); /* Set up Printer class */
- X counter_init(); /* Set up Counter class */
- X
- X /* Class usage */
- X
- X c = cool_msg("Counter", "Create", "C");
- X cool_msg(c, "Reset");
- X cool_msg(c, "Increment");
- X cool_msg(c, "Increment");
- X cool_msg(c, "Decrement");
- X p = cool_componentof(c, "P");
- X p2 = cool_object("C.P");
- X if (p != p2) {
- X printf("Object component lookups don't match!\n");
- X exit(1);
- X }
- X printf("This better be one: ");
- X cool(void, (p, "Print", cool(int, (c, "Value", NULL))));
- X printf("\n");
- X cool_msg("Counter", "Destroy", c);
- X}
- X
- X
- SHAR_EOF
- chmod 0644 tst8.c ||
- echo 'restore of tst8.c failed'
- Wc_c="`wc -c < 'tst8.c'`"
- test 1600 -eq "$Wc_c" ||
- echo 'tst8.c: original size 1600, current size' "$Wc_c"
- fi
- # ============= bench.c ==============
- if test -f 'bench.c' -a X"$1" != X"-c"; then
- echo 'x - skipping bench.c (File already exists)'
- else
- echo 'x - extracting bench.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'bench.c' &&
- X/*
- X * Copyright 1991 by Lance Norskog
- X *
- X * Updated from Henk Davids' VMS port. Cleaner and portable now.
- X */
- X
- X/* Simple benchmark of Object messaging v.s. case/subroutine */
- X
- X#include <setjmp.h>
- X#include <stdio.h>
- X#include <sys/types.h>
- X#ifndef VMS
- X#include <sys/param.h>
- X#include <sys/times.h>
- X#endif
- X#include "cool.h"
- X#include "coolint.h"
- X
- X/* From Henk Davids' VMS port. */
- Xvoid startclock();
- Xdouble cputime();
- X
- X/* If the fastest test takes less than 1 second, increase. */
- X#define ITERATIONS 5000000
- X
- X/* Counter class: local storage for class is an integer. */
- X
- Xvoid increment(ip) int *ip; { (*ip)++; }
- Xvoid decrement(ip) int *ip; { (*ip)--; }
- Xint value(ip) int *ip; { return *ip; }
- Xvoid reset(ip) int *ip; { *ip = 0; }
- X
- Xcounter_init() {
- X /* No parent classes */
- X cool_msg("Class", "Create", "Counter", sizeof(int), NULL);
- X cool_msg("Counter", "Method", "Increment", increment, 0);
- X cool_msg("Counter", "Method", "Decrement", decrement, 0);
- X cool_msg("Counter", "Method", "Value", value, 0);
- X cool_msg("Counter", "Method", "Reset", reset, 0);
- X}
- X
- Xmain() {
- X object_t c;
- X method_t inc;
- X double obtime, cooltime, tabtime, casetime, calltime, codetime, scale;
- X int i;
- X void (*functab[8])();
- X
- X
- X cool_init(); /* Must be called first */
- X counter_init(); /* Set up Counter class */
- X
- X /* Class usage */
- X
- X c = cool_msg("Counter", "Create", NULL);
- X cool_msg(c, "Reset");
- X inc = cool_method(c, "Increment");
- X {
- X startclock();
- X for(i = 0; i < ITERATIONS; i++)
- X cool_msg(c, inc);
- X obtime = cputime();
- X printf("Object:\t%g\n", obtime);
- X }
- X {
- X cool_vars /* Local variables for fast messaging */
- X
- X startclock();
- X for(i = 0; i < ITERATIONS; i++)
- X coolv(c, inc);
- X cooltime = cputime();
- X printf("Coolt:\t%g\n", cooltime);
- X }
- X {
- X for(i = 0; i < 8; i++)
- X functab[i] = increment;
- X startclock();
- X i = 0;
- X while(i < ITERATIONS)
- X (* functab[i & 7])(&i);
- X tabtime = cputime();
- X printf("Table:\t%g\n", tabtime);
- X }
- X {
- X startclock(0);
- X i = 0;
- X while(i < ITERATIONS) {
- X switch(i & 7) {
- X case 0:
- X increment(&i);
- X break;
- X case 2:
- X increment(&i);
- X break;
- X case 3:
- X increment(&i);
- X break;
- X case 6:
- X increment(&i);
- X break;
- X default:
- X increment(&i);
- X break;
- X }
- X }
- X casetime = cputime();
- X printf("Case:\t%g\n", casetime);
- X }
- X {
- X startclock();
- X i = 0;
- X while(i < ITERATIONS)
- X increment(&i);
- X calltime = cputime();
- X printf("Call:\t%g\n", calltime);
- X }
- X {
- X startclock();
- X for(i = 0; i < ITERATIONS; i++)
- X continue;
- X codetime = cputime();
- X printf("Code:\t%g\n", codetime);
- X }
- X scale = 1.0/casetime;
- X printf("Object: %2.2g Cool %2.2g Table %2.2g Case %2.2g Call %2.2g Code %2.2g\n",
- X obtime * scale, cooltime * scale, tabtime * scale,
- X casetime * scale, calltime * scale, codetime * scale);
- X}
- SHAR_EOF
- chmod 0644 bench.c ||
- echo 'restore of bench.c failed'
- Wc_c="`wc -c < 'bench.c'`"
- test 2781 -eq "$Wc_c" ||
- echo 'bench.c: original size 2781, current size' "$Wc_c"
- fi
- # ============= timer.c ==============
- if test -f 'timer.c' -a X"$1" != X"-c"; then
- echo 'x - skipping timer.c (File already exists)'
- else
- echo 'x - extracting timer.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'timer.c' &&
- X/****************************\
- X* *
- X* Timer functions for VMS *
- X* *
- X\****************************/
- X
- X/* From Henk Davids. */
- X
- X/*
- X * The SUN4 gives bogus readings using the BSD-style resource values.
- X * I don't know if the code is bogus, or the SUN4 does resources wrong.
- X */
- X
- X#include <stdio.h>
- X#if defined(USG) || defined(sparc)
- X#include <sys/types.h>
- X#include <sys/param.h>
- X#include <sys/times.h>
- X#else
- X#include <sys/time.h>
- X#include <sys/file.h>
- X#ifdef VMS
- X#ifdef TESTING
- X#include <unixio.h>
- X#include <redexp.VMS>
- X#endif /* TESTING */
- X#else
- X#include <limits.h>
- X#include <sys/types.h>
- X#include <sys/resource.h>
- X#endif
- X#endif
- X
- Xvoid startclock();
- Xdouble elapsedtime();
- Xdouble cputime();
- X
- X#ifdef TESTING
- Xstatic void doit();
- X#define LOOPS 100000
- X
- Xmain(argc, argv)
- X int argc;
- X char * argv[];
- X{
- X double elapsed, cpu;
- X
- X startclock();
- X
- X doit();
- X
- X elapsed = elapsedtime();
- X cpu = cputime();
- X printf("%.2f sec, cpu: %.2f (%.2f%%)\n",
- X elapsed, cpu, (cpu/elapsed*100));
- X printf("Per loop:\n");
- X printf("%f sec, cpu: %f (%.2f%%)\n",
- X elapsed/LOOPS, cpu/LOOPS, (cpu/elapsed*100));
- X exit(0);
- X}
- X
- Xstatic float dummy;
- Xdouble sin();
- Xvoid doit()
- X{
- X int i;
- X for (i=0; i<LOOPS; i++) {
- X dummy = sin((1.0*i)/LOOPS);
- X }
- X}
- X#endif /* TESTING */
- X
- X/* ----- timer functions -----
- X * void startclock();
- X * double elapsedtime(); in secs, since last startclock()
- X * double cputime(); in secs, since last startclock()
- X */
- X
- X#define FACTORV5 100000.0
- X#define FACTORV8 100000000.0
- X
- Xstatic double cpu_so_far();
- Xstatic double time_so_far();
- Xstatic void error_exit();
- X
- Xstatic double last_cpustamp = 0.0;
- Xstatic double last_timestamp = 0.0;
- X
- X#if defined(USG) || defined(sparc)
- Xint hz = 0;
- X#endif
- X
- Xvoid
- Xstartclock()
- X{
- X#if defined(USG) || defined(sparc)
- X if (! hz)
- X if (getenv("HZ") && atoi(getenv("HZ")))
- X hz = atoi(getenv("HZ"));
- X else hz = HZ;
- X#endif
- X last_cpustamp = cpu_so_far();
- X last_timestamp = time_so_far();
- X}
- X
- Xdouble elapsedtime() {
- X return(time_so_far() - last_timestamp);
- X}
- Xdouble cputime() {
- X return(cpu_so_far() - last_cpustamp);
- X}
- X
- Xstatic double
- Xcpu_so_far()
- X{
- X#ifdef VMS
- X tbuffer_t tms;
- X
- X times(&tms);
- X return ((double) tms.proc_user_time) / ((double) CLK_TCK) +
- X ((double) tms.proc_system_time) / ((double) CLK_TCK);
- X#elif defined(USG) || defined(sparc)
- X struct tms tms;
- X times(&tms);
- X return ((double) tms.tms_utime / (double) hz);
- X#else
- X struct rusage rusage;
- X
- X getrusage(RUSAGE_SELF, &rusage);
- X return
- X ((double) rusage.ru_utime.tv_sec) +
- X (((double) rusage.ru_utime.tv_usec) / FACTORV8) +
- X ((double) rusage.ru_stime.tv_sec) +
- X (((double) rusage.ru_stime.tv_usec) / FACTORV8);
- X#endif
- X}
- X
- Xstatic double
- Xtime_so_far()
- X{
- X#ifdef VMS
- X timeb_t tms;
- X
- X ftime(&tms);
- X return ((double) tms.time + (double) tms.millitm / FACTORV5);
- X#elif defined(USG) || defined(sparc)
- X /* system time in clicks */
- X struct tms tms;
- X return (double) times(&tms) / (double) hz;
- X#else
- X struct timeval tp;
- X
- X if (gettimeofday(&tp, (struct timezone *) NULL) == -1)
- X error_exit("gettimeofday");
- X return ((double) (tp.tv_sec)) +
- X (((double) tp.tv_usec) / FACTORV8);
- X#endif
- X}
- X
- Xstatic void
- Xerror_exit(message)
- X char * message;
- X{
- X char buf[BUFSIZ];
- X
- X sprintf(buf, "error in %s", message);
- X perror(buf);
- X exit(1);
- X}
- X
- X
- X
- X
- X
- SHAR_EOF
- chmod 0644 timer.c ||
- echo 'restore of timer.c failed'
- Wc_c="`wc -c < 'timer.c'`"
- test 3319 -eq "$Wc_c" ||
- echo 'timer.c: original size 3319, current size' "$Wc_c"
- fi
- true || echo 'restore of benchall failed'
- echo End of part 3, continue with part 4
- exit 0
- --
-
- Lance Norskog
-
- Data is not information is not knowledge is not wisdom.
-