summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/dl03/dl-bit-alloc.c
blob: e4e3cf4c82571143edafc3efd9cb0510934c8050 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (c) 2016, 2018 Chris Johns <chrisj@rtems.org>.  All rights reserved.
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "tmacros.h"

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include <rtems.h>
#include <rtems/dumpbuf.h>

#include <rtems/rtl/rtl.h>
#include <rtems/rtl/rtl-trace.h>

#include "dl-bit-alloc.h"

#include "rtl-bit-alloc.h"

#define NUM(m) (sizeof(m) / sizeof(m[0]))

typedef struct
{
  size_t          size;
  const uint32_t* map;
  size_t          map_size;
} alloc_check;

const uint32_t map_1[] = {
  0x0000003f,
  0x00000000,
  0x00000000,
  0x00000000
};

const uint32_t map_a_1[] = {
  0xffffffff,
  0x00000000,
  0x00000000,
  0x00000000
};

const uint32_t map_a_2[] = {
  0xffffffff,
  0x00000001,
  0x00000000,
  0x00000000
};

const uint32_t map_a_3[] = {
  0xffffffff,
  0x00000007,
  0x00000000,
  0x00000000
};

const uint32_t map_a_4[] = {
  0xffffffff,
  0x00000fff,
  0x00000000,
  0x00000000
};

const uint32_t map_a_5[] = {
  0xffffffff,
  0x00001fff,
  0x00000000,
  0x00000000
};

const uint32_t map_a_6[] = {
  0xffffffff,
  0x00007fff,
  0x00000000,
  0x00000000
};

const uint32_t map_a_7[] = {
  0xffffffff,
  0x0007ffff,
  0x00000000,
  0x00000000
};

const uint32_t map_a_8[] = {
  0xffffffff,
  0x07ffffff,
  0x00000000,
  0x00000000
};

const uint32_t map_b_1[] = {
  0xffffffff,
  0x07ffffff,
  0x00000000,
  0x00000000
};

const uint32_t map_b_2[] = {
  0xffffffff,
  0x07ffffff,
  0x00000000,
  0x00000000
};

const uint32_t map_b_3[] = {
  0xffffffff,
  0x07ffffff,
  0x00000000,
  0x00000000
};

const uint32_t map_b_4[] = {
  0xffffffff,
  0x07ffffff,
  0x00000000,
  0x00000000
};

const uint32_t map_free_1[] = {
  0xffffffff,
  0x07ffffff,
  0x00000000,
  0x00000000
};

alloc_check a_allocs[] = {
  { 26 * sizeof(uint32_t), map_a_1, NUM(map_a_1) },
  { 4,                     map_a_2, NUM(map_a_2) },
  { 8,                     map_a_3, NUM(map_a_3) },
  { 34,                    map_a_4, NUM(map_a_4) },
  { 4,                     map_a_5, NUM(map_a_5) },
  { 8,                     map_a_6, NUM(map_a_6) },
  { 16,                    map_a_7, NUM(map_a_7) },
  { 32,                    map_a_8, NUM(map_a_8) }
};

alloc_check b_allocs[] = {
  { 24, map_b_1, NUM(map_b_1) },
  { 30, map_b_2, NUM(map_b_2) },
  { 8,  map_b_3, NUM(map_b_3) },
  { 4,  map_b_4, NUM(map_b_4) }
};

static bool check_bit_map(rtems_rtl_bit_alloc* balloc,
                          const uint32_t*      map,
                          size_t               size)
{
  size_t i;
  rtems_print_buffer ((const unsigned char *) balloc->bits, size * sizeof(uint32_t));
  for (i = 0; i < size; ++i)
  {
    if (balloc->bits[i] != map[i])
    {
      printf("bit-map mismatch: %zu: %08" PRIx32 "\n", i, map[i]);
      return false;
    }
  }
  return true;
}

