]>
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 * 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 <kern/queue.h>
25 #include <kern/kalloc.h>
26 #include <kern/lock.h>
27 #include <kern/assert.h>
28 #include <vm/vm_kern.h>
30 #include "libsa/malloc.h"
32 extern void panic(const char *string
, ...);
34 /*********************************************************************
35 * Structure for a client memory block. Contains linked-list pointers,
36 * a size field giving the TOTAL size of the block, including this
37 * header, and the address of the client's block. The client block
38 * field is guaranteed to lie on a 16-byte boundary.
39 *********************************************************************/
40 typedef struct malloc_block
{
42 struct malloc_block
*malFwd
;
43 struct malloc_block
*malBwd
;
48 static malloc_block malAnchor
= {&malAnchor
, &malAnchor
, 0, 0};
50 static int malInited
= 0;
51 static mutex_t
*malloc_lock
;
54 void * malloc(size_t size
) {
57 unsigned int nmem
, rmem
;
62 nsize
= size
+ sizeof(malloc_block
) + 15; /* Make sure we get enough to fit */
64 nmem
= (unsigned int)kalloc(nsize
); /* Get some */
65 if(!nmem
) { /* Got any? */
66 panic("malloc: no memory for a %08X sized request\n", nsize
);
69 rmem
= (nmem
+ 15) & -16; /* Round to 16 byte boundary */
70 amem
= (malloc_block
*)rmem
; /* Point to the block */
71 amem
->malActl
= (unsigned int)nmem
; /* Set the actual address */
72 amem
->malSize
= nsize
; /* Size */
74 mutex_lock(malloc_lock
);
76 amem
->malFwd
= malAnchor
.malFwd
; /* Move anchor to our forward */
77 amem
->malBwd
= &malAnchor
; /* We point back to anchor */
78 malAnchor
.malFwd
->malBwd
= amem
; /* The old forward's back points to us */
79 malAnchor
.malFwd
= amem
; /* Now we point the anchor to us */
81 mutex_unlock(malloc_lock
); /* Unlock now */
83 return (void *)(rmem
+ 16); /* Return the block */
88 /*********************************************************************
91 *********************************************************************/
93 void free(void * address
) {
96 malloc_block
*amem
, *fore
, *aft
;
98 if(!(unsigned int)address
) return; /* Leave if they try to free nothing */
101 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to the header */
103 mutex_lock(malloc_lock
);
105 fore
= amem
->malFwd
; /* Get the guy in front */
106 aft
= amem
->malBwd
; /* And the guy behind */
107 fore
->malBwd
= aft
; /* The next guy's previous is now my previous */
108 aft
->malFwd
= fore
; /* The previous guy's forward is now mine */
110 mutex_unlock(malloc_lock
); /* Unlock now */
112 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
118 /*********************************************************************
121 * Allocate the mutual exclusion lock that protect malloc's data.
122 *********************************************************************/
123 __private_extern__
void
126 malloc_lock
= mutex_alloc(ETAP_IO_AHA
);
131 /*********************************************************************
134 * Walks through the list of VM-allocated regions, destroying them
135 * all. Any subsequent access by clients to allocated data will cause
136 * a segmentation fault.
137 *********************************************************************/
139 void malloc_reset(void) {
141 malloc_block
*amem
, *bmem
;
143 mutex_lock(malloc_lock
);
145 amem
= malAnchor
.malFwd
; /* Get the first one */
147 while(amem
!= &malAnchor
) { /* Go until we hit the anchor */
149 bmem
= amem
->malFwd
; /* Next one */
150 kfree(amem
->malActl
, amem
->malSize
); /* Toss it */
151 amem
= bmem
; /* Skip to it */
155 malAnchor
.malFwd
= (struct malloc_block
*) 0x666; /* Cause a fault if we try again */
156 malAnchor
.malBwd
= (struct malloc_block
*) 0x666; /* Cause a fault if we try again */
158 mutex_unlock(malloc_lock
); /* Unlock now */
160 mutex_free(malloc_lock
);
163 } /* malloc_reset() */
166 /*********************************************************************
169 * This function simply allocates a new block and copies the existing
170 * data into it. Nothing too clever here, as cleanup and efficient
171 * memory usage are not important in this allocator package.
172 *********************************************************************/
174 void * realloc(void * address
, size_t new_client_size
) {
178 amem
= (malloc_block
*)((unsigned int)address
- sizeof(malloc_block
)); /* Point to allocation block */
180 new_address
= malloc(new_client_size
); /* get a new one */
181 if(!new_address
) { /* Did we get it? */
182 panic("realloc: can not reallocate one of %08X size\n", new_client_size
);
185 memcpy(new_address
, address
, amem
->malSize
- sizeof(malloc_block
)); /* Copy the old in */
187 free(address
); /* Toss the old one */