/* Release memory at the given address, as well as the corresponding executable page if it's separate. */ static int dlmunmap (void *start, size_t length) { /* We don't bother decreasing execsize or truncating the file, since we can't quite tell whether we're unmapping the end of the file. We don't expect frequent deallocation anyway. If we did, we could locate pages in the file by writing to the pages being deallocated and checking that the file contents change. Yuck. */ msegmentptr seg = segment_holding (gm, start); void *code; #if FFI_CLOSURE_TEST printf ("unmapping %zi\n", length); #endif if (seg && (code = add_segment_exec_offset (start, seg)) != start) { int ret = munmap (code, length); if (ret) return ret; } return munmap (start, length); }
/* Allocate a chunk of memory with the given size. Returns a pointer to the writable address, and sets *CODE to the executable corresponding virtual address. */ void * ffi_closure_alloc (size_t size, void **code) { void *ptr; if (!code) return NULL; ptr = dlmalloc (size); if (ptr) { msegmentptr seg = segment_holding (gm, ptr); *code = add_segment_exec_offset (ptr, seg); } return ptr; }