40
40
* IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
41
41
*/
42
42
43
+ #include "bson.h"
43
44
#include "common-b64-private.h"
44
45
45
46
#define Assert (Cond ) \
@@ -258,11 +259,26 @@ static const uint8_t mongoc_b64rmap_end = 0xfd;
258
259
static const uint8_t mongoc_b64rmap_space = 0xfe ;
259
260
static const uint8_t mongoc_b64rmap_invalid = 0xff ;
260
261
261
- /**
262
- * Initializing the reverse map is not thread safe, do it at startup.
263
- **/
264
- void
265
- bson_b64_initialize_rmap (void )
262
+ /* initializing the reverse map isn't thread safe, do it in pthread_once. here
263
+ * in a common source file we don't have access to bson or mongoc defines like
264
+ * BSON_ONCE_FUN. */
265
+ #if defined(BSON_OS_UNIX )
266
+ #include <pthread.h>
267
+ #define mongoc_common_once_t pthread_once_t
268
+ #define mongoc_common_once pthread_once
269
+ #define MONGOC_COMMON_ONCE_FUN (n ) void n (void)
270
+ #define MONGOC_COMMON_ONCE_RETURN return
271
+ #define MONGOC_COMMON_ONCE_INIT PTHREAD_ONCE_INIT
272
+ #else
273
+ #define mongoc_common_once_t INIT_ONCE
274
+ #define MONGOC_COMMON_ONCE_INIT INIT_ONCE_STATIC_INIT
275
+ #define mongoc_common_once (o , c ) InitOnceExecuteOnce (o, c, NULL, NULL)
276
+ #define MONGOC_COMMON_ONCE_FUN (n ) \
277
+ BOOL CALLBACK n (PINIT_ONCE _ignored_a, PVOID _ignored_b, PVOID *_ignored_c)
278
+ #define MONGOC_COMMON_ONCE_RETURN return true
279
+ #endif
280
+
281
+ static MONGOC_COMMON_ONCE_FUN (bson_b64_initialize_rmap )
266
282
{
267
283
int i ;
268
284
unsigned char ch ;
@@ -286,6 +302,8 @@ bson_b64_initialize_rmap (void)
286
302
/* Fill reverse mapping for base64 chars */
287
303
for (i = 0 ; Base64 [i ] != '\0' ; ++ i )
288
304
mongoc_b64rmap [(uint8_t ) Base64 [i ]] = i ;
305
+
306
+ MONGOC_COMMON_ONCE_RETURN ;
289
307
}
290
308
291
309
static int
@@ -501,6 +519,10 @@ mongoc_b64_pton_len (char const *src)
501
519
int
502
520
bson_b64_pton (char const * src , uint8_t * target , size_t targsize )
503
521
{
522
+ static mongoc_common_once_t once = MONGOC_COMMON_ONCE_INIT ;
523
+
524
+ mongoc_common_once (& once , bson_b64_initialize_rmap );
525
+
504
526
if (target )
505
527
return mongoc_b64_pton_do (src , target , targsize );
506
528
else
0 commit comments