Memory management in shared library

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
cmpitg
Posts: 10
Joined: Thu May 05, 2011 1:10 am
Location: Hanoi
Contact:

Memory management in shared library

Post by cmpitg »

Hi newLISPers,

I have a question about memory management when loading shared library. In C/C++, after finishing using a library, one needs to unload it to ensure that:
  • memory is properly deallocated, and
  • the system could do some clean-up, like closing file handlers, ...
In newLISP, should/must we use the same method? If yes, then how to do it properly. Let's take the example in newLISP Code Patterns:

We have this C program:

Code: Select all

/* compile with:
 *     $ gcc <filename> -fPIC -shared -o <libname>
 * or
 *     $ tcc <filename> -shared -o <libname>
 **/

#include <string.h>
#include <stdlib.h>

typedef struct mystruc
{
    int number;
    char *ptr;
} MyStruc;

MyStruc *foo3(char *ptr, int num)
{
    MyStruc *astruc;
    astruc = malloc( sizeof( MyStruc ) );
    astruc->ptr = malloc( len( ptr ) + 1 );
    strcpy( astruc->ptr, ptr );
    astruc->number = num;
    return astruc;
}
After compiling it to a shared library, we can import ``MyStruc`` to our newLISP program by using:

Code: Select all

(import "mystruc.so" "foo3")
(setq 'astruc (foo3 "hello world" 123))
(get-string (get-integer (+ a struc 4))) ;; 4 = sizeof int in C
(get-integer astruc)
Do we have to manually deallocate memory allocated to ``astruc``? According to what I understand, since ``astruc`` is created by newLISP, exists in newLISP environment, newLISP's garbage collector automatically takes care of memory management for us. Right?
"Life is a hack"
My web log: http://cmpitg.wordpress.com

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Memory management in shared library

Post by Lutz »

'astruc' will contain a pointer to the memory allocated during the C call to foo3(). And you have to de-allocate that memory manually - it is not managed by newLISP. You could simply import free() from libc and then free it:

Code: Select all

(import "/usr/lib/libc.so" "free")

(free astruc)
When you do:

Code: Select all

(set 'myvar (get-string (get-integer (+ astruc 4))))
then 'myvar' would contain a copy of "hello world" and that memory in 'myvar' would be managed by newLISP.

Often when calling functions part of the MS Window API, you have to pre-allocate memory in newLISP, then pass a pointer to that function to place some stuff into that memory area:

Code: Select all

(import "kernel32.dll" "GetWindowsDirectoryA")
(set 'str (dup "\000" 64)  ; reserve space and initialize

(GetWindowsDirectoryA str (length str))

(trim str)  → "C:\\WINDOWS"
this example is from the manual entry for 'import'. In this case too, newLISP will take care of memory management. When the variable 'str' gets reused for something else, then the old contents gets freed automatically by newLISP.

cmpitg
Posts: 10
Joined: Thu May 05, 2011 1:10 am
Location: Hanoi
Contact:

Re: Memory management in shared library

Post by cmpitg »

Thank you, Lutz. That makes sense. Let me summarize a little bit to see if I really understand it right. Please correct me if I'm wrong:
  • Any allocation performed by foreign functions (functions which are imported from shared libraries) has to be deallocated manually if there's no call to do so.
  • In case of calling foreign functions with passing by reference, memory for variables need to be allocated beforehand by newLISP, and hence, they need not be deallocated manually.
Examples:
  1. Taken from newLISP Code Patterns, one needs to free the memory with C's ``free`` function.
  2. Using newLISP with gtk-server. No manual deallocation should be taken since gtk-server does that after the call for ``gtk_quit()`` or so.
  3. Taken from the reference of ``import`` function in newLISP Manual, which you have just mentioned.
"Life is a hack"
My web log: http://cmpitg.wordpress.com

cmpitg
Posts: 10
Joined: Thu May 05, 2011 1:10 am
Location: Hanoi
Contact:

Re: Memory management in shared library

Post by cmpitg »

Should these notes be available on newLISP Code Patterns or newLISP Manual and Reference, guys? I think extra cautions, especially cautions about memory, are never redundant. Right?
"Life is a hack"
My web log: http://cmpitg.wordpress.com

Locked