]>
git.saurik.com Git - apple/xnu.git/blob - libsa/malloc.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
24 #include <mach/mach_types.h>
26 #include <kern/kern_types.h>
27 #include <kern/queue.h>
28 #include <kern/kalloc.h>
29 #include <kern/lock.h>
30 #include <kern/assert.h>
32 #include <vm/vm_kern.h>
34 #include "libsa/malloc.h"
36 extern void panic(const char *string
, ...);
38 /*********************************************************************
39 * Structure for a client memory block. Contains linked-list pointers,
40 * a size field giving the TOTAL size of the block, including this
41 * header, and the address of the client's block. The client block
42 * field is guaranteed to lie on a 16-byte boundary.
43 *********************************************************************/
44 typedef struct malloc_block
{
46 struct malloc_block
*malFwd
;
47 struct malloc_block
*malBwd
;
52 static malloc_block malAnchor
= {&malAnchor
, &malAnchor
, 0, 0};
54 static int malInited
= 0;
55 static mutex_t
*malloc_lock
;
58 void * malloc(size_t size
) {
61 unsigned int nmem
, rmem
;
66 nsize
= size
+ sizeof(malloc_block
) + 15; /* Make sure we get enough to fit */
68 nmem
= (unsigned int)kalloc(nsize
); /* Get some */
69 if(!nmem
) { /* Got any? */
70 panic("malloc: no memory for a %08X sized request\n", nsize
);
73 rmem
= (nmem
+ 15) & -16; /* Round to 16 byte boundary */
74 amem
= (malloc_block
*)rmem
; /* Point to the block */
75 amem
->malActl
= nmem
; /* Set the actual address */
76 amem
->malSize
= nsize
; /* Size */
78 mutex_lock(malloc_lock
);
80 amem
->malFwd
= malAnchor
.malFwd
; /* Move anchor to our forward */
81 amem
->malBwd
= &malAnchor
; /* We point back to anchor */
82 malAnchor
.malFwd
->malBwd
= amem
; /* The old forward's back points to us */
83 malAnchor
.malFwd
= amem
; /* Now we point the anchor to us */
85 mutex_unlock(malloc_lock
); /* Unlock now */
87 return (void *)(rmem
+ 16); /* Return the block */
92 /*********************************************************************
95 *********************************************************************/
97 void free(void * address
) {
100 malloc_block
*amem
, *fore
, *aft
;
102 if(!(unsigned int)address
) return; /* Leave if they try to free nothing */
105 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to the header */
107 mutex_lock(malloc_lock
);
109 fore
= amem
->malFwd
; /* Get the guy in front */
110 aft
= amem
->malBwd
; /* And the guy behind */
111 fore
->malBwd
= aft
; /* The next guy's previous is now my previous */
112 aft
->malFwd
= fore
; /* The previous guy's forward is now mine */
114 mutex_unlock(malloc_lock
); /* Unlock now */
116 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
122 /*********************************************************************
125 * Allocate the mutual exclusion lock that protect malloc's data.
126 *********************************************************************/
127 __private_extern__
void
130 malloc_lock
= mutex_alloc(0);
135 /*********************************************************************
138 * Walks through the list of VM-allocated regions, destroying them
139 * all. Any subsequent access by clients to allocated data will cause
140 * a segmentation fault.
141 *********************************************************************/
143 void malloc_reset(void) {
145 malloc_block
*amem
, *bmem
;
147 mutex_lock(malloc_lock
);
149 amem
= malAnchor
.malFwd
; /* Get the first one */
151 while(amem
!= &malAnchor
) { /* Go until we hit the anchor */
153 bmem
= amem
->malFwd
; /* Next one */
154 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
155 amem
= bmem
; /* Skip to it */
159 malAnchor
.malFwd
= (struct malloc_block
*) 0x666; /* Cause a fault if we try again */
160 malAnchor
.malBwd
= (struct malloc_block
*) 0x666; /* Cause a fault if we try again */
162 mutex_unlock(malloc_lock
); /* Unlock now */
164 mutex_free(malloc_lock
);
167 } /* malloc_reset() */
170 /*********************************************************************
173 * This function simply allocates a new block and copies the existing
174 * data into it. Nothing too clever here, as cleanup and efficient
175 * memory usage are not important in this allocator package.
176 *********************************************************************/
178 void * realloc(void * address
, size_t new_client_size
) {
182 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to allocation block */
184 new_address
= malloc(new_client_size
); /* get a new one */
185 if(!new_address
) { /* Did we get it? */
186 panic("realloc: can not reallocate one of %08X size\n", new_client_size
);
189 memcpy(new_address
, address
, amem
->malSize
- sizeof(malloc_block
)); /* Copy the old in */
191 free(address
); /* Toss the old one */