aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Gaignard <benjamin.gaignard@linaro.org>2016-05-03 11:29:20 +0200
committerBenjamin Gaignard <benjamin.gaignard@linaro.org>2016-05-03 11:29:20 +0200
commit7d55a5f578d480ee97689f961891a7e9091d6937 (patch)
treeda03303b59b2950cd89160ec528023cffca37868
parent8eb643b5ae40b64b64f0c82f915ab278bf7fed18 (diff)
downloadlibsmaf-7d55a5f578d480ee97689f961891a7e9091d6937.tar.gz
allow to get allocator names
This ioctl is available since smaf v7 Signed-off-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
-rw-r--r--lib/libsmaf.c46
-rw-r--r--lib/libsmaf.h8
-rw-r--r--lib/smaf.h14
-rw-r--r--tests/test_smaf.c133
4 files changed, 182 insertions, 19 deletions
diff --git a/lib/libsmaf.c b/lib/libsmaf.c
index ee2ba81..47a9299 100644
--- a/lib/libsmaf.c
+++ b/lib/libsmaf.c
@@ -122,3 +122,49 @@ int smaf_get_secure(int fd)
return flag.secure;
}
+
+int smaf_allocator_count()
+{
+ struct smaf_info info;
+ int ret;
+
+ if (smaf_fd == -1)
+ return -1;
+
+ memset(&info, 0, sizeof(info));
+
+ ret = ioctl(smaf_fd, SMAF_IOC_GET_INFO, &info);
+
+ if (ret)
+ return ret;
+
+ return info.count;
+}
+
+char *smaf_get_allocator_name(int index)
+{
+ struct smaf_info info;
+ char *name;
+ int ret;
+
+ if (smaf_fd == -1)
+ return NULL;
+
+ memset(&info, 0, sizeof(info));
+ info.index = index;
+
+ ret = ioctl(smaf_fd, SMAF_IOC_GET_INFO, &info);
+
+ if (ret)
+ return NULL;
+
+ if (!info.count)
+ return NULL;
+
+ name = (char*)malloc(ALLOCATOR_NAME_LENGTH);
+ strncpy(name, info.name, ALLOCATOR_NAME_LENGTH);
+ return name;
+}
+
+
+
diff --git a/lib/libsmaf.h b/lib/libsmaf.h
index 8cde1b3..f7be135 100644
--- a/lib/libsmaf.h
+++ b/lib/libsmaf.h
@@ -39,6 +39,14 @@ int smaf_create_buffer(unsigned int length, unsigned int flags, char *name, int
int smaf_set_secure(int fd, int secure);
int smaf_get_secure(int fd);
+/* get number of registered allocators */
+int smaf_allocator_count();
+
+/* return alloctor name per index
+ * it is up to caller to release returned value
+ */
+char *smaf_get_allocator_name(int index);
+
#if defined(__cplusplus)
}
#endif
diff --git a/lib/smaf.h b/lib/smaf.h
index 428168e..5a9201b 100644
--- a/lib/smaf.h
+++ b/lib/smaf.h
@@ -38,6 +38,18 @@ struct smaf_secure_flag {
int secure;
};
+/**
+ * struct smaf_info - get registered allocator name per index
+ * @index: allocator's index
+ * @count: return number of registered allocators
+ * @name: return allocator name
+ */
+struct smaf_info {
+ int index;
+ int count;
+ char name[ALLOCATOR_NAME_LENGTH];
+};
+
#define SMAF_IOC_MAGIC 'S'
#define SMAF_IOC_CREATE _IOWR(SMAF_IOC_MAGIC, 0, \
@@ -49,4 +61,6 @@ struct smaf_secure_flag {
#define SMAF_IOC_SET_SECURE_FLAG _IOWR(SMAF_IOC_MAGIC, 2, \
struct smaf_secure_flag)
+#define SMAF_IOC_GET_INFO _IOWR(SMAF_IOC_MAGIC, 3, struct smaf_info)
+
#endif
diff --git a/tests/test_smaf.c b/tests/test_smaf.c
index 710ae62..e01980a 100644
--- a/tests/test_smaf.c
+++ b/tests/test_smaf.c
@@ -21,10 +21,28 @@
#include <../lib/libsmaf.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/mman.h>
#define LENGTH 1024*16
+static void test_create_named(char *name)
+{
+ int ret;
+ int fd;
+
+ ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, name, &fd);
+
+ if (ret || (fd == -1)) {
+ printf("%s: smaf_create_buffer() failed %d\n", __func__, ret);
+ return;
+ }
+
+ printf("%s: smaf_create_buffer() for allocator %s successed\n", __func__, name);
+
+ close(fd);
+}
+
static void test_create_named_invalid(void)
{
int ret;
@@ -33,30 +51,59 @@ static void test_create_named_invalid(void)
ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, "deadbeef", &fd);
if (!ret) {
- printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
+ printf("%s: smaf_create_buffer() failed %d\n", __func__, ret);
return;
}
- printf("%s successed\n", __func__);
+ printf("%s: successed\n", __func__);
}
-static void test_create_named(void)
+static void test_iter_over_allocators(void)
{
- int ret;
- int fd;
+ int i;
+ int count = smaf_allocator_count();
+ char *name;
- ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, "smaf-cma", &fd);
+ if (count <= 0) {
+ printf("%s: smaf_allocator_count() failed %d\n", __func__, count);
+ return;
+ }
- if (ret || (fd == -1)) {
- printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
+ printf("%s: smaf_allocator_count() found %d allocators\n", __func__, count);
+ for (i = 0; i < count; i++) {
+ name = smaf_get_allocator_name(i);
+ if (!name) {
+ printf("%s: smaf_get_allocator_name() failed %d\n", __func__, i);
+ return;
+ }
+
+ test_create_named(name);
+ free(name);
+ }
+}
+
+static void test_invalid_allocator_index(void)
+{
+ int count = smaf_allocator_count();
+ char *name;
+
+ if (count <= 0) {
+ printf("%s: smaf_allocator_count() failed %d\n", __func__, count);
return;
}
- printf("%s successed\n", __func__);
+ printf("%s: smaf_allocator_count() found %d allocators\n", __func__, count);
+ name = smaf_get_allocator_name(count);
+ if (name) {
+ printf("%s: smaf_get_allocator_name() failed %s\n", __func__, name);
+ free(name);
+ return;
+ }
- close(fd);
+ printf("%s: successed\n", __func__);
}
+
static void test_secure(void)
{
int ret;
@@ -65,23 +112,23 @@ static void test_secure(void)
ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, NULL, &fd);
if (ret || (fd == -1)) {
- printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
+ printf("%s: smaf_create_buffer() failed %d\n", __func__, ret);
return;
}
ret = smaf_set_secure(fd, 1);
if (ret) {
- printf("%s smaf_set_secure() failed %d\n", __func__, ret);
+ printf("%s: smaf_set_secure() failed %d\n", __func__, ret);
goto end;
}
ret = smaf_get_secure(fd);
if (!ret) {
- printf("%s smaf_get_secure() failed %d\n", __func__, ret);
+ printf("%s: smaf_get_secure() failed %d\n", __func__, ret);
goto end;
}
- printf("%s successed\n", __func__);
+ printf("%s: successed\n", __func__);
end:
close(fd);
}
@@ -128,18 +175,61 @@ static void test_create_non_page_aligned(void)
static void test_create_unnamed(void)
{
- int ret;
- int fd;
+ test_create_named(NULL);
+}
+
+static void test_mmap(void)
+{
+ int ret, fd;
+ char *data;
ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, NULL, &fd);
if (ret || (fd == -1)) {
- printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
+ printf("%s: smaf_create_buffer() failed %d\n", __func__, ret);
return;
}
+ data = mmap(NULL, LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (data == MAP_FAILED) {
+ printf("%s: mmap failed\n", __func__);
+ goto end;
+ }
+
+ munmap(data, LENGTH);
+ printf("%s: successed\n", __func__);
+end:
+ close(fd);
+}
+
+static void test_mmap_secure(void)
+{
+ int ret, fd;
+ char *data;
+
+ ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, NULL, &fd);
+
+ if (ret || (fd == -1)) {
+ printf("%s: smaf_create_buffer() failed %d\n", __func__, ret);
+ return;
+ }
+
+ ret = smaf_set_secure(fd, 1);
+ if (ret) {
+ printf("%s: smaf_set_secure() failed %d\n", __func__, ret);
+ goto end;
+ }
+
+ data = mmap(NULL, LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (data == MAP_FAILED) {
+ printf("%s: mmap failed\n", __func__);
+ goto end;
+ }
+
+ munmap(data, LENGTH);
+ printf("%s: successed\n", __func__);
+end:
close(fd);
- printf("%s successed\n", __func__);
}
void main (void)
@@ -150,11 +240,16 @@ void main (void)
}
test_create_unnamed();
- test_create_named();
+
+ test_iter_over_allocators();
+ test_invalid_allocator_index();
test_create_named_invalid();
test_create_non_page_aligned();
test_create_non_page_aligned_mmap();
test_secure();
+ test_mmap();
+ test_mmap_secure();
+
smaf_close();
}