summaryrefslogtreecommitdiff
path: root/media/libcubeb/src/cubeb_strings.c
blob: 5fe5b791e2629c4150176ced97ad4289f77284a1 (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
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
/*
 * Copyright © 2011 Mozilla Foundation
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */

#include "cubeb_strings.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#define CUBEB_STRINGS_INLINE_COUNT 4

struct cubeb_strings {
  uint32_t size;
  uint32_t count;
  char ** data;
  char * small_store[CUBEB_STRINGS_INLINE_COUNT];
};

int
cubeb_strings_init(cubeb_strings ** strings)
{
  cubeb_strings * strs = NULL;

  if (!strings) {
    return CUBEB_ERROR;
  }

  strs = calloc(1, sizeof(cubeb_strings));
  assert(strs);

  if (!strs) {
    return CUBEB_ERROR;
  }

  strs->size = sizeof(strs->small_store) / sizeof(strs->small_store[0]);
  strs->count = 0;
  strs->data = strs->small_store;

  *strings = strs;

  return CUBEB_OK;
}

void
cubeb_strings_destroy(cubeb_strings * strings)
{
  char ** sp = NULL;
  char ** se = NULL;

  if (!strings) {
    return;
  }

  sp = strings->data;
  se = sp + strings->count;

  for (; sp != se; sp++) {
    if (*sp) {
      free(*sp);
    }
  }

  if (strings->data != strings->small_store) {
    free(strings->data);
  }

  free(strings);
}

/** Look for string in string storage.
    @param strings Opaque pointer to interned string storage.
    @param s String to look up.
    @retval Read-only string or NULL if not found. */
static char const *
cubeb_strings_lookup(cubeb_strings * strings, char const * s)
{
  char ** sp = NULL;
  char ** se = NULL;

  if (!strings || !s) {
    return NULL;
  }

  sp = strings->data;
  se = sp + strings->count;

  for (; sp != se; sp++) {
    if (*sp && strcmp(*sp, s) == 0) {
      return *sp;
    }
  }

  return NULL;
}

static char const *
cubeb_strings_push(cubeb_strings * strings, char const * s)
{
  char * is = NULL;

  if (strings->count == strings->size) {
    char ** new_data;
    uint32_t value_size = sizeof(char const *);
    uint32_t new_size = strings->size * 2;
    if (!new_size || value_size > (uint32_t)-1 / new_size) {
      // overflow
      return NULL;
    }

    if (strings->small_store == strings->data) {
      // First time heap allocation.
      new_data = malloc(new_size * value_size);
      if (new_data) {
        memcpy(new_data, strings->small_store, sizeof(strings->small_store));
      }
    } else {
      new_data = realloc(strings->data, new_size * value_size);
    }

    if (!new_data) {
      // out of memory
      return NULL;
    }

    strings->size = new_size;
    strings->data = new_data;
  }

  is = strdup(s);
  strings->data[strings->count++] = is;

  return is;
}

char const *
cubeb_strings_intern(cubeb_strings * strings, char const * s)
{
  char const * is = NULL;

  if (!strings || !s) {
    return NULL;
  }

  is = cubeb_strings_lookup(strings, s);
  if (is) {
    return is;
  }

  return cubeb_strings_push(strings, s);
}