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