tags:

views:

52

answers:

1

I cannot seem to figure it out why the following does not work

import ctypes
from ctypes.util import find_library
libc = ctypes.CDLL(find_library('c'))

userpointer = ctypes.c_void_p
sizeimage = 320*240*2

if libc.posix_memalign(userpointer, libc.getpagesize(), sizeimage) != 0:
    raise Exception('ENOMEM')

I am trying to capture using v4l2. I cannot allocate memory. I tried passing ctypes.addressof(userpointer) I tried to cast userpointer to c_void_p but still nothing.

+1  A: 

You have two problems in your code: you make userpointer the ctypes.c_void_p type rather than an instance of it, and you're passing the void* that is userpointer to posix_memalign directly, instead of the void** that posix_memalign requires. The following will do what you want:

import ctypes
from ctypes.util import find_library
libc = ctypes.CDLL(find_library('c'))

userpointer = ctypes.c_void_p()
sizeimage = 320*240*2

if libc.posix_memalign(ctypes.byref(userpointer), libc.getpagesize(),
                       sizeimage) != 0:
    raise Exception('ENOMEM')
Thomas Wouters
yes. that worked just fine. thanks.
manson54