ftp.nice.ch/pub/next/graphics/movie/Movie.3.0.NIHS.bs.tar.gz#/Movie3.0/Source/xanim/aux.c

This is aux.c in view mode; [Download] [Up]

/*
 *  some useful definitions for memory allocation and string
 *  handling
 */
 
#define MODNAME "aux"
 
#include "aux.h"

#include <sys/time.h>
#include <sys/times.h>
#include <sys/param.h>
#ifdef IRIS
#include <task.h>   /* for getpid */
#endif

#if PARIX
char *rindex(char *s, char c)
{
    char *p, *pi;
    
    for(pi=NULL, p=s; *p; p++) if (*p==c) pi=p;
    return pi;
}
#endif

#ifdef CM5
#include <cm/cmmd.h>  /* for timer functions */
#endif

int list_mem;
int mem_size;
int mem_s=0, mem_pl=0, mem_grid=0, mem_bo=0, mem_nb=0;

int memsize()
{
	return 0;
}

char *checkalloc(p, len, deflen)
    char *p;
    int len, deflen;
{
	if (p==NULL) {
	    p = malloc(deflen);
	    if (p==NULL) { 
                 fprintf(stderr,"Could not allocate memory.\n");
	        exit(1);
            }
	}
	if (len<=deflen) return p;
	p = (char*)realloc(p,len);
	if (p==NULL) { 
             fprintf(stderr,"Could not extend memory.\n");
	    exit(1);
        }
	return p;
}

char *copy(str)
    char *str;
{
    char *string;
    int l;
    
    if (str==NULL) return NULL;
    l = strlen(str)+1;
    string = (char *)malloc(l);
    if (string==NULL) { 
        fprintf(stderr,"Could not string string.\n");
	exit(1);
    }
    strcpy(string, str);
    return(string);
}

char aux_buf[aux_bufsiz], aux_buf2[aux_bufsiz], *aux_bufp;

char *substr(const char *str, int ofs, int len)
{
    char *s;
    
    aux_bufnstring(s, len+1);
    bcopy(str+ofs, s, len);
    s[ofs+len] = '\0';
    return s;
}

char *stringf(char *format, ...)
{
    char *p;
    
    aux_sprintf(p, format);
    return(p);
}

char *copydata(str, len)
    char *str;
    int len;
{
    register char *s,*p;
    
    if (str==NULL) return NULL;
    s = (char *)malloc(len);
    if (s==NULL) { 
         fprintf(stderr,"Could not allocate string.\n");
	exit(1);
    }
    p=s; s+=len;
    while (p<s) *(p++)=(*(str++));
    return(s-len);
}

char *append(s1, s2)
    char *s1, *s2;
{
    register char *s;
    
    s = (char*)realloc(s1,strlen(s1)+strlen(s2)+1);
    return strcat(s,s2);
}
      
      
FILE *ropen(fn)
    char *fn;
{
	FILE *fd;
	
	if (strcmp(fn,"-")==0) fd=fdopen(0,"r"); else  fd=fopen(fn,"r");
  	if (fd==NULL) {
		fprintf(stderr,"Could not open %s for input\n",fn);
		exit(1);
	}
	return fd;
}
      
FILE *wopen(fn)
    char *fn;
{
	FILE *fd;
	
	if (strcmp(fn,"-")==0) fd=fdopen(1,"w"); else  fd=fopen(fn,"w");
  	if (fd==NULL) {
		fprintf(stderr,"Could not open %s for output\n",fn);
		exit(1);
	}
	return fd;
}

int debug_flag=1;
int memory_flag=0;

void warning(char *format, ...)
{
    char *p;

    aux_sprintf(p, format);
    printf("%s Warning: %s", deb_pref, p);
    fflush(stdout);
}

int deb_crash=DEB_EXIT;

void error(char *format, ...)
{
    char *p;
    
    aux_sprintf(p, format);
    printf("%s %s\n", deb_pref, p);
    fflush(stdout);
    if (deb_crash==DEB_CORE) kill(getpid(),6);
    else exit(1);
}

char *deb_pref="";
int deb_mode=DEB_CPU;
double deb_cpu=-1, deb_real=-1;

real time_temp=0.0;

double realtime()
{
#ifdef PARIX
    return ((double)TimeNowLow())/(double)CLK_TCK_LOW;
#else
#ifdef CM5
    if (time_temp==0.0) {
        CMMD_node_timer_clear(0);
        CMMD_node_timer_start(0);
    }
    CMMD_node_timer_stop(0);
    time_temp = (real)CMMD_node_timer_elapsed(0);
    CMMD_node_timer_start(0);
    return time_temp;
#else
    struct timeval tv;
    struct timezone tzv;

    gettimeofday(&tv, &tzv);
/*    debug("Time: %d seconds, %d microseconds, % 15.3f\n",
	tv.tv_sec, tv.tv_usec, (double)tv.tv_sec + 1e-6*(double)tv.tv_usec); */
    return (double)tv.tv_sec + 1e-6*(double)tv.tv_usec;
#endif
#endif
}

double cputime() {
#ifdef CM5
    return realtime();
#else
    struct tms buf;

    times(&buf);
    return (double)buf.tms_utime/(double)CLK_TCK;
#endif
}


