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