1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2004-2009 by Daniel Stenberg
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
16 */
17
18 #include "ares_setup.h"
19
20 #include "ares.h"
21 #include "ares_private.h"
22
23 /* library-private global and unique instance vars */
24
25 #if defined(ANDROID) || defined(__ANDROID__)
26 #include "ares_android.h"
27 #endif
28
29 /* library-private global vars with source visibility restricted to this file */
30
31 static unsigned int ares_initialized;
32 static int ares_init_flags;
33
34 /* library-private global vars with visibility across the whole library */
35
36 /* Some systems may return either NULL or a valid pointer on malloc(0). c-ares should
37 * never call malloc(0) so lets return NULL so we're more likely to find an issue if it
38 * were to occur. */
39
default_malloc(size_t size)40 static void *default_malloc(size_t size) { if (size == 0) { return NULL; } return malloc(size); }
41
42 #if defined(WIN32)
43 /* We need indirections to handle Windows DLL rules. */
default_realloc(void * p,size_t size)44 static void *default_realloc(void *p, size_t size) { return realloc(p, size); }
default_free(void * p)45 static void default_free(void *p) { free(p); }
46 #else
47 # define default_realloc realloc
48 # define default_free free
49 #endif
50 void *(*ares_malloc)(size_t size) = default_malloc;
51 void *(*ares_realloc)(void *ptr, size_t size) = default_realloc;
52 void (*ares_free)(void *ptr) = default_free;
53
ares_library_init(int flags)54 int ares_library_init(int flags)
55 {
56 if (ares_initialized)
57 {
58 ares_initialized++;
59 return ARES_SUCCESS;
60 }
61 ares_initialized++;
62
63 /* NOTE: ARES_LIB_INIT_WIN32 flag no longer used */
64
65 ares_init_flags = flags;
66
67 return ARES_SUCCESS;
68 }
69
ares_library_init_mem(int flags,void * (* amalloc)(size_t size),void (* afree)(void * ptr),void * (* arealloc)(void * ptr,size_t size))70 int ares_library_init_mem(int flags,
71 void *(*amalloc)(size_t size),
72 void (*afree)(void *ptr),
73 void *(*arealloc)(void *ptr, size_t size))
74 {
75 if (amalloc)
76 ares_malloc = amalloc;
77 if (arealloc)
78 ares_realloc = arealloc;
79 if (afree)
80 ares_free = afree;
81 return ares_library_init(flags);
82 }
83
84
ares_library_cleanup(void)85 void ares_library_cleanup(void)
86 {
87 if (!ares_initialized)
88 return;
89 ares_initialized--;
90 if (ares_initialized)
91 return;
92
93 /* NOTE: ARES_LIB_INIT_WIN32 flag no longer used */
94
95 #if defined(ANDROID) || defined(__ANDROID__)
96 ares_library_cleanup_android();
97 #endif
98
99 ares_init_flags = ARES_LIB_INIT_NONE;
100 ares_malloc = malloc;
101 ares_realloc = realloc;
102 ares_free = free;
103 }
104
105
ares_library_initialized(void)106 int ares_library_initialized(void)
107 {
108 #ifdef USE_WINSOCK
109 if (!ares_initialized)
110 return ARES_ENOTINITIALIZED;
111 #endif
112 return ARES_SUCCESS;
113 }
114