1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2008 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@
29 #include <mach/mach.h>
32 extern "C" void* __dso_handle
;
35 #include "dyldLibSystemInterface.h"
38 // dyld initially allocates all memory from a pool inside dyld.
39 // Once libSystem.dylib is initialized, dyld uses libSystem's malloc/free.
43 // room for about ~1000 initial dylibs
44 #define DYLD_POOL_CHUNK_SIZE 200*1024
46 // room for about ~900 initial dylibs
47 #define DYLD_POOL_CHUNK_SIZE 150*1024
50 struct dyld_static_pool
{
51 dyld_static_pool
* previousPool
;
57 // allocate initial pool independently of pool header to take less space on disk
58 static uint8_t initialPoolContent
[DYLD_POOL_CHUNK_SIZE
] __attribute__((__aligned__(16)));
59 static dyld_static_pool initialPool
= { NULL
, initialPoolContent
, &initialPoolContent
[DYLD_POOL_CHUNK_SIZE
] };
60 static dyld_static_pool
* currentPool
= &initialPool
;
63 void* malloc(size_t size
)
65 if ( dyld::gLibSystemHelpers
!= NULL
) {
66 void* p
= dyld::gLibSystemHelpers
->malloc(size
);
67 //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
71 if ( size
> DYLD_POOL_CHUNK_SIZE
) {
72 dyld::log("dyld malloc overflow: size=%zu\n", size
);
75 size
= (size
+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
76 uint8_t* result
= currentPool
->current
;
77 currentPool
->current
+= size
;
78 if ( currentPool
->current
> currentPool
->end
) {
79 vm_address_t addr
= 0;
80 kern_return_t r
= vm_allocate(mach_task_self(), &addr
, DYLD_POOL_CHUNK_SIZE
, VM_FLAGS_ANYWHERE
);
81 if ( r
!= KERN_SUCCESS
) {
82 dyld::log("out of address space for dyld memory pool\n");
85 dyld_static_pool
* newPool
= (dyld_static_pool
*)addr
;
86 newPool
->previousPool
= NULL
;
87 newPool
->current
= newPool
->pool
;
88 newPool
->end
= (uint8_t*)(addr
+ DYLD_POOL_CHUNK_SIZE
);
89 newPool
->previousPool
= currentPool
;
90 currentPool
= newPool
;
91 if ( (currentPool
->current
+ size
) > currentPool
->end
) {
92 dyld::log("dyld memory pool exhausted: size=%lu\n", size
);
95 result
= currentPool
->current
;
96 currentPool
->current
+= size
;
98 //dyld::log("%p = malloc(%3lu) from pool %p, free space = %lu\n", result, size, currentPool, (long)(currentPool->end - currentPool->current));
106 // ignore any pointer within dyld (i.e. stuff from pool or static strings)
107 if ( (dyld::gLibSystemHelpers
!= NULL
) && ((ptr
< &__dso_handle
) || (ptr
>= &initialPoolContent
[DYLD_POOL_CHUNK_SIZE
])) ) {
108 // ignore stuff in any dynamically alloated dyld pools
109 for (dyld_static_pool
* p
= currentPool
; p
!= NULL
; p
= p
->previousPool
) {
110 if ( (p
->pool
<= ptr
) && (ptr
< p
->end
) ) {
111 // do nothing, pool entries can't be reclaimed
112 //dyld::log("free(%p) from dynamic pool\n", ptr);
117 //dyld::log("free(%p) from libSystem\n", ptr);
118 return dyld::gLibSystemHelpers
->free(ptr
);
121 // do nothing, pool entries can't be reclaimed
122 //dyld::log("free(%p) from static pool\n", ptr);
127 void* calloc(size_t count
, size_t size
)
129 if ( dyld::gLibSystemHelpers
!= NULL
) {
130 void* result
= dyld::gLibSystemHelpers
->malloc(size
*count
);
131 bzero(result
, size
*count
);
135 // Check for overflow of integer multiplication
136 size_t total
= count
* size
;
137 if ( total
/count
!= size
) {
138 dyld::log("dyld calloc overflow: count=%zu, size=%zu\n", count
, size
);
141 return malloc(total
);
146 void* realloc(void *ptr
, size_t size
)
148 void* result
= malloc(size
);
149 memcpy(result
, ptr
, size
);
153 // void* reallocf(void *ptr, size_t size);
154 // void* valloc(size_t size);
156 // needed __libc_init()
157 extern "C" int _malloc_lock
;
158 int _malloc_lock
= 0;
161 // <rdar://problem/12857033> dyld calls this which uses libSystem.dylib's vm_allocate if available
162 int vm_alloc(vm_address_t
* addr
, vm_size_t size
, uint32_t flags
)
164 if ( (dyld::gLibSystemHelpers
!= NULL
) && (dyld::gLibSystemHelpers
->version
>= 12) ) {
165 return dyld::gLibSystemHelpers
->vm_alloc(mach_task_self(), addr
, size
, flags
);
168 return ::vm_allocate(mach_task_self(), addr
, size
, flags
);
172 void* xmmap(void* addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
174 if ( (dyld::gLibSystemHelpers
!= NULL
) && (dyld::gLibSystemHelpers
->version
>= 12) ) {
175 return dyld::gLibSystemHelpers
->mmap(addr
, len
, prot
, flags
, fd
, offset
);
178 return ::mmap(addr
, len
, prot
, flags
, fd
, offset
);