rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

test_mem_allocator.c (5389B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "mem_allocator.h"
     17 #include "rsys.h"
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <string.h>
     21 
     22 static void
     23 test_regular(void)
     24 {
     25   void* p = NULL;
     26   void* q[3] = {NULL, NULL, NULL};
     27   size_t i = 0;
     28 
     29   p = mem_alloc_aligned(1024, ALIGNOF(char));
     30   CHK(p != NULL);
     31   CHK(IS_ALIGNED((uintptr_t)p, ALIGNOF(char)) == 1);
     32   mem_rm( p );
     33 
     34   q[0] = mem_alloc_aligned(10, 64);
     35   q[1] = mem_alloc(58);
     36   q[2] = mem_alloc(78);
     37   CHK(q[0] != NULL);
     38   CHK(q[1] != NULL);
     39   CHK(q[2] != NULL);
     40   CHK(IS_ALIGNED((uintptr_t )q[0], 64 ) == 1);
     41 
     42   p = mem_calloc(1, 4);
     43   CHK(p != NULL);
     44   FOR_EACH(i, 0, 4) {
     45     CHK(((char*)p)[i] == 0);
     46   }
     47   FOR_EACH(i, 0, 4) {
     48     ((char*)p)[i] = (char)i;
     49   }
     50 
     51   mem_rm(q[1]);
     52 
     53   p = mem_realloc(p, 8);
     54   FOR_EACH(i, 0, 4) {
     55     CHK(((char*)p)[i] == (char)i);
     56   }
     57   FOR_EACH(i, 4, 8) {
     58     ((char*)p)[i] = (char)i;
     59   }
     60 
     61   mem_rm(q[2]);
     62 
     63   p = mem_realloc(p, 5);
     64   FOR_EACH(i, 0, 5) {
     65     CHK(((char*)p )[i] == (char)i);
     66   }
     67 
     68   mem_rm(p);
     69 
     70   p = NULL;
     71   p = mem_realloc(NULL, 16);
     72   CHK(p != NULL);
     73   p = mem_realloc(p, 0);
     74 
     75   mem_rm(q[0]);
     76 
     77   CHK(mem_alloc_aligned(1024, 0 ) == NULL);
     78   CHK(mem_alloc_aligned(1024, 3 ) == NULL);
     79 }
     80 
     81 static void
     82 test_allocator(struct mem_allocator* allocator)
     83 {
     84   char dump[24];
     85   void* p = NULL;
     86   void* q[3] = {NULL, NULL, NULL};
     87   size_t i = 0;
     88 
     89   p = MEM_ALLOC_ALIGNED(allocator, 1024, ALIGNOF(char));
     90   CHK(p != NULL);
     91   CHK(IS_ALIGNED((uintptr_t)p, ALIGNOF(char)) == 1);
     92   MEM_RM(allocator, p);
     93 
     94   q[0] = MEM_ALLOC_ALIGNED(allocator, 10, 8);
     95   q[1] = MEM_CALLOC(allocator, 1, 58);
     96   q[2] = MEM_ALLOC(allocator, 78);
     97   CHK(q[0] != NULL);
     98   CHK(q[1] != NULL);
     99   CHK(q[2] != NULL);
    100   CHK(IS_ALIGNED((uintptr_t)q[0], 8) == 1);
    101 
    102   p = MEM_CALLOC(allocator, 2, 2);
    103   CHK(p != NULL);
    104   for(i = 0; i < 4; ++i)
    105     CHK(((char*)p)[i] == 0);
    106   for(i = 0; i < 4; ++i)
    107     ((char*)p)[i] = (char)i;
    108 
    109   MEM_DUMP(allocator, dump, 24);
    110   printf("dump:\n%s\n", dump);
    111   MEM_DUMP(allocator, dump, 16);
    112   printf("truncated dump:\n%s\n", dump);
    113   MEM_DUMP(allocator, NULL, 0); /* may not crash */
    114 
    115   MEM_RM(allocator, q[1]);
    116 
    117   p = MEM_REALLOC(allocator, p, 8);
    118   for(i = 0; i < 4; ++i)
    119     CHK(((char*)p)[i] == (char)i);
    120   for(i = 4; i < 8; ++i)
    121     ((char*)p)[i] = (char)i;
    122 
    123   MEM_RM(allocator, q[2]);
    124 
    125   p = MEM_REALLOC(allocator, p, 5);
    126   for(i = 0; i < 5; ++i)
    127     CHK(((char*)p)[i] == (char)i);
    128 
    129   MEM_RM(allocator, p);
    130 
    131   p = NULL;
    132   p = MEM_REALLOC(allocator, NULL, 16);
    133   CHK(p != NULL);
    134   p = MEM_REALLOC(allocator, p, 0);
    135 
    136   MEM_RM(allocator, q[0]);
    137 
    138   CHK(MEM_ALLOC_ALIGNED(allocator, 1024, 0) == NULL);
    139   CHK(MEM_ALLOC_ALIGNED(allocator, 1024, 3) == NULL);
    140   CHK(MEM_ALLOCATED_SIZE(allocator) == 0);
    141 }
    142 
    143 int
    144 main(int argc, char** argv)
    145 {
    146   struct mem_allocator allocator;
    147   struct mem_allocator allocator2;
    148   int mem[8];
    149 
    150   (void)argc;
    151   (void)argv;
    152 
    153   printf("-- Common allocation functions\n");
    154   test_regular();
    155 
    156   printf("-- Default allocator\n");
    157   test_allocator(&mem_default_allocator);
    158 
    159   printf("\n-- Regular allocator\n");
    160   CHK(mem_init_regular_allocator(NULL) == RES_BAD_ARG);
    161   CHK(mem_init_regular_allocator(&allocator) == RES_OK);
    162   test_allocator(&allocator);
    163   mem_shutdown_regular_allocator(&allocator);
    164 
    165   printf("\n-- Proxy allocator of default allocator\n");
    166   CHK(mem_init_proxy_allocator(NULL, NULL) == RES_BAD_ARG);
    167   CHK(mem_init_proxy_allocator(&allocator, NULL) == RES_BAD_ARG);
    168   CHK(mem_init_proxy_allocator(NULL, &mem_default_allocator) == RES_BAD_ARG);
    169   CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK);
    170   test_allocator(&allocator);
    171 
    172   printf("\n-- Proxy allocator of proxy allocator\n");
    173   CHK(mem_init_proxy_allocator(&allocator2, &allocator) == RES_OK);
    174   test_allocator(&allocator2);
    175 
    176   mem_shutdown_proxy_allocator(&allocator2);
    177   mem_shutdown_proxy_allocator(&allocator);
    178 
    179   printf("\n-- LIFO allocator\n");
    180   CHK(mem_init_lifo_allocator(NULL, NULL, 4096) == RES_BAD_ARG);
    181   CHK(mem_init_lifo_allocator(&allocator, NULL, 4096) == RES_BAD_ARG);
    182   CHK(mem_init_lifo_allocator(NULL, &mem_default_allocator, 4096) == RES_BAD_ARG);
    183   CHK(mem_init_lifo_allocator(&allocator, &mem_default_allocator, 4096) == RES_OK);
    184   CHK(MEM_ALLOC(&allocator, 4097) == NULL);
    185   test_allocator(&allocator);
    186   mem_shutdown_lifo_allocator(&allocator);
    187 
    188   CHK(MEM_AREA_OVERLAP(mem, sizeof(int[2]), mem + 2, sizeof(int[6])) == 0);
    189   CHK(MEM_AREA_OVERLAP(mem + 4, sizeof(int[4]), mem, sizeof(int[4])) == 0);
    190   CHK(MEM_AREA_OVERLAP(mem, sizeof(int[2]), mem + 1, sizeof(int[7])) == 1);
    191   CHK(MEM_AREA_OVERLAP(mem + 7, sizeof(int[1]), mem, sizeof(int[8])) == 1);
    192 
    193   CHK(MEM_ALLOCATED_SIZE(&mem_default_allocator) == 0);
    194   CHK(mem_allocated_size() == 0);
    195 
    196   return 0;
    197 }