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