annotate dmlib.c @ 294:dd9809a93425

Improve mutex debugging facilities.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 11 Oct 2012 16:04:02 +0300
parents 32250b436bca
children 3c5ebe88e52f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #include "dmlib.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #include <errno.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 int dmVerbosity = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 char *dmProgName = NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 *dmProgDesc = NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 *dmProgVersion = NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 *dmProgAuthor = NULL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 *dmProgLicense = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 void dmInitProg(char *name, char *desc, char *version, char *author, char *license)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 dmProgName = name;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 dmProgDesc = desc;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 dmProgVersion = version;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 if (author)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 dmProgAuthor = author;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 dmProgAuthor = DM_PROG_AUTHOR;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 if (license)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 dmProgLicense = license;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 dmProgLicense = DM_PROG_LICENSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 void dmPrintBanner(FILE *outFile, const char *name, const char *usage)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 fprintf(outFile,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 "\n%s v%s (%s)\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 "%s\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 "%s\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 "Usage: %s %s\n",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 dmProgName, dmProgVersion, dmProgDesc,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 dmProgAuthor, dmProgLicense, name, usage);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 void dmMsgVA(int level, const char *fmt, va_list ap)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 if (dmVerbosity >= level)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 fprintf(stderr, "%s: ", dmProgName);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 vfprintf(stderr, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 void dmPrintVA(int level, const char *fmt, va_list ap)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 if (dmVerbosity >= level)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 vfprintf(stderr, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 void dmErrorVA(const char *fmt, va_list ap)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 fprintf(stderr, "%s: ", dmProgName);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 vfprintf(stderr, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 void dmError(const char *fmt, ...)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 va_list ap;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 va_start(ap, fmt);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 dmErrorVA(fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 va_end(ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 void dmMsg(int level, const char *fmt, ...)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 va_list ap;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 va_start(ap, fmt);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 dmMsgVA(level, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 va_end(ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 void dmPrint(int level, const char *fmt, ...)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 va_list ap;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 va_start(ap, fmt);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 dmPrintVA(level, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 va_end(ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 /* Memory handling routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 void *dmMalloc(size_t l)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 return malloc(l);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 void *dmMalloc0(size_t l)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 return calloc(1, l);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 void *dmCalloc(size_t n, size_t l)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 return calloc(n, l);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 void *dmRealloc(void *p, size_t l)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 return realloc(p, l);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 void dmFree(void *p)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 /* Check for NULL pointers for portability due to some libc
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 * implementations not handling free(NULL) too well.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 if (p) free(p);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 BOOL dmGetIntVal(const char *s, int *i)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 if (s[0] == '$')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 if (sscanf(&s[1], "%x", i) < 1)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 else if (s[0] == '0' && s[1] == 'x')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 if (sscanf(&s[2], "%x", i) < 1)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 if (sscanf(s, "%u", i) < 1)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 return TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 /* Error handling
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 #define DM_SYSTEM_ERRORS 100000
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 int dmGetErrno()
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 return DM_SYSTEM_ERRORS + errno;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 const char *dmErrorStr(int error)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 if (error >= DM_SYSTEM_ERRORS)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 return strerror(error - DM_SYSTEM_ERRORS);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 switch (error)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 case DMERR_OK: return "No error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 case DMERR_FOPEN: return "File open error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 case DMERR_FREAD: return "Read error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 case DMERR_FWRITE: return "Write error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 case DMERR_FSEEK: return "Seek error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 case DMERR_NOT_FOUND: return "Resource not found";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 case DMERR_INVALID_DATA: return "Invalid data";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 case DMERR_MALLOC: return "Memory allocation failure";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 case DMERR_ALREADY_INIT: return "Already initialized";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 case DMERR_INIT_FAIL: return "Initialization failed";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 case DMERR_INVALID_ARGS: return "Invalid arguments";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 case DMERR_NULLPTR: return "NULL pointer";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 case DMERR_NOT_SUPPORTED: return "Operation not supported";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 case DMERR_OUT_OF_DATA: return "Out of data";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 case DMERR_EXTRA_DATA: return "Extra data";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 case DMERR_BOUNDS: return "Bounds check failed";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 case DMERR_NOTPACK: return "File is not a PACK";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 case DMERR_VERSION: return "Unsupported PACK version";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 case DMERR_INVALID: return "Invalid data, corrupted file";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 case DMERR_COMPRESSION: return "Error in compression";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 default: return "Unknown error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 }
294
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
200
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
201
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
202 #ifdef DM_MUTEX_DEBUG
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
203
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
204 static DMMutexLock * dmGetMutexThreadIDLock(DMMutex *mutex)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
205 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
206 Uint32 id = SDL_ThreadID();
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
207 int i;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
208 for (i = 0; i < 8; i++)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
209 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
210 DMMutexLock *lock = &(mutex->locks[i]);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
211 if (lock->used && lock->id == id)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
212 return lock;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
213 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
214 return NULL;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
215 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
216
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
217 static void dmPrintMutexLocks(DMMutex *mutex, const char *state, const char *file, const int line)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
218 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
219 int i;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
220
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
221 fprintf(stderr,
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
222 "----------------------\n"
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
223 "%s --> %p @ %s:%d\n"
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
224 "Current thread: %d\n"
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
225 "Mutex : %p (created @ %s:%d)\n",
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
226 state, mutex, file, line,
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
227 SDL_ThreadID(), mutex, mutex->cr_file, mutex->cr_line);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
228
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
229 for (i = 0; i < 8; i++)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
230 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
231 DMMutexLock *lock = &(mutex->locks[i]);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
232 if (lock->used)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
233 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
234 fprintf(stderr,
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
235 "Lock #%d: thread=%d, state=%d\n",
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
236 i, lock->id, lock->state);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
237 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
238 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
239 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
240
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
241 int dmDOMutexLock(DMMutex *mutex, const char *file, const int line)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
242 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
243 if (mutex != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
244 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
245 dmPrintMutexLocks(mutex, "LOCKING", file, line);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
246
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
247 DMMutexLock *lock = dmGetMutexThreadIDLock(mutex);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
248 if (lock != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
249 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
250 int res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
251 if (lock->state == 0)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
252 res = SDL_mutexP(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
253 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
254 res = 1;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
255 lock->state++;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
256 fprintf(stderr, "LOCKING %p @ thread=%d done [1].\n", mutex, SDL_ThreadID());
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
257 return res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
258 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
259 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
260 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
261 int i;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
262 for (i = 0; i < 8; i++)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
263 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
264 DMMutexLock *lock = &(mutex->locks[i]);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
265 if (!lock->used)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
266 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
267 int res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
268 lock->used = TRUE;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
269 lock->id = SDL_ThreadID();
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
270 lock->state++;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
271 res = SDL_mutexP(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
272 fprintf(stderr, "LOCKING %p @ thread=%d done [2].\n", mutex, SDL_ThreadID());
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
273 return res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
274 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
275 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
276 return -2;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
277 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
278 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
279 return -1;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
280 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
281
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
282
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
283 int dmDOMutexUnlock(DMMutex *mutex, const char *file, const int line)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
284 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
285 if (mutex != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
286 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
287 dmPrintMutexLocks(mutex, "UN-LOCKING", file, line);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
288
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
289 DMMutexLock *lock = dmGetMutexThreadIDLock(mutex);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
290 if (lock != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
291 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
292 int res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
293 lock->state--;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
294 if (lock->state == 0)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
295 res = SDL_mutexV(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
296 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
297 res = lock->state;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
298 return res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
299 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
300 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
301 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
302 return -2;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
303 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
304 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
305 return -1;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
306 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
307
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
308
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
309 DMMutex * dmDOCreateMutex(const char *file, const int line)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
310 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
311 DMMutex *mutex = dmMalloc0(sizeof(DMMutex));
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
312 if (mutex == NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
313 return NULL;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
314 mutex->cr_file = dm_strdup(file);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
315 mutex->cr_line = line;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
316 mutex->m = SDL_CreateMutex();
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
317 return mutex;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
318 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
319
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
320
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
321 void dmDestroyMutex(DMMutex *mutex)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
322 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
323 if (mutex != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
324 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
325 SDL_DestroyMutex(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
326 dmFree(mutex);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
327 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
328 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
329
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
330 #endif