#include "main.h"
#include "misc.h"
#include <time.h>

/*
 * Counter Functions
 */
unsigned long count(struct counter_t *c, int i) {
	time_t this_time;
	if(c->active) {
		this_time = time(NULL);
		if(this_time == c->last_time) {
			c->count += i;
		} else {
			c->last_time = this_time;
			c->value = c->count + i;
			c->count = 0;
		}
	}
	return c->value;
}

void counter_init(struct counter_t *c, int active) {
	c->value = 0;
	c->count = 0;
	c->active = active;
	c->last_time = 0;
}

void *xmalloc(size_t size) {
	void *p = NULL;
	if(DEBUG) {fprintf(stderr,"xmalloc: request %d\n",size);}
	p = malloc(size);
	if(p == NULL) {
		fprintf(stderr,"Malloc Failed --- Self-Destruct\n");
		exit(1);
	} else {
		if(DEBUG) {fprintf(stderr,"xmalloc: return %p\n",p);}
		return p;
	}
}
