xref: /onnv-gate/usr/src/uts/common/zmod/zutil.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #include <sys/systm.h>
9*0Sstevel@tonic-gate #include <sys/cmn_err.h>
10*0Sstevel@tonic-gate #include <sys/kobj.h>
11*0Sstevel@tonic-gate #include <sys/kobj_impl.h>
12*0Sstevel@tonic-gate 
13*0Sstevel@tonic-gate /*
14*0Sstevel@tonic-gate  * This module is used both during the normal operation of the kernel (i.e.
15*0Sstevel@tonic-gate  * after kmem has been initialized) and during boot (before unix`_start has
16*0Sstevel@tonic-gate  * been called).  kobj_alloc is able to tell the difference between the two
17*0Sstevel@tonic-gate  * cases, and as such must be used instead of kmem_alloc.
18*0Sstevel@tonic-gate  */
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate void
21*0Sstevel@tonic-gate zmemcpy(uchar_t *dest, const uchar_t *source, uint_t len)
22*0Sstevel@tonic-gate {
23*0Sstevel@tonic-gate 	bcopy(source, dest, len);
24*0Sstevel@tonic-gate }
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate struct zchdr {
27*0Sstevel@tonic-gate 	uint_t zch_magic;
28*0Sstevel@tonic-gate 	uint_t zch_size;
29*0Sstevel@tonic-gate };
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #define	ZCH_MAGIC	0x3cc13cc1
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*ARGSUSED*/
34*0Sstevel@tonic-gate void *
35*0Sstevel@tonic-gate zcalloc(void *opaque, uint_t items, uint_t size)
36*0Sstevel@tonic-gate {
37*0Sstevel@tonic-gate 	size_t nbytes = sizeof (struct zchdr) + items * size;
38*0Sstevel@tonic-gate 	struct zchdr *z = kobj_zalloc(nbytes, KM_NOWAIT|KM_TMP);
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 	if (z == NULL)
41*0Sstevel@tonic-gate 		return (NULL);
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	z->zch_magic = ZCH_MAGIC;
44*0Sstevel@tonic-gate 	z->zch_size = nbytes;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	return (z + 1);
47*0Sstevel@tonic-gate }
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*ARGSUSED*/
50*0Sstevel@tonic-gate void
51*0Sstevel@tonic-gate zcfree(void *opaque, void *ptr)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	struct zchdr *z = ((struct zchdr *)ptr) - 1;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	if (z->zch_magic != ZCH_MAGIC)
56*0Sstevel@tonic-gate 		panic("zcfree region corrupt: hdr=%p ptr=%p", (void *)z, ptr);
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	kobj_free(z, z->zch_size);
59*0Sstevel@tonic-gate }
60