]> git.saurik.com Git - apple/xnu.git/blob - libsa/malloc.c
36b66579dafd6f1fc357dc85cf8e87fe8d96be4c
[apple/xnu.git] / libsa / malloc.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <string.h>
24
25 #include <mach/mach_types.h>
26
27 #include <kern/kern_types.h>
28 #include <kern/queue.h>
29 #include <kern/kalloc.h>
30 #include <kern/lock.h>
31 #include <kern/assert.h>
32
33 #include <vm/vm_kern.h>
34
35 #include "libsa/malloc.h"
36
37 extern void panic(const char *string, ...);
38
39 /*********************************************************************
40 * Structure for a client memory block. Contains linked-list pointers,
41 * a size field giving the TOTAL size of the block, including this
42 * header, and the address of the client's block. The client block
43 * field is guaranteed to lie on a 16-byte boundary.
44 *********************************************************************/
45 typedef struct malloc_block {
46
47 struct malloc_block *malFwd;
48 struct malloc_block *malBwd;
49 void *malActl;
50 unsigned int malSize;
51 } malloc_block;
52
53 static malloc_block malAnchor = {&malAnchor, &malAnchor, 0, 0};
54
55 static int malInited = 0;
56 static mutex_t *malloc_lock;
57
58 __private_extern__
59 void * malloc(size_t size) {
60
61 unsigned int nsize;
62 unsigned int nmem, rmem;
63 malloc_block *amem;
64
65 assert(malInited);
66
67 nsize = size + sizeof(malloc_block) + 15; /* Make sure we get enough to fit */
68
69 nmem = (unsigned int)kalloc(nsize); /* Get some */
70 if(!nmem) { /* Got any? */
71 panic("malloc: no memory for a %08X sized request\n", nsize);
72 }
73
74 rmem = (nmem + 15) & -16; /* Round to 16 byte boundary */
75 amem = (malloc_block *)rmem; /* Point to the block */
76 amem->malActl = nmem; /* Set the actual address */
77 amem->malSize = nsize; /* Size */
78
79 mutex_lock(malloc_lock);
80
81 amem->malFwd = malAnchor.malFwd; /* Move anchor to our forward */
82 amem->malBwd = &malAnchor; /* We point back to anchor */
83 malAnchor.malFwd->malBwd = amem; /* The old forward's back points to us */
84 malAnchor.malFwd = amem; /* Now we point the anchor to us */
85
86 mutex_unlock(malloc_lock); /* Unlock now */
87
88 return (void *)(rmem + 16); /* Return the block */
89
90 } /* malloc() */
91
92
93 /*********************************************************************
94 * free()
95 *
96 *********************************************************************/
97 __private_extern__
98 void free(void * address) {
99
100
101 malloc_block *amem, *fore, *aft;
102
103 if(!(unsigned int)address) return; /* Leave if they try to free nothing */
104
105
106 amem = (malloc_block *)((unsigned int)address - sizeof(malloc_block)); /* Point to the header */
107
108 mutex_lock(malloc_lock);
109
110 fore = amem->malFwd; /* Get the guy in front */
111 aft = amem->malBwd; /* And the guy behind */
112 fore->malBwd = aft; /* The next guy's previous is now my previous */
113 aft->malFwd = fore; /* The previous guy's forward is now mine */
114
115 mutex_unlock(malloc_lock); /* Unlock now */
116
117 kfree(amem->malActl, amem->malSize); /* Toss it */
118
119 return;
120
121 } /* free() */
122
123 /*********************************************************************
124 * malloc_reset()
125 *
126 * Allocate the mutual exclusion lock that protect malloc's data.
127 *********************************************************************/
128 __private_extern__ void
129 malloc_init(void)
130 {
131 malloc_lock = mutex_alloc(0);
132 malInited = 1;
133 }
134
135
136 /*********************************************************************
137 * malloc_reset()
138 *
139 * Walks through the list of VM-allocated regions, destroying them
140 * all. Any subsequent access by clients to allocated data will cause
141 * a segmentation fault.
142 *********************************************************************/
143 __private_extern__
144 void malloc_reset(void) {
145
146 malloc_block *amem, *bmem;
147
148 mutex_lock(malloc_lock);
149
150 amem = malAnchor.malFwd; /* Get the first one */
151
152 while(amem != &malAnchor) { /* Go until we hit the anchor */
153
154 bmem = amem->malFwd; /* Next one */
155 kfree(amem->malActl, amem->malSize); /* Toss it */
156 amem = bmem; /* Skip to it */
157
158 }
159
160 malAnchor.malFwd = (struct malloc_block *) 0x666; /* Cause a fault if we try again */
161 malAnchor.malBwd = (struct malloc_block *) 0x666; /* Cause a fault if we try again */
162
163 mutex_unlock(malloc_lock); /* Unlock now */
164
165 mutex_free(malloc_lock);
166 return;
167
168 } /* malloc_reset() */
169
170
171 /*********************************************************************
172 * realloc()
173 *
174 * This function simply allocates a new block and copies the existing
175 * data into it. Nothing too clever here, as cleanup and efficient
176 * memory usage are not important in this allocator package.
177 *********************************************************************/
178 __private_extern__
179 void * realloc(void * address, size_t new_client_size) {
180 void * new_address;
181 malloc_block *amem;
182
183 amem = (malloc_block *)((unsigned int)address - sizeof(malloc_block)); /* Point to allocation block */
184
185 new_address = malloc(new_client_size); /* get a new one */
186 if(!new_address) { /* Did we get it? */
187 panic("realloc: can not reallocate one of %08X size\n", new_client_size);
188 }
189
190 memcpy(new_address, address, amem->malSize - sizeof(malloc_block)); /* Copy the old in */
191
192 free(address); /* Toss the old one */
193
194 return new_address;
195
196 } /* realloc() */
197
198