-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
mxml-private.c
280 lines (214 loc) · 5.88 KB
/
mxml-private.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Private functions for Mini-XML, a small XML file parsing library.
//
// https://www.msweet.org/mxml
//
// Copyright © 2003-2024 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#include "mxml-private.h"
//
// Some crazy people think that unloading a shared object is a good or safe
// thing to do. Unfortunately, most objects are simply *not* safe to unload
// and bad things *will* happen.
//
// The following mess of conditional code allows us to provide a destructor
// function in Mini-XML for our thread-global storage so that it can possibly
// be unloaded safely, although since there is no standard way to do so I
// can't even provide any guarantees that you can do it safely on all platforms.
//
// This code currently supports AIX, HP-UX, Linux, macOS, Solaris, and
// Windows. It might work on the BSDs and IRIX, but I haven't tested that.
//
#if defined(__sun) || defined(_AIX)
# pragma fini(_mxml_fini)
# define _MXML_FINI _mxml_fini
#elif defined(__hpux)
# pragma FINI _mxml_fini
# define _MXML_FINI _mxml_fini
#elif defined(__GNUC__) // Linux and macOS
# define _MXML_FINI __attribute((destructor)) _mxml_fini
#else
# define _MXML_FINI _fini
#endif // __sun
//
// 'mxmlSetStringCallbacks()' - Set the string copy/free callback functions.
//
// This function sets the string copy/free callback functions for the current
// thread. The `strcopy_cb` function makes a copy of the provided string while
// the `strfree_cb` function frees the copy. Each callback accepts the
// `str_cbdata` pointer along with the pointer to the string:
//
// ```c
// char *my_strcopy_cb(void *cbdata, const char *s)
// {
// ... make a copy of "s" ...
// }
//
// void my_strfree_cb(void *cbdata, char *s)
// {
// ... release the memory used by "s" ...
// }
// ```
//
// The default `strcopy_cb` function calls `strdup` while the default
// `strfree_cb` function calls `free`.
//
void
mxmlSetStringCallbacks(
mxml_strcopy_cb_t strcopy_cb, // I - String copy callback function
mxml_strfree_cb_t strfree_cb, // I - String free callback function
void *str_cbdata) // I - String callback data
{
_mxml_global_t *global = _mxml_global();
// Global data
global->strcopy_cb = strcopy_cb;
global->strfree_cb = strfree_cb;
global->str_cbdata = str_cbdata;
}
//
// '_mxml_strcopy()' - Copy a string.
//
char * // O - Copy of string
_mxml_strcopy(const char *s) // I - String
{
_mxml_global_t *global = _mxml_global();
// Global data
if (!s)
return (NULL);
if (global->strcopy_cb)
return ((global->strcopy_cb)(global->str_cbdata, s));
else
return (strdup(s));
}
//
// '_mxml_strfree()' - Free a string.
//
void
_mxml_strfree(char *s) // I - String
{
_mxml_global_t *global = _mxml_global();
// Global data
if (!s)
return;
if (global->strfree_cb)
(global->strfree_cb)(global->str_cbdata, s);
else
free((void *)s);
}
#ifdef HAVE_PTHREAD_H // POSIX threading
# include <pthread.h>
static int _mxml_initialized = 0;
// Have we been initialized?
static pthread_key_t _mxml_key; // Thread local storage key
static pthread_once_t _mxml_key_once = PTHREAD_ONCE_INIT;
// One-time initialization object
static void _mxml_init(void);
static void _mxml_destructor(void *g);
//
// '_mxml_destructor()' - Free memory used for globals...
//
static void
_mxml_destructor(void *g) // I - Global data
{
free(g);
}
//
// '_mxml_fini()' - Clean up when unloaded.
//
static void
_MXML_FINI(void)
{
if (_mxml_initialized)
pthread_key_delete(_mxml_key);
}
//
// '_mxml_global()' - Get global data.
//
_mxml_global_t * // O - Global data
_mxml_global(void)
{
_mxml_global_t *global; // Global data
pthread_once(&_mxml_key_once, _mxml_init);
if ((global = (_mxml_global_t *)pthread_getspecific(_mxml_key)) == NULL)
{
global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
pthread_setspecific(_mxml_key, global);
}
return (global);
}
//
// '_mxml_init()' - Initialize global data...
//
static void
_mxml_init(void)
{
_mxml_initialized = 1;
pthread_key_create(&_mxml_key, _mxml_destructor);
}
#elif defined(_WIN32) && defined(MXML1_EXPORTS) // WIN32 threading
# include <windows.h>
static DWORD _mxml_tls_index; // Index for global storage
//
// 'DllMain()' - Main entry for library.
//
BOOL WINAPI // O - Success/failure
DllMain(HINSTANCE hinst, // I - DLL module handle
DWORD reason, // I - Reason
LPVOID reserved) // I - Unused
{
_mxml_global_t *global; // Global data
(void)hinst;
(void)reserved;
switch (reason)
{
case DLL_PROCESS_ATTACH : // Called on library initialization
if ((_mxml_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES)
return (FALSE);
break;
case DLL_THREAD_DETACH : // Called when a thread terminates
if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
free(global);
break;
case DLL_PROCESS_DETACH : // Called when library is unloaded
if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
free(global);
TlsFree(_mxml_tls_index);
break;
default:
break;
}
return (TRUE);
}
//
// '_mxml_global()' - Get global data.
//
_mxml_global_t * // O - Global data
_mxml_global(void)
{
_mxml_global_t *global; // Global data
if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) == NULL)
{
global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
TlsSetValue(_mxml_tls_index, (LPVOID)global);
}
return (global);
}
#else // No threading
//
// '_mxml_global()' - Get global data.
//
_mxml_global_t * // O - Global data
_mxml_global(void)
{
static _mxml_global_t global = // Global data
{
NULL, // strcopy_cb
NULL, // strfree_cb
NULL, // str_cbdata
};
return (&global);
}
#endif // HAVE_PTHREAD_H