summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJérôme Glisse <jglisse@redhat.com>2017-08-03 18:52:56 -0400
committerJérôme Glisse <jglisse@redhat.com>2017-08-04 17:38:34 -0400
commita4e1562dfe0664bfc2254e05fc9dd1d152d8ec9d (patch)
tree14b95783b7ddc80fc213439783335f5487bfc1cc
parenta471504e4bb164d72d2bb02119cac40b8306a8ce (diff)
compote: basic infrastructure to allocate memory using compote
Signed-off-by: Jérôme Glisse <jglisse@redhat.com>
-rw-r--r--compote-uapi.h36
-rw-r--r--compote.c63
-rw-r--r--compote.h12
3 files changed, 111 insertions, 0 deletions
diff --git a/compote-uapi.h b/compote-uapi.h
new file mode 100644
index 0000000..bbedb2a
--- /dev/null
+++ b/compote-uapi.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2017 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * Authors: Jérôme Glisse <jglisse@redhat.com>
+ */
+#ifndef COMPOTE_UAPI_H
+#define COMPOTE_UAPI_H
+
+#include <linux/types.h>
+#include <linux/ioctl.h>
+#include <linux/irqnr.h>
+
+struct compote_ioctl_mem_alloc {
+ uint64_t nbytes;
+ uint64_t foffset;
+};
+
+struct compote_ioctl_mem_free {
+ uint64_t foffset;
+};
+
+/* Expose the address space of the calling process through hmm dummy dev file */
+#define COMPOTE_IOCTL_MEM_ALLOC _IOWR('H', 0x00, struct compote_ioctl_mem_alloc)
+#define COMPOTE_IOCTL_MEM_FREE _IOWR('H', 0x01, struct compote_ioctl_mem_free)
+
+#endif /* COMPOTE_UAPI_H */
diff --git a/compote.c b/compote.c
index 1267118..190aff6 100644
--- a/compote.c
+++ b/compote.c
@@ -25,6 +25,7 @@
#include <fcntl.h>
#include "compote.h"
+#include "compote-uapi.h"
int compote_context_new(compote_context_t **ctxp)
@@ -59,9 +60,65 @@ void compote_context_del(compote_context_t **ctxp)
free(ctx);
}
+int compote_context_ioctl(compote_context_t *ctx, int command, void *arg)
+{
+ do {
+ int ret;
+
+ ret = ioctl(ctx->fd, command, arg);
+ if (ret && errno != EINTR) {
+ printf("ret %d errno %d\n", ret, errno);
+ return ret;
+ }
+ } while (errno == EINTR);
+
+ return 0;
+}
+
+int compote_mo_new(compote_context_t *ctx, compote_mo_t **mop, uint64_t nbytes)
+{
+ struct compote_ioctl_mem_alloc arg;
+ compote_mo_t *mo;
+ int ret;
+
+ *mop = NULL;
+ mo = calloc(1, sizeof(*mo));
+ if (mo == NULL) {
+ return -ENOMEM;
+ }
+
+ arg.nbytes = nbytes;
+ ret = compote_context_ioctl(ctx, COMPOTE_IOCTL_MEM_ALLOC, &arg);
+ if (ret) {
+ free(mo);
+ return ret;
+ }
+
+ mo->foffset = arg.foffset;
+ mo->nbytes = nbytes;
+ *mop = mo;
+ return 0;
+}
+
+void compote_mo_del(compote_context_t *ctx, compote_mo_t **mop)
+{
+ struct compote_ioctl_mem_free arg;
+ compote_mo_t *mo = *mop;
+
+ *mop = NULL;
+ if (mo == NULL) {
+ return;
+ }
+
+ arg.foffset = mo->foffset;
+ compote_context_ioctl(ctx, COMPOTE_IOCTL_MEM_FREE, &arg);
+ free(mo);
+}
+
int main(int argc, char *argv[])
{
compote_context_t *ctx;
+ compote_mo_t *mo;
int ret;
ret = compote_context_new(&ctx);
@@ -69,8 +126,14 @@ int main(int argc, char *argv[])
return ret;
}
+ ret = compote_mo_new(ctx, &mo, 64 << 10);
+ if (ret) {
+ return ret;
+ }
+
printf("La compote c'est bon !\n");
+ compote_mo_del(ctx, &mo);
compote_context_del(&ctx);
return 0;
}
diff --git a/compote.h b/compote.h
index f2ea929..4c5bb18 100644
--- a/compote.h
+++ b/compote.h
@@ -16,11 +16,23 @@
#ifndef COMPOTE_H
#define COMPOTE_H
+#include <stdint.h>
+
typedef struct {
int fd;
} compote_context_t;
+typedef struct {
+ uint64_t foffset;
+ uint64_t nbytes;
+ void *ptr;
+} compote_mo_t;
+
int compote_context_new(compote_context_t **ctxp);
void compote_context_del(compote_context_t **ctxp);
+int compote_context_ioctl(compote_context_t *ctx, int command, void *arg);
+
+int compote_mo_new(compote_context_t *ctx, compote_mo_t **mop, uint64_t nbytes);
+void compote_mo_del(compote_context_t *ctx, compote_mo_t **mop);
#endif // COMPOTE_H