Message ID | 20191114093732.12637-2-noaos@mellanox.com (mailing list archive) |
---|---|
State | Not Applicable |
Headers | show |
Series | pyverbs: Introduce parent domain | expand |
On Thu, Nov 14, 2019 at 09:37:45AM +0000, Noa Osherovich wrote: > From: Edward Srouji <edwards@mellanox.com> > > Add new MemAlloc class which wraps some C memory allocation functions > such as aligned_alloc, malloc and free. > This allows Pyverbs' users to easily allocate and free memory in C > style, i.e when the memory address must be passed to a driver API. > > Signed-off-by: Edward Srouji <edwards@mellanox.com> > Reviewd-by: Daria Velikovsky <daria@mellanox.com> > Reviewd-by: Noa Osherovich <noaos@mellanox.com> > pyverbs/base.pxd | 3 +++ > pyverbs/base.pyx | 26 ++++++++++++++++++++++++++ > 2 files changed, 29 insertions(+) > > diff --git a/pyverbs/base.pxd b/pyverbs/base.pxd > index e85f7c020e1c..e956a79915ff 100644 > +++ b/pyverbs/base.pxd > @@ -9,3 +9,6 @@ cdef class PyverbsObject(object): > > cdef class PyverbsCM(PyverbsObject): > cpdef close(self) > + > +cdef class MemAlloc(object): > + pass > diff --git a/pyverbs/base.pyx b/pyverbs/base.pyx > index 8b3e6741ae19..a41cfc748ad0 100644 > +++ b/pyverbs/base.pyx > @@ -3,8 +3,14 @@ > > import logging > from pyverbs.pyverbs_error import PyverbsRDMAError > +from libc.stdlib cimport malloc, free > +from libc.stdint cimport uintptr_t > from libc.errno cimport errno > > +cdef extern from 'stdlib.h': > + void *aligned_alloc(size_t alignment, size_t size) posix_memalign() is the correct function to use, and a cdef for it is already in posix.stdlib > +cdef class MemAlloc(object): > + @staticmethod > + def malloc(size): > + ptr = malloc(size) > + if not ptr: > + raise MemoryError('Failed to allocate memory') > + return <uintptr_t>ptr > + > + @staticmethod > + def aligned_alloc(size, alignment=8): > + ptr = aligned_alloc(alignment, size) > + if not ptr: > + raise MemoryError('Failed to allocate memory') > + return <uintptr_t>ptr > + > + @staticmethod > + def free(ptr): > + free(<void*><uintptr_t>ptr) Why is this in a class? Jason
I put the memory related functions under MemAlloc for multiple reasons: - The methods won't be confused with the C functions - The class is extensible with more memory related methods and functionality I think the other option would be defining them in another, separate, module (outside "base" module - which I less preferred) -----Original Message----- From: Jason Gunthorpe <jgg@mellanox.com> Sent: Thursday, November 14, 2019 3:34 PM To: Noa Osherovich <noaos@mellanox.com> Cc: dledford@redhat.com; Leon Romanovsky <leonro@mellanox.com>; linux-rdma@vger.kernel.org; Edward Srouji <edwards@mellanox.com>; Daria Velikovsky <daria@mellanox.com> Subject: Re: [PATCH rdma-core 1/4] pyverbs: Add memory allocation class On Thu, Nov 14, 2019 at 09:37:45AM +0000, Noa Osherovich wrote: > From: Edward Srouji <edwards@mellanox.com> > > Add new MemAlloc class which wraps some C memory allocation functions > such as aligned_alloc, malloc and free. > This allows Pyverbs' users to easily allocate and free memory in C > style, i.e when the memory address must be passed to a driver API. > > Signed-off-by: Edward Srouji <edwards@mellanox.com> > Reviewd-by: Daria Velikovsky <daria@mellanox.com> > Reviewd-by: Noa Osherovich <noaos@mellanox.com> pyverbs/base.pxd | 3 > +++ pyverbs/base.pyx | 26 ++++++++++++++++++++++++++ > 2 files changed, 29 insertions(+) > > diff --git a/pyverbs/base.pxd b/pyverbs/base.pxd index > e85f7c020e1c..e956a79915ff 100644 > +++ b/pyverbs/base.pxd > @@ -9,3 +9,6 @@ cdef class PyverbsObject(object): > > cdef class PyverbsCM(PyverbsObject): > cpdef close(self) > + > +cdef class MemAlloc(object): > + pass > diff --git a/pyverbs/base.pyx b/pyverbs/base.pyx index > 8b3e6741ae19..a41cfc748ad0 100644 > +++ b/pyverbs/base.pyx > @@ -3,8 +3,14 @@ > > import logging > from pyverbs.pyverbs_error import PyverbsRDMAError > +from libc.stdlib cimport malloc, free from libc.stdint cimport > +uintptr_t > from libc.errno cimport errno > > +cdef extern from 'stdlib.h': > + void *aligned_alloc(size_t alignment, size_t size) posix_memalign() is the correct function to use, and a cdef for it is already in posix.stdlib > +cdef class MemAlloc(object): > + @staticmethod > + def malloc(size): > + ptr = malloc(size) > + if not ptr: > + raise MemoryError('Failed to allocate memory') > + return <uintptr_t>ptr > + > + @staticmethod > + def aligned_alloc(size, alignment=8): > + ptr = aligned_alloc(alignment, size) > + if not ptr: > + raise MemoryError('Failed to allocate memory') > + return <uintptr_t>ptr > + > + @staticmethod > + def free(ptr): > + free(<void*><uintptr_t>ptr) Why is this in a class? Jason
On Thu, Nov 14, 2019 at 05:00:00PM +0000, Edward Srouji wrote: > I put the memory related functions under MemAlloc for multiple reasons: > - The methods won't be confused with the C functions > - The class is extensible with more memory related methods and functionality > > I think the other option would be defining them in another, separate, module (outside "base" module - which I less preferred) It is an improper way to use a class Jason
diff --git a/pyverbs/base.pxd b/pyverbs/base.pxd index e85f7c020e1c..e956a79915ff 100644 --- a/pyverbs/base.pxd +++ b/pyverbs/base.pxd @@ -9,3 +9,6 @@ cdef class PyverbsObject(object): cdef class PyverbsCM(PyverbsObject): cpdef close(self) + +cdef class MemAlloc(object): + pass diff --git a/pyverbs/base.pyx b/pyverbs/base.pyx index 8b3e6741ae19..a41cfc748ad0 100644 --- a/pyverbs/base.pyx +++ b/pyverbs/base.pyx @@ -3,8 +3,14 @@ import logging from pyverbs.pyverbs_error import PyverbsRDMAError +from libc.stdlib cimport malloc, free +from libc.stdint cimport uintptr_t from libc.errno cimport errno +cdef extern from 'stdlib.h': + void *aligned_alloc(size_t alignment, size_t size) + + cpdef PyverbsRDMAErrno(str msg): return PyverbsRDMAError(msg, errno) @@ -58,3 +64,23 @@ cdef class PyverbsCM(PyverbsObject): cpdef close(self): pass + + +cdef class MemAlloc(object): + @staticmethod + def malloc(size): + ptr = malloc(size) + if not ptr: + raise MemoryError('Failed to allocate memory') + return <uintptr_t>ptr + + @staticmethod + def aligned_alloc(size, alignment=8): + ptr = aligned_alloc(alignment, size) + if not ptr: + raise MemoryError('Failed to allocate memory') + return <uintptr_t>ptr + + @staticmethod + def free(ptr): + free(<void*><uintptr_t>ptr)