]> git.saurik.com Git - apple/xnu.git/blame - libsa/malloc.c
xnu-517.7.7.tar.gz
[apple/xnu.git] / libsa / malloc.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
1c79356b
A
22#include <string.h>
23
55e303ae
A
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>
de355530 29
55e303ae 30#include "libsa/malloc.h"
1c79356b 31
55e303ae 32extern void panic(const char *string, ...);
1c79356b
A
33
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*********************************************************************/
40typedef struct malloc_block {
1c79356b 41
55e303ae
A
42 struct malloc_block *malFwd;
43 struct malloc_block *malBwd;
44 unsigned int malSize;
45 unsigned int malActl;
46} malloc_block;
de355530 47
55e303ae 48static malloc_block malAnchor = {&malAnchor, &malAnchor, 0, 0};
de355530 49
55e303ae
A
50static int malInited = 0;
51static mutex_t *malloc_lock;
de355530 52
1c79356b
A
53__private_extern__
54void * malloc(size_t size) {
de355530 55
55e303ae
A
56 unsigned int nsize;
57 unsigned int nmem, rmem;
58 malloc_block *amem;
59
60 assert(malInited);
61
62 nsize = size + sizeof(malloc_block) + 15; /* Make sure we get enough to fit */
63
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);
67 }
68
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 */
73
74 mutex_lock(malloc_lock);
75
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 */
80
81 mutex_unlock(malloc_lock); /* Unlock now */
82
83 return (void *)(rmem + 16); /* Return the block */
1c79356b
A
84
85} /* malloc() */
86
87
88/*********************************************************************
89* free()
90*
1c79356b
A
91*********************************************************************/
92__private_extern__
93void free(void * address) {
1c79356b 94
1c79356b 95
55e303ae
A
96 malloc_block *amem, *fore, *aft;
97
98 if(!(unsigned int)address) return; /* Leave if they try to free nothing */
99
100
101 amem = (malloc_block *)((unsigned int)address - sizeof(malloc_block)); /* Point to the header */
1c79356b 102
55e303ae 103 mutex_lock(malloc_lock);
1c79356b 104
55e303ae
A
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 */
de355530 109
55e303ae
A
110 mutex_unlock(malloc_lock); /* Unlock now */
111
112 kfree(amem->malActl, amem->malSize); /* Toss it */
de355530 113
55e303ae 114 return;
de355530
A
115
116} /* free() */
d7e50217 117
55e303ae
A
118/*********************************************************************
119* malloc_reset()
120*
121* Allocate the mutual exclusion lock that protect malloc's data.
122*********************************************************************/
123__private_extern__ void
124malloc_init(void)
125{
126 malloc_lock = mutex_alloc(ETAP_IO_AHA);
127 malInited = 1;
128}
129
1c79356b
A
130
131/*********************************************************************
132* malloc_reset()
133*
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*********************************************************************/
138__private_extern__
139void malloc_reset(void) {
55e303ae
A
140
141 malloc_block *amem, *bmem;
142
143 mutex_lock(malloc_lock);
144
145 amem = malAnchor.malFwd; /* Get the first one */
146
147 while(amem != &malAnchor) { /* Go until we hit the anchor */
148
149 bmem = amem->malFwd; /* Next one */
150 kfree(amem->malActl, amem->malSize); /* Toss it */
151 amem = bmem; /* Skip to it */
152
153 }
154
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 */
157
158 mutex_unlock(malloc_lock); /* Unlock now */
159
160 mutex_free(malloc_lock);
1c79356b
A
161 return;
162
163} /* malloc_reset() */
164
165
166/*********************************************************************
167* realloc()
168*
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*********************************************************************/
173__private_extern__
174void * realloc(void * address, size_t new_client_size) {
1c79356b 175 void * new_address;
55e303ae
A
176 malloc_block *amem;
177
178 amem = (malloc_block *)((unsigned int)address - sizeof(malloc_block)); /* Point to allocation block */
179
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);
183 }
184
185 memcpy(new_address, address, amem->malSize - sizeof(malloc_block)); /* Copy the old in */
186
187 free(address); /* Toss the old one */
188
189 return new_address;
1c79356b
A
190
191} /* realloc() */
192
193