public inbox for linux-rdma@vger.kernel.org
 help / color / mirror / Atom feed
From: Noa Osherovich <noaos@mellanox.com>
To: "dledford@redhat.com" <dledford@redhat.com>,
	Jason Gunthorpe <jgg@mellanox.com>,
	Leon Romanovsky <leonro@mellanox.com>
Cc: "linux-rdma@vger.kernel.org" <linux-rdma@vger.kernel.org>,
	Edward Srouji <edwards@mellanox.com>,
	Daria Velikovsky <daria@mellanox.com>,
	Noa Osherovich <noaos@mellanox.com>
Subject: [PATCH rdma-core 1/4] pyverbs: Add memory allocation class
Date: Thu, 14 Nov 2019 09:37:45 +0000	[thread overview]
Message-ID: <20191114093732.12637-2-noaos@mellanox.com> (raw)
In-Reply-To: <20191114093732.12637-1-noaos@mellanox.com>

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
--- 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)
-- 
2.21.0


  reply	other threads:[~2019-11-14  9:37 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-14  9:37 [PATCH rdma-core 0/4] pyverbs: Introduce parent domain Noa Osherovich
2019-11-14  9:37 ` Noa Osherovich [this message]
2019-11-14 13:33   ` [PATCH rdma-core 1/4] pyverbs: Add memory allocation class Jason Gunthorpe
2019-11-14 17:00     ` Edward Srouji
2019-11-14 17:04       ` Jason Gunthorpe
2019-11-14  9:37 ` [PATCH rdma-core 2/4] pyverbs: Introduce ParentDomain class Noa Osherovich
2019-11-14 13:39   ` Jason Gunthorpe
2019-11-14 16:49     ` Edward Srouji
2019-11-14 16:50       ` Jason Gunthorpe
     [not found]         ` <VI1PR05MB4157C8E9CA220454969633A6D74E0@VI1PR05MB4157.eurprd05.prod.outlook.com>
2019-11-21 16:31           ` Jason Gunthorpe
2019-11-14  9:37 ` [PATCH rdma-core 3/4] pyverbs: Document ParentDomain class and add a simple example Noa Osherovich
2019-11-14  9:37 ` [PATCH rdma-core 4/4] tests: Add a test for parent domain Noa Osherovich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191114093732.12637-2-noaos@mellanox.com \
    --to=noaos@mellanox.com \
    --cc=daria@mellanox.com \
    --cc=dledford@redhat.com \
    --cc=edwards@mellanox.com \
    --cc=jgg@mellanox.com \
    --cc=leonro@mellanox.com \
    --cc=linux-rdma@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox