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
|
#include "m_pd.h"
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
typedef struct shmem
{
t_object x_obj;
int segment_id;
t_float *share_memory;
int segment_size;
} t_shmem;
inline t_float min(t_float x1,t_float x2)
{
if (x1 <= x2) {
return x1;
} else {
return x2;
}
}
void shmem_set(t_shmem *x, t_symbol *unused, int argc, t_atom *argv)
{
t_garray *a;
int npoints;
int array_offset;
t_word *vec;
int i, index_max, shmem_offset;
if (argc <= 1) {
pd_error(x, "shmem usage : [memset offset data [offset_data]( (data can be a float, a list, or a table name)");
return;
}
if (argv[0].a_type != A_FLOAT) {
pd_error(x, "shmem usage : [memset offset data [offset_data]( (data can be a float, a list, or a table name)");
return;
}
shmem_offset = atom_getfloatarg(0,argc,argv);
if (shmem_offset < 0) shmem_offset=0;
if (argv[1].a_type == A_SYMBOL) { // argument is an arrayname
t_symbol *s = atom_getsymbolarg(1,argc,argv);
if (!(a = (t_garray *)pd_findbyclass(s, garray_class)))
pd_error(x, "%s: no such array", s->s_name);
else if (!garray_getfloatwords(a, &npoints, &vec))
pd_error(x, "%s: bad template for tabread", s->s_name);
else {
if (argc == 2) { // memset index_mem arrayname
index_max = min(x->segment_size-shmem_offset,npoints);
// post("i=%d", index_max);
for (i=0; i<index_max; i++)
x->share_memory[i+shmem_offset] = vec[i].w_float;
}
else if (argc == 3) { // memset index_mem arrayname array_offset
array_offset = atom_getfloatarg(2,argc,argv);
index_max = min(x->segment_size-shmem_offset,npoints-array_offset);
//post("i=%d", x->segment_size-shmem_offset);
//post("i=%d", npoints);
//post("i=%d", index_max);
for (i=0; i<index_max; i++)
x->share_memory[i+shmem_offset] = vec[i+array_offset].w_float;
}
}
}
else if (argv[1].a_type == A_FLOAT) { // argument is a float
index_max = x->segment_size - shmem_offset;
if (index_max > argc-1) index_max = argc-1;
for (i=0; i<index_max ; i++)
x->share_memory[i+shmem_offset]=atom_getfloatarg(i+1, argc, argv);
}
}
void shmem_dump(t_shmem *x, t_symbol *unused, int argc, t_atom *argv)
{
int i, vecsize, index_max, shmem_offset, dest_offset;
t_garray *a;
t_word *vec;
if (argc < 2) {
pd_error(x, "shmem usage : [memdump offset dest [offset_dest]( ");
return;
}
if (argv[0].a_type != A_FLOAT) {
pd_error(x, "shmem usage : [memdump offset dest [offset_dest]( ");
return;
}
shmem_offset = atom_getfloatarg(0,argc,argv);
if (shmem_offset < 0) shmem_offset = 0;
if (shmem_offset > x->segment_size-1) i = x->segment_size-1;
// post("i=%d", offset);
if (argv[1].a_type == A_SYMBOL) { // [memdump offset dest
t_symbol *s = atom_getsymbolarg(1,argc,argv);
if (!(a = (t_garray *)pd_findbyclass(s, garray_class)))
pd_error(x, "%s: no such array", s->s_name);
else if (!garray_getfloatwords(a, &vecsize, &vec))
pd_error(x, "%s: bad template for tabwrite", s->s_name);
else {
if (argc == 2) {
index_max = x->segment_size-shmem_offset;
if (index_max > vecsize)
index_max = vecsize;
// post("i=%d", index_max);
for (i=0; i<index_max; i++)
vec[i].w_float = x->share_memory[i+shmem_offset];
}
else if (argc == 3) {
dest_offset = atom_getfloatarg(2,argc,argv);
index_max = min(x->segment_size-shmem_offset,vecsize-dest_offset);
// post("i=%d", index_max);
for (i=0; i<index_max; i++)
vec[i+dest_offset].w_float = x->share_memory[i+shmem_offset];
}
garray_redraw(a);
}
}
else {
pd_error(x, "shmem usage : [memdump offset dest [offset_dest]( ");
return;
}
}
void shmem_read(t_shmem *x, t_float index)
{
int i, vecsize, index_max, shmem_offset;
t_garray *a;
t_word *vec;
shmem_offset = index;
if (shmem_offset < 0) shmem_offset = 0;
if (shmem_offset > x->segment_size-1) i = x->segment_size-1;
// post("i=%d", offset);
outlet_float(x->x_obj.ob_outlet, x->share_memory[shmem_offset]);
}
t_class *shmem_class;
void *shmem_new( t_floatarg id, t_floatarg size)
{
struct shmid_ds shmbuffer;
// post ("id, size: %f, %f", id, size);
t_shmem *x = (t_shmem *)pd_new(shmem_class);
x->segment_size = (int)size;
x->segment_id = shmget ((int)id, sizeof(t_float)*x->segment_size,
IPC_CREAT | S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
if(x->segment_id>0) {
x->share_memory = (t_float*) shmat (x->segment_id, 0, 0);
// post("shmem memory attached at address %p\n", x->share_memory);
shmctl (x->segment_id, IPC_STAT, &shmbuffer);
// post ("segment size: %d\n", shmbuffer.shm_segsz);
if ((int)shmbuffer.shm_segsz < (int)sizeof(t_float)*x->segment_size) {
// there was a problem, set object meme size to 0
error("could not allocate shmem memory Id : %d, size %d", (int)id, x->segment_size );
x->segment_size = 0;
}
}
else {
error("could not allocate shmem memory Id : %d, size %d", (int)id, x->segment_size );
x->segment_size = 0;
}
outlet_new(&x->x_obj, 0);
return (void *)x;
}
void shmem_free(t_shmem *x)
{
shmdt (x->share_memory);
shmctl (x->segment_id, IPC_RMID, 0);
}
void shmem_clear(t_shmem *x)
{
int i;
for (i=0; i<x->segment_size; i++)
x->share_memory[i] = 0;
}
void shmem_setup(void)
{
shmem_class = class_new(gensym("shmem"), (t_newmethod)shmem_new, (t_method)shmem_free,
sizeof(t_shmem), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
class_addmethod(shmem_class, (t_method)shmem_set, gensym("memset"), A_GIMME, 0);
class_addmethod(shmem_class, (t_method)shmem_dump, gensym("memdump"), A_GIMME, 0);
class_addmethod(shmem_class, (t_method)shmem_clear, gensym("memclear"), 0);
class_addmethod(shmem_class, (t_method)shmem_read, gensym("memread"),A_DEFFLOAT, 0);
}
|