]> git.saurik.com Git - apple/dyld.git/blob - src/dyldNew.cpp
dyld-132.13.tar.gz
[apple/dyld.git] / src / dyldNew.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2008 Apple Inc. All rights reserved.
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
25
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <mach/mach.h>
30
31 extern "C" void* __dso_handle;
32
33 #include "dyld.h"
34 #include "dyldLibSystemInterface.h"
35
36 //
37 // dyld initially allocates all memory from a pool inside dyld.
38 // Once libSystem.dylib is initialized, dyld uses libSystem's malloc/free.
39 //
40
41 #if __LP64__
42 // room for about ~1000 initial dylibs
43 #define DYLD_POOL_CHUNK_SIZE 200*1024
44 #else
45 // room for about ~900 initial dylibs
46 #define DYLD_POOL_CHUNK_SIZE 150*1024
47 #endif
48
49 struct dyld_static_pool {
50 dyld_static_pool* previousPool;
51 uint8_t* current;
52 uint8_t* end;
53 uint8_t pool[1];
54 };
55
56 // allocate initial pool independently of pool header to take less space on disk
57 static uint8_t initialPoolContent[DYLD_POOL_CHUNK_SIZE] __attribute__((__aligned__(16)));
58 static dyld_static_pool initialPool = { NULL, initialPoolContent, &initialPoolContent[DYLD_POOL_CHUNK_SIZE] };
59 static dyld_static_pool* currentPool = &initialPool;
60
61
62 void* malloc(size_t size)
63 {
64 if ( dyld::gLibSystemHelpers != NULL) {
65 void* p = dyld::gLibSystemHelpers->malloc(size);
66 //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
67 return p;
68 }
69 else {
70 size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
71 uint8_t* result = currentPool->current;
72 currentPool->current += size;
73 if ( currentPool->current > currentPool->end ) {
74 vm_address_t addr = 0;
75 kern_return_t r = vm_allocate(mach_task_self(), &addr, DYLD_POOL_CHUNK_SIZE, VM_FLAGS_ANYWHERE);
76 if ( r != KERN_SUCCESS ) {
77 dyld::log("out of address space for dyld memory pool\n");
78 exit(1);
79 }
80 dyld_static_pool* newPool = (dyld_static_pool*)addr;
81 newPool->previousPool = NULL;
82 newPool->current = newPool->pool;
83 newPool->end = (uint8_t*)(addr + DYLD_POOL_CHUNK_SIZE);
84 newPool->previousPool = currentPool;
85 currentPool = newPool;
86 if ( (currentPool->current + size) > currentPool->end ) {
87 dyld::log("dyld memory pool exhausted: size=%lu\n", size);
88 exit(1);
89 }
90 result = currentPool->current;
91 currentPool->current += size;
92 }
93 //dyld::log("%p = malloc(%3lu) from pool %p, free space = %lu\n", result, size, currentPool, (long)(currentPool->end - currentPool->current));
94 return result;
95 }
96 }
97
98
99 void free(void* ptr)
100 {
101 // ignore any pointer within dyld (i.e. stuff from pool or static strings)
102 if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &initialPoolContent[DYLD_POOL_CHUNK_SIZE])) ) {
103 // ignore stuff in any dynamically alloated dyld pools
104 for (dyld_static_pool* p = initialPool.previousPool; p != NULL; p = p->previousPool) {
105 if ( (p->pool < ptr) && (ptr < p->end) ) {
106 // do nothing, pool entries can't be reclaimed
107 //dyld::log("free(%p) from dynamic pool\n", ptr);
108 return;
109 }
110 }
111
112 //dyld::log("free(%p) from libSystem\n", ptr);
113 return dyld::gLibSystemHelpers->free(ptr);
114 }
115 else {
116 // do nothing, pool entries can't be reclaimed
117 //dyld::log("free(%p) from static pool\n", ptr);
118 }
119 }
120
121
122 void* calloc(size_t count, size_t size)
123 {
124 if ( dyld::gLibSystemHelpers != NULL ) {
125 void* result = dyld::gLibSystemHelpers->malloc(size*count);
126 bzero(result, size*count);
127 return result;
128 }
129 else {
130 return malloc(count*size);
131 }
132 }
133
134
135 void* realloc(void *ptr, size_t size)
136 {
137 void* result = malloc(size);
138 memcpy(result, ptr, size);
139 return result;
140 }
141
142 // void* reallocf(void *ptr, size_t size);
143 // void* valloc(size_t size);
144
145 // needed __libc_init()
146 extern "C" int _malloc_lock;
147 int _malloc_lock = 0;
148
149