Hi Bradley,
On Sun, Mar 3, 2013 at 10:09 PM, Bradley Froehle <
brad.f...@gmail.com> wrote:
> Also, I find the '
ffi.new("Data *")' syntax quite confusing. Shouldn't this
> just create enough memory to hold a pointer? That is, this seems like a
> direct translation of the line 'Data *ptr;' in C which would not allocate an
> entire struct's worth of memory.
We went through various solutions in the past. The idea is that
ffi.new("Data *") should be equivalent to:
Data *ptr = malloc(sizeof(Data));
It has been found less confusing than calling this operation
ffi.new("Data"), because it returns a <cdata 'Data *'>. It is natural
in this context: say you want to do the equivalent of the following C
code:
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
struct dirent *entry = malloc(size(struct dirent));
struct dirent **result = malloc(size(struct dirent *));
readdir_r(dirp, entry, result);
Then it's in Python:
entry =
ffi.new("struct dirent *")
result =
ffi.new("struct dirent **")
readdir_r(dirp, entry, result)
We could possibly change it again --- this thread gives the idea that
malloc() is not the right abstraction at all, and instead we should
just go with "variable declarations". This would mean that we
translate this C code:
struct dirent entry;
struct dirent *result;
readdir_r(dirp, &entry, &result);
into:
entry = ffi.var("struct dirent")
result = ffi.var("struct dirent *")
readdir_r(dirp, ffi.addressof(entry), ffi.addressof(result))
But we already tweaked and changed this particular interface a few
times (though not like that so far), so I guess I'll need more input
to know if it looks like a good idea or now...
A bientôt,
Armin.