]> Dogcows Code - chaz/rasterize/blobdiff - common.c
refactor the animation script a bit
[chaz/rasterize] / common.c
index 850778bfedfe83121d740a390aab486df3f1683d..2e3f6e2dc0edbe6beca300202b1f2d22832558c7 100644 (file)
--- a/common.c
+++ b/common.c
@@ -5,11 +5,13 @@
  * mcgarvey@eng.utah.edu
  */
 
+#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/time.h>
 
 #include "common.h"
 
@@ -23,7 +25,7 @@ static int _mem_blocks = 0;
  * Check the result of the allocation function and call the callback function
  * if it failed.
  */
-__fast__
+INLINE_MAYBE
 void* _mem_check(void* mem, void* old, size_t size)
 {
     if (mem == NULL && size != 0) {
@@ -48,8 +50,8 @@ done:
 void* mem_alloc(size_t size)
 {
     void* mem = _mem_check(malloc(size), NULL, size);
-#ifdef MEM_TRACE
-    fprintf(stderr, " ALLOC    %6d  %18p  %10zd\n", _mem_blocks - 1, mem, size);
+#if MEM_TRACE
+    fprintf(stderr, "alloc    %6d  %18p  %10zd\n", _mem_blocks - 1, mem, size);
 #endif
     return mem;
 }
@@ -58,8 +60,8 @@ void* mem_realloc(void* mem, size_t size)
 {
     void* old = mem;
     mem = _mem_check(realloc(mem, size), mem, size);
-#ifdef MEM_TRACE
-    fprintf(stderr, " REALLOC  %6d  %18p  %10zd  (was %p)\n", _mem_blocks - 1, mem, size, old);
+#if MEM_TRACE
+    fprintf(stderr, "realloc  %6d  %18p  %10zd  (was %p)\n", _mem_blocks - 1, mem, size, old);
 #endif
     return mem;
 }
@@ -67,8 +69,8 @@ void* mem_realloc(void* mem, size_t size)
 void mem_free(void* mem)
 {
     --_mem_blocks;
-#ifdef MEM_TRACE
-    fprintf(stderr, " FREE     %6d  %18p\n", _mem_blocks, mem);
+#if MEM_TRACE
+    fprintf(stderr, "free     %6d  %18p\n", _mem_blocks, mem);
 #endif
     free(mem);
 }
@@ -98,3 +100,34 @@ void ltrim(char *str)
     memmove(str, m, strlen(m) + 1);
 }
 
+
+static struct timeval _timer = {0, 0l};
+
+static struct timeval timer_diff(struct timeval a, struct timeval b)
+{
+    struct timeval tv;
+    if ((b.tv_usec - a.tv_usec) < 0) {
+        tv.tv_sec  = b.tv_sec - a.tv_sec - 1;
+        tv.tv_usec = 1000000l + b.tv_usec - a.tv_usec;
+    } else {
+        tv.tv_sec  = b.tv_sec  - a.tv_sec;
+        tv.tv_usec = b.tv_usec - a.tv_usec;
+    }
+    return tv;
+}
+
+void timer_start()
+{
+    int result = gettimeofday(&_timer, NULL);
+    assert(result == 0 && "umm... gettimeofday failed!?");
+}
+
+long timer_stop()
+{
+    struct timeval tv;
+    int result = gettimeofday(&tv, NULL);
+    assert(result == 0 && "umm... gettimeofday failed!?");
+    tv = timer_diff(_timer, tv);
+    return (long)tv.tv_sec * 1000000l + tv.tv_usec;
+}
+
This page took 0.018362 seconds and 4 git commands to generate.