xref: /titanic_41/usr/src/stand/lib/sa/malloc.c (revision b509e89b2befbaa42939abad9da1d7f5a8c6aaae)
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 2002-2003 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 <sys/salib.h>
30 
31 /*
32  * For documentation on these functions, see malloc(3C).
33  */
34 
35 void *
36 malloc(size_t size)
37 {
38 	size_t *iaddr;
39 
40 	iaddr = (size_t *)bkmem_alloc(size + sizeof (size_t));
41 	if (iaddr == NULL) {
42 		errno = ENOMEM;
43 		return (NULL);
44 	}
45 
46 	iaddr[0] = size;
47 	return (&iaddr[1]);
48 }
49 
50 void *
51 calloc(size_t number, size_t size)
52 {
53 	void *addr;
54 
55 	addr = malloc(number * size);
56 	if (addr == NULL)
57 		return (NULL);
58 
59 	return (memset(addr, 0, number * size));
60 }
61 
62 void *
63 realloc(void *oldaddr, size_t size)
64 {
65 	void *addr;
66 	size_t oldsize;
67 
68 	addr = malloc(size);
69 	if (oldaddr != NULL) {
70 		oldsize = ((size_t *)oldaddr)[-1];
71 		if (addr != NULL) {
72 			bcopy(oldaddr, addr, (oldsize > size ? oldsize : size));
73 			free(oldaddr);
74 		}
75 	}
76 
77 	return (addr);
78 }
79 
80 void
81 free(void *addr)
82 {
83 	size_t *lenloc;
84 
85 	if (addr == NULL)
86 		return;
87 	lenloc = (size_t *)addr - 1;
88 	bkmem_free((caddr_t)lenloc, *lenloc + sizeof (size_t));
89 }
90