slang-users mailing list

[2007 Date Index] [2007 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] export an array of structs


Brian McQueen <mcqueenorama@xxxxxxxxx> wrote:
>I am able to gain access to my shm from the interpreter.  But so far
>its only been to the really simple parts of my shm.  One part of my
>shm is an array of structs.  How do I make an array of structs
>available to the interpreter?  Its application managed and of a fixed
>size.  I tried this to get the very first element of the array, but

Instead of trying to map the array of structs directly onto slang
arrays, have you considered creating simple functions that are
callable from the interpreter to access the array?  I think that in
the end this simpler choice will also turn out to be more robust if
you were to change the internal representation.

>Here is what I've tried in order to get a peek at the first element of
>the array of structs:
>
>static SLang_IStruct_Field_Type slang_struct_array_in_shm [] = {
>    MAKE_ISTRUCT_FIELD(post_req_t, value1, "value1", SLANG_ULLONG_TYPE, 1),
>    MAKE_ISTRUCT_FIELD(post_req_t, total_num, "total_size",
>SLANG_ULLONG_TYPE, 1),
>    MAKE_ISTRUCT_FIELD(post_req_t, cur_num, "cur_size", SLANG_ULLONG_TYPE, 1),
>    MAKE_ISTRUCT_FIELD(post_req_t, rtime, "max_time", SLANG_ULLONG_TYPE, 1),
>    SLANG_END_ISTRUCT_TABLE
>};

If each element of the array consists of unsigned long long values,
then I would create a function that returns the ith element of the
array as a slang structure.  For example, suppose your code defines
the structure

   typedef struct
   {
      post_req_t rtime;
      post_req_t cur_num;
        .
	.
   } Foo_Type

and an array of NUM of them:

   #define NUM_FOOS 512
   Foo_Type My_Foos[NUM_FOOS];

Then use:
   SLang_CStruct_Field_Type My_Foo_Fields [] =
   {
      MAKE_CSTRUCT_FIELD(Foo_Type, rtime, "max_time", SLANG_ULLONG_TYPE, 0),
      MAKE_CSTRUCT_FIELD(Foo_Type, cur_num, "cur_size", SLANG_ULLONG_TYPE, 0),
         .
	 .
      SLANG_END_CSTRUCT_TABLE
   };


   static void get_foo (int *ip)
   {
       Foo_Type *foop;
       if ((*ip < 0) || (*ip >= NUM_FOOS))
         {
	    SLang_set_error (SL_InvalidParm_Error);
	    return;
	 }
       foop = &Array_of_Foos[*ip];
       SLang_push_cstruct ((VOID_STAR) foop, My_Foo_Fields);
   }

   static void set_foo (int *ip)
   {
       Foo_Type *foop;
       if ((*ip < 0) || (*ip >= NUM_FOOS))
         {
	    SLang_set_error (SL_InvalidParm_Error);
	    return;
	 }
       foop = &Array_of_Foos[*ip];
       (void) SLang_pop_cstruct ((VOID_STAR) foop, My_Foo_Fields);
   }

Then add these functions as slang intrinsics using lines such as
        
    MAKE_INTRINSIC_0("get_foo", get_foo, SLANG_VOID_TYPE, SLANG_INT_TYPE),
    MAKE_INTRINSIC_0("set_foo", set_foo, SLANG_VOID_TYPE, SLANG_INT_TYPE),
   
in your intrinsic table.

Then from the interpreter you should be able to get, say the 10th
element of your array using

      s = get_foo (10);

and set the the max_time field using

      s.max_time = whatever;
      set_foo (s, 10);

The advantage of this approach is that it gives you flexibility for
future changes to the underlying data structures.

I hope this idea is clear.

Thanks,
--John



[2007 date index] [2007 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]