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