void deb_print(char *p)
{
    char tform[]="%s%6.2f %s", aform[]="%s%6.2f %2d%% %s";
    real ct, rt;
    int eff;

    if (deb_cpu<0) deb_cpu=cputime();
    if (deb_real<0) deb_real=realtime();
    switch (deb_mode) {
	case DEB_NONE:
	    printf("%s %s", deb_pref, p);
	    break;
	case DEB_CPU:
	    printf(tform, deb_pref, cputime()-deb_cpu, p);
	    break;
	case DEB_REAL:
	    printf(tform, deb_pref, realtime()-deb_real, p);
	    break;
	case DEB_ALL:
	    ct = cputime()-deb_cpu;
	    rt = realtime()-deb_real;
	    if (rt!=0) eff = (int)(ct*100/rt); else eff=99;
	    if (eff>99) eff=99;
	    printf(aform, deb_pref, rt, eff, p);
	    break;
	case DEB_MEM:
	    printf("%s %d %s", deb_pref, mem_size, p);
	    break;
	default:
	    error("Illegal debugging mode %d.\n", deb_mode);
    }
    deb_cpu = cputime();
    deb_real = realtime();
    fflush(stdout);
}

void pdebug(char *format, ...)
{
    char *p;
    
    if (!debug_flag) return;
    aux_sprintf(p, format);
    deb_print(p);
}

void npdebug(char *format, ...)
{
    char *p;
    
    if (!debug_flag) return;
    aux_sprintf(p, format);
    printf("%s", p);
    fflush(stdout);
}


#define time_check 1
#define time_n1 20
#define time_n2 16384

real time_cpu0[time_n1], time_real0[time_n1];
real time_cpu[time_n1], time_real[time_n1];
char *time_names[time_n1];

short time_ind[time_n2];
short time_next;
short time_last;
short time_initflag=0;

void time_init(void)
{
	int i;

	for (i=0; i<time_n1; i++) {
	    time_cpu[i]=time_cpu[i]=time_cpu0[i]=time_cpu0[i]=0;
	    time_names[i]=NULL;
	}
	for (i=0; i<time_n2; i++) time_ind[i]=-1;
	time_next=0;
	time_last=0;
	time_initflag=1;
}

int time_label(char *str, int create)
{
    int i, j;
    char *s;

    if (!time_initflag) error("Timer not initialized.");
    for (i=0, s=str; *s && i<time_n2; s++) i+=(int)*s;
    j = time_ind[i];
    if (j<0) {
	if (!create) error("Timer label \"%s\" not found.", str);
	if (time_next>=time_n1) error("Too many Timer labels.");
	time_ind[i] = j = time_next++;
	time_names[j] = copy(str);
    }
#if time_check
    else if (strcmp(time_names[j], str))
	error("Time label conflict: %s and %s.\n", str, time_names[j]);
#endif
    return j;
}

void time_start(char *str)
{
    if (!time_initflag) time_init();
    time_last = time_label(str, 1);
    time_cpu0[time_last] = cputime();
    time_real0[time_last] = realtime();
}

void time_stop(char *str)
{
    int i;

    i = time_label(str, 0);
    time_cpu[i] += cputime() - time_cpu0[i];
    time_real[i] += realtime() - time_real0[i];
    if (time_last==i) time_last = -1;
}

void time_reset(char *str)
{
    int i;

    i = time_label(str, 0);
    time_cpu[i] = time_real[i] = 0;
}

real time_val(char *str)
{
    int i;
    
    i = time_label(str, 0);
    return time_cpu[i];
}

real time_lap(char *str)
{
    int i;
    
    i = time_label(str, 0);
    return cputime() - time_cpu0[i];
}

char *time_string_lap(char *str)
{
    int i;

    i = time_label(str, 0);
    return stringf("cpu: %6.2f, real: %6.2f", cputime() - time_cpu0[i], realtime() - time_real0[i]);
}

void time_seq(char *s)
{
    if (time_last>=0) {
	time_cpu[time_last] += cputime() - time_cpu0[time_last];
	time_real[time_last] += realtime() - time_real0[time_last];
	time_last = -1;
    }
    if (s) time_start(s);
}

void time_print(void)
{
    int i;
    char *s;

    if (!time_initflag) error("Timer not initialized.");
    time_seq(NULL);
    s = "\nCumulative timing results:\n    Label     Real       CPU\n";
    for (i=0; i<time_n1; i++) {
	if (!time_names[i]) break;
	s = stringf("%s%8s %10.3f %10.3f\n", s, time_names[i], time_real[i], time_cpu[i]);
    }
    printf("%s\n", s);
}


#if SRANDOM
extern int rand();
#else
#ifndef DEC
extern long random();
#endif
#endif

real ran_real(void)
{
    real r;

#if SRANDOM
    r = (real)rand()/32768.0;
#else
    r = (real)random()/2147483648.0;
#endif
/*    printf("%g\n", r);  */
    return r;
}

char *s_lst(int* p, int len)
{
    char *s;
    int i;
    
    s = "";
    for (i=0; i<len; i++) s = stringf("%s %2d", s, p[i]);
    return s;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.