Пример #1
0
// setUpRing sets up the shared-memory ring buffer between the user process and the kernel.
func (h *TPacket) setUpRing() (err error) {
	totalSize := C.uint(h.opts.framesPerBlock * h.opts.numBlocks * h.opts.frameSize)
	switch h.tpVersion {
	case TPacketVersion1, TPacketVersion2:
		var tp C.struct_tpacket_req
		tp.tp_block_size = C.uint(h.opts.blockSize)
		tp.tp_block_nr = C.uint(h.opts.numBlocks)
		tp.tp_frame_size = C.uint(h.opts.frameSize)
		tp.tp_frame_nr = C.uint(h.opts.framesPerBlock * h.opts.numBlocks)
		if _, err := C.setsockopt(h.fd, C.SOL_PACKET, C.PACKET_RX_RING, unsafe.Pointer(&tp), C.socklen_t(unsafe.Sizeof(tp))); err != nil {
			return fmt.Errorf("setsockopt packet_rx_ring: %v", err)
		}
	case TPacketVersion3:
		var tp C.struct_tpacket_req3
		tp.tp_block_size = C.uint(h.opts.blockSize)
		tp.tp_block_nr = C.uint(h.opts.numBlocks)
		tp.tp_frame_size = C.uint(h.opts.frameSize)
		tp.tp_frame_nr = C.uint(h.opts.framesPerBlock * h.opts.numBlocks)
		tp.tp_retire_blk_tov = C.uint(h.opts.blockTimeout / time.Millisecond)
		if _, err := C.setsockopt(h.fd, C.SOL_PACKET, C.PACKET_RX_RING, unsafe.Pointer(&tp), C.socklen_t(unsafe.Sizeof(tp))); err != nil {
			return fmt.Errorf("setsockopt packet_rx_ring v3: %v", err)
		}
	default:
		return errors.New("invalid tpVersion")
	}
	if h.ring, err = C.mmap(nil, C.size_t(totalSize), C.PROT_READ|C.PROT_WRITE, C.MAP_SHARED, C.int(h.fd), 0); err != nil {
		return
	}
	if h.ring == nil {
		return errors.New("no ring")
	}
	return nil
}
Пример #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
}
Пример #3
0
func init() {
	mfile, err := os.OpenFile("/dev/mem", os.O_RDWR|os.O_SYNC, 0666)
	if err == nil {
		fd := C.int(mfile.Fd())
		gpiomem := C.malloc(8191)
		if gpiomem == nil {
			err = errors.New("init: malloc failed")
		}

		if err == nil {
			up := uintptr(gpiomem)
			if up%4096 != 0 {
				up = up + 4096 - (up % 4096)
			}
			np := unsafe.Pointer(up)
			gmap := C.mmap(np, 4096, C.PROT_READ|C.PROT_WRITE, C.MAP_SHARED|C.MAP_FIXED, fd, 0x20000000+0x200000)
			gpioRegBase = uintptr(gmap)
			if -1 == int(gpioRegBase) {
				err = errors.New("init: mmap failed")
			}

			if err == nil {
				gps := gpioRegBase + 0x1C
				gpc := gpioRegBase + 0x28
				gpl := gpioRegBase + 0x34
				gpioSet = (*uint)(unsafe.Pointer(gps))
				gpioClear = (*uint)(unsafe.Pointer(gpc))
				gpioLevel = (*uint)(unsafe.Pointer(gpl))
			}
		}
	}
}
Пример #4
0
func main() {
	fd, err := C.my_shm_open(C.CString(SHM_NAME))
	if err != nil {
		fmt.Println(err)
		return
	}

	ptr, err := C.mmap(nil, SHM_SIZE, C.PROT_READ|C.PROT_WRITE, C.MAP_SHARED, fd, 0)
	if err != nil {
		fmt.Println(err)
		return
	}
	C.close(fd)

	data := (*MyData)(unsafe.Pointer(ptr))

	fmt.Println(data)
}
Пример #5
0
func LeggoSetup() unsafe.Pointer {
	// We use mmap'd memory to communicate what to display;
	// c-leggo.c refresh() will copy this to the screen each frame

	size := (C.RESOLUTION_H + 1) * (C.RESOLUTION_W + 1) * 4 // RGBA
	screenMap, err := C.mmap(nil, C.size_t(size), C.PROT_READ|C.PROT_WRITE,
		C.MAP_ANON|C.MAP_SHARED,
		// TODO: on Mach, use VM_MAKE_TAG() in fd so vmmap can distinguish it
		-1, 0)
	if err != nil {
		panic(fmt.Sprintf("LeggoSetup(): failed to mmap: %s\n", err))
	}

	C.set_screen_map(screenMap, C.size_t(size))

	//fmt.Printf("sm = %s\n", screenMap)

	return screenMap
}
Пример #6
0
func main() {
	fd, err := C.my_shm_new(C.CString(SHM_NAME))
	if err != nil {
		fmt.Println(err)
		return
	}

	C.ftruncate(fd, SHM_SIZE)

	ptr, err := C.mmap(nil, SHM_SIZE, C.PROT_READ|C.PROT_WRITE, C.MAP_SHARED, fd, 0)
	if err != nil {
		fmt.Println(err)
		return
	}
	C.close(fd)

	data := (*MyData)(unsafe.Pointer(ptr))

	data.Col1 = 500
	data.Col2 = 200
	data.Col3 = 100
}