aboutsummaryrefslogtreecommitdiffstats
path: root/aligned_memdup.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-09-02 15:32:56 +0200
committerMattias Andrée <maandree@kth.se>2018-09-02 15:32:56 +0200
commit47128b7def4954c4cf544924c94f018d88033374 (patch)
treec75e6c76c078a095ff691d42a1f0b2d0ea0f0357 /aligned_memdup.c
parentAdd malloc function attribute were appropriate (diff)
downloadlibsimple-47128b7def4954c4cf544924c94f018d88033374.tar.gz
libsimple-47128b7def4954c4cf544924c94f018d88033374.tar.bz2
libsimple-47128b7def4954c4cf544924c94f018d88033374.tar.xz
misc
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--aligned_memdup.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/aligned_memdup.c b/aligned_memdup.c
new file mode 100644
index 0000000..2dbba7b
--- /dev/null
+++ b/aligned_memdup.c
@@ -0,0 +1,40 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+#ifndef TEST
+
+
+void *
+libsimple_aligned_memdup(const void *s, size_t alignment, size_t n)
+{
+ void *ret = aligned_alloc(alignment, n + (alignment - n % alignment) % alignment);
+ if (!ret)
+ return NULL;
+ return memcpy(ret, s, n);
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ struct allocinfo *info;
+ const char *s = "test";
+ void *p = libsimple_aligned_memdup(s, 4, 5);
+ assert(p);
+ assert(p != s);
+ assert(!((uintptr_t)s % 4));
+ if (have_custom_malloc()) {
+ assert((info = get_allocinfo(p)));
+ assert(info->size == 8);
+ assert(info->alignment == 4);
+ }
+ assert(!strcmpnul(p, s));
+ memset(p, 0, 5);
+ assert(!strcmpnul(s, "test"));
+ free(p);
+ return 0;
+}
+
+#endif