]>
git.saurik.com Git - apple/xnu.git/blob - libsa/malloc.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
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>
33 #include "libsa/malloc.h"
35 extern void panic(const char *string
, ...);
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
{
45 struct malloc_block
*malFwd
;
46 struct malloc_block
*malBwd
;
51 static malloc_block malAnchor
= {&malAnchor
, &malAnchor
, 0, 0};
53 static int malInited
= 0;
54 static mutex_t
*malloc_lock
;
57 void * malloc(size_t size
) {
60 unsigned int nmem
, rmem
;
65 nsize
= size
+ sizeof(malloc_block
) + 15; /* Make sure we get enough to fit */
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
);
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 */
77 mutex_lock(malloc_lock
);
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 */
84 mutex_unlock(malloc_lock
); /* Unlock now */
86 return (void *)(rmem
+ 16); /* Return the block */
91 /*********************************************************************
94 *********************************************************************/
96 void free(void * address
) {
99 malloc_block
*amem
, *fore
, *aft
;
101 if(!(unsigned int)address
) return; /* Leave if they try to free nothing */
104 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to the header */
106 mutex_lock(malloc_lock
);
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 */
113 mutex_unlock(malloc_lock
); /* Unlock now */
115 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
121 /*********************************************************************
124 * Allocate the mutual exclusion lock that protect malloc's data.
125 *********************************************************************/
126 __private_extern__
void
129 malloc_lock
= mutex_alloc(ETAP_IO_AHA
);
134 /*********************************************************************
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 *********************************************************************/
142 void malloc_reset(void) {
144 malloc_block
*amem
, *bmem
;
146 mutex_lock(malloc_lock
);
148 amem
= malAnchor
.malFwd
; /* Get the first one */
150 while(amem
!= &malAnchor
) { /* Go until we hit the anchor */
152 bmem
= amem
->malFwd
; /* Next one */
153 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
154 amem
= bmem
; /* Skip to it */
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 */
161 mutex_unlock(malloc_lock
); /* Unlock now */
163 mutex_free(malloc_lock
);
166 } /* malloc_reset() */
169 /*********************************************************************
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 *********************************************************************/
177 void * realloc(void * address
, size_t new_client_size
) {
181 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to allocation block */
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
);
188 memcpy(new_address
, address
, amem
->malSize
- sizeof(malloc_block
)); /* Copy the old in */
190 free(address
); /* Toss the old one */