summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/rfs/rtems-rfs-bitmaps.c
blob: b8bd0b3386038038999fea7855acd7f28caf8f85 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/**
 * @file
 *
 * @brief RTEMS File Systems Bitmap Routines
 * @ingroup rtems_rfs
 * 
 * These functions manage bit maps. A bit map consists of the map of bit
 * allocated in a block and a search map where a bit represents 32 actual
 * bits. The search map allows for a faster search for an available bit as 32
 * search bits can checked in a test.
 */

/*
 *  COPYRIGHT (c) 2010 Chris Johns <chrisj@rtems.org>
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

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

/**
 * Set to 1 to enable warnings when developing.
 */
#define RTEMS_RFS_BITMAP_WARNINGS 0

#if RTEMS_RFS_BITMAP_WARNINGS
#include <stdio.h>
#endif
#include <stdlib.h>
#include <rtems/rfs/rtems-rfs-bitmaps.h>

/**
 * Test a bit in an element. If set return true else return false.
 *
 * @param target The target to test the bit in.
 * @param bit The bit to test.
 * @retval true The bit is set.
 * @retval false The bit is clear.
 */
static bool
rtems_rfs_bitmap_test (rtems_rfs_bitmap_element target,
                       rtems_rfs_bitmap_bit     bit)
{
  return RTEMS_RFS_BITMAP_TEST_BIT (target, bit);
}

/**
 * Set the bits in the element. Bits not set in the bit argument are left
 * unchanged.
 *
 * @param target The target element bits are set.
 * @param bits The bits in the target to set. A 1 in the bits will set the
 *             same bit in the target.
 */
static rtems_rfs_bitmap_element
rtems_rfs_bitmap_set (rtems_rfs_bitmap_element target,
                      rtems_rfs_bitmap_element bits)
{
  return RTEMS_RFS_BITMAP_SET_BITS (target, bits);
}

/**
 * Clear the bits in the element. Bits not set in the bit argument are left
 * unchanged.
 *
 * @param target The target element to clear the bits in.
 * @param bits The bits in the target to clear. A 1 in the bits will clear the
 *             bit in the target.
 */
static rtems_rfs_bitmap_element
rtems_rfs_bitmap_clear (rtems_rfs_bitmap_element target,
                        rtems_rfs_bitmap_element bits)
{
  return RTEMS_RFS_BITMAP_CLEAR_BITS (target, bits);
}

/**
 * Merge the bits in 2 variables based on the mask. A set bit in the mask will
 * merge the bits from bits1 and a clear bit will merge the bits from bits2.
 * The mask is always defined as 1 being set and 0 being clear.
 */
static rtems_rfs_bitmap_element
rtems_rfs_bitmap_merge (rtems_rfs_bitmap_element bits1,
                        rtems_rfs_bitmap_element bits2,
                        rtems_rfs_bitmap_element mask)
{
  /*
   * Use the normal bit operators because we do not change the bits just merge
   * the 2 separate parts.
   */
  bits1 &= mask;
  bits2 &= RTEMS_RFS_BITMAP_INVERT_MASK (mask);
  return bits1 | bits2;
}

/**
 * Match the bits of 2 elements and return true if they match else return
 * false.
 *
 * @param bits1 One set of bits to match.
 * @param bits2 The second set of bits to match.
 * @retval true The bits match.
 * @retval false The bits do not match.
 */
static bool
rtems_rfs_bitmap_match (rtems_rfs_bitmap_element bits1,
                        rtems_rfs_bitmap_element bits2)
{
  return bits1 ^ bits2 ? false : true;
}

#if RTEMS_NOT_USED_BUT_KEPT
/**
 * Match the bits of 2 elements within the mask and return true if they match
 * else return false.
 *
 * @param mask The mask over which the match occurs. A 1 is a valid mask bit.
 * @param bits1 One set of bits to match.
 * @param bits2 The second set of bits to match.
 * @retval true The bits match.
 * @retval false The bits do not match.
 */
static bool
rtems_rfs_bitmap_match_masked (rtems_rfs_bitmap_element mask,
                               rtems_rfs_bitmap_element bits1,
                               rtems_rfs_bitmap_element bits2)
{
  return (bits1 ^ bits2) & mask ? false : true;
}
#endif

