// RWFromMem prepares a read-write memory buffer for use with RWops.
// (https://wiki.libsdl.org/SDL_RWFromMem)
424520
func RWFromMem(mem []byte) (*RWops, error) {
	if mem == nil {
		return nil, ErrInvalidParameters
	}

	header := (*reflect.SliceHeader)(unsafe.Pointer(&mem))
	_mem := unsafe.Pointer(header.Data)

	rwops := (*RWops)(unsafe.Pointer(C.SDL_RWFromMem(_mem, C.int(len(mem)))))
	if rwops == nil {
		return nil, GetError()
	}
	return rwops, nil
}
253949
func (rwops *RWops) WriteBE32(value uint32) uint {
	if rwops == nil {
		return 0
	}
	return uint(C.SDL_WriteBE32(rwops.cptr(), C.Uint32(value)))
}
849528
func LoadPCXRW(src *sdl.RWops) (*sdl.Surface, error) {
	_src := (*C.SDL_RWops)(unsafe.Pointer(src))
	_surface := C.IMG_LoadPCX_RW(_src)
	if _surface == nil {
		return nil, GetError()
	}
	return (*sdl.Surface)(unsafe.Pointer(_surface)), nil
}
1331674
func NewMem() Data {
	return &memData{l: &sync.RWMutex{}, strings: make(map[string]string), ints: make(map[string]int)}
}
218798
func LoadXCFRW(src *sdl.RWops) (*sdl.Surface, error) {
	_src := (*C.SDL_RWops)(unsafe.Pointer(src))
	_surface := C.IMG_LoadXCF_RW(_src)
	if _surface == nil {
		return nil, GetError()
	}
	return (*sdl.Surface)(unsafe.Pointer(_surface)), nil
}
1127467
func NewMemoryMappedWriter(loc string, size int) (*MemoryMappedWriter, error) {
	if _, err := os.Stat(loc); err == nil {
		err = os.Remove(loc)
		if err != nil {
			return nil, err
		}
	}

	// ensure destination directory exists
	dir := filepath.Dir(loc)
	err := os.MkdirAll(dir, 0700)
	if err != nil {
		return nil, err
	}

	f, err := os.OpenFile(loc, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0644)
	if err != nil {
		return nil, err
	}

	l, err := f.Write(make([]byte, size))
	if err != nil {
		return nil, err
	}
	if l < size {
		return nil, errors.Errorf("Could not initialize %d bytes", size)
	}

	b, err := mmap.Map(f, mmap.RDWR, 0)
	if err != nil {
		return nil, err
	}

	return &MemoryMappedWriter{
		NewByteWriterSlice(b),
		f,
		loc,
		size,
	}, nil
}
922711
func (servo *Servo) WriteMicroseconds(ms int) {
	// just pass to the underlying PWM pin.
	servo.PWM.SetDuty(servo.Pin, int64(ms*1000))
}
566807
func copyMemory(dest unsafe.Pointer, src unsafe.Pointer, length uint32) {
	procCopyMemory.Call(uintptr(dest), uintptr(src), uintptr(length))
}
867987
func (rwops *RWops) WriteLE64(value uint64) uint {
	if rwops == nil {
		return 0
	}
	return uint(C.SDL_WriteLE64(rwops.cptr(), C.Uint64(value)))
}
1959
func (rwops *RWops) Free() error {
	if rwops == nil {
		return ErrInvalidParameters
	}

	C.SDL_FreeRW(rwops.cptr())
	return nil
}
998910
func LoadGIFRW(src *sdl.RWops) (*sdl.Surface, error) {
	_src := (*C.SDL_RWops)(unsafe.Pointer(src))
	_surface := C.IMG_LoadGIF_RW(_src)
	if _surface == nil {
		return nil, GetError()
	}
	return (*sdl.Surface)(unsafe.Pointer(_surface)), nil
}
513632
func instrImmMem(ctx context, instr Instruction, imm int64, dst *register, dstName string, dstOffset int) string {
	asm := fmt.Sprintf("%-9v    $%v, %v+%v(%v)\n", instr, imm, dstName, dstOffset, dst.name)
	return strings.Replace(asm, "+-", "-", -1)
}
1335132
func LoadBMPRW(src *sdl.RWops) (*sdl.Surface, error) {
	_src := (*C.SDL_RWops)(unsafe.Pointer(src))
	_surface := C.IMG_LoadBMP_RW(_src)
	if _surface == nil {
		return nil, GetError()
	}
	return (*sdl.Surface)(unsafe.Pointer(_surface)), nil
}
1237882
func LoadMUSTypeRW(src *sdl.RWops, type_ MusicType, freesrc int) (mus *Music, err error) {
	_src := (*C.SDL_RWops)(unsafe.Pointer(src))
	_type := (C.Mix_MusicType)(type_)
	_freesrc := (C.int)(freesrc)
	mus = (*Music)(unsafe.Pointer(C.Mix_LoadMUSType_RW(_src, _type,
		_freesrc)))
	if mus == nil {
		err = sdl.GetError()
	}
	return
}
1352983
func (i *InstructionClearActions) ReadFrom(r io.Reader) (int64, error) {
	return encoding.ReadFrom(r, &defaultPad8)
}
335621
func (i *InstructionWriteMetadata) ReadFrom(r io.Reader) (int64, error) {
	return encoding.ReadFrom(r, &instruction{},
		&defaultPad4, &i.Metadata, &i.MetadataMask)
}
531604
func memoryMap(path string) (*os.File, mmap.MMap, []uint32, error) {
	file, err := os.OpenFile(path, os.O_RDONLY, 0644)
	if err != nil {
		return nil, nil, nil, err
	}
	mem, buffer, err := memoryMapFile(file, false)
	if err != nil {
		file.Close()
		return nil, nil, nil, err
	}
	for i, magic := range dumpMagic {
		if buffer[i] != magic {
			mem.Unmap()
			file.Close()
			return nil, nil, nil, ErrInvalidDumpMagic
		}
	}
	return file, mem, buffer[len(dumpMagic):], err
}