SML# Document Version 4.0.0
29 SML# Run-time data management

29.2 Effect of garbage collection

Data of type whose tag is 1 are managed under SML#’s memory management system. In what follows, we refer to those data as boxed data. The memory area of boxed data is allocated implicitly when its data constructor is evaluated, and released automatically by SML#’s garbage collector. To pass boxed data to C functions, you need to pay attention to not only their contents but also the timing when the memory is released. In perspective of C functions, boxed data have the following properties:

  1. 1.

    Boxed data are not moved similarly to the data allocated by C’s malloc function. Therefore, the identity of SML#’s arrays and refs is preserved even in C functions. Since tuple and record values are neigher moved as long as the values are alive.

  2. 2.

    Arguments passed from SML# to C functions are passed without any modification. Neither data conversion, allocation, nor regeneration may occur. Therefore, C function may update SML#’s arrays and refs and SML# program see the effect of C’s memory update.

  3. 3.

    Boxed data passed to C function are never released during the C function ends. When C function ends and SML# program has control, the boxed data passed to C is not alive in the SML# program, the boxed data is released.

  4. 4.

    Callback functions are never released until the program ends. It is allowed for C functions to store callback functions to global variables. The value of free variables of callback functions are preserved even if the thread is different between the caller and callback. Note that you need to pay attetion to memory leak when you use callback functions. The SML# compiler guarantees that no memory leak occur if you only use top-level functions as callback functions.

  5. 5.

    SML#’s garbage collector is accurate garbage collector that only scans SML#’s heap and stack. GC may release boxed data to which SML# program cannot refer even if they are stored in C heap and hence C functions can refer to them. Therefore, it is not allowed for C functions to store boxed data to C heap. Since boxed data are alive until C function ends, it is safe to store them in registers and stack frames.