/**
 * Return the map after loading from disk if not already loaded.
 *
 * @param control The bitmap control.
 * @param rtems_rfs_bitmap_map* Pointer to the bitmap map data if no error.
 * @return int The error number (errno). No error if 0.
 */
static int
rtems_rfs_bitmap_load_map (rtems_rfs_bitmap_control* control,
                           rtems_rfs_bitmap_map*     map)
{
  int rc;

  if (!control->buffer)
    return ENXIO;

  *map = NULL;

  rc = rtems_rfs_buffer_handle_request (control->fs,
                                        control->buffer,
                                        control->block,
                                        true);
  if (rc)
    return rc;

  *map = rtems_rfs_buffer_data (control->buffer);
  return 0;
}

rtems_rfs_bitmap_element
rtems_rfs_bitmap_mask (unsigned int size)
{
  rtems_rfs_bitmap_element mask = RTEMS_RFS_BITMAP_ELEMENT_FULL_MASK;
  mask >>= (rtems_rfs_bitmap_element_bits () - size);
  return mask;
}

rtems_rfs_bitmap_element
rtems_rfs_bitmap_mask_section (unsigned int start, unsigned int end)
{
  rtems_rfs_bitmap_element mask = 0;
  if (end > start)
    mask = rtems_rfs_bitmap_mask (end - start) << start;
  return mask;
}

int
rtems_rfs_bitmap_map_set (rtems_rfs_bitmap_control* control,
                          rtems_rfs_bitmap_bit      bit)
{
  rtems_rfs_bitmap_map map;
  rtems_rfs_bitmap_map search_map;
  int                  index;
  int                  offset;
  int                 rc;
  rc = rtems_rfs_bitmap_load_map (control, &map);
  if (rc > 0)
    return rc;
  if (bit >= control->size)
    return EINVAL;
  search_map = control->search_bits;
  index      = rtems_rfs_bitmap_map_index (bit);
  offset     = rtems_rfs_bitmap_map_offset (bit);
  map[index] = rtems_rfs_bitmap_set (map[index], 1 << offset);
  if (rtems_rfs_bitmap_match(map[index], RTEMS_RFS_BITMAP_ELEMENT_SET))
  {
    bit = index;
    index  = rtems_rfs_bitmap_map_index (bit);
    offset = rtems_rfs_bitmap_map_offset (bit);
    search_map[index] = rtems_rfs_bitmap_set (search_map[index], 1 << offset);
    control->free--;
    rtems_rfs_buffer_mark_dirty (control->buffer);
  }
  return 0;
}

int
rtems_rfs_bitmap_map_clear (rtems_rfs_bitmap_control* control,
                            rtems_rfs_bitmap_bit      bit)
{
  rtems_rfs_bitmap_map map;
  rtems_rfs_bitmap_map search_map;
  int                  index;
  int                  offset;
  int                  rc;
  rc = rtems_rfs_bitmap_load_map (control, &map);
  if (rc > 0)
    return rc;
  if (bit >= control->size)
    return EINVAL;
  search_map        = control->search_bits;
  index             = rtems_rfs_bitmap_map_index (bit);
  offset            = rtems_rfs_bitmap_map_offset (bit);
  map[index]        = rtems_rfs_bitmap_clear (map[index], 1 << offset);
  bit               = index;
  index             = rtems_rfs_bitmap_map_index (bit);
  offset            = rtems_rfs_bitmap_map_offset(bit);
  search_map[index] = rtems_rfs_bitmap_clear (search_map[index], 1 << offset);
  rtems_rfs_buffer_mark_dirty (control->buffer);
  control->free++;
  return 0;
}

int
rtems_rfs_bitmap_map_test (rtems_rfs_bitmap_control* control,
                           rtems_rfs_bitmap_bit      bit,
                           bool*                     state)
{
  rtems_rfs_bitmap_map map;
  int                  index;
  int                  rc;
  rc = rtems_rfs_bitmap_load_map (control, &map);
  if (rc > 0)
    return rc;
  if (bit >= control->size)
    return EINVAL;
  index = rtems_rfs_bitmap_map_index (bit);
  *state = rtems_rfs_bitmap_test (map[index], bit);
  return 0;
}

