]> git.xonotic.org Git - xonotic/d0_blind_id.git/blobdiff - d0.c
stupid hack
[xonotic/d0_blind_id.git] / d0.c
diff --git a/d0.c b/d0.c
index fccc4e51326645b85f8725b9e8dc7c452cdbe5c2..c8920b4ef5a120ad66f94b9d3355f152239063f1 100644 (file)
--- a/d0.c
+++ b/d0.c
@@ -1,9 +1,11 @@
+#include "d0.h"
+
 /*
  * include the license notice into the dynamic library to "reproduce the
  * copyright notice" automatically, so the application developer does not have
  * to care about this term
  */
-const char *d0_bsd_license_notice = "\n"
+const char *d0_bsd_license_notice D0_USED = "\n"
 "/*\n"
 " * FILE:      d0.c\n"
 " * AUTHOR:    Rudolf Polzer - divVerent@xonotic.org\n"
@@ -39,31 +41,85 @@ const char *d0_bsd_license_notice = "\n"
 " * $Id$\n"
 " */\n";
 
-#include "d0.h"
-
 #include <stdlib.h>
 
-void *(*d0_malloc)(size_t len) = malloc;
-void (*d0_free)(void *p) = free;
+//#define MUTEX_DEBUG
 
-static void *dummy = (void *) 1; // some dummy non-NULL pointer
+#ifdef MUTEX_DEBUG
+#define NUM_MUTEXES 1024
+#include <stdio.h>
+static mutexarray[NUM_MUTEXES];
+static int mutexpos = 0;
 static void *dummy_createmutex(void)
 {
-       return NULL;
+       if(mutexpos >= NUM_MUTEXES)
+       {
+               printf("We don't support creating so many mutexes here\n");
+               return NULL;
+       }
+       return &mutexarray[mutexpos++];
 }
 static void dummy_destroymutex(void *m)
 {
+       if(*(int *)m != 0)
+               printf("Destroying in-use mutex\n");
+       *(int *)m = -1;
 }
 static int dummy_lockmutex(void *m)
 {
+       if(*(int *)m != 0)
+               printf("Locking in-use mutex\n");
+       *(int *)m += 1;
        return 0;
 }
 static int dummy_unlockmutex(void *m)
+{
+       if(*(int *)m != 1)
+               printf("Unlocking not-in-use mutex\n");
+       *(int *)m -= 1;
+       return 0;
+}
+#else
+static void *dummy_createmutex(void)
+{
+       return (void *) 1; // some dummy non-NULL pointer
+}
+static void dummy_destroymutex(void *m)
+{
+}
+static int dummy_lockmutex(void *m)
 {
        return 0;
 }
+static int dummy_unlockmutex(void *m)
+{
+       return 0;
+}
+#endif
 
-void *(*d0_createmutex)(void) = dummy_createmutex;
-void (*d0_destroymutex)(void *) = dummy_destroymutex;
-int (*d0_lockmutex)(void *) = dummy_lockmutex;
-int (*d0_unlockmutex)(void *) = dummy_unlockmutex;
+d0_malloc_t *d0_malloc = NULL;
+d0_free_t *d0_free = NULL;
+d0_createmutex_t *d0_createmutex = NULL;
+d0_destroymutex_t *d0_destroymutex = NULL;
+d0_lockmutex_t *d0_lockmutex = NULL;
+d0_unlockmutex_t *d0_unlockmutex = NULL;
+
+void d0_setmallocfuncs(d0_malloc_t *m, d0_free_t *f)
+{
+       d0_malloc = (m ? m : malloc);
+       d0_free = (f ? f : free);
+}
+
+void d0_setmutexfuncs(d0_createmutex_t *c, d0_destroymutex_t *d, d0_lockmutex_t *l, d0_unlockmutex_t *u)
+{
+       d0_createmutex = (c ? c : dummy_createmutex);
+       d0_destroymutex = (d ? d : dummy_destroymutex);
+       d0_lockmutex = (l ? l : dummy_lockmutex);
+       d0_unlockmutex = (u ? u : dummy_unlockmutex);
+}
+
+void d0_initfuncs(void)
+{
+       d0_setmallocfuncs(d0_malloc, d0_free);
+       d0_setmutexfuncs(d0_createmutex, d0_destroymutex, d0_lockmutex, d0_unlockmutex);
+}