Esempio n. 1
0
// Set a pixel through direct memory access
// TODO: higher-level functions
func WritePixel(x int, y int, r byte, g byte, b byte, a byte) {
	if x > C.RESOLUTION_W || y > C.RESOLUTION_H {
		// This is important, because write_byte has no bounds checking!
		panic(fmt.Sprintf("WritePixel out of range: (%d,%d)\n", x, y))
	}
	offset := 4 * (x + y*C.RESOLUTION_W)
	C.write_byte(C.off_t(offset+0), C.uint8_t(r))
	C.write_byte(C.off_t(offset+1), C.uint8_t(g))
	C.write_byte(C.off_t(offset+2), C.uint8_t(b))
	C.write_byte(C.off_t(offset+3), C.uint8_t(a))
}
Esempio n. 2
0
func (w *window) newBuffer(width, height int,
	format uint32) *C.struct_wl_buffer {

	stride := width * 4
	size := stride * height

	fd := C.os_create_anonymous_file(C.off_t(size))
	if fd < 0 {
		panic("Could not create buffer file.")
	}

	data := C.mmap(nil, C.size_t(size), C.PROT_READ|C.PROT_WRITE,
		C.MAP_SHARED, C.int(fd), 0)
	if *(*int)(data) == -1 {
		panic("mmap failed")
		C.close(fd)
		return nil
	}

	pool := C.wl_shm_create_pool(w.display.shm,
		C.int32_t(fd), C.int32_t(size))
	buffer := C.wl_shm_pool_create_buffer(pool, 0,
		C.int32_t(width), C.int32_t(height),
		C.int32_t(stride), C.uint32_t(format))
	C.wl_shm_pool_destroy(pool)
	C.close(fd)

	w.shmData = data
	return buffer
}
Esempio n. 3
0
//export pread_cb
func pread_cb(handle unsafe.Pointer, buf unsafe.Pointer, count C.size_t, offset C.off_t) C.off_t {
	v, ok := preadHandleCallbacks[(*interface{})(handle)]
	if !ok {
		return -1 // couldn't find callback
	}
	return C.off_t(v((*interface{})(handle), C.GoBytes(buf, C.int(count)), int64(offset)))
}
Esempio n. 4
0
func (a *asset) Seek(offset int64, whence int) (int64, error) {
	// TODO(crawshaw): use AAsset_seek64 if it is available.
	off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence))
	if off == -1 {
		return 0, a.errorf("seek", "bad result for offset=%d, whence=%d", offset, whence)
	}
	return int64(off), nil
}
Esempio n. 5
0
//export gogpgme_seekfunc
func gogpgme_seekfunc(handle unsafe.Pointer, offset C.off_t, whence C.int) C.off_t {
	d := callbackLookup(uintptr(handle)).(*Data)
	n, err := d.s.Seek(int64(offset), int(whence))
	if err != nil {
		C.gpgme_err_set_errno(C.EIO)
		return -1
	}
	return C.off_t(n)
}
Esempio n. 6
0
// ReadAt reads the information from the vdi starting from the
// given offset. We implemented the io.ReaderAt interface here.
// The returned values match the returned values of Read.
func (v *VDI) ReadAt(buf []byte, offset int) (int, error) {
	_, err := C.sd_vdi_read(
		v.cluster.cluster,
		v.vdi,
		unsafe.Pointer(&buf[0]),
		C.size_t(len(buf)),
		C.off_t(offset))
	return len(buf), err
}
Esempio n. 7
0
File: fd.go Progetto: kshlm/gogfapi
func (fd *Fd) Fallocate(mode int, offset int64, len int64) error {
	ret, err := C.glfs_fallocate(fd.fd, C.int(mode),
		C.off_t(offset), C.size_t(len))

	if ret == 0 {
		err = nil
	}
	return err
}
Esempio n. 8
0
// WriteAt helps implement the io.WriterAt interface by allowing
// an additional offset to start writing to within the VDI.
func (v *VDI) WriteAt(buf []byte, offset int) (int, error) {
	if _, err := C.sd_vdi_write(
		v.cluster.cluster,
		v.vdi,
		unsafe.Pointer(&buf[0]),
		C.size_t(len(buf)),
		C.off_t(offset),
	); err != nil {
		return 0, err
	}

	return len(buf), nil
}
Esempio n. 9
0
func (a *InoAttr) toCStat(o *C.struct_stat, timeout *C.double) {
	o.st_ino = C.__darwin_ino64_t(a.Ino)
	o.st_mode = C.mode_t(a.Mode)
	o.st_nlink = C.nlink_t(a.Nlink)
	o.st_size = C.off_t(a.Size)
	if a.Uid != nil {
		o.st_uid = C.uid_t(*a.Uid)
	}
	if a.Gid != nil {
		o.st_gid = C.gid_t(*a.Gid)
	}
	toCTime(&o.st_ctimespec, a.Ctim)
	toCTime(&o.st_mtimespec, a.Mtim)
	toCTime(&o.st_atimespec, a.Atim)
	if timeout != nil {
		(*timeout) = C.double(a.Timeout)
	}
}
Esempio n. 10
0
func falloc(fileSize int64, filePath string) error {
	err := checkParam(fileSize, filePath)
	if err != nil {
		return err
	}

	f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		return err
	}

	errno := C.posix_fallocate(C.int(f.Fd()), 0, C.off_t(int32(fileSize)))
	if errno != 0 {
		return syscall.Errno(errno)
	}

	return nil
}
Esempio n. 11
0
func (buf *VirtioBuffer) doIO(
	fd int,
	fd_offset int64,
	buf_offset int,
	length int,
	write C.int) (int, error) {

	// Gather the appropriate elements.
	ptrs, lens := buf.Gather(buf_offset, length)

	// Actually execute our readv/writev.
	rval := C.do_iovec(
		C.int(fd),
		C.int(len(ptrs)),
		&ptrs[0],
		&lens[0],
		C.off_t(fd_offset),
		write)
	if rval < 0 {
		return 0, syscall.Errno(int(-rval))
	}

	return int(rval), nil
}
Esempio n. 12
0
func (d *Data) Seek(offset int64, whence int) (int64, error) {
	n, err := C.gpgme_data_seek(d.dh, C.off_t(offset), C.int(whence))
	return int64(n), err
}
Esempio n. 13
0
// Pwrite writes len(b) bytes from b into the Fd from offset off
//
// Returns number of bytes written on success and error on failure
func (fd *Fd) Pwrite(b []byte, off int64) (int, error) {
	n, err := C.glfs_pwrite(fd.fd, unsafe.Pointer(&b[0]), C.size_t(len(b)), C.off_t(off), 0)

	return int(n), err
}
Esempio n. 14
0
// Ftruncate truncates the size of the Fd to the given size
//
// Returns error on failure
func (fd *Fd) Ftruncate(size int64) error {
	_, err := C.glfs_ftruncate(fd.fd, C.off_t(size))

	return err
}
Esempio n. 15
0
func (fd *Fd) lseek(offset int64, whence int) (int64, error) {
	ret, err := C.glfs_lseek(fd.fd, C.off_t(offset), C.int(whence))

	return int64(ret), err
}
Esempio n. 16
0
func (d *dirBuf) Add(name string, ino int64, mode int, next int64) bool {
	cstr := C.CString(name)
	res := C.DirBufAdd(d.db, cstr, C.fuse_ino_t(ino), C.int(mode), C.off_t(next))
	C.free(unsafe.Pointer(cstr))
	return res == 0
}
Esempio n. 17
0
File: easy.go Progetto: ym/go-curl
// curl_easy_setopt - set options for a curl easy handle
// WARNING: a function pointer is &fun, but function addr is reflect.ValueOf(fun).Pointer()
func (curl *CURL) Setopt(opt int, param interface{}) error {
	p := curl.handle
	if param == nil {
		// NOTE: some option will crash program when got a nil param
		return newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), nil))
	}
	switch {
	// not really set
	case opt == OPT_READDATA: // OPT_INFILE
		curl.readData = &param
		return nil
	case opt == OPT_DEBUGDATA:
		curl.debugData = &param
		return nil
	case opt == OPT_PROGRESSDATA:
		curl.progressData = &param
		return nil
	case opt == OPT_HEADERDATA: // also known as OPT_WRITEHEADER
		curl.headerData = &param
		return nil
	case opt == OPT_WRITEDATA: // OPT_FILE
		curl.writeData = &param
		return nil

	case opt == OPT_READFUNCTION:
		fun := param.(func([]byte, interface{}) int)
		curl.readFunction = &fun

		ptr := C.return_read_function()
		if err := newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), ptr)); err == nil {
			return newCurlError(C.curl_easy_setopt_pointer(p, OPT_READDATA,
				unsafe.Pointer(reflect.ValueOf(curl).Pointer())))
		} else {
			return err
		}

	case opt == OPT_PROGRESSFUNCTION:
		fun := param.(func(float64, float64, float64, float64, interface{}) bool)
		curl.progressFunction = &fun

		ptr := C.return_progress_function()
		if err := newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), ptr)); err == nil {
			return newCurlError(C.curl_easy_setopt_pointer(p, OPT_PROGRESSDATA,
				unsafe.Pointer(reflect.ValueOf(curl).Pointer())))
		} else {
			return err
		}

	case opt == OPT_DEBUGFUNCTION:
		fun := param.(func(int, []byte) bool)
		curl.debugFunction = &fun

		ptr := C.return_debug_function()
		if err := newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), ptr)); err == nil {
			return newCurlError(C.curl_easy_setopt_pointer(p, OPT_DEBUGDATA,
				unsafe.Pointer(reflect.ValueOf(curl).Pointer())))
		} else {
			return err
		}

	case opt == OPT_HEADERFUNCTION:
		fun := param.(func([]byte, interface{}) bool)
		curl.headerFunction = &fun

		ptr := C.return_header_function()
		if err := newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), ptr)); err == nil {
			return newCurlError(C.curl_easy_setopt_pointer(p, OPT_HEADERDATA,
				unsafe.Pointer(reflect.ValueOf(curl).Pointer())))
		} else {
			return err
		}

	case opt == OPT_WRITEFUNCTION:
		fun := param.(func([]byte, interface{}) bool)
		curl.writeFunction = &fun

		ptr := C.return_write_function()
		if err := newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), ptr)); err == nil {
			return newCurlError(C.curl_easy_setopt_pointer(p, OPT_WRITEDATA,
				unsafe.Pointer(reflect.ValueOf(curl).Pointer())))
		} else {
			return err
		}

	// for OPT_HTTPPOST, use struct Form
	case opt == OPT_HTTPPOST:
		post := param.(*Form)
		ptr := post.head
		return newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), unsafe.Pointer(ptr)))

	case opt >= C.CURLOPTTYPE_OFF_T:
		val := C.off_t(0)
		switch t := param.(type) {
		case int:
			val = C.off_t(t)
		case uint64:
			val = C.off_t(t)
		default:
			panic("OFF_T conversion not supported")
		}
		return newCurlError(C.curl_easy_setopt_off_t(p, C.CURLoption(opt), val))

	case opt >= C.CURLOPTTYPE_FUNCTIONPOINT:
		// function pointer
		panic("function pointer not implemented yet!")

	case opt >= C.CURLOPTTYPE_OBJECTPOINT:
		switch t := param.(type) {
		case string:
			ptr := C.CString(t)
			curl.mallocAddPtr(ptr)
			return newCurlError(C.curl_easy_setopt_string(p, C.CURLoption(opt), ptr))
		case CurlString:
			ptr := (*C.char)(t)
			return newCurlError(C.curl_easy_setopt_string(p, C.CURLoption(opt), ptr))
		case []string:
			if len(t) > 0 {
				ptr := C.CString(t[0])
				curl.mallocAddPtr(ptr)
				a_slist := C.curl_slist_append(nil, ptr)
				for _, s := range t[1:] {
					ptr := C.CString(s)
					curl.mallocAddPtr(ptr)
					a_slist = C.curl_slist_append(a_slist, ptr)
				}
				return newCurlError(C.curl_easy_setopt_slist(p, C.CURLoption(opt), a_slist))
			} else {
				return newCurlError(C.curl_easy_setopt_slist(p, C.CURLoption(opt), nil))
			}
		case []CurlString:
			if len(t) > 0 {
				ptr := (*C.char)(t[0])
				a_slist := C.curl_slist_append(nil, ptr)
				for _, s := range t[1:] {
					ptr := (*C.char)(s)
					a_slist = C.curl_slist_append(a_slist, ptr)
				}
				return newCurlError(C.curl_easy_setopt_slist(p, C.CURLoption(opt), a_slist))
			} else {
				return newCurlError(C.curl_easy_setopt_slist(p, C.CURLoption(opt), nil))
			}
		default:
			// It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
			// val := reflect.ValueOf(param)
			//fmt.Printf("DEBUG(Setopt): param=%x\n", val.Pointer())
			//println("DEBUG can addr =", val.Pointer(), "opt=", opt)
			// pass a pointer to GoInterface
			return newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt),
				unsafe.Pointer(&param)))
		}
	case opt >= C.CURLOPTTYPE_LONG:
		val := C.long(0)
		switch t := param.(type) {
		case int:
			val = C.long(t)
		case bool:
			if t {
				val = 1
			}
		case int64:
			val = C.long(t)
		case int32:
			val = C.long(t)
		default:
			panic("not supported converstion to c long")
		}
		return newCurlError(C.curl_easy_setopt_long(p, C.CURLoption(opt), val))
	}
	panic("opt param error!")
}