]> git.saurik.com Git - apple/dyld.git/blob - src/dyldNew.cpp
4a5dcad48f7bcbeaefc5a5e2c816d66ccfa5a79b
[apple/dyld.git] / src / dyldNew.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2007 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
30 extern "C" void* __dso_handle;
31
32 #include "dyld.h"
33 #include "dyldLibSystemInterface.h"
34
35 //
36 // dyld initially allocates all memory from a pool inside dyld.
37 // Once libSystem.dylib is initialized, dyld uses libSystem's malloc/free.
38 //
39
40 #if __LP64__
41 // room for about ~1000 initial dylibs
42 #define DYLD_INITIAL_POOL_SIZE 400*1024
43 #else
44 // room for about ~900 initial dylibs
45 #define DYLD_INITIAL_POOL_SIZE 200*1024
46 #endif
47 static uint8_t dyldPool[DYLD_INITIAL_POOL_SIZE];
48 static uint8_t* curPoolPtr = dyldPool;
49
50 void* malloc(size_t size)
51 {
52 if ( dyld::gLibSystemHelpers != NULL) {
53 void* p = dyld::gLibSystemHelpers->malloc(size);
54 //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
55 return p;
56 }
57 else {
58 size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
59 uint8_t* result = curPoolPtr;
60 if ( (curPoolPtr + size) > &dyldPool[DYLD_INITIAL_POOL_SIZE] ) {
61 dyld::log("initial dyld memory pool exhausted\n");
62 _exit(1);
63 }
64 curPoolPtr += size;
65 //dyld::log("%p = malloc(%lu) from pool, total = %d\n", result, size, curPoolPtr-dyldPool);
66 return result;
67 }
68 }
69
70
71 void free(void* ptr)
72 {
73 // ignore any pointer within dyld (i.e. stuff from pool or static strings)
74 if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &dyldPool[DYLD_INITIAL_POOL_SIZE])) ) {
75 //dyld::log("free(%p) from libSystem\n", ptr);
76 return dyld::gLibSystemHelpers->free(ptr);
77 }
78 else {
79 // do nothing, pool entries can't be reclaimed
80 //dyld::log("free(%p) from pool\n", ptr);
81 }
82 }
83
84
85 void* calloc(size_t count, size_t size)
86 {
87 if ( dyld::gLibSystemHelpers != NULL ) {
88 void* result = dyld::gLibSystemHelpers->malloc(size);
89 bzero(result, size);
90 return result;
91 }
92 else {
93 return malloc(count*size);
94 }
95 }
96
97
98 void* realloc(void *ptr, size_t size)
99 {
100 void* result = malloc(size);
101 memcpy(result, ptr, size);
102 return result;
103 }
104
105 // void* reallocf(void *ptr, size_t size);
106 // void* valloc(size_t size);
107
108 // needed __libc_init()
109 extern "C" int _malloc_lock;
110 int _malloc_lock = 0;
111
112