summaryrefslogtreecommitdiff
path: root/legacy/evil/win32/common/fnmatch_list_of_states.c
blob: 3400a9b95f58e9ff64a2e49bb0a0040acb5a3e43 (plain)
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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include "fnmatch_private.h"

struct list_of_states*
fnmatch_list_of_states_alloc(size_t n, size_t pattern_len)
{
  struct list_of_states	*l;

  const size_t	reserved	= pattern_len + 1;
  const size_t	states_size	= sizeof (*l->states) * reserved;
  const size_t	has_size	= sizeof (*l->has) * reserved;
  const size_t	states_has_size = states_size + has_size;
  const size_t	struct_size	= sizeof (*l) + states_has_size;

  uint8_t *states;
  uint8_t *has;
  size_t i;

  if (! (l = malloc(n * struct_size)))
    return 0;

  states = (uint8_t *) (l + n);
  has = states + states_size;

  for (i = 0; i < n; ++i)
    {
      l[i].reserved = reserved;
      l[i].states = (size_t *) states;
      l[i].has = (_Bool *) has;
      states += states_has_size;
      has += states_has_size;
    }

  return l;
}

void
fnmatch_list_of_states_free(struct list_of_states *lists, size_t n)
{
  assert(lists);

  (void) n;
  free(lists);
}

void
fnmatch_list_of_states_insert(struct list_of_states *list, size_t state)
{
  assert(list);
  assert(state < list->reserved);

  if (list->has[state])
    return;

  list->states[list->size++] = state;
  list->has[state] = 1;
}

void
fnmatch_list_of_states_clear(struct list_of_states *list)
{
  assert(list);

  list->size = 0;
  memset(list->has, 0, list->reserved * sizeof (*list->has));
}