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