Loading...
--- dyld/dyld-45.1/src/dyldNew.cpp
+++ dyld/dyld-96.2/src/dyldNew.cpp
@@ -1,6 +1,6 @@
/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
*
- * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
@@ -22,53 +22,91 @@
* @APPLE_LICENSE_HEADER_END@
*/
-#include <new>
-#include <malloc/malloc.h>
-//#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+extern "C" void* __dso_handle;
+
+#include "dyld.h"
+#include "dyldLibSystemInterface.h"
//
-// dyld does not use malloc anywhere, instead C++ new is used.
-// All dyld allocations go in dyld-only zone so as to be not co-mingled with target proccess's allocations
-//
-//
+// dyld initially allocates all memory from a pool inside dyld.
+// Once libSystem.dylib is initialized, dyld uses libSystem's malloc/free.
//
-static malloc_zone_t* sZone = NULL; // could be initialized to malloc_create_zone, but that would require careful ordering of initializers
+#if __LP64__
+ // room for about ~1000 initial dylibs
+ #define DYLD_INITIAL_POOL_SIZE 400*1024
+#else
+ // room for about ~900 initial dylibs
+ #define DYLD_INITIAL_POOL_SIZE 200*1024
+#endif
+static uint8_t dyldPool[DYLD_INITIAL_POOL_SIZE];
+static uint8_t* curPoolPtr = dyldPool;
-
-void* operator new(std::size_t len) throw (std::bad_alloc)
+void* malloc(size_t size)
{
- if ( sZone == NULL ) {
- sZone = malloc_create_zone(40960, 0);
- malloc_set_zone_name(sZone, "dyld heap");
+ if ( dyld::gLibSystemHelpers != NULL) {
+ void* p = dyld::gLibSystemHelpers->malloc(size);
+ //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
+ return p;
}
- //fprintf(stderr, "new(%d)\n", len);
- return malloc_zone_malloc(sZone, len);
-}
-
-void* operator new[](std::size_t len) throw (std::bad_alloc)
-{
- if ( sZone == NULL ) {
- sZone = malloc_create_zone(40960, 0);
- malloc_set_zone_name(sZone, "dyld heap");
+ else {
+ size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
+ uint8_t* result = curPoolPtr;
+ if ( (curPoolPtr + size) > &dyldPool[DYLD_INITIAL_POOL_SIZE] ) {
+ dyld::log("initial dyld memory pool exhausted\n");
+ _exit(1);
+ }
+ curPoolPtr += size;
+ //dyld::log("%p = malloc(%lu) from pool, total = %d\n", result, size, curPoolPtr-dyldPool);
+ return result;
}
- //fprintf(stderr, "new[](%d)\n", len);
- return malloc_zone_malloc(sZone, len);
}
-void operator delete(void* obj) throw()
+void free(void* ptr)
{
- //fprintf(stderr, "delete(%p)\n", obj);
- malloc_zone_free(sZone, obj);
+ // ignore any pointer within dyld (i.e. stuff from pool or static strings)
+ if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &dyldPool[DYLD_INITIAL_POOL_SIZE])) ) {
+ //dyld::log("free(%p) from libSystem\n", ptr);
+ return dyld::gLibSystemHelpers->free(ptr);
+ }
+ else {
+ // do nothing, pool entries can't be reclaimed
+ //dyld::log("free(%p) from pool\n", ptr);
+ }
}
-void operator delete[](void* obj) throw()
+void* calloc(size_t count, size_t size)
{
- //fprintf(stderr, "delete[](%p)\n", obj);
- malloc_zone_free(sZone, obj);
+ if ( dyld::gLibSystemHelpers != NULL ) {
+ void* result = dyld::gLibSystemHelpers->malloc(size);
+ bzero(result, size);
+ return result;
+ }
+ else {
+ return malloc(count*size);
+ }
}
+void* realloc(void *ptr, size_t size)
+{
+ void* result = malloc(size);
+ memcpy(result, ptr, size);
+ return result;
+}
+
+// void* reallocf(void *ptr, size_t size);
+// void* valloc(size_t size);
+
+// needed __libc_init()
+extern "C" int _malloc_lock;
+int _malloc_lock = 0;
+
+