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@xxxxxxxxxxxx> Sent: Thursday, November 14, 2019 3:34 PM To: Noa Osherovich <noaos@xxxxxxxxxxxx> Cc: dledford@xxxxxxxxxx; Leon Romanovsky <leonro@xxxxxxxxxxxx>; linux-rdma@xxxxxxxxxxxxxxx; Edward Srouji <edwards@xxxxxxxxxxxx>; Daria Velikovsky <daria@xxxxxxxxxxxx> 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@xxxxxxxxxxxx> > > 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@xxxxxxxxxxxx> > Reviewd-by: Daria Velikovsky <daria@xxxxxxxxxxxx> > Reviewd-by: Noa Osherovich <noaos@xxxxxxxxxxxx> 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