]> git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0.c
Merge branch 'little_update' into 'master'
[xonotic/d0_blind_id.git] / d0.c
1 #include "d0.h"
2
3 /*
4  * include the license notice into the dynamic library to "reproduce the
5  * copyright notice" automatically, so the application developer does not have
6  * to care about this term
7  */
8 const char *d0_bsd_license_notice D0_USED = "\n"
9 "/*\n"
10 " * FILE:       d0.c\n"
11 " * AUTHOR:     Rudolf Polzer - divVerent@xonotic.org\n"
12 " * \n"
13 " * Copyright (c) 2010, Rudolf Polzer\n"
14 " * All rights reserved.\n"
15 " *\n"
16 " * Redistribution and use in source and binary forms, with or without\n"
17 " * modification, are permitted provided that the following conditions\n"
18 " * are met:\n"
19 " * 1. Redistributions of source code must retain the above copyright\n"
20 " *    notice, this list of conditions and the following disclaimer.\n"
21 " * 2. Redistributions in binary form must reproduce the above copyright\n"
22 " *    notice, this list of conditions and the following disclaimer in the\n"
23 " *    documentation and/or other materials provided with the distribution.\n"
24 " * 3. Neither the name of the copyright holder nor the names of contributors\n"
25 " *    may be used to endorse or promote products derived from this software\n"
26 " *    without specific prior written permission.\n"
27 " * \n"
28 " * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND\n"
29 " * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n"
30 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
31 " * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE\n"
32 " * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
33 " * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
34 " * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
35 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n"
36 " * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n"
37 " * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n"
38 " * SUCH DAMAGE.\n"
39 " *\n"
40 " * $Format:commit %H$\n"
41 " * $Id$\n"
42 " */\n";
43
44 #include <stdlib.h>
45
46 //#define MUTEX_DEBUG
47
48 #ifdef MUTEX_DEBUG
49 #define NUM_MUTEXES 1024
50 #include <stdio.h>
51 static mutexarray[NUM_MUTEXES];
52 static int mutexpos = 0;
53 static void *dummy_createmutex(void)
54 {
55         if(mutexpos >= NUM_MUTEXES)
56         {
57                 printf("We don't support creating so many mutexes here\n");
58                 return NULL;
59         }
60         return &mutexarray[mutexpos++];
61 }
62 static void dummy_destroymutex(void *m)
63 {
64         if(*(int *)m != 0)
65                 printf("Destroying in-use mutex\n");
66         *(int *)m = -1;
67 }
68 static int dummy_lockmutex(void *m)
69 {
70         if(*(int *)m != 0)
71                 printf("Locking in-use mutex\n");
72         *(int *)m += 1;
73         return 0;
74 }
75 static int dummy_unlockmutex(void *m)
76 {
77         if(*(int *)m != 1)
78                 printf("Unlocking not-in-use mutex\n");
79         *(int *)m -= 1;
80         return 0;
81 }
82 #else
83 static void *dummy_createmutex(void)
84 {
85         return (void *) 1; // some dummy non-NULL pointer
86 }
87 static void dummy_destroymutex(void *m)
88 {
89 }
90 static int dummy_lockmutex(void *m)
91 {
92         return 0;
93 }
94 static int dummy_unlockmutex(void *m)
95 {
96         return 0;
97 }
98 #endif
99
100 d0_malloc_t *d0_malloc = NULL;
101 d0_free_t *d0_free = NULL;
102 d0_createmutex_t *d0_createmutex = NULL;
103 d0_destroymutex_t *d0_destroymutex = NULL;
104 d0_lockmutex_t *d0_lockmutex = NULL;
105 d0_unlockmutex_t *d0_unlockmutex = NULL;
106
107 void d0_setmallocfuncs(d0_malloc_t *m, d0_free_t *f)
108 {
109         d0_malloc = (m ? m : malloc);
110         d0_free = (f ? f : free);
111 }
112
113 void d0_setmutexfuncs(d0_createmutex_t *c, d0_destroymutex_t *d, d0_lockmutex_t *l, d0_unlockmutex_t *u)
114 {
115         d0_createmutex = (c ? c : dummy_createmutex);
116         d0_destroymutex = (d ? d : dummy_destroymutex);
117         d0_lockmutex = (l ? l : dummy_lockmutex);
118         d0_unlockmutex = (u ? u : dummy_unlockmutex);
119 }
120
121 void d0_initfuncs(void)
122 {
123         d0_setmallocfuncs(d0_malloc, d0_free);
124         d0_setmutexfuncs(d0_createmutex, d0_destroymutex, d0_lockmutex, d0_unlockmutex);
125 }