static void delta_bit_map(rtems_rtl_bit_alloc* balloc, uint32_t* last, size_t size)
{
  size_t i;
  for (i = 0; i < size; ++i)
    last[i] = last[i] ^ balloc->bits[i];
  printf("Delta:\n");
  rtems_print_buffer ((const unsigned char *) last, size * sizeof(uint32_t));
  memcpy(last, balloc->bits, size * sizeof(uint32_t));
}

static void dl_init_rtl(void)
{
  /*
   * Check the RTL object is created and can be locked and unlocked.
   */
  rtems_test_assert(rtems_rtl_lock () != NULL);
  rtems_rtl_unlock ();
  rtems_test_assert(rtems_rtl_data_unprotected () != NULL);
  rtems_rtl_trace_set_mask(RTEMS_RTL_TRACE_ALL | RTEMS_RTL_TRACE_CACHE);
}

int dl_bit_alloc_test(void)
{
  rtems_rtl_bit_alloc* balloc;
  void*                a[NUM(a_allocs)];
  void*                b[NUM(b_allocs)];
  uint32_t             last_map[4] = { 0 };
  size_t               i;

  /*
   * Make sure the RTL can initialise.
   */
  dl_init_rtl();

  printf("bit-alloc: open\n");
  balloc = rtems_rtl_bit_alloc_open(NULL, 64 * 1024, sizeof(uint32_t), 21);
  rtems_test_assert(balloc != NULL);

  rtems_test_assert(check_bit_map(balloc, map_1, NUM(map_1)));

  for (i = 0; i < NUM(a_allocs); ++i)
  {
    printf("balloc: %zu\n", a_allocs[i].size);
    a[i] = rtems_rtl_bit_alloc_balloc(balloc, a_allocs[i].size);
    rtems_test_assert(a[i] != NULL);
    printf("balloc: a[%zu] %zu %p\n", i, a_allocs[i].size, a[i]);
    rtems_test_assert(check_bit_map(balloc, a_allocs[i].map, a_allocs[i].map_size));
    delta_bit_map(balloc, last_map, NUM(last_map));
  }

  printf("bfree: %p %zu\n", a[3], a_allocs[3].size);
  rtems_rtl_bit_alloc_bfree(balloc, a[3], a_allocs[3].size);
  delta_bit_map(balloc, last_map, NUM(last_map));
  a[3] = NULL;

  for (i = 0; i < NUM(b_allocs); ++i)
  {
    printf("balloc: %zu\n", b_allocs[i].size);
    b[i] = rtems_rtl_bit_alloc_balloc(balloc, b_allocs[i].size);
    rtems_test_assert(b[i] != NULL);
    printf("balloc: b[%zu] %zu %p\n", i, b_allocs[i].size, b[i]);
    rtems_print_buffer ((const unsigned char *) balloc->bits, 8 * sizeof(uint32_t));
    delta_bit_map(balloc, last_map, NUM(last_map));
  }

  for (i = 0; i < NUM(a_allocs); ++i)
  {
    if (a[i] != NULL)
    {
      printf("bfree: a[%zu] %p %zu\n", i, a[i], a_allocs[i].size);
      rtems_rtl_bit_alloc_bfree(balloc, a[i], a_allocs[i].size);
      rtems_print_buffer ((const unsigned char *) balloc->bits, 8 * sizeof(uint32_t));
      delta_bit_map(balloc, last_map, NUM(last_map));
    }
  }

  for (i = 0; i < NUM(b_allocs); ++i)
  {
    if (b[i] != NULL)
    {
      printf("bfree: b[%zu] %p %zu\n", i, b[i], b_allocs[i].size);
      rtems_rtl_bit_alloc_bfree(balloc, b[i], b_allocs[i].size);
      rtems_print_buffer ((const unsigned char *) balloc->bits, 8 * sizeof(uint32_t));
    }
  }

  printf("bit-alloc: close\n");
  rtems_rtl_bit_alloc_close(balloc);

  return 0;
}