xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_alloc.c (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <umem.h>
32 #include <poll.h>
33 #include <errno.h>
34 
35 #include <fmd_alloc.h>
36 #include <fmd_subr.h>
37 #include <fmd_module.h>
38 #include <fmd_scheme.h>
39 #include <fmd.h>
40 
41 void *
42 fmd_alloc(size_t size, int flags)
43 {
44 	void *data = umem_alloc(size, UMEM_DEFAULT);
45 	uint_t try, lim, msecs;
46 
47 	if (data != NULL || size == 0 || !(flags & FMD_SLEEP))
48 		return (data); /* in common cases just return result */
49 
50 	lim = fmd.d_alloc_tries;
51 	msecs = fmd.d_alloc_msecs;
52 
53 	for (try = 0; data == NULL && try < lim; try++) {
54 		(void) poll(NULL, 0, msecs);
55 		msecs *= 10;
56 		data = umem_alloc(size, UMEM_DEFAULT);
57 	}
58 
59 	if (data == NULL) {
60 		fmd_modhash_tryapply(fmd.d_mod_hash, fmd_module_trygc);
61 		fmd_scheme_hash_trygc(fmd.d_schemes);
62 		data = umem_alloc(size, UMEM_DEFAULT);
63 	}
64 
65 	if (data == NULL)
66 		fmd_panic("insufficient memory (%u bytes needed)\n", size);
67 
68 	return (data);
69 }
70 
71 void *
72 fmd_zalloc(size_t size, int flags)
73 {
74 	void *data = fmd_alloc(size, flags);
75 
76 	if (data != NULL)
77 		bzero(data, size);
78 
79 	return (data);
80 }
81 
82 void
83 fmd_free(void *data, size_t size)
84 {
85 	umem_free(data, size);
86 }
87