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
;
55 // libunwind, and probably others, need the pool to be 16-byte aligned as malloc guarantees that
56 __attribute__((__aligned__(16)))
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
;
66 void* malloc(size_t size
)
68 if ( (dyld::gLibSystemHelpers
!= NULL
) && dyld::gProcessInfo
->libSystemInitialized
) {
69 void* p
= dyld::gLibSystemHelpers
->malloc(size
);
70 //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
74 // keep allocations 16-byte aligned
75 size
= ((size
+ 15) & -16);
77 if ( size
> DYLD_POOL_CHUNK_SIZE
) {
78 dyld::log("dyld malloc overflow: size=%lu\n", size
);
79 dyld::halt("dyld malloc overflow\n");
81 size
= (size
+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
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
) {
88 dyld::halt("out of address space for dyld memory pool\n");
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
);
98 dyld::halt("dyld memory pool exhausted\n");
100 result
= currentPool
->current
;
101 currentPool
->current
+= size
;
103 //dyld::log("%p = malloc(%3lu) from pool %p, free space = %lu\n", result, size, currentPool, (long)(currentPool->end - currentPool->current));
111 // ignore any pointer within dyld (i.e. stuff from pool or static strings)
112 if ( (dyld::gLibSystemHelpers
!= NULL
) && ((ptr
< &__dso_handle
) || (ptr
>= &initialPoolContent
[DYLD_POOL_CHUNK_SIZE
])) ) {
113 // ignore stuff in any dynamically alloated dyld pools
114 for (dyld_static_pool
* p
= currentPool
; p
!= NULL
; p
= p
->previousPool
) {
115 if ( (p
->pool
<= ptr
) && (ptr
< p
->end
) ) {
116 // do nothing, pool entries can't be reclaimed
117 //dyld::log("free(%p) from dynamic pool\n", ptr);
122 //dyld::log("free(%p) from libSystem\n", ptr);
123 return dyld::gLibSystemHelpers
->free(ptr
);
126 // do nothing, pool entries can't be reclaimed
127 //dyld::log("free(%p) from static pool\n", ptr);
132 void* calloc(size_t count
, size_t size
)
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");
140 if ( dyld::gLibSystemHelpers
!= NULL
) {
141 void* result
= dyld::gLibSystemHelpers
->malloc(total
);
142 if ( result
!= NULL
)
143 bzero(result
, total
);
147 // this allocates out of static buffer which is already zero filled
148 return malloc(total
);
153 void* realloc(void *ptr
, size_t size
)
155 void* result
= malloc(size
);
156 memcpy(result
, ptr
, size
);
160 // void* reallocf(void *ptr, size_t size);
161 // void* valloc(size_t size);
163 // needed __libc_init()
164 extern "C" int _malloc_lock
;
165 int _malloc_lock
= 0;
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
)
171 if ( (dyld::gLibSystemHelpers
!= NULL
) && (dyld::gLibSystemHelpers
->version
>= 12) ) {
172 return dyld::gLibSystemHelpers
->vm_alloc(mach_task_self(), addr
, size
, flags
);
175 return ::vm_allocate(mach_task_self(), addr
, size
, flags
);
179 void* xmmap(void* addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
181 if ( (dyld::gLibSystemHelpers
!= NULL
) && (dyld::gLibSystemHelpers
->version
>= 12) ) {
182 return dyld::gLibSystemHelpers
->mmap(addr
, len
, prot
, flags
, fd
, offset
);
185 return ::mmap(addr
, len
, prot
, flags
, fd
, offset
);