]> git.saurik.com Git - apple/libc.git/blob - gen/zone.c
Libc-498.1.1.tar.gz
[apple/libc.git] / gen / zone.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import <objc/zone.h>
25 #import <stdio.h>
26 #import <libc.h>
27 #import <pthread.h>
28 #import <stdlib.h>
29 #import <unistd.h>
30
31 #define OBSOLETED
32
33 static pthread_mutex_t _zone_mutex = PTHREAD_MUTEX_INITIALIZER;
34
35 enum {
36 nXDefaultMallocZone = 0,
37 nXCreateZone,
38 nXNameZone,
39 nXZoneMalloc,
40 nXZoneRealloc,
41 nXZoneCalloc,
42 nXZoneFree,
43 nXDestroyZone,
44 nXZoneFromPtr,
45 nXZonePtrInfo,
46 nXMallocCheck,
47 _nXMallocDumpZones
48 };
49 static char *once[] = {
50 "NXDefaultMallocZone",
51 "NXCreateZone",
52 "NXNameZone",
53 "NXZoneMalloc",
54 "NXZoneRealloc",
55 "NXZoneCalloc",
56 "NXZoneFree",
57 "NXDestroyZone",
58 "NXZoneFromPtr",
59 "NXZonePtrInfo",
60 "NXMallocCheck",
61 "_NXMallocDumpZones"
62 };
63
64 extern int __is_threaded;
65
66 /********* NX functions ************/
67
68 static void
69 _deprecated(int index)
70 {
71 if(__is_threaded)
72 pthread_mutex_lock(&_zone_mutex);
73 if(once[index]) {
74 fprintf(stderr, "*** %s[%d]: %s() is deprecated and will be removed in the future\n", getprogname(), getpid(), once[index]);
75 once[index] = NULL;
76 }
77 if(__is_threaded)
78 pthread_mutex_unlock(&_zone_mutex);
79 }
80
81 malloc_zone_t *NXDefaultMallocZone() {
82 _deprecated(nXDefaultMallocZone);
83 return malloc_default_zone();
84 }
85
86 malloc_zone_t *NXCreateZone(size_t startsize, size_t granularity, int canfree) {
87 _deprecated(nXCreateZone);
88 return malloc_create_zone(startsize, 0);
89 }
90
91 void NXNameZone(malloc_zone_t *z, const char *name) {
92 _deprecated(nXNameZone);
93 malloc_set_zone_name(z, name);
94 }
95
96 void *NXZoneMalloc(malloc_zone_t *zone, size_t size) {
97 _deprecated(nXZoneMalloc);
98 return malloc_zone_malloc(zone, size);
99 }
100
101 void *NXZoneRealloc(malloc_zone_t *zone, void *ptr, size_t size) {
102 _deprecated(nXZoneRealloc);
103 return malloc_zone_realloc(zone, ptr, size);
104 }
105
106 void *NXZoneCalloc(malloc_zone_t *zone, size_t num_items, size_t size) {
107 _deprecated(nXZoneCalloc);
108 return malloc_zone_calloc(zone, num_items, size);
109 }
110
111 void NXZoneFree(malloc_zone_t *zone, void *ptr) {
112 _deprecated(nXZoneFromPtr);
113 malloc_zone_free(zone, ptr);
114 }
115
116 void NXDestroyZone(malloc_zone_t *zone) {
117 _deprecated(nXDestroyZone);
118 if (zone == malloc_default_zone()) return; // we avoid destroying child zones
119 malloc_destroy_zone(zone);
120 }
121
122 NXZone *NXZoneFromPtr(void *ptr) {
123 NXZone *zone = malloc_zone_from_ptr(ptr);
124 _deprecated(nXZoneFromPtr);
125 if (!zone) {
126 malloc_printf("*** NXZoneFromPtr() did not find any zone for %p; returning default\n", ptr);
127 zone = NX_NOZONE;
128 }
129 return zone;
130 }
131
132 #ifndef OBSOLETED
133 void NXAddRegion(void *start, size_t size, malloc_zone_t *zone) {
134 malloc_printf("*** OBSOLETE: NXAddRegion()\n");
135 }
136
137 void NXRemoveRegion(void *start) {
138 malloc_printf("*** OBSOLETE: NXRemoveRegion()\n");
139 }
140 #endif /* OBSOLETED */
141
142 void NXZonePtrInfo(void *ptr) {
143 _deprecated(nXZonePtrInfo);
144 malloc_zone_print_ptr_info(ptr);
145 }
146
147 int NXMallocCheck(void) {
148 _deprecated(nXMallocCheck);
149 malloc_zone_check(NULL);
150 return 1;
151 }
152
153 void _NXMallocDumpZones(void) {
154 _deprecated(_nXMallocDumpZones);
155 malloc_zone_print(NULL, 0);
156 }
157
158 /***************** UNIMPLEMENTED ENTRY POINTS ********************/
159
160 #ifndef OBSOLETED
161 void NXMergeZone(malloc_zone_t *z) {
162 static char warned = 0;
163 if (!warned) {
164 malloc_printf("*** NXMergeZone() now obsolete, does nothing\n");
165 warned = 1;
166 }
167 }
168
169 boolean_t NXProtectZone(malloc_zone_t *zone, int protection) {
170 malloc_printf("*** NXProtectZone() is obsolete\n");
171 return 0;
172 }
173
174 malloc_zone_t *NXCreateChildZone(malloc_zone_t *parentzone, size_t startsize, size_t granularity, int canfree) {
175 // We can not remove this one as it is still used by IndexingKit
176 static char warned = 0;
177 if (!warned) {
178 malloc_printf("*** NXCreateChildZone() now obsolete, has been defined to create new zone\n");
179 warned = 1;
180 }
181 return NXCreateZone(startsize, granularity, canfree);
182 }
183
184 void _NXMallocDumpFrees(void) {
185 malloc_printf("*** OBSOLETE: _NXMallocDumpFrees()\n");
186 }
187 #endif /* OBSOLETED */