From bbe6157afdc151a948d7bafae0eec56281716108 Mon Sep 17 00:00:00 2001
From: Willie Betschart
Date: Thu, 17 Apr 2014 10:55:35 +0200
Subject: [PATCH 1/2] New API - Validation of buffer allocation
---
driver/rapp_malloc.c | 12 +++++
include/rapp_malloc.h | 28 +++++++++-
test/Makefile.am | 1 +
test/rapp_test_malloc.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++
test/rapp_tests.def | 4 ++
5 files changed, 185 insertions(+), 1 deletion(-)
create mode 100644 test/rapp_test_malloc.c
diff --git a/driver/rapp_malloc.c b/driver/rapp_malloc.c
index a12a572..184db43 100644
--- a/driver/rapp_malloc.c
+++ b/driver/rapp_malloc.c
@@ -61,6 +61,18 @@ RAPP_API(size_t, rapp_align, (size_t size))
return rc_align(size);
}
+RAPP_API(int, rapp_validate_buffer,
+ (uint8_t *ptr, const int dim,
+ const int width, const int height))
+{
+ if (!RAPP_INITIALIZED()) {
+ RAPP_ABORT_FOR_ASSERTED_RETURNS();
+ return -1;
+ }
+
+ return RAPP_VALIDATE_U(ptr, dim, width, height) == true ? 1 : 0;
+}
+
RAPP_API(void*, rapp_malloc, (size_t size, unsigned hint))
{
if (!RAPP_INITIALIZED()) {
diff --git a/include/rapp_malloc.h b/include/rapp_malloc.h
index 283008f..bdabda8 100644
--- a/include/rapp_malloc.h
+++ b/include/rapp_malloc.h
@@ -54,7 +54,8 @@
#ifndef RAPP_MALLOC_H
#define RAPP_MALLOC_H
-#include /* size_t */
+#include /* size_t */
+#include /* bool */
#include "rapp_export.h"
#ifdef __cplusplus
@@ -90,6 +91,31 @@ extern const RAPP_EXPORT unsigned int rapp_alignment;
RAPP_EXPORT size_t
rapp_align(size_t size);
+
+/**
+ * Validate if current buffer is properly allocated
+ * due to RAPP's buffer alignment requirements.
+ *
+ * The purpose is to test buffers with unknown
+ * allocation methods.
+ *
+ * It is not necessary to test a buffer allocated with
+ * rapp_malloc, which will always give an aligned allocation.
+ *
+ * @param ptr The buffer to validate.
+ * @param dim Buffer row dimension in bytes.
+ * @param width Image width in pixels.
+ * @param height Image height in pixels.
+ * @return 1 if buffer has a proper allocation.
+ * 0 if buffer's memory alignment is not
+ * qualifying RAPP's requirement of being
+ * an aligned buffer.
+ * -1 if RAPP library is not initialized.
+ */
+RAPP_EXPORT int
+rapp_validate_buffer(uint8_t *ptr, const int dim,
+ const int width, const int height);
+
/**
* Allocate an aligned chunk of memory.
*
diff --git a/test/Makefile.am b/test/Makefile.am
index a1a7e20..f92da95 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -85,6 +85,7 @@ rapptest_SOURCES = $(testapp_src) \
rapp_tests.def \
rapp_test_util.h \
rapp_test_util.c \
+ rapp_test_malloc.c \
rapp_test_pixel.c \
rapp_test_bitblt.c \
rapp_test_pixop.c \
diff --git a/test/rapp_test_malloc.c b/test/rapp_test_malloc.c
new file mode 100644
index 0000000..821c4ea
--- /dev/null
+++ b/test/rapp_test_malloc.c
@@ -0,0 +1,141 @@
+/* Copyright (C) 2005-2014, Axis Communications AB, LUND, SWEDEN
+ *
+ * This file is part of RAPP.
+ *
+ * RAPP is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * You can use the comments under either the terms of the GNU Lesser General
+ * Public License version 3 as published by the Free Software Foundation,
+ * either version 3 of the License or (at your option) any later version, or
+ * the GNU Free Documentation License version 1.3 or any later version
+ * published by the Free Software Foundation; with no Invariant Sections, no
+ * Front-Cover Texts, and no Back-Cover Texts.
+ * A copy of the license is included in the documentation section entitled
+ * "GNU Free Documentation License".
+ *
+ * RAPP 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License and a copy of the GNU Free Documentation License along
+ * with RAPP. If not, see .
+ */
+
+/**
+ * @file rapp_test_malloc.c
+ * @brief Test allocation and alignment.
+ */
+
+#include "rapp.h" /* RAPP API */
+#include "rapp_test_util.h" /* Test utils */
+
+
+/*
+ * -------------------------------------------------------------
+ * Constants
+ * -------------------------------------------------------------
+ */
+
+/**
+ * The number of test iterations.
+ */
+#define RAPP_TEST_ITER 1024
+
+/**
+ * Test image maximum width.
+ */
+#define RAPP_TEST_WIDTH 256
+
+/**
+ * Test image maximum height.
+ */
+#define RAPP_TEST_HEIGHT 128
+
+
+/*
+ * -------------------------------------------------------------
+ * Exported functions
+ * -------------------------------------------------------------
+ */
+
+bool
+rapp_test_validate_alignment(void)
+{
+ bool result = true;
+
+ if (rapp_align(1) != rapp_alignment) {
+ result = false;
+ printf("Alignment error %d %d\n",
+ rapp_align(1), rapp_alignment);
+ goto Fail;
+ }
+
+Fail:
+ return result;
+}
+
+bool
+rapp_test_validate_buffer(void)
+{
+ bool result = true;
+ uint8_t *aligned_buf = NULL;
+ uint8_t *misaligned_buf = NULL;
+
+ int aligned_dim = rapp_align(RAPP_TEST_WIDTH);
+ int misaligned_dim = aligned_dim - 1;
+
+ aligned_buf = rapp_malloc(aligned_dim * RAPP_TEST_HEIGHT,0);
+ misaligned_buf = malloc(misaligned_dim * RAPP_TEST_HEIGHT);
+
+ if (rapp_validate_buffer(aligned_buf,
+ aligned_dim,
+ RAPP_TEST_WIDTH,
+ RAPP_TEST_HEIGHT) != 1) {
+ result = false;
+ printf("rapp_validate_alignment (aligned) error\n");
+ goto Fail;
+ }
+
+ if (rapp_validate_buffer(misaligned_buf,
+ misaligned_dim,
+ RAPP_TEST_WIDTH,
+ RAPP_TEST_HEIGHT) != 0) {
+ result = false;
+ printf("rapp_validate_alignment (misaligned) error\n");
+ goto Fail;
+ }
+
+ /* Temporary terminate library.*/
+ rapp_terminate();
+ if (rapp_validate_buffer(aligned_buf,
+ aligned_dim,
+ RAPP_TEST_WIDTH,
+ RAPP_TEST_HEIGHT) != -1) {
+ result = false;
+ printf("rapp_validate_alignment (initialization) error\n");
+ }
+
+ /* Restore library initialization. */
+ rapp_initialize();
+
+ if (result == false){
+ goto Fail;
+ }
+
+ /* Run again to verify initialization works. */
+ if (rapp_align(1) != rapp_alignment) {
+ result = false;
+ printf("Alignment error %d %d\n",
+ rapp_align(1), rapp_alignment);
+ goto Fail;
+ }
+
+Fail:
+ rapp_free(aligned_buf);
+ free(misaligned_buf);
+ return result;
+}
diff --git a/test/rapp_tests.def b/test/rapp_tests.def
index 6a71ddb..ac3742e 100644
--- a/test/rapp_tests.def
+++ b/test/rapp_tests.def
@@ -6,6 +6,10 @@ RAPP_TEST(pixel_set_bin)
#warning Pruning test-suite because of logging to avoid excessive log file size
#else
+/* Test cases for rapp memory functions */
+RAPP_TESTH(validate_alignment, "rapp_malloc - allocation and alignment functions")
+RAPP_TEST(validate_buffer)
+
/* Test cases for rapp_bitblt functions */
RAPP_TESTH(bitblt_copy_bin, "rapp_bitblt - bitblit operations")
RAPP_TEST(bitblt_not_bin)
--
1.8.3.2