[PATCH 1/2] utils: add xmemdup() & xstrdup()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add small helpers for copying a memory buffer or a string
into a newly allocated buffer.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 Documentation/api.rst |  1 +
 Makefile              |  1 +
 lib.h                 |  1 +
 utils.c               | 17 +++++++++++++++++
 utils.h               | 25 +++++++++++++++++++++++++
 5 files changed, 45 insertions(+)
 create mode 100644 utils.c
 create mode 100644 utils.h

diff --git a/Documentation/api.rst b/Documentation/api.rst
index d1a1d3ca4..1270551cb 100644
--- a/Documentation/api.rst
+++ b/Documentation/api.rst
@@ -9,6 +9,7 @@ Utilities
 ~~~~~~~~~
 
 .. c:autodoc:: ptrlist.c
+.. c:autodoc:: utils.h
 
 Parsing
 ~~~~~~~
diff --git a/Makefile b/Makefile
index c89f2c85e..0b6c32cf5 100644
--- a/Makefile
+++ b/Makefile
@@ -61,6 +61,7 @@ LIB_OBJS += symbol.o
 LIB_OBJS += target.o
 LIB_OBJS += tokenize.o
 LIB_OBJS += unssa.o
+LIB_OBJS += utils.o
 
 PROGRAMS :=
 PROGRAMS += compile
diff --git a/lib.h b/lib.h
index 4d613936e..0e5f3e4a8 100644
--- a/lib.h
+++ b/lib.h
@@ -33,6 +33,7 @@
 
 #include "compat.h"
 #include "ptrlist.h"
+#include "utils.h"
 
 #define DO_STRINGIFY(x) #x
 #define STRINGIFY(x) DO_STRINGIFY(x)
diff --git a/utils.c b/utils.c
new file mode 100644
index 000000000..4945e1ca1
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: MIT
+// Copyright (C) 2018 Luc Van Oostenryck
+
+#include "utils.h"
+#include "allocate.h"
+#include <string.h>
+
+
+void *xmemdup(const void *src, size_t len)
+{
+	return memcpy(__alloc_bytes(len), src, len);
+}
+
+char *xstrdup(const char *src)
+{
+	return xmemdup(src, strlen(src) + 1);
+}
diff --git a/utils.h b/utils.h
new file mode 100644
index 000000000..38749be29
--- /dev/null
+++ b/utils.h
@@ -0,0 +1,25 @@
+#ifndef UTILS_H
+#define UTILS_H
+
+///
+// Miscellaneous utilities
+// -----------------------
+
+#include <stddef.h>
+
+///
+// duplicate a memory buffer in a newly allocated buffer.
+// @src: a pointer to the memory buffer to be duplicated
+// @len: the size of the memory buffer to be duplicated
+// @return: a pointer to a copy of @src allocated via
+//	:func:`__alloc_bytes()`.
+void *xmemdup(const void *src, size_t len);
+
+///
+// duplicate a null-terminated string in a newly allocated buffer.
+// @src: a pointer to string to be duplicated
+// @return: a pointer to a copy of @str allocated via
+//	:func:`__alloc_bytes()`.
+char *xstrdup(const char *src);
+
+#endif
-- 
2.17.1

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux