Source-Changes-HG archive

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

[src/trunk]: src/sys add strndup and an alias to strdup.



details:   https://anonhg.NetBSD.org/src/rev/21684432297c
branches:  trunk
changeset: 828881:21684432297c
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Jan 09 01:53:55 2018 +0000

description:
add strndup and an alias to strdup.

diffstat:

 sys/kern/subr_kmem.c |  21 +++++++++++++++++++--
 sys/sys/kmem.h       |   4 +++-
 2 files changed, 22 insertions(+), 3 deletions(-)

diffs (60 lines):

diff -r 1450095c8a77 -r 21684432297c sys/kern/subr_kmem.c
--- a/sys/kern/subr_kmem.c      Tue Jan 09 01:51:20 2018 +0000
+++ b/sys/kern/subr_kmem.c      Tue Jan 09 01:53:55 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_kmem.c,v 1.65 2017/11/09 23:20:12 riastradh Exp $ */
+/*     $NetBSD: subr_kmem.c,v 1.66 2018/01/09 01:53:55 christos Exp $  */
 
 /*-
  * Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
@@ -100,7 +100,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.65 2017/11/09 23:20:12 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.66 2018/01/09 01:53:55 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kmem.h"
@@ -544,6 +544,23 @@
        return ptr;
 }
 
+char *
+kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
+{
+       KASSERT(str != NULL);
+       KASSERT(maxlen != 0);
+
+       size_t len = strnlen(str, maxlen);
+       char *ptr = kmem_alloc(len + 1, flags);
+       if (ptr == NULL)
+               return NULL;
+
+       memcpy(ptr, str, len);
+       ptr[len] = '\0';
+
+       return ptr;
+}
+
 void
 kmem_strfree(char *str)
 {
diff -r 1450095c8a77 -r 21684432297c sys/sys/kmem.h
--- a/sys/sys/kmem.h    Tue Jan 09 01:51:20 2018 +0000
+++ b/sys/sys/kmem.h    Tue Jan 09 01:53:55 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kmem.h,v 1.10 2017/11/07 18:35:57 christos Exp $       */
+/*     $NetBSD: kmem.h,v 1.11 2018/01/09 01:53:55 christos Exp $       */
 
 /*-
  * Copyright (c)2006 YAMAMOTO Takashi,
@@ -47,6 +47,8 @@
 char * kmem_asprintf(const char *, ...) __printflike(1, 2);
 
 char * kmem_strdupsize(const char *, size_t *, km_flag_t);
+#define kmem_strdup(s, f)      kmem_strdupsize((s), NULL, (f))
+char * kmem_strndup(const char *, size_t, km_flag_t);
 void   kmem_strfree(char *);
 
 /*



Home | Main Index | Thread Index | Old Index