Re: API to get length of array cdata

  • From: Cosmin Apreutesei <cosmin.apreutesei@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 13 May 2012 21:05:11 +0300

Nice trick. I'm still looking for a garbageless solution for hot code
though. Not sure how much this obsession with not generating garbage
in hot loops is justified with the new generational gc and/or LJ2, but
since this discussion is not really accessible to my therapist.. :)

On Sun, May 13, 2012 at 5:52 PM, William Adams <william_a_adams@xxxxxxx> wrote:
> The hacked up way I'm doing it is this:
>
> function array_size(buff)
>     local typestr = tostring(ffi.typeof(buff))
>     local elemtype, nelem = string.match(typestr,
> "ctype<(%w+)%s+%[(%d+)%]>")
>
>     return nelem;
> end
>
> It's fragile because it depends on the string representation returned by the
> "tostring", but it seems to work.
> I'm sure if the string representation changes, this definition could be made
> a bit more adaptable.
> Since there's only a single numeric value in the string representation, you
> could just return the
> numeric part: (%d+) and ignore all the rest.
>
>  local nelem = string.match(typestr, "(%d+)")
>
> of course, if you've done a double, triple, or more array, then the string
> will look like:
>  local arr2 = ffi.new("int[256][10]")
> ctype<int [256][10]>
>
> In that case, you'll have to use a more complex pattern, and use gmatch to
> capture them all.
>
>
>
> -- William
>
> ===============================
> - Shaping clay is easier than digging it out of the ground.
>
> http://williamaadams.wordpress.com
> http://www.thingiverse.com/WilliamAAdams
> https://github.com/Wiladams
>
>
>
>> From: cosmin.apreutesei@xxxxxxxxx
>> Date: Sat, 12 May 2012 18:24:06 +0300
>> Subject: API to get length of array cdata
>> To: luajit@xxxxxxxxxxxxx
>
>>
>> Hi,
>>
>> Is it possible to have a function to get the length of an array cdata
>> (VLA or not)? Right now I have a function that returns sizeof(cdata) /
>> sizeof(elem_ctype) but I have to pass it the element's ctype. #cdata
>> would be swell.
>>
>> Cosmin.
>>

Other related posts: