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
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.
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]
23 * Copyright (c) 1991-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * zmalloc - use mmap(2) to allocate memory from /dev/zero.
31 * zfree - use munmap(2) to unmap (free) memory.
33 * These functions should be better than malloc(3) for large memory allocation.
40 #include <sys/types.h>
47 * a utility structure to keep track of the (possibly) multiple mmaps
48 * that we have done...
51 struct buffer_map
*bm_next
;
56 static void *bm_empty
= (void *) ""; /* special buffer */
57 static struct buffer_map
*bm_list
; /* NULL by default */
59 static struct buffer_map
*
60 insert_bm(char *buf
, size_t size
)
62 struct buffer_map
*bm
;
64 bm
= (struct buffer_map
*)malloc(sizeof (struct buffer_map
));
67 bm
->bm_next
= bm_list
;
78 register struct buffer_map
*p_curr
;
79 register struct buffer_map
*p_prev
;
83 while (p_curr
!= NULL
) {
84 if (p_curr
->bm_buffer
== buf
) {
86 bm_list
= p_curr
->bm_next
;
88 p_prev
->bm_next
= p_curr
->bm_next
;
89 size
= p_curr
->bm_size
;
95 p_curr
= p_curr
->bm_next
;
106 /* XXX - Special case: never allocate 0 bytes, use a special buffer */
108 return ((void *)NULL
); /* return (bm_empty); */
110 if ((fd
= open("/dev/zero", O_RDWR
)) < 0) {
112 return ((void *) NULL
);
115 mbuf
= mmap(0, size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
118 if (mbuf
== (caddr_t
)-1) {
119 perror("zmalloc: mmap");
120 return ((void *) NULL
);
123 (void) insert_bm(mbuf
, size
);
125 return ((void *) mbuf
);
133 if (mbuf
== bm_empty
)
137 if (size
= delete_bm((caddr_t
)mbuf
)) {
138 if (munmap((char *)mbuf
, size
) < 0)
139 perror("zfree: munmap");