quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ares_library_init.c (4149B)


      1 /* MIT License
      2  *
      3  * Copyright (c) 1998 Massachusetts Institute of Technology
      4  * Copyright (c) 2004 Daniel Stenberg
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  * SOFTWARE.
     24  *
     25  * SPDX-License-Identifier: MIT
     26  */
     27 
     28 #include "ares_private.h"
     29 
     30 /* library-private global and unique instance vars */
     31 
     32 #if defined(ANDROID) || defined(__ANDROID__)
     33 #  include "ares_android.h"
     34 #endif
     35 
     36 /* library-private global vars with source visibility restricted to this file */
     37 
     38 static unsigned int ares_initialized;
     39 static int          ares_init_flags;
     40 
     41 /* library-private global vars with visibility across the whole library */
     42 
     43 /* Some systems may return either NULL or a valid pointer on malloc(0).  c-ares
     44  * should never call malloc(0) so lets return NULL so we're more likely to find
     45  * an issue if it were to occur. */
     46 
     47 static void        *default_malloc(size_t size)
     48 {
     49   if (size == 0) {
     50     return NULL;
     51   }
     52   return malloc(size);
     53 }
     54 
     55 static void *default_realloc(void *p, size_t size)
     56 {
     57   return realloc(p, size);
     58 }
     59 
     60 static void default_free(void *p)
     61 {
     62   free(p);
     63 }
     64 
     65 static void *(*__ares_malloc)(size_t size)             = default_malloc;
     66 static void *(*__ares_realloc)(void *ptr, size_t size) = default_realloc;
     67 static void (*__ares_free)(void *ptr)                  = default_free;
     68 
     69 void *ares_malloc(size_t size)
     70 {
     71   return __ares_malloc(size);
     72 }
     73 
     74 void *ares_realloc(void *ptr, size_t size)
     75 {
     76   return __ares_realloc(ptr, size);
     77 }
     78 
     79 void ares_free(void *ptr)
     80 {
     81   __ares_free(ptr);
     82 }
     83 
     84 void *ares_malloc_zero(size_t size)
     85 {
     86   void *ptr = ares_malloc(size);
     87   if (ptr != NULL) {
     88     memset(ptr, 0, size);
     89   }
     90 
     91   return ptr;
     92 }
     93 
     94 void *ares_realloc_zero(void *ptr, size_t orig_size, size_t new_size)
     95 {
     96   void *p = ares_realloc(ptr, new_size);
     97   if (p == NULL) {
     98     return NULL;
     99   }
    100 
    101   if (new_size > orig_size) {
    102     memset((unsigned char *)p + orig_size, 0, new_size - orig_size);
    103   }
    104 
    105   return p;
    106 }
    107 
    108 int ares_library_init(int flags)
    109 {
    110   if (ares_initialized) {
    111     ares_initialized++;
    112     return ARES_SUCCESS;
    113   }
    114   ares_initialized++;
    115 
    116   /* NOTE: ARES_LIB_INIT_WIN32 flag no longer used */
    117 
    118   ares_init_flags = flags;
    119 
    120   return ARES_SUCCESS;
    121 }
    122 
    123 int ares_library_init_mem(int flags, void *(*amalloc)(size_t size),
    124                           void (*afree)(void *ptr),
    125                           void *(*arealloc)(void *ptr, size_t size))
    126 {
    127   if (amalloc) {
    128     __ares_malloc = amalloc;
    129   }
    130   if (arealloc) {
    131     __ares_realloc = arealloc;
    132   }
    133   if (afree) {
    134     __ares_free = afree;
    135   }
    136   return ares_library_init(flags);
    137 }
    138 
    139 void ares_library_cleanup(void)
    140 {
    141   if (!ares_initialized) {
    142     return;
    143   }
    144   ares_initialized--;
    145   if (ares_initialized) {
    146     return;
    147   }
    148 
    149   /* NOTE: ARES_LIB_INIT_WIN32 flag no longer used */
    150 
    151 #if defined(ANDROID) || defined(__ANDROID__)
    152   ares_library_cleanup_android();
    153 #endif
    154 
    155   ares_init_flags = ARES_LIB_INIT_NONE;
    156   __ares_malloc   = default_malloc;
    157   __ares_realloc  = default_realloc;
    158   __ares_free     = default_free;
    159 }
    160 
    161 int ares_library_initialized(void)
    162 {
    163 #ifdef USE_WINSOCK
    164   if (!ares_initialized) {
    165     return ARES_ENOTINITIALIZED;
    166   }
    167 #endif
    168   return ARES_SUCCESS;
    169 }