lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


the luaglut library
(http://lua-users.org/files/wiki_insecure/users/VarolKaptan/) has a
component called memarray which may be used to create pools of raw
memory and view them as uchar, char, int, double, etc. Instead of
creating new memory you can also provide alternative views to an
already allocated memory by providing the starting address as a light
userdata.

Some examples:

varol@thales:~$ lua
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
require 'memarray'
a = memarray('int', 8)
=a:size(), a:length()
32	8
for i = 0, a:length()-1 do a[i] = i end
for i = 0, a:length()-1 do io.write(a[i], ' ') end print()
0 1 2 3 4 5 6 7
b = memarray('uint', 4, a:ptr(4)) -- just a different view of a, starting from 4'th integer
for i = 0, b:length()-1 do io.write(b[i], ' ') end print()
4 5 6 7
=a[4], b[0]
4	4
a[4] = -1
=a[4], b[0]
-1	4294967295


Varol Kaptan

On 9/26/06, Wesley Smith <wesley.hoke@gmail.com> wrote:
Hi,
I'm wanting to use Lua's Light User Data functionality to prototype
some data processing routines.  I'm basically dealing with a pointer
to raw memory and a header struct that describes the memory layout.
It looks something like this in C:

char *memory_in;
t_mem_header *memory_in_header;

char *memory_out;
t_mem_header *memory_out_header;

What I want to do is pass all 4 of these pointers to lua in as light
user data.  Allocation and deallocation of this memory is handled by
the app.  Typical C usage of this goes as follow:

void process_data(t_mem_header *memory_in_header, char *memory_in,
t_mem_header *memory_out_header, char *memory_out)
{
    /*
        leaving out stuff like variable declarations
   */

      for(j=0; j < height; j++) {
            for(i=0; i < width; i++) {
                  //process input and put result in output
              }
      }
}


Now, the data can be many flavors like uchar, long, float, or double.
I'd like to use Lua to replace the double for loops such that I can
load little processing scripts and see how things work and when I get
something that works well I would then most likely go back to C and
hard code it.  A secondary motive for doing this is to get to know Lua
more and just try some ideas out.  I've looked around for some info
and examples on uses of Light User Data, but I'm still not sure
exactly where to start.  How do I go about writing Lua functions that
process the Light User Data such that I can script the processing?

As a simple starter, how would I recreate the following:
 *(float *)(memory_out + j*width + i) =  1.0 + *(float *)(memory_in+
j*width + i);

thanks,
wes