tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Allocator.c (4475B)


      1 /***************************************************************************************************
      2 
      3  Zyan Core Library (Zycore-C)
      4 
      5  Original Author : Florian Bernd
      6 
      7 * Permission is hereby granted, free of charge, to any person obtaining a copy
      8 * of this software and associated documentation files (the "Software"), to deal
      9 * in the Software without restriction, including without limitation the rights
     10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     11 * copies of the Software, and to permit persons to whom the Software is
     12 * furnished to do so, subject to the following conditions:
     13 *
     14 * The above copyright notice and this permission notice shall be included in all
     15 * copies or substantial portions of the 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 ***************************************************************************************************/
     26 
     27 #include "zydis/Zycore/Allocator.h"
     28 #include "zydis/Zycore/LibC.h"
     29 
     30 /* ============================================================================================== */
     31 /* Internal functions                                                                             */
     32 /* ============================================================================================== */
     33 
     34 /* ---------------------------------------------------------------------------------------------- */
     35 /* Default allocator                                                                              */
     36 /* ---------------------------------------------------------------------------------------------- */
     37 
     38 #ifndef ZYAN_NO_LIBC
     39 
     40 static ZyanStatus ZyanAllocatorDefaultAllocate(ZyanAllocator* allocator, void** p,
     41    ZyanUSize element_size, ZyanUSize n)
     42 {
     43    ZYAN_ASSERT(allocator);
     44    ZYAN_ASSERT(p);
     45    ZYAN_ASSERT(element_size);
     46    ZYAN_ASSERT(n);
     47 
     48    ZYAN_UNUSED(allocator);
     49 
     50    *p = ZYAN_MALLOC(element_size * n);
     51    if (!*p)
     52    {
     53        return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
     54    }
     55 
     56    return ZYAN_STATUS_SUCCESS;
     57 }
     58 
     59 static ZyanStatus ZyanAllocatorDefaultReallocate(ZyanAllocator* allocator, void** p,
     60    ZyanUSize element_size, ZyanUSize n)
     61 {
     62    ZYAN_ASSERT(allocator);
     63    ZYAN_ASSERT(p);
     64    ZYAN_ASSERT(element_size);
     65    ZYAN_ASSERT(n);
     66 
     67    ZYAN_UNUSED(allocator);
     68 
     69    void* const x = ZYAN_REALLOC(*p, element_size * n);
     70    if (!x)
     71    {
     72        return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
     73    }
     74    *p = x;
     75 
     76    return ZYAN_STATUS_SUCCESS;
     77 }
     78 
     79 static ZyanStatus ZyanAllocatorDefaultDeallocate(ZyanAllocator* allocator, void* p,
     80    ZyanUSize element_size, ZyanUSize n)
     81 {
     82    ZYAN_ASSERT(allocator);
     83    ZYAN_ASSERT(p);
     84    ZYAN_ASSERT(element_size);
     85    ZYAN_ASSERT(n);
     86 
     87    ZYAN_UNUSED(allocator);
     88    ZYAN_UNUSED(element_size);
     89    ZYAN_UNUSED(n);
     90 
     91    ZYAN_FREE(p);
     92 
     93    return ZYAN_STATUS_SUCCESS;
     94 }
     95 
     96 #endif // ZYAN_NO_LIBC
     97 
     98 /* ---------------------------------------------------------------------------------------------- */
     99 
    100 /* ============================================================================================== */
    101 /* Exported functions                                                                             */
    102 /* ============================================================================================== */
    103 
    104 ZyanStatus ZyanAllocatorInit(ZyanAllocator* allocator, ZyanAllocatorAllocate allocate,
    105    ZyanAllocatorAllocate reallocate, ZyanAllocatorDeallocate deallocate)
    106 {
    107    if (!allocator || !allocate || !reallocate || !deallocate)
    108    {
    109        return ZYAN_STATUS_INVALID_ARGUMENT;
    110    }
    111 
    112    allocator->allocate   = allocate;
    113    allocator->reallocate = reallocate;
    114    allocator->deallocate = deallocate;
    115 
    116    return ZYAN_STATUS_SUCCESS;
    117 }
    118 
    119 #ifndef ZYAN_NO_LIBC
    120 
    121 ZyanAllocator* ZyanAllocatorDefault(void)
    122 {
    123    static ZyanAllocator allocator =
    124    {
    125        &ZyanAllocatorDefaultAllocate,
    126        &ZyanAllocatorDefaultReallocate,
    127        &ZyanAllocatorDefaultDeallocate
    128    };
    129    return &allocator;
    130 }
    131 
    132 #endif
    133 
    134 /* ============================================================================================== */