-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathosc_array.c
More file actions
150 lines (137 loc) · 3.65 KB
/
osc_array.c
File metadata and controls
150 lines (137 loc) · 3.65 KB
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
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2009-ll, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "osc.h"
#include "osc_bundle_u.r"
#include "osc_bundle_s.r"
#include "osc_message_u.r"
#include "osc_message_s.r"
#include "osc_atom_u.r"
#include "osc_atom_s.r"
#include "osc_array.h"
#include "osc_array.r"
#include "osc_mem.h"
#include "osc_error.h"
#include <string.h>
t_osc_array *osc_array_allocWithSize(long len, size_t membersize)
{
t_osc_array *ar = osc_mem_alloc(sizeof(t_osc_array));
if(!ar){
return NULL;
}
if(len == 0 || membersize == 0){
ar->ar = NULL;
}else{
ar->ar = osc_mem_alloc(len * membersize);
}
ar->len = len;
ar->membersize = membersize;
#ifdef OSC_ARRAY_CLEAR_ON_ALLOC
osc_array_clear(ar);
#endif
return ar;
}
void osc_array_free(t_osc_array *ar)
{
if(ar){
osc_mem_free(ar->ar);
osc_mem_free(ar);
}
}
void osc_array_clear(t_osc_array *ar)
{
if(ar){
memset(ar->ar, '\0', ar->len * ar->membersize);
}
}
void *osc_array_get(t_osc_array *ar, long idx){
if(!ar){
return NULL;
}
if(ar->len <= idx){
return NULL;
}
return ar->ar + (idx * ar->membersize);
}
long osc_array_getLen(t_osc_array *ar)
{
if(!ar){
return 0;
}
return ar->len;
}
t_osc_array *osc_array_copy(t_osc_array *array)
{
if(!array){
return NULL;
}
t_osc_ar *cp = osc_array_allocWithSize(array->len, array->membersize);
memcpy(cp->ar, array->ar, cp->len * cp->membersize);
return cp;
}
t_osc_err osc_array_copyInto(t_osc_array **dest, t_osc_array *src, long offset)
{
if(!src){
return OSC_ERR_INVAL;
}
long srclen = src->len;
long srcsize = src->membersize;
long srclen_bytes = srclen * srcsize;
long destlen = srclen + offset;
long destlen_bytes = destlen * srcsize;
if(!(*dest)){
*dest = osc_array_allocWithSize(destlen, srcsize);
}else{
if(destlen_bytes < srclen_bytes){
(*dest)->ar = osc_mem_resize((*dest)->ar, destlen_bytes);
if(!((*dest)->ar)){
return OSC_ERR_OUTOFMEM;
}
}
}
memcpy((*dest)->ar + (offset * srcsize), src->ar, srclen_bytes);
return OSC_ERR_NONE;
}
t_osc_err osc_array_resize(t_osc_array *array, int newlen)
{
if(!array){
return OSC_ERR_INVAL;
}
array->ar = osc_mem_resize(array->ar, newlen * array->membersize);
if(array->ar){
#ifdef OSC_ARRAY_CLEAR_ON_ALLOC
if(newlen > array->len){
memset(array->ar + (array->len * array->membersize), '\0', (newlen - array->len) * array->membersize);
}
#endif
array->len = newlen;
return OSC_ERR_NONE;
}else{
array->len = 0;
return OSC_ERR_OUTOFMEM;
}
}
void osc_array_set(t_osc_array *ar, void *ptr, long len, size_t membersize)
{
if(ar){
ar->ar = ptr;
ar->len = len;
ar->membersize = membersize;
}
}