aboutsummaryrefslogtreecommitdiff
path: root/read_shared_mem.cc
blob: 1fec2604fbee1656ac2a4211401dc5c4722f03d3 (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
#include <octave/oct.h>

#include <unistd.h>
#include <string.h>
#include "pdoctave_dataframe.h"

Matrix writeFloatIntoOctMatrix (int n, int m, float *f)
{
   int i;
   int j;
   Matrix mtx = Matrix(n,m);
   for (j = 0; j < n; j++)
      for (i=0; i < m; i++)
	 mtx(j,i) = (double)*f++;
   return mtx;
}

double writeFloatIntoDouble (float f)
{
   return (double) f;
}

RowVector writeFloatIntoRowVector (int n, float *f)
{
   RowVector rv = RowVector(n);
   int i;
   for (i = 0; i<n; i++)
      rv(i) = (double) *f++;
   return rv;
}
	 
ColumnVector writeFloatIntoColumnVector (int n, float *f)
{
   ColumnVector cv = ColumnVector(n);
   int i;
   for (i = 0; i<n; i++)
      cv(i) = (double) *f++;
   return cv;
}

DEFUN_DLD (read_shared_mem, args, , "reading and returning a pd-value in octave")
{
   SharedDataFrame *sdf;
   void *data;
   octave_value convert_result;
   int shmem_id = args(0).int_value();
   float *f;
   std::string str;
   std::string quote_sign = "\"";
   
   if (shmem_id == -1) {
      error("failed to get valid id\n");
      return octave_value();
   }
   sdf = getSharedDataFrame (shmem_id);
   
   if (!sdf) {
      error("failed to attach memory!\n");
      return octave_value();
   }
   if((sleepUntilWriteBlocked (sdf))==0) {
	   error("read_shared_mem: pd and octave scheduling error, restart pd!");
	   return octave_value();
   }
   if((sleepUntilReadBlocked (sdf))==0) {
	   error("read_shared_mem: pd and octave scheduling error, restart pd!");
	   return octave_value();
   }
   
   data = getSharedData (sdf);
   if (!data) {
     error("failed to attach data!\n");
     freeSharedDataFrame (&sdf);
     unBlockForReading (sdf);
     return octave_value();
   }

   f = (float*) data;
 
   switch (getSharedDataType (sdf)) {
      case FLOAT: 
	 convert_result = octave_value(writeFloatIntoDouble (*f));
	 break;
      case LIST:
	 convert_result = octave_value( 
	       writeFloatIntoRowVector (getSharedDataVecLength(sdf), f));
	 break;
      case SYMBOL:
	 str = (std::string) (char *) data;
	 convert_result = octave_value(quote_sign+str+quote_sign);
	 break;
      case MATRIX:
	 convert_result = octave_value(writeFloatIntoOctMatrix ((int)f[0],(int)f[1],f+2));
	 break;
      case UNKNOWN:
	 error("unknown pdoctave type");
	 convert_result = octave_value ();
   }
   unBlockForReading (sdf);
   removeSharedData (sdf, &data);
   freeSharedDataFrame (&sdf);
   return (convert_result);
}