int
rtems_rfs_bitmap_map_set_all (rtems_rfs_bitmap_control* control)
{
  rtems_rfs_bitmap_map map;
  size_t               elements;
  int                  e;
  int                  rc;

  rc = rtems_rfs_bitmap_load_map (control, &map);
  if (rc > 0)
    return rc;

  elements = rtems_rfs_bitmap_elements (control->size);

  control->free = 0;

  for (e = 0; e < elements; e++)
    map[e] = RTEMS_RFS_BITMAP_ELEMENT_SET;

  elements = rtems_rfs_bitmap_elements (elements);

  for (e = 0; e < elements; e++)
    control->search_bits[e] = RTEMS_RFS_BITMAP_ELEMENT_SET;

  rtems_rfs_buffer_mark_dirty (control->buffer);

  return 0;
}

int
rtems_rfs_bitmap_map_clear_all (rtems_rfs_bitmap_control* control)
{
  rtems_rfs_bitmap_map map;
  rtems_rfs_bitmap_bit last_search_bit;
  size_t               elements;
  int                  e;
  int                  rc;

  rc = rtems_rfs_bitmap_load_map (control, &map);
  if (rc > 0)
    return rc;

  elements = rtems_rfs_bitmap_elements (control->size);

  control->free = elements;

  for (e = 0; e < elements; e++)
    map[e] = RTEMS_RFS_BITMAP_ELEMENT_CLEAR;

  /*
   * Set the un-mapped bits in the last search element so the available logic
   * works.
   */
  last_search_bit = rtems_rfs_bitmap_map_offset (elements);

  if (last_search_bit == 0)
    last_search_bit = rtems_rfs_bitmap_element_bits ();

  elements = rtems_rfs_bitmap_elements (elements);

  for (e = 0; e < (elements - 1); e++)
    control->search_bits[e] = RTEMS_RFS_BITMAP_ELEMENT_CLEAR;

  control->search_bits[elements - 1] =
    rtems_rfs_bitmap_merge (RTEMS_RFS_BITMAP_ELEMENT_CLEAR,
                            RTEMS_RFS_BITMAP_ELEMENT_SET,
                            rtems_rfs_bitmap_mask (last_search_bit));

  rtems_rfs_buffer_mark_dirty (control->buffer);

  return 0;
}

