xref: /titanic_44/usr/src/uts/common/zmod/zutil.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
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 /*
24  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/systm.h>
31 #include <sys/cmn_err.h>
32 #include <sys/kobj.h>
33 #include <sys/kobj_impl.h>
34 
35 /*
36  * This module is used both during the normal operation of the kernel (i.e.
37  * after kmem has been initialized) and during boot (before unix`_start has
38  * been called).  kobj_alloc is able to tell the difference between the two
39  * cases, and as such must be used instead of kmem_alloc.
40  */
41 
42 void
43 zmemcpy(uchar_t *dest, const uchar_t *source, uint_t len)
44 {
45 	bcopy(source, dest, len);
46 }
47 
48 struct zchdr {
49 	uint_t zch_magic;
50 	uint_t zch_size;
51 };
52 
53 #define	ZCH_MAGIC	0x3cc13cc1
54 
55 /*ARGSUSED*/
56 void *
57 zcalloc(void *opaque, uint_t items, uint_t size)
58 {
59 	size_t nbytes = sizeof (struct zchdr) + items * size;
60 	struct zchdr *z = kobj_zalloc(nbytes, KM_NOWAIT|KM_TMP);
61 
62 	if (z == NULL)
63 		return (NULL);
64 
65 	z->zch_magic = ZCH_MAGIC;
66 	z->zch_size = nbytes;
67 
68 	return (z + 1);
69 }
70 
71 /*ARGSUSED*/
72 void
73 zcfree(void *opaque, void *ptr)
74 {
75 	struct zchdr *z = ((struct zchdr *)ptr) - 1;
76 
77 	if (z->zch_magic != ZCH_MAGIC)
78 		panic("zcfree region corrupt: hdr=%p ptr=%p", (void *)z, ptr);
79 
80 	kobj_free(z, z->zch_size);
81 }
82