]>
Commit | Line | Data |
---|---|---|
0959b6d4 A |
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- |
2 | * | |
39a8cd10 | 3 | * Copyright (c) 2004-2008 Apple Inc. All rights reserved. |
0959b6d4 A |
4 | * |
5 | * @APPLE_LICENSE_HEADER_START@ | |
6 | * | |
7 | * This file contains Original Code and/or Modifications of Original Code | |
8 | * as defined in and that are subject to the Apple Public Source License | |
9 | * Version 2.0 (the 'License'). You may not use this file except in | |
10 | * compliance with the License. Please obtain a copy of the License at | |
11 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
12 | * file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
19 | * Please see the License for the specific language governing rights and | |
20 | * limitations under the License. | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | ||
0959b6d4 | 25 | |
bac542e6 A |
26 | #include <stdlib.h> |
27 | #include <stdint.h> | |
28 | #include <string.h> | |
39a8cd10 | 29 | #include <mach/mach.h> |
2fd3f4e8 | 30 | #include <sys/mman.h> |
bac542e6 A |
31 | |
32 | extern "C" void* __dso_handle; | |
33 | ||
cf998323 | 34 | #include "dyld2.h" |
bac542e6 | 35 | #include "dyldLibSystemInterface.h" |
0959b6d4 A |
36 | |
37 | // | |
bac542e6 A |
38 | // dyld initially allocates all memory from a pool inside dyld. |
39 | // Once libSystem.dylib is initialized, dyld uses libSystem's malloc/free. | |
0959b6d4 A |
40 | // |
41 | ||
bac542e6 A |
42 | #if __LP64__ |
43 | // room for about ~1000 initial dylibs | |
19894a12 | 44 | #define DYLD_POOL_CHUNK_SIZE 200*1024 |
bac542e6 A |
45 | #else |
46 | // room for about ~900 initial dylibs | |
39a8cd10 | 47 | #define DYLD_POOL_CHUNK_SIZE 150*1024 |
bac542e6 | 48 | #endif |
39a8cd10 A |
49 | |
50 | struct dyld_static_pool { | |
51 | dyld_static_pool* previousPool; | |
52 | uint8_t* current; | |
53 | uint8_t* end; | |
bc3b7c8c A |
54 | |
55 | // libunwind, and probably others, need the pool to be 16-byte aligned as malloc guarantees that | |
56 | __attribute__((__aligned__(16))) | |
57 | uint8_t pool[1]; | |
39a8cd10 A |
58 | }; |
59 | ||
60 | // allocate initial pool independently of pool header to take less space on disk | |
61 | static uint8_t initialPoolContent[DYLD_POOL_CHUNK_SIZE] __attribute__((__aligned__(16))); | |
62 | static dyld_static_pool initialPool = { NULL, initialPoolContent, &initialPoolContent[DYLD_POOL_CHUNK_SIZE] }; | |
63 | static dyld_static_pool* currentPool = &initialPool; | |
64 | ||
0959b6d4 | 65 | |
bac542e6 | 66 | void* malloc(size_t size) |
0959b6d4 | 67 | { |
9f83892a | 68 | if ( (dyld::gLibSystemHelpers != NULL) && dyld::gProcessInfo->libSystemInitialized ) { |
bac542e6 A |
69 | void* p = dyld::gLibSystemHelpers->malloc(size); |
70 | //dyld::log("malloc(%lu) => %p from libSystem\n", size, p); | |
71 | return p; | |
72 | } | |
73 | else { | |
bc3b7c8c A |
74 | // keep allocations 16-byte aligned |
75 | size = ((size + 15) & -16); | |
76 | ||
df9d6cf7 | 77 | if ( size > DYLD_POOL_CHUNK_SIZE ) { |
6cae9b63 A |
78 | dyld::log("dyld malloc overflow: size=%lu\n", size); |
79 | dyld::halt("dyld malloc overflow\n"); | |
df9d6cf7 | 80 | } |
bac542e6 | 81 | size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align |
39a8cd10 A |
82 | uint8_t* result = currentPool->current; |
83 | currentPool->current += size; | |
84 | if ( currentPool->current > currentPool->end ) { | |
85 | vm_address_t addr = 0; | |
86 | kern_return_t r = vm_allocate(mach_task_self(), &addr, DYLD_POOL_CHUNK_SIZE, VM_FLAGS_ANYWHERE); | |
87 | if ( r != KERN_SUCCESS ) { | |
6cae9b63 | 88 | dyld::halt("out of address space for dyld memory pool\n"); |
39a8cd10 A |
89 | } |
90 | dyld_static_pool* newPool = (dyld_static_pool*)addr; | |
91 | newPool->previousPool = NULL; | |
92 | newPool->current = newPool->pool; | |
93 | newPool->end = (uint8_t*)(addr + DYLD_POOL_CHUNK_SIZE); | |
94 | newPool->previousPool = currentPool; | |
95 | currentPool = newPool; | |
96 | if ( (currentPool->current + size) > currentPool->end ) { | |
97 | dyld::log("dyld memory pool exhausted: size=%lu\n", size); | |
6cae9b63 | 98 | dyld::halt("dyld memory pool exhausted\n"); |
39a8cd10 A |
99 | } |
100 | result = currentPool->current; | |
101 | currentPool->current += size; | |
bac542e6 | 102 | } |
39a8cd10 | 103 | //dyld::log("%p = malloc(%3lu) from pool %p, free space = %lu\n", result, size, currentPool, (long)(currentPool->end - currentPool->current)); |
bac542e6 | 104 | return result; |
0959b6d4 | 105 | } |
0959b6d4 A |
106 | } |
107 | ||
bac542e6 A |
108 | |
109 | void free(void* ptr) | |
0959b6d4 | 110 | { |
bac542e6 | 111 | // ignore any pointer within dyld (i.e. stuff from pool or static strings) |
39a8cd10 A |
112 | if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &initialPoolContent[DYLD_POOL_CHUNK_SIZE])) ) { |
113 | // ignore stuff in any dynamically alloated dyld pools | |
19894a12 A |
114 | for (dyld_static_pool* p = currentPool; p != NULL; p = p->previousPool) { |
115 | if ( (p->pool <= ptr) && (ptr < p->end) ) { | |
39a8cd10 A |
116 | // do nothing, pool entries can't be reclaimed |
117 | //dyld::log("free(%p) from dynamic pool\n", ptr); | |
118 | return; | |
119 | } | |
120 | } | |
121 | ||
bac542e6 A |
122 | //dyld::log("free(%p) from libSystem\n", ptr); |
123 | return dyld::gLibSystemHelpers->free(ptr); | |
124 | } | |
125 | else { | |
126 | // do nothing, pool entries can't be reclaimed | |
39a8cd10 | 127 | //dyld::log("free(%p) from static pool\n", ptr); |
0959b6d4 | 128 | } |
0959b6d4 A |
129 | } |
130 | ||
131 | ||
bac542e6 | 132 | void* calloc(size_t count, size_t size) |
0959b6d4 | 133 | { |
8074fd5c A |
134 | // Check for overflow of integer multiplication |
135 | size_t total = count * size; | |
136 | if ( total/count != size ) { | |
137 | dyld::log("dyld calloc overflow: count=%zu, size=%zu\n", count, size); | |
138 | dyld::halt("dyld calloc overflow"); | |
139 | } | |
bac542e6 | 140 | if ( dyld::gLibSystemHelpers != NULL ) { |
8074fd5c A |
141 | void* result = dyld::gLibSystemHelpers->malloc(total); |
142 | if ( result != NULL ) | |
143 | bzero(result, total); | |
bac542e6 A |
144 | return result; |
145 | } | |
146 | else { | |
8074fd5c | 147 | // this allocates out of static buffer which is already zero filled |
df9d6cf7 | 148 | return malloc(total); |
bac542e6 | 149 | } |
0959b6d4 A |
150 | } |
151 | ||
152 | ||
bac542e6 | 153 | void* realloc(void *ptr, size_t size) |
0959b6d4 | 154 | { |
bac542e6 A |
155 | void* result = malloc(size); |
156 | memcpy(result, ptr, size); | |
157 | return result; | |
0959b6d4 A |
158 | } |
159 | ||
bac542e6 A |
160 | // void* reallocf(void *ptr, size_t size); |
161 | // void* valloc(size_t size); | |
162 | ||
163 | // needed __libc_init() | |
164 | extern "C" int _malloc_lock; | |
165 | int _malloc_lock = 0; | |
166 | ||
0959b6d4 | 167 | |
2fd3f4e8 A |
168 | // <rdar://problem/12857033> dyld calls this which uses libSystem.dylib's vm_allocate if available |
169 | int vm_alloc(vm_address_t* addr, vm_size_t size, uint32_t flags) | |
170 | { | |
171 | if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) { | |
172 | return dyld::gLibSystemHelpers->vm_alloc(mach_task_self(), addr, size, flags); | |
173 | } | |
174 | else { | |
175 | return ::vm_allocate(mach_task_self(), addr, size, flags); | |
176 | } | |
177 | } | |
178 | ||
179 | void* xmmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset) | |
180 | { | |
181 | if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) { | |
182 | return dyld::gLibSystemHelpers->mmap(addr, len, prot, flags, fd, offset); | |
183 | } | |
184 | else { | |
185 | return ::mmap(addr, len, prot, flags, fd, offset); | |
186 | } | |
187 | } | |
188 | ||
189 | ||
190 |