aboutsummaryrefslogtreecommitdiff
path: root/write_shared_mem.cc
blob: 2149e91db84d8e4b6e501fade8d3b0a5f5ea10fb (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"

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 *s, char *c) 
{
   strcpy (s,c);
}


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();
   }

   sleepUntilWriteBlocked (sdf,STD_USLEEP_TIME);

   if (args(0).is_string()) {
      pdtype = SYMBOL;
   }
   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);
	 freeSharedDataFrame (&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);
	 freeSharedDataFrame (&sdf);
	 return octave_value();
      }
   }
   else 
      std::cout << " no mehtod for argument conversion" << std::endl;

   unBlockForWriting (sdf);

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

   return octave_value();
}