static int
rtems_rfs_search_map_for_clear_bit (rtems_rfs_bitmap_control* control,
                                    rtems_rfs_bitmap_bit*     bit,
                                    bool*                     found,
                                    size_t                    window,
                                    int                       direction)
{
  rtems_rfs_bitmap_map      map;
  rtems_rfs_bitmap_bit      test_bit;
  rtems_rfs_bitmap_bit      end_bit;
  rtems_rfs_bitmap_element* search_bits;
  int                       search_index;
  int                       search_offset;
  rtems_rfs_bitmap_element* map_bits;
  int                       map_index;
  int                       map_offset;
  int                       rc;

  *found = false;

  /*
   * Load the bitmap.
   */
  rc = rtems_rfs_bitmap_load_map (control, &map);
  if (rc > 0)
    return rc;

  /*
   * Calculate the bit we are testing plus the end point we search over.
   */
  test_bit = *bit;
  end_bit  = test_bit + (window * direction);

  if (end_bit < 0)
    end_bit = 0;
  else if (end_bit >= control->size)
    end_bit = control->size - 1;

  map_index     = rtems_rfs_bitmap_map_index (test_bit);
  map_offset    = rtems_rfs_bitmap_map_offset (test_bit);
  search_index  = rtems_rfs_bitmap_map_index (map_index);
  search_offset = rtems_rfs_bitmap_map_offset (map_index);

  search_bits = &control->search_bits[search_index];
  map_bits    = &map[map_index];

  /*
   * Check each bit from the search map offset for a clear bit.
   */
  do
  {
    /*
     * If any bit is clear find that bit and then search the map element. If
     * all bits are set there are no map bits so move to the next search
     * element.
     */
    if (!rtems_rfs_bitmap_match (*search_bits, RTEMS_RFS_BITMAP_ELEMENT_SET))
    {
      while ((search_offset >= 0)
             && (search_offset < rtems_rfs_bitmap_element_bits ()))
      {
        if (!rtems_rfs_bitmap_test (*search_bits, search_offset))
        {
          /*
           * Find the clear bit in the map. Update the search map and map if
           * found. We may find none are spare if searching up from the seed.
           */
          while ((map_offset >= 0)
                 && (map_offset < rtems_rfs_bitmap_element_bits ()))
          {
            if (!rtems_rfs_bitmap_test (*map_bits, map_offset))
            {
              *map_bits = rtems_rfs_bitmap_set (*map_bits, 1 << map_offset);
              if (rtems_rfs_bitmap_match(*map_bits,
                                         RTEMS_RFS_BITMAP_ELEMENT_SET))
                *search_bits = rtems_rfs_bitmap_set (*search_bits,
                                                     1 << search_offset);
              control->free--;
              *bit = test_bit;
              *found = true;
              rtems_rfs_buffer_mark_dirty (control->buffer);
              return 0;
            }

            if (test_bit == end_bit)
              break;

            map_offset += direction;
            test_bit   += direction;
          }
        }

        map_bits  += direction;
        map_index += direction;
        map_offset = direction > 0 ? 0 : rtems_rfs_bitmap_element_bits () - 1;

        test_bit = (map_index * rtems_rfs_bitmap_element_bits ()) + map_offset;

        search_offset += direction;

        if (((direction < 0) && (test_bit <= end_bit))
            || ((direction > 0) && (test_bit >= end_bit)))
          break;
      }
    }
    else
    {
      /*
       * Move to the next search element. We need to determine the number of
       * bits in the search offset that are being skipped so the map bits
       * pointer can be updated. If we are moving down and we have a search
       * offset of 0 then the search map adjustment is to the top bit of the
       * pervious search bit's value.
       *
       * Align test_bit either up or down depending on the direction to next 32
       * bit boundary.
       */
      rtems_rfs_bitmap_bit bits_skipped;
      test_bit &= ~((1 << RTEMS_RFS_ELEMENT_BITS_POWER_2) - 1);
      if (direction > 0)
      {
        bits_skipped = rtems_rfs_bitmap_element_bits () - search_offset;
        test_bit += bits_skipped * rtems_rfs_bitmap_element_bits ();
        map_offset = 0;
      }
      else
      {
        bits_skipped = search_offset + 1;
        /*
         * Need to remove 1 for the rounding up. The above rounds down and
         * adds 1. Remember the logic is for subtraction.
         */
        test_bit -= ((bits_skipped - 1) * rtems_rfs_bitmap_element_bits ()) + 1;
        map_offset = rtems_rfs_bitmap_element_bits () - 1;
      }
      map_bits += direction * bits_skipped;
      map_index += direction * bits_skipped;
    }

    search_bits  += direction;
    search_offset = direction > 0 ? 0 : rtems_rfs_bitmap_element_bits () - 1;
  }
  while (((direction < 0) && (test_bit >= end_bit))
         || ((direction > 0) && (test_bit <= end_bit)));

  return 0;
}

