]>
git.saurik.com Git - apple/dyld.git/blob - src/dyldNew.cpp
4a5dcad48f7bcbeaefc5a5e2c816d66ccfa5a79b
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
30 extern "C" void* __dso_handle
;
33 #include "dyldLibSystemInterface.h"
36 // dyld initially allocates all memory from a pool inside dyld.
37 // Once libSystem.dylib is initialized, dyld uses libSystem's malloc/free.
41 // room for about ~1000 initial dylibs
42 #define DYLD_INITIAL_POOL_SIZE 400*1024
44 // room for about ~900 initial dylibs
45 #define DYLD_INITIAL_POOL_SIZE 200*1024
47 static uint8_t dyldPool
[DYLD_INITIAL_POOL_SIZE
];
48 static uint8_t* curPoolPtr
= dyldPool
;
50 void* malloc(size_t size
)
52 if ( dyld::gLibSystemHelpers
!= NULL
) {
53 void* p
= dyld::gLibSystemHelpers
->malloc(size
);
54 //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
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");
65 //dyld::log("%p = malloc(%lu) from pool, total = %d\n", result, size, curPoolPtr-dyldPool);
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
);
79 // do nothing, pool entries can't be reclaimed
80 //dyld::log("free(%p) from pool\n", ptr);
85 void* calloc(size_t count
, size_t size
)
87 if ( dyld::gLibSystemHelpers
!= NULL
) {
88 void* result
= dyld::gLibSystemHelpers
->malloc(size
);
93 return malloc(count
*size
);
98 void* realloc(void *ptr
, size_t size
)
100 void* result
= malloc(size
);
101 memcpy(result
, ptr
, size
);
105 // void* reallocf(void *ptr, size_t size);
106 // void* valloc(size_t size);
108 // needed __libc_init()
109 extern "C" int _malloc_lock
;
110 int _malloc_lock
= 0;