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

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

DatTyp classifyOctPDType (octave_value res)
{
   DatTyp pdtyp;
   if (res.is_real_scalar())
      pdtyp = FLOAT;
   else if (res.is_real_matrix()) 
      pdtyp = MATRIX;
   else if (res.is_string())
      pdtyp = SYMBOL;
   else
      pdtyp = UNKNOWN;
}

void writeOctMatrixIntoFloat (Matrix mtx, float *f)
{
   int n = mtx.rows();
   int m = mtx.columns();
   int i;
   int j;

   *f++ = n;
   *f++ = m;

   for (j = 0; j < m; j++)
      for (i=0; i < n; i++)
	 *f++ = (float) mtx(i,j);
}

void writeOctScalarIntoFloat (double d, float *f)
{
   *f = (float) d;
}
void writeOctStringIntoString (char *dst, char *src) 
{
   strcpy (dst, src);
}


DEFUN_DLD (write_shared_mem, args, , "returning an octave value to pd-value")
{
   SharedDataFrame *sdf;
   int size;
   void *data;
   DatTyp pdtype;
   int shmem_id = args(1).int_value();
   
   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("write_shared_mem: pd and octave scheduling error, restart pd!");
	   return octave_value();
   }
   if (args(0).is_empty()) {
	   unBlockForWriting (sdf);
	   removeSharedDataFrame (&sdf);
	   return octave_value();
   }
   else if (args(0).is_string()) {
      pdtype = SYMBOL;
      size = args(0).string_value().size();
      if (data = newSharedData (sdf, size, sizeof(char),pdtype)) {
	 writeOctStringIntoString( (char*)data, (char*)args(0).string_value().c_str() );
      }
      else {
	 error("failed to get new data memory!");
	 unBlockForWriting (sdf);
	 removeSharedDataFrame (&sdf);
	 return octave_value();
      }

   }
   else if (args(0).is_real_matrix()) {
      pdtype = MATRIX;
      size = args(0).columns() * args(0).rows()+2;
      if (data = newSharedData (sdf, size, sizeof(float),pdtype)) {
	 writeOctMatrixIntoFloat (args(0).matrix_value(), (float *) data);
      }
      else {
	 error("failed to get new data memory!");
	 unBlockForWriting (sdf);
	 removeSharedDataFrame (&sdf);
	 return octave_value();
      }
   }
   else if (args(0).is_real_scalar()) {
      pdtype = FLOAT;
      if (data = newSharedData (sdf, 1, sizeof(float), pdtype)) {
	 writeOctScalarIntoFloat(args(0).scalar_value(), (float *) data);
      }
      else {
	 error("failed to get new data memory!");
	 unBlockForWriting (sdf);
	 removeSharedDataFrame (&sdf);
	 return octave_value();
      }
   }
   else 
      std::cout << " no mehtod for argument conversion" << std::endl;

   unBlockForWriting (sdf);

   freeSharedData (sdf, &data);
   freeSharedDataFrame (&sdf);

   return octave_value();
}