File: helpers.c

package info (click to toggle)
lv2dynparam1 2-6
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 1,756 kB
  • ctags: 504
  • sloc: sh: 9,039; ansic: 4,352; makefile: 26
file content (141 lines) | stat: -rw-r--r-- 3,250 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*****************************************************************************
 *
 *   This file is part of lv2dynparam libraries
 *
 *   Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *****************************************************************************/

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

#include "memory_atomic.h"
#include "helpers.h"

//#define LOG_LEVEL LOG_LEVEL_DEBUG
#include "log.h"

char *
lv2dynparam_strdup_atomic(
  rtsafe_memory_handle memory,
  const char * source)
{
  size_t size;
  char * dest;

  size = strlen(source) + 1;

  dest = rtsafe_memory_allocate(memory, size);
  if (dest == NULL)
  {
    return NULL;
  }

  memcpy(dest, source, size);

  return dest;
}

char *
lv2dynparam_strdup_sleepy(
  rtsafe_memory_handle memory,
  const char * source)
{
  size_t size;
  char * dest;

  size = strlen(source) + 1;

  dest = rtsafe_memory_allocate_sleepy(memory, size);
  if (dest == NULL)
  {
    return NULL;
  }

  memcpy(dest, source, size);

  return dest;
}

char **
lv2dynparam_enum_duplicate(
  rtsafe_memory_handle memory,
  const char * const * values_ptr_ptr,
  unsigned int values_count)
{
  unsigned int i;
  char ** values;

  LOG_DEBUG("Duplicating enum array of %u elements", values_count);
  for (i = 0 ; i < values_count ; i++)
  {
    LOG_DEBUG("\"%s\"", values_ptr_ptr[i]);
  }

  values = rtsafe_memory_allocate(memory, values_count * sizeof(char *));
  if (values == NULL)
  {
    LOG_DEBUG("Failed to allocate memory for enum array");
    return NULL;
  }

  for (i = 0 ; i < values_count ; i++)
  {
    LOG_DEBUG("Allocating memory for enum array entry at index %u", i);
    values[i] = lv2dynparam_strdup_atomic(memory, values_ptr_ptr[i]);
    if (values[i] == NULL)
    {
      LOG_DEBUG("Failed to allocate memory for enum array entry");

      while (i > 0)
      {
        i--;
        LOG_DEBUG("Deallocating memory for enum array entry at index %u", i);
        rtsafe_memory_deallocate(values[i]);
      }

      rtsafe_memory_deallocate(values);

      return NULL;
    }
  }

  LOG_DEBUG("Duplicated enum array of %u elements", values_count);
  for (i = 0 ; i < values_count ; i++)
  {
    LOG_DEBUG("\"%s\"", values[i]);
  }

  return values;
}

void
lv2dynparam_enum_free(
  rtsafe_memory_handle memory,
  char ** values,
  unsigned int values_count)
{
  while (values_count > 0)
  {
    values_count--;

    rtsafe_memory_deallocate(values[values_count]);
  }

  rtsafe_memory_deallocate(values);
}