dyld-46.16.tar.gz
[apple/dyld.git] / src / dyldNew.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2005 Apple Computer, 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 #include <new>
26 #include <malloc/malloc.h>
27 //#include <stdio.h>
28
29
30 //
31 // dyld does not use malloc anywhere, instead C++ new is used.
32 // All dyld allocations go in dyld-only zone so as to be not co-mingled with target proccess's allocations
33 //
34 //
35 //
36
37 static malloc_zone_t* sZone = NULL; // could be initialized to malloc_create_zone, but that would require careful ordering of initializers
38
39
40 void* operator new(std::size_t len) throw (std::bad_alloc)
41 {
42 if ( sZone == NULL ) {
43 sZone = malloc_create_zone(40960, 0);
44 malloc_set_zone_name(sZone, "dyld heap");
45 }
46 //fprintf(stderr, "new(%d)\n", len);
47 return malloc_zone_malloc(sZone, len);
48 }
49
50 void* operator new[](std::size_t len) throw (std::bad_alloc)
51 {
52 if ( sZone == NULL ) {
53 sZone = malloc_create_zone(40960, 0);
54 malloc_set_zone_name(sZone, "dyld heap");
55 }
56 //fprintf(stderr, "new[](%d)\n", len);
57 return malloc_zone_malloc(sZone, len);
58 }
59
60
61 void operator delete(void* obj) throw()
62 {
63 //fprintf(stderr, "delete(%p)\n", obj);
64 malloc_zone_free(sZone, obj);
65 }
66
67
68 void operator delete[](void* obj) throw()
69 {
70 //fprintf(stderr, "delete[](%p)\n", obj);
71 malloc_zone_free(sZone, obj);
72 }
73
74