int
rtems_rfs_bitmap_map_alloc (rtems_rfs_bitmap_control* control,
                            rtems_rfs_bitmap_bit      seed,
                            bool*                     allocated,
                            rtems_rfs_bitmap_bit*     bit)
{
  rtems_rfs_bitmap_bit upper_seed;
  rtems_rfs_bitmap_bit lower_seed;
  rtems_rfs_bitmap_bit window;     /* may become a parameter */
  int                  rc = 0;

  /*
   * By default we assume the allocation failed.
   */
  *allocated = false;

  /*
   * The window is the number of bits we search over in either direction each
   * time.
   */
  window = RTEMS_RFS_BITMAP_SEARCH_WINDOW;

  /*
   * Start from the seed and move in either direction. Search in window amounts
   * of bits from the original seed above then below. That is search from the
   * seed up then from the seed down a window number of bits, then repeat the
   * process from the window distance from the seed, again above then
   * below. Keep moving out until all bits have been searched.
   */
  upper_seed = seed;
  lower_seed = seed;

  /*
   * If the upper and lower seed values have reached the limits of the bitmap
   * we have searched all of the map. The seed may not be aligned to a window
   * boundary so we may need to search a partial window and this may also not
   * be balanced for the upper or lower seeds. We move to the limits, search
   * then return false if no clear bits are found.
   */
  while (((upper_seed >= 0) && (upper_seed < control->size))
         || ((lower_seed >= 0) && (lower_seed < control->size)))
  {
    /*
     * Search up first so bits allocated in succession are grouped together.
     */
    if (upper_seed < control->size)
    {
      *bit = upper_seed;
      rc = rtems_rfs_search_map_for_clear_bit (control, bit, allocated,
                                               window, 1);
      if ((rc > 0) || *allocated)
        break;
    }

    if (lower_seed >= 0)
    {
      *bit = lower_seed;
      rc = rtems_rfs_search_map_for_clear_bit (control, bit, allocated,
                                               window, -1);
      if ((rc > 0) || *allocated)
        break;
    }

    /*
     * Do not bound the limits at the edges of the map. Do not update if an
     * edge has been passed.
     */
    if (upper_seed < control->size)
      upper_seed += window;
    if (lower_seed >= 0)
      lower_seed -= window;
  }

  return 0;
}

int
rtems_rfs_bitmap_create_search (rtems_rfs_bitmap_control* control)
{
  rtems_rfs_bitmap_map search_map;
  rtems_rfs_bitmap_map map;
  size_t               size;
  rtems_rfs_bitmap_bit bit;
  int                  rc;

  rc = rtems_rfs_bitmap_load_map (control, &map);
  if (rc > 0)
    return rc;

  control->free = 0;
  search_map = control->search_bits;
  size = control->size;
  bit = 0;

  *search_map = RTEMS_RFS_BITMAP_ELEMENT_CLEAR;
  while (size)
  {
    rtems_rfs_bitmap_element bits;
    int                      available;
    if (size < rtems_rfs_bitmap_element_bits ())
    {
      bits = rtems_rfs_bitmap_merge (*map,
                                     RTEMS_RFS_BITMAP_ELEMENT_SET,
                                     rtems_rfs_bitmap_mask_section (0, size));
      available = size;
    }
    else
    {
      bits      = *map;
      available = rtems_rfs_bitmap_element_bits ();
    }

    if (rtems_rfs_bitmap_match (bits, RTEMS_RFS_BITMAP_ELEMENT_SET))
      rtems_rfs_bitmap_set (*search_map, bit);
    else
    {
      int b;
      for (b = 0; b < available; b++)
        if (!rtems_rfs_bitmap_test (bits, b))
          control->free++;
    }

    size -= available;

    /* Iterate from 0 to 1 less than the number of bits in an element */
    if (bit == (rtems_rfs_bitmap_element_bits () - 1))
    {
      bit = 0;
      search_map++;
      *search_map = RTEMS_RFS_BITMAP_ELEMENT_CLEAR;
    }
    else
      bit++;
    map++;
  }

  return 0;
}

int
rtems_rfs_bitmap_open (rtems_rfs_bitmap_control* control,
                       rtems_rfs_file_system*    fs,
                       rtems_rfs_buffer_handle*  buffer,
                       size_t                    size,
                       rtems_rfs_buffer_block    block)
{
  size_t elements = rtems_rfs_bitmap_elements (size);

  control->buffer = buffer;
  control->fs = fs;
  control->block = block;
  control->size = size;

  elements = rtems_rfs_bitmap_elements (elements);
  control->search_bits = malloc (elements * sizeof (rtems_rfs_bitmap_element));

  if (!control->search_bits)
    return ENOMEM;

  return rtems_rfs_bitmap_create_search (control);
}

int
rtems_rfs_bitmap_close (rtems_rfs_bitmap_control* control)
{
  free (control->search_bits);
  return 0;
}