## 3 Memory Usage ¶

Note that memory allocated by `ffi_closure_alloc` and freed by `ffi_closure_free` does not come from the same general pool of memory that `malloc` and `free` use. To accomodate security settings, `libffi` may aquire memory, for example, by mapping temporary files into multiple places in the address space (once to write out the closure, a second to execute it). The search follows this list, using the first that works:

- A anonymous mapping (i.e. not file-backed)
- `memfd_create()`, if the kernel supports it.
- A file created in the directory referenced by the environment variable `LIBFFI_TMPDIR`.
- Likewise for the environment variable `TMPDIR`.
- A file created in `/tmp`.
- A file created in `/var/tmp`.
- A file created in `/dev/shm`.
- A file created in the user’s home directory (`$HOME`).
- A file created in any directory listed in `/etc/mtab`.
- A file created in any directory listed in `/proc/mounts`.

If security settings prohibit using any of these for closures, `ffi_closure_alloc` will fail.