Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:32

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2016, 2018 Chris Johns <chrisj@rtems.org>.  All rights reserved.
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include "tmacros.h"
0033 
0034 #include <sys/types.h>
0035 #include <unistd.h>
0036 #include <stdlib.h>
0037 #include <stdio.h>
0038 
0039 #include <rtems.h>
0040 #include <rtems/dumpbuf.h>
0041 
0042 #include <rtems/rtl/rtl.h>
0043 #include <rtems/rtl/rtl-trace.h>
0044 
0045 #include "dl-bit-alloc.h"
0046 
0047 #include "rtl-bit-alloc.h"
0048 
0049 #define NUM(m) (sizeof(m) / sizeof(m[0]))
0050 
0051 typedef struct
0052 {
0053   size_t          size;
0054   const uint32_t* map;
0055   size_t          map_size;
0056 } alloc_check;
0057 
0058 const uint32_t map_1[] = {
0059   0x0000003f,
0060   0x00000000,
0061   0x00000000,
0062   0x00000000
0063 };
0064 
0065 const uint32_t map_a_1[] = {
0066   0xffffffff,
0067   0x00000000,
0068   0x00000000,
0069   0x00000000
0070 };
0071 
0072 const uint32_t map_a_2[] = {
0073   0xffffffff,
0074   0x00000001,
0075   0x00000000,
0076   0x00000000
0077 };
0078 
0079 const uint32_t map_a_3[] = {
0080   0xffffffff,
0081   0x00000007,
0082   0x00000000,
0083   0x00000000
0084 };
0085 
0086 const uint32_t map_a_4[] = {
0087   0xffffffff,
0088   0x00000fff,
0089   0x00000000,
0090   0x00000000
0091 };
0092 
0093 const uint32_t map_a_5[] = {
0094   0xffffffff,
0095   0x00001fff,
0096   0x00000000,
0097   0x00000000
0098 };
0099 
0100 const uint32_t map_a_6[] = {
0101   0xffffffff,
0102   0x00007fff,
0103   0x00000000,
0104   0x00000000
0105 };
0106 
0107 const uint32_t map_a_7[] = {
0108   0xffffffff,
0109   0x0007ffff,
0110   0x00000000,
0111   0x00000000
0112 };
0113 
0114 const uint32_t map_a_8[] = {
0115   0xffffffff,
0116   0x07ffffff,
0117   0x00000000,
0118   0x00000000
0119 };
0120 
0121 const uint32_t map_b_1[] = {
0122   0xffffffff,
0123   0x07ffffff,
0124   0x00000000,
0125   0x00000000
0126 };
0127 
0128 const uint32_t map_b_2[] = {
0129   0xffffffff,
0130   0x07ffffff,
0131   0x00000000,
0132   0x00000000
0133 };
0134 
0135 const uint32_t map_b_3[] = {
0136   0xffffffff,
0137   0x07ffffff,
0138   0x00000000,
0139   0x00000000
0140 };
0141 
0142 const uint32_t map_b_4[] = {
0143   0xffffffff,
0144   0x07ffffff,
0145   0x00000000,
0146   0x00000000
0147 };
0148 
0149 const uint32_t map_free_1[] = {
0150   0xffffffff,
0151   0x07ffffff,
0152   0x00000000,
0153   0x00000000
0154 };
0155 
0156 alloc_check a_allocs[] = {
0157   { 26 * sizeof(uint32_t), map_a_1, NUM(map_a_1) },
0158   { 4,                     map_a_2, NUM(map_a_2) },
0159   { 8,                     map_a_3, NUM(map_a_3) },
0160   { 34,                    map_a_4, NUM(map_a_4) },
0161   { 4,                     map_a_5, NUM(map_a_5) },
0162   { 8,                     map_a_6, NUM(map_a_6) },
0163   { 16,                    map_a_7, NUM(map_a_7) },
0164   { 32,                    map_a_8, NUM(map_a_8) }
0165 };
0166 
0167 alloc_check b_allocs[] = {
0168   { 24, map_b_1, NUM(map_b_1) },
0169   { 30, map_b_2, NUM(map_b_2) },
0170   { 8,  map_b_3, NUM(map_b_3) },
0171   { 4,  map_b_4, NUM(map_b_4) }
0172 };
0173 
0174 static bool check_bit_map(rtems_rtl_bit_alloc* balloc,
0175                           const uint32_t*      map,
0176                           size_t               size)
0177 {
0178   size_t i;
0179   rtems_print_buffer ((const unsigned char *) balloc->bits, size * sizeof(uint32_t));
0180   for (i = 0; i < size; ++i)
0181   {
0182     if (balloc->bits[i] != map[i])
0183     {
0184       printf("bit-map mismatch: %zu: %08" PRIx32 "\n", i, map[i]);
0185       return false;
0186     }
0187   }
0188   return true;
0189 }
0190 
0191 static void delta_bit_map(rtems_rtl_bit_alloc* balloc, uint32_t* last, size_t size)
0192 {
0193   size_t i;
0194   for (i = 0; i < size; ++i)
0195     last[i] = last[i] ^ balloc->bits[i];
0196   printf("Delta:\n");
0197   rtems_print_buffer ((const unsigned char *) last, size * sizeof(uint32_t));
0198   memcpy(last, balloc->bits, size * sizeof(uint32_t));
0199 }
0200 
0201 static void dl_init_rtl(void)
0202 {
0203   /*
0204    * Check the RTL object is created and can be locked and unlocked.
0205    */
0206   rtems_test_assert(rtems_rtl_lock () != NULL);
0207   rtems_rtl_unlock ();
0208   rtems_test_assert(rtems_rtl_data_unprotected () != NULL);
0209   rtems_rtl_trace_set_mask(RTEMS_RTL_TRACE_ALL | RTEMS_RTL_TRACE_CACHE);
0210 }
0211 
0212 int dl_bit_alloc_test(void)
0213 {
0214   rtems_rtl_bit_alloc* balloc;
0215   void*                a[NUM(a_allocs)];
0216   void*                b[NUM(b_allocs)];
0217   uint32_t             last_map[4] = { 0 };
0218   size_t               i;
0219 
0220   /*
0221    * Make sure the RTL can initialise.
0222    */
0223   dl_init_rtl();
0224 
0225   printf("bit-alloc: open\n");
0226   balloc = rtems_rtl_bit_alloc_open(NULL, 64 * 1024, sizeof(uint32_t), 21);
0227   rtems_test_assert(balloc != NULL);
0228 
0229   rtems_test_assert(check_bit_map(balloc, map_1, NUM(map_1)));
0230 
0231   for (i = 0; i < NUM(a_allocs); ++i)
0232   {
0233     printf("balloc: %zu\n", a_allocs[i].size);
0234     a[i] = rtems_rtl_bit_alloc_balloc(balloc, a_allocs[i].size);
0235     rtems_test_assert(a[i] != NULL);
0236     printf("balloc: a[%zu] %zu %p\n", i, a_allocs[i].size, a[i]);
0237     rtems_test_assert(check_bit_map(balloc, a_allocs[i].map, a_allocs[i].map_size));
0238     delta_bit_map(balloc, last_map, NUM(last_map));
0239   }
0240 
0241   printf("bfree: %p %zu\n", a[3], a_allocs[3].size);
0242   rtems_rtl_bit_alloc_bfree(balloc, a[3], a_allocs[3].size);
0243   delta_bit_map(balloc, last_map, NUM(last_map));
0244   a[3] = NULL;
0245 
0246   for (i = 0; i < NUM(b_allocs); ++i)
0247   {
0248     printf("balloc: %zu\n", b_allocs[i].size);
0249     b[i] = rtems_rtl_bit_alloc_balloc(balloc, b_allocs[i].size);
0250     rtems_test_assert(b[i] != NULL);
0251     printf("balloc: b[%zu] %zu %p\n", i, b_allocs[i].size, b[i]);
0252     rtems_print_buffer ((const unsigned char *) balloc->bits, 8 * sizeof(uint32_t));
0253     delta_bit_map(balloc, last_map, NUM(last_map));
0254   }
0255 
0256   for (i = 0; i < NUM(a_allocs); ++i)
0257   {
0258     if (a[i] != NULL)
0259     {
0260       printf("bfree: a[%zu] %p %zu\n", i, a[i], a_allocs[i].size);
0261       rtems_rtl_bit_alloc_bfree(balloc, a[i], a_allocs[i].size);
0262       rtems_print_buffer ((const unsigned char *) balloc->bits, 8 * sizeof(uint32_t));
0263       delta_bit_map(balloc, last_map, NUM(last_map));
0264     }
0265   }
0266 
0267   for (i = 0; i < NUM(b_allocs); ++i)
0268   {
0269     if (b[i] != NULL)
0270     {
0271       printf("bfree: b[%zu] %p %zu\n", i, b[i], b_allocs[i].size);
0272       rtems_rtl_bit_alloc_bfree(balloc, b[i], b_allocs[i].size);
0273       rtems_print_buffer ((const unsigned char *) balloc->bits, 8 * sizeof(uint32_t));
0274     }
0275   }
0276 
0277   printf("bit-alloc: close\n");
0278   rtems_rtl_bit_alloc_close(balloc);
0279 
0280   return 0;
0281 }