Add two utility functions to help use kmem with strings: kmem_strdupsize,

kmem_strfree.
This commit is contained in:
christos 2017-11-07 18:35:57 +00:00
parent d6735baf16
commit 3afe107bee
5 changed files with 39 additions and 19 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_event.c,v 1.96 2017/10/25 08:12:39 maya Exp $ */
/* $NetBSD: kern_event.c,v 1.97 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@ -58,7 +58,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.96 2017/10/25 08:12:39 maya Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.97 2017/11/07 18:35:57 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -370,9 +370,7 @@ kfilter_register(const char *name, const struct filterops *filtops,
/* Adding new slot */
kfilter = &user_kfilters[user_kfilterc++];
reuse:
kfilter->namelen = strlen(name) + 1;
kfilter->name = kmem_alloc(kfilter->namelen, KM_SLEEP);
memcpy(__UNCONST(kfilter->name), name, kfilter->namelen);
kfilter->name = kmem_strdupsize(name, &kfilter->namelen, KM_SLEEP);
kfilter->filter = (kfilter - user_kfilters) + EVFILT_SYSCOUNT;

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_veriexec.c,v 1.17 2017/09/13 22:24:46 sevan Exp $ */
/* $NetBSD: kern_veriexec.c,v 1.18 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c) 2005, 2006 Elad Efrat <elad@NetBSD.org>
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_veriexec.c,v 1.17 2017/09/13 22:24:46 sevan Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_veriexec.c,v 1.18 2017/11/07 18:35:57 christos Exp $");
#include "opt_veriexec.h"
@ -1104,9 +1104,8 @@ veriexec_file_add(struct lwp *l, prop_dictionary_t dict)
vfe->status = FINGERPRINT_NOTEVAL;
if (prop_bool_true(prop_dictionary_get(dict, "keep-filename"))) {
vfe->filename_len = strlen(file) + 1;
vfe->filename = kmem_alloc(vfe->filename_len, KM_SLEEP);
strlcpy(vfe->filename, file, vfe->filename_len);
vfe->filename = kmem_strdupsize(file, &vfe->filename_len,
KM_SLEEP);
} else
vfe->filename = NULL;

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr_devsw.c,v 1.37 2017/04/25 08:46:38 pgoyette Exp $ */
/* $NetBSD: subr_devsw.c,v 1.38 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@ -69,7 +69,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.37 2017/04/25 08:46:38 pgoyette Exp $");
__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.38 2017/11/07 18:35:57 christos Exp $");
#ifdef _KERNEL_OPT
#include "opt_dtrace.h"
@ -128,7 +128,6 @@ devsw_attach(const char *devname,
struct devsw_conv *conv;
char *name;
int error, i;
size_t len;
if (devname == NULL || cdev == NULL)
return (EINVAL);
@ -204,14 +203,12 @@ devsw_attach(const char *devname,
max_devsw_convs = new_convs;
}
len = strlen(devname) + 1;
name = kmem_alloc(len, KM_NOSLEEP);
name = kmem_strdupsize(devname, NULL, KM_NOSLEEP);
if (name == NULL) {
devsw_detach_locked(bdev, cdev);
error = ENOMEM;
goto fail;
}
strlcpy(name, devname, len);
devsw_conv[i].d_name = name;
devsw_conv[i].d_bmajor = *bmajor;

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr_kmem.c,v 1.63 2017/04/12 20:05:54 christos Exp $ */
/* $NetBSD: subr_kmem.c,v 1.64 2017/11/07 18:35:57 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.63 2017/04/12 20:05:54 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.64 2017/11/07 18:35:57 christos Exp $");
#ifdef _KERNEL_OPT
#include "opt_kmem.h"
@ -527,6 +527,29 @@ kmem_asprintf(const char *fmt, ...)
return str;
}
char *
kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
{
size_t len = strlen(str) + 1;
char *ptr = kmem_alloc(len, flags);
if (ptr == NULL)
return NULL;
if (lenp)
*lenp = len;
memcpy(ptr, str, len);
return ptr;
}
void
kmem_strfree(char *str)
{
if (str == NULL)
return;
kmem_free(str, strlen(str) + 1);
}
/* ------------------ DEBUG / DIAGNOSTIC ------------------ */
#if defined(KMEM_POISON) || defined(KMEM_REDZONE)

View File

@ -1,4 +1,4 @@
/* $NetBSD: kmem.h,v 1.9 2012/02/05 03:40:08 rmind Exp $ */
/* $NetBSD: kmem.h,v 1.10 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c)2006 YAMAMOTO Takashi,
@ -46,6 +46,9 @@ void kmem_intr_free(void *, size_t);
char * kmem_asprintf(const char *, ...) __printflike(1, 2);
char * kmem_strdupsize(const char *, size_t *, km_flag_t);
void kmem_strfree(char *);
/*
* km_flag_t values:
*/