[gambit-list] How to properly allocate foreign object?

Jörg F. Wittenberger Joerg.Wittenberger at softeyes.net
Fri Apr 10 15:12:15 EDT 2020


Hi,

I'm afraid I don't grock the documentation wrt. memory management of
foreign bjects.

> I really need a unix domain socket.  Actually an abstract socket on
> Linux.

The example at at hand: a `struct sockaddr_storage`.  That is, in
general, any kind of C struct to allocate.

Likely I'm doing it all wrong.  My great excuse(TM): I learned gambit's
FFI from `gamsock` while adding domain socket support.  No, Mam, it's
not been me. ;-)

# Part I, the easy thing

(c-define-type socket-address 
 (pointer (struct "sockaddr_storage") socket-address))

(c-lambda
 (...) socket-address
 "...
  ___result_voidstar = malloc(sizeof(struct sockaddr_storage)));
 ...")

That seems to work.  But I'd bet this leaks memory as gamsock never
frees this memory.

Q: Does it leak?  Or is there some gambit magic default?

# Part II, the crazy thing

If I got that right, than what I'd need to fix the leak was to attach a
/will/ to the socket-address, which would free the storage and return
___NO_ERR.

Q: Correct?

If yes, is this a good idea?  I'd assume that allocating short lived
objects from the Scheme heap should be much less expesive.  Isn't it?


# Part III, how?

1. How would I allocate a short lived object on the scheme heap?
2. Still have it properly tagges as foreign object for type checking.
   (I.e., I know I could allocate a u8vector of the size I need.
    But this is an u8vector for Scheme, which I don't want.)

Thanks

/Jörg





More information about the Gambit-list mailing list