func Alloc1d(n int) []complex128 { buffer := (unsafe.Pointer)(C.fftw_malloc((C.size_t)(16 * n))) var slice []complex128 header := (*reflect.SliceHeader)(unsafe.Pointer(&slice)) *header = reflect.SliceHeader{uintptr(buffer), n, n} return slice }
func Alloc1d(n int) []complex128 { // Try to allocate memory. buffer, err := C.fftw_malloc(C.size_t(16 * n)) if err != nil { // If malloc failed, invoke garbage collector and try again. runtime.GC() buffer, err = C.fftw_malloc(C.size_t(16 * n)) if err != nil { // If it still failed, then panic. panic(fmt.Sprint("Could not fftw_malloc for ", n, " elements: ", err)) } } // Create a slice header for the memory. var slice []complex128 header := (*reflect.SliceHeader)(unsafe.Pointer(&slice)) header.Data = uintptr(buffer) header.Len = n header.Cap = n // In the spirit of Go, initialize all memory to zero. for i := 0; i < n; i++ { slice[i] = 0 } return slice }
// Alloc2D allocates a complex matrix for fftw func Alloc2D(m, n int) ([][]complex128, error) { if m <= 0 || n <= 0 { return [][]complex128{}, fmt.Errorf("positive dimensions required") } samples := m * n var slice []complex128 header := (*reflect.SliceHeader)(unsafe.Pointer(&slice)) *header = reflect.SliceHeader{ Data: uintptr(C.fftw_malloc((C.size_t)(16 * samples))), Len: samples, Cap: samples, } r := make([][]complex128, m) for i := range r { r[i] = slice[i*n : (i+1)*n] } return r, nil }