]>
git.saurik.com Git - apple/xnu.git/blob - libsa/malloc.c
36b66579dafd6f1fc357dc85cf8e87fe8d96be4c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <mach/mach_types.h>
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>
33 #include <vm/vm_kern.h>
35 #include "libsa/malloc.h"
37 extern void panic(const char *string
, ...);
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
{
47 struct malloc_block
*malFwd
;
48 struct malloc_block
*malBwd
;
53 static malloc_block malAnchor
= {&malAnchor
, &malAnchor
, 0, 0};
55 static int malInited
= 0;
56 static mutex_t
*malloc_lock
;
59 void * malloc(size_t size
) {
62 unsigned int nmem
, rmem
;
67 nsize
= size
+ sizeof(malloc_block
) + 15; /* Make sure we get enough to fit */
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
);
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 */
79 mutex_lock(malloc_lock
);
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 */
86 mutex_unlock(malloc_lock
); /* Unlock now */
88 return (void *)(rmem
+ 16); /* Return the block */
93 /*********************************************************************
96 *********************************************************************/
98 void free(void * address
) {
101 malloc_block
*amem
, *fore
, *aft
;
103 if(!(unsigned int)address
) return; /* Leave if they try to free nothing */
106 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to the header */
108 mutex_lock(malloc_lock
);
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 */
115 mutex_unlock(malloc_lock
); /* Unlock now */
117 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
123 /*********************************************************************
126 * Allocate the mutual exclusion lock that protect malloc's data.
127 *********************************************************************/
128 __private_extern__
void
131 malloc_lock
= mutex_alloc(0);
136 /*********************************************************************
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 *********************************************************************/
144 void malloc_reset(void) {
146 malloc_block
*amem
, *bmem
;
148 mutex_lock(malloc_lock
);
150 amem
= malAnchor
.malFwd
; /* Get the first one */
152 while(amem
!= &malAnchor
) { /* Go until we hit the anchor */
154 bmem
= amem
->malFwd
; /* Next one */
155 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
156 amem
= bmem
; /* Skip to it */
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 */
163 mutex_unlock(malloc_lock
); /* Unlock now */
165 mutex_free(malloc_lock
);
168 } /* malloc_reset() */
171 /*********************************************************************
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 *********************************************************************/
179 void * realloc(void * address
, size_t new_client_size
) {
183 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to allocation block */
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
);
190 memcpy(new_address
, address
, amem
->malSize
- sizeof(malloc_block
)); /* Copy the old in */
192 free(address
); /* Toss the old one */