xref: /freebsd/sys/contrib/openzfs/lib/libspl/kmem.c (revision 8ac904ce090b1c2e355da8aa122ca2252183f4e1)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
26  * Copyright (c) 2025, Klara, Inc.
27  */
28 
29 #include <sys/kmem.h>
30 
31 char *
kmem_vasprintf(const char * fmt,va_list adx)32 kmem_vasprintf(const char *fmt, va_list adx)
33 {
34 	char *buf = NULL;
35 	va_list adx_copy;
36 
37 	va_copy(adx_copy, adx);
38 	VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
39 	va_end(adx_copy);
40 
41 	return (buf);
42 }
43 
44 char *
kmem_asprintf(const char * fmt,...)45 kmem_asprintf(const char *fmt, ...)
46 {
47 	char *buf = NULL;
48 	va_list adx;
49 
50 	va_start(adx, fmt);
51 	VERIFY(vasprintf(&buf, fmt, adx) != -1);
52 	va_end(adx);
53 
54 	return (buf);
55 }
56 
57 /*
58  * kmem_scnprintf() will return the number of characters that it would have
59  * printed whenever it is limited by value of the size variable, rather than
60  * the number of characters that it did print. This can cause misbehavior on
61  * subsequent uses of the return value, so we define a safe version that will
62  * return the number of characters actually printed, minus the NULL format
63  * character.  Subsequent use of this by the safe string functions is safe
64  * whether it is snprintf(), strlcat() or strlcpy().
65  */
66 int
kmem_scnprintf(char * restrict str,size_t size,const char * restrict fmt,...)67 kmem_scnprintf(char *restrict str, size_t size, const char *restrict fmt, ...)
68 {
69 	int n;
70 	va_list ap;
71 
72 	/* Make the 0 case a no-op so that we do not return -1 */
73 	if (size == 0)
74 		return (0);
75 
76 	va_start(ap, fmt);
77 	n = vsnprintf(str, size, fmt, ap);
78 	va_end(ap);
79 
80 	if (n >= size)
81 		n = size - 1;
82 
83 	return (n);
84 }
85 
86 fstrans_cookie_t
spl_fstrans_mark(void)87 spl_fstrans_mark(void)
88 {
89 	return ((fstrans_cookie_t)0);
90 }
91 
92 void
spl_fstrans_unmark(fstrans_cookie_t cookie)93 spl_fstrans_unmark(fstrans_cookie_t cookie)
94 {
95 	(void) cookie;
96 }
97 
98 int
kmem_cache_reap_active(void)99 kmem_cache_reap_active(void)
100 {
101 	return (0);
102 }
103