Sun Nov 20 22:58:31 2011 UTC ()
simplify, no need for va_copy here. Add KASSERT.


(christos)
diff -r1.37 -r1.38 src/sys/kern/subr_kmem.c

cvs diff -r1.37 -r1.38 src/sys/kern/subr_kmem.c (expand / switch to unified diff)

--- src/sys/kern/subr_kmem.c 2011/11/20 21:27:26 1.37
+++ src/sys/kern/subr_kmem.c 2011/11/20 22:58:31 1.38
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: subr_kmem.c,v 1.37 2011/11/20 21:27:26 apb Exp $ */ 1/* $NetBSD: subr_kmem.c,v 1.38 2011/11/20 22:58:31 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -53,27 +53,27 @@ @@ -53,27 +53,27 @@
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE. 55 * SUCH DAMAGE.
56 */ 56 */
57 57
58/* 58/*
59 * allocator of kernel wired memory. 59 * allocator of kernel wired memory.
60 * 60 *
61 * TODO: 61 * TODO:
62 * - worth to have "intrsafe" version? maybe.. 62 * - worth to have "intrsafe" version? maybe..
63 */ 63 */
64 64
65#include <sys/cdefs.h> 65#include <sys/cdefs.h>
66__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.37 2011/11/20 21:27:26 apb Exp $"); 66__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.38 2011/11/20 22:58:31 christos Exp $");
67 67
68#include <sys/param.h> 68#include <sys/param.h>
69#include <sys/callback.h> 69#include <sys/callback.h>
70#include <sys/kmem.h> 70#include <sys/kmem.h>
71#include <sys/vmem.h> 71#include <sys/vmem.h>
72#include <sys/debug.h> 72#include <sys/debug.h>
73#include <sys/lockdebug.h> 73#include <sys/lockdebug.h>
74#include <sys/cpu.h> 74#include <sys/cpu.h>
75 75
76#include <uvm/uvm_extern.h> 76#include <uvm/uvm_extern.h>
77#include <uvm/uvm_map.h> 77#include <uvm/uvm_map.h>
78#include <uvm/uvm_kmguard.h> 78#include <uvm/uvm_kmguard.h>
79 79
@@ -462,33 +462,31 @@ kmem_size_check(const void *p, size_t sz @@ -462,33 +462,31 @@ kmem_size_check(const void *p, size_t sz
462 if (psz != sz) { 462 if (psz != sz) {
463 panic("kmem_free(%p, %zu) != allocated size %zu", 463 panic("kmem_free(%p, %zu) != allocated size %zu",
464 (const uint8_t *)p + SIZE_SIZE, sz - SIZE_SIZE, psz); 464 (const uint8_t *)p + SIZE_SIZE, sz - SIZE_SIZE, psz);
465 } 465 }
466} 466}
467#endif /* defined(KMEM_SIZE) */ 467#endif /* defined(KMEM_SIZE) */
468 468
469/* 469/*
470 * Used to dynamically allocate string with kmem accordingly to format. 470 * Used to dynamically allocate string with kmem accordingly to format.
471 */ 471 */
472char * 472char *
473kmem_asprintf(const char *fmt, ...) 473kmem_asprintf(const char *fmt, ...)
474{ 474{
475 int size, str_len; 475 int size, len;
476 va_list va, va2; 476 va_list va;
477 char *str; 477 char *str;
478 char buf[1]; 
479  478
480 va_start(va, fmt); 479 va_start(va, fmt);
481 va_copy(va2, va); 480 len = vsnprintf(NULL, 0, fmt, va);
482 str_len = vsnprintf(buf, sizeof(buf), fmt, va) + 1; 
483 va_end(va); 481 va_end(va);
484 482
485 str = kmem_alloc(str_len, KM_SLEEP); 483 str = kmem_alloc(len + 1, KM_SLEEP);
486 484
487 if ((size = vsnprintf(str, str_len, fmt, va2)) == -1) { 485 va_start(va, fmt);
488 kmem_free(str, str_len); 486 size = vsnprintf(str, len + 1, fmt, va);
489 return NULL; 487 va_end(va);
490 } 488
491 va_end(va2); 489 KASSERT(size == len);
492 490
493 return str; 491 return str;
494} 492}