On Tue, Mar 18, 2025 at 12:08 AM Jason E. Aten <j.e.a...@gmail.com> wrote: > > I'm porting some C code that does manual filesystem "extent" > file space management from Linux to Darwin (and into Go). > > The Linux fallocate() call and its FALLOC_FL_INSERT_RANGE > operation are not directly available on Darwin, but suggestions from > StackOverflow > indicate that fcntl(2) with F_PREALLOCATE might be a close substitute. > > It seems to be working. Full code here in this playground, but I have a > couple of questions-- > > https://go.dev/play/p/Z5JB__fUBpI > > Can I get away without the runtime.Pinner? (Am I still safe from 'store' > being pre-maturely garbage collected without the pin, since Go memory has > been converted to an uintptr before calling unix.Syscall())
You don't need the runtime.Pinner but you do need to do the conversion to uintptr in the call to unix.Syscall. That is unix.Syscall(fcntl64Syscall, uintptr(fd), uintptr(syscall.F_PREALLOCATE), uintptr(unsafe.Pointer(ustore))) This uses rule 4 at https://pkg.go.dev/unsafe#Pointer. The memory will be pinned for the duration of the system call. > Is there a more appropriate way to call fcntl() (on Darwin)? Darwin doesn't particularly like calling unix.Syscall directly, although it does work. They prefer that you call the C function. You could do that via cgo by calling C.fcntl. Or in this case it seems simpler to just use the unix.FcntlFstore function which already exists and does the right thing. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUNVtT_crhssAdcBvbh9UXh4Z_u2xtWrDRqr5VDpk4LfQ%40mail.gmail.com.