annotate src/dmlib.c @ 2294:7f6ba3b32f54

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 03 Jul 2019 10:28:43 +0300
parents 3d40a6767a4e
children 69a5af2eb1ea
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;
1690
12d4da502fa4 Minor NULL check cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1689
diff changeset
18 dmProgAuthor = author != NULL ? author : DM_PROG_AUTHOR;
12d4da502fa4 Minor NULL check cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1689
diff changeset
19 dmProgLicense = license != NULL ? license : DM_PROG_LICENSE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 void dmPrintBanner(FILE *outFile, const char *name, const char *usage)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 {
1689
ef644e3f891f Remove newline from start of the banner.
Matti Hamalainen <ccr@tnsp.org>
parents: 1452
diff changeset
25 fprintf(outFile, "%s", dmProgName);
1063
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
26 if (dmProgVersion != NULL)
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
27 fprintf(outFile, " v%s", dmProgVersion);
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
28 if (dmProgDesc != NULL)
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
29 fprintf(outFile, " (%s)", dmProgDesc);
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
30 fprintf(outFile, "\n");
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
31
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
32 if (dmProgAuthor != NULL)
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
33 fprintf(outFile, "%s\n", dmProgAuthor);
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
34
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
35 if (dmProgLicense != NULL)
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
36 fprintf(outFile, "%s\n", dmProgLicense);
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
37
ada47e30d0c9 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
38 fprintf(outFile, "Usage: %s %s\n", name, usage);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
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 void dmMsgVA(int level, const char *fmt, va_list ap)
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 if (dmVerbosity >= level)
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 fprintf(stderr, "%s: ", dmProgName);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 vfprintf(stderr, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 }
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 void dmPrintVA(int level, const char *fmt, va_list ap)
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 if (dmVerbosity >= level)
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 vfprintf(stderr, fmt, ap);
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 }
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 void dmMsg(int level, const char *fmt, ...)
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 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 va_start(ap, fmt);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 dmMsgVA(level, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 va_end(ap);
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 void dmPrint(int level, const char *fmt, ...)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 va_list ap;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 va_start(ap, fmt);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 dmPrintVA(level, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 va_end(ap);
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 /* Memory handling routines
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 */
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
83 void *dmMalloc(size_t len)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 {
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
85 return malloc(len);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 }
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
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
89 void *dmMalloc0(size_t len)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 {
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
91 return calloc(1, len);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
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
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
95 void *dmCalloc(size_t n, size_t len)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 {
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
97 return calloc(n, len);
0
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
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
101 void *dmRealloc(void *ptr, size_t len)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 {
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
103 return realloc(ptr, len);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 }
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
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
107 void dmFree(void *ptr)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 /* Check for NULL pointers for portability due to some libc
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 * implementations not handling free(NULL) too well.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 */
1451
4f82e7cda289 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1273
diff changeset
112 if (ptr) free(ptr);
0
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
1452
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
116 void dmFreeRReal(void **ptr)
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
117 {
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
118 if (ptr != NULL)
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
119 {
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
120 dmFree(*ptr);
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
121 *ptr = NULL;
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
122 }
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
123 }
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
124
3a1f4e810874 Add dmFreeR() macro/function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1451
diff changeset
125
1166
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
126 #ifndef DM_HAVE_MEMSET
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
127 void * dmMemset(void *ptr, const int c, size_t n)
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
128 {
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
129 Uint8 *p = (Uint8 *) ptr;
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
130
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
131 while (n--)
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
132 *p++ = c;
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
133
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
134 return ptr;
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
135 }
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
136 #endif
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
137
f29fa5b6b748 Add dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
138
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
139 BOOL dmGetIntVal(const char *str, unsigned int *value, BOOL *neg)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 {
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
141 int ch;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
142 BOOL hex = FALSE;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
143
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
144 // Is the value negative?
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
145 if (*str == '-')
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 {
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
147 if (neg == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 return FALSE;
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
149
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
150 *neg = TRUE;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
151 str++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
153 else
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
154 if (neg != NULL)
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
155 *neg = FALSE;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
156
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
157 // Is it hexadecimal?
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
158 if (*str == '$')
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
159 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
160 hex = TRUE;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
161 str++;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
162 }
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
163 else
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
164 if (str[0] == '0' && str[1] == 'x')
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 {
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
166 hex = TRUE;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
167 str += 2;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
168 }
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
169
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
170 // Parse the value
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
171 *value = 0;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
172 if (hex)
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
173 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
174 while ((ch = *str++))
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
175 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
176 if (ch >= '0' && ch <= '9')
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
177 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
178 *value <<= 4;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
179 *value |= ch - '0';
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
180 }
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
181 else
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
182 if (ch >= 'A' && ch <= 'F')
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
183 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
184 *value <<= 4;
2009
3d40a6767a4e Oops, fix hexadecimal value parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2004
diff changeset
185 *value |= ch - 'A' + 10;
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
186 }
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
187 else
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
188 if (ch >= 'a' && ch <= 'f')
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
189 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
190 *value <<= 4;
2009
3d40a6767a4e Oops, fix hexadecimal value parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2004
diff changeset
191 *value |= ch - 'a' + 10;
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
192 }
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
193 else
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
194 return FALSE;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
195 }
0
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 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 {
2004
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
199 while ((ch = *str++))
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
200 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
201 if (ch >= '0' && ch <= '9')
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
202 {
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
203 *value *= 10;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
204 *value += ch - '0';
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
205 }
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
206 else
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
207 return FALSE;
161e731eb152 Improve dmGetIntVal() to accept an optional negative value boolean flag
Matti Hamalainen <ccr@tnsp.org>
parents: 1690
diff changeset
208 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 return TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 908
diff changeset
214 /*
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 908
diff changeset
215 * Error handling and messages
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 #define DM_SYSTEM_ERRORS 100000
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 908
diff changeset
219
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 int dmGetErrno()
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 return DM_SYSTEM_ERRORS + errno;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225
957
b66653c9acb3 Oops, forgot to add const in the actual function .. 100L.
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
226 const char *dmErrorStr(const int error)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 if (error >= DM_SYSTEM_ERRORS)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 return strerror(error - DM_SYSTEM_ERRORS);
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1063
diff changeset
230
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 switch (error)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 case DMERR_OK: return "No error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 case DMERR_FOPEN: return "File open error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 case DMERR_FREAD: return "Read error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 case DMERR_FWRITE: return "Write error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 case DMERR_FSEEK: return "Seek error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 case DMERR_NOT_FOUND: return "Resource not found";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 case DMERR_INVALID_DATA: return "Invalid data";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 case DMERR_MALLOC: return "Memory allocation failure";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 case DMERR_ALREADY_INIT: return "Already initialized";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 case DMERR_INIT_FAIL: return "Initialization failed";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 case DMERR_INVALID_ARGS: return "Invalid arguments";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 case DMERR_NULLPTR: return "NULL pointer";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 case DMERR_NOT_SUPPORTED: return "Operation not supported";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 case DMERR_OUT_OF_DATA: return "Out of data";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 case DMERR_EXTRA_DATA: return "Extra data";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 case DMERR_BOUNDS: return "Bounds check failed";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251
908
03bda6477ad4 Add new error code DMERR_DATA_ERROR.
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
252 case DMERR_DATA_ERROR: return "Data decoding/encoding/parsing error";
03bda6477ad4 Add new error code DMERR_DATA_ERROR.
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
253
1273
132706e3b94b Improve error handling and reporting slightly.
Matti Hamalainen <ccr@tnsp.org>
parents: 1166
diff changeset
254 case DMERR_VERSION: return "Unsupported file format version";
132706e3b94b Improve error handling and reporting slightly.
Matti Hamalainen <ccr@tnsp.org>
parents: 1166
diff changeset
255
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 case DMERR_NOTPACK: return "File is not a PACK";
1273
132706e3b94b Improve error handling and reporting slightly.
Matti Hamalainen <ccr@tnsp.org>
parents: 1166
diff changeset
257 case DMERR_PACK_VERSION: return "Unsupported PACK version";
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 case DMERR_INVALID: return "Invalid data, corrupted file";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 case DMERR_COMPRESSION: return "Error in compression";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 default: return "Unknown error";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 }
294
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
264
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
265
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
266 int dmErrorVA(const int error, const char *fmt, va_list ap)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
267 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
268 if (dmProgName != NULL)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
269 fprintf(stderr, "%s: ", dmProgName);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
270
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
271 vfprintf(stderr, fmt, ap);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
272 return error;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
273 }
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
274
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
275
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
276 int dmError(const int error, const char *fmt, ...)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
277 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
278 int ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
279 va_list ap;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
280
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
281 va_start(ap, fmt);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
282 ret = dmErrorVA(error, fmt, ap);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
283 va_end(ap);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
284 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
285 }
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
286
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
287
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
288 void dmErrorMsg(const char *fmt, ...)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
289 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
290 va_list ap;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
291
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
292 va_start(ap, fmt);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
293 dmErrorVA(DMERR_INTERNAL, fmt, ap);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
294 va_end(ap);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
295 }
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
296
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
297
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
298 /*
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
299 * Mutex debugging
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 957
diff changeset
300 */
294
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
301 #ifdef DM_MUTEX_DEBUG
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
302
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
303 static DMMutexLock * dmGetMutexThreadIDLock(DMMutex *mutex)
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 Uint32 id = SDL_ThreadID();
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
306 int i;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
307 for (i = 0; i < 8; i++)
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 DMMutexLock *lock = &(mutex->locks[i]);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
310 if (lock->used && lock->id == id)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
311 return lock;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
312 }
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 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
315
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
316 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
317 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
318 int i;
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 fprintf(stderr,
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
321 "----------------------\n"
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
322 "%s --> %p @ %s:%d\n"
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
323 "Current thread: %d\n"
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
324 "Mutex : %p (created @ %s:%d)\n",
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
325 state, mutex, file, line,
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
326 SDL_ThreadID(), mutex, mutex->cr_file, mutex->cr_line);
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 for (i = 0; i < 8; i++)
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 DMMutexLock *lock = &(mutex->locks[i]);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
331 if (lock->used)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
332 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
333 fprintf(stderr,
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
334 "Lock #%d: thread=%d, state=%d\n",
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
335 i, lock->id, lock->state);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
336 }
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1063
diff changeset
337 }
294
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
338 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
339
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
340 int dmDOMutexLock(DMMutex *mutex, const char *file, const int line)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
341 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
342 if (mutex != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
343 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
344 dmPrintMutexLocks(mutex, "LOCKING", file, line);
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1063
diff changeset
345
294
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
346 DMMutexLock *lock = dmGetMutexThreadIDLock(mutex);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
347 if (lock != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
348 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
349 int res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
350 if (lock->state == 0)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
351 res = SDL_mutexP(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
352 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
353 res = 1;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
354 lock->state++;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
355 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
356 return res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
357 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
358 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
359 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
360 int i;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
361 for (i = 0; i < 8; i++)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
362 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
363 DMMutexLock *lock = &(mutex->locks[i]);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
364 if (!lock->used)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
365 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
366 int res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
367 lock->used = TRUE;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
368 lock->id = SDL_ThreadID();
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
369 lock->state++;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
370 res = SDL_mutexP(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
371 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
372 return res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
373 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
374 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
375 return -2;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
376 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
377 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
378 return -1;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
379 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
380
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
381
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
382 int dmDOMutexUnlock(DMMutex *mutex, const char *file, const int line)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
383 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
384 if (mutex != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
385 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
386 dmPrintMutexLocks(mutex, "UN-LOCKING", file, line);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
387
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
388 DMMutexLock *lock = dmGetMutexThreadIDLock(mutex);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
389 if (lock != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
390 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
391 int res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
392 lock->state--;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
393 if (lock->state == 0)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
394 res = SDL_mutexV(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
395 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
396 res = lock->state;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
397 return res;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
398 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
399 else
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
400 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
401 return -2;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
402 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
403 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
404 return -1;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
405 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
406
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
407
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
408 DMMutex * dmDOCreateMutex(const char *file, const int line)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
409 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
410 DMMutex *mutex = dmMalloc0(sizeof(DMMutex));
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
411 if (mutex == NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
412 return NULL;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
413 mutex->cr_file = dm_strdup(file);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
414 mutex->cr_line = line;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
415 mutex->m = SDL_CreateMutex();
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
416 return mutex;
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
417 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
418
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
419
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
420 void dmDestroyMutex(DMMutex *mutex)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
421 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
422 if (mutex != NULL)
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
423 {
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
424 SDL_DestroyMutex(mutex->m);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
425 dmFree(mutex);
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
426 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
427 }
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
428
dd9809a93425 Improve mutex debugging facilities.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
429 #endif