]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSAutoreleasePool+misc.m
fixed gcc 4.1 warnings
[sope] / sope-core / NGExtensions / FdExt.subproj / NSAutoreleasePool+misc.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import "common.h"
23 #import "NSAutoreleasePool+misc.h"
24
25 #if defined(LIB_FOUNDATION_LIBRARY)
26
27 BOOL __autoreleaseEnableRetainRemove = NO;
28
29 #if !LIB_FOUNDATION_BOEHM_GC
30
31 @implementation NSAutoreleasePool(misc)
32
33 // retain/remove check
34
35 + (void)enableRetainRemove:(BOOL)enable {
36     __autoreleaseEnableRetainRemove = enable;
37 }
38
39 + (NSAutoreleasePool *)findReleasingPoolForObject:(id)_obj {
40     NSAutoreleasePool *pool = nil;
41     
42     for (pool = [self defaultPool]; pool; pool = pool->parentPool) {
43         NSAutoreleasePoolChunk *ch;
44         int i;
45
46         for (ch = pool->firstChunk; ch; ch = ch->next) {
47             for (i = 0; i < ch->used; i++) {
48                 if (ch->objects[i] == _obj)
49                     return pool;
50             }
51         }
52         //if ([pool doesReleaseObject:_obj])
53         //    return pool;
54     }
55     return nil;
56 }
57
58 + (BOOL)retainAutoreleasedObject:(id)_obj {
59     register NSAutoreleasePool *pool = nil;
60     
61     for (pool = [self defaultPool]; pool; pool = pool->parentPool) {
62         register NSAutoreleasePoolChunk *ch;
63         register int i;
64
65         for (ch = pool->firstChunk; ch; ch = ch->next) {
66             for (i = 0; i < ch->used; i++) {
67                 if (ch->objects[i] == _obj) {
68                     ch->objects[i] = nil;
69                     return YES;
70                 }
71             }
72         }
73         //if ([pool doesReleaseObject:_obj])
74         //    return pool;
75     }
76     return NO;
77 }
78
79 - (BOOL)retainAutoreleasedObject:(id)_obj {
80     NSAutoreleasePoolChunk *ch;
81     int i;
82
83     for (ch = firstChunk; ch; ch = ch->next) {
84         for (i = 0; i < ch->used; i++) {
85             if (ch->objects[i] == _obj) {
86                 ch->objects[i] = nil;
87                 return YES;
88             }
89         }
90     }
91     return NO;
92 }
93
94 @end
95
96 @implementation NSObject(RC)
97
98 - (oneway void)release
99 {
100 #if BUILD_libFoundation_DLL && defined(__WIN32__)
101     extern __declspec(dllimport) BOOL __autoreleaseEnableCheck;
102 #else
103     extern BOOL __autoreleaseEnableCheck;
104 #endif
105
106     // check if retainCount is Ok
107     if (__autoreleaseEnableCheck) {
108         unsigned int toCome = [NSAutoreleasePool autoreleaseCountForObject:self];
109         if (toCome+1 > [self retainCount]) {
110             NSLog(@"Release[0x%p<%@>] release check for object %@ "
111                   @"has %d references "
112                   @"and %d pending calls to release in autorelease pools\n", 
113                   (unsigned)self, NSStringFromClass([self class]),
114                   self,
115                   [self retainCount], toCome);
116             NSLog(@"  description='%@'", [self description]);
117             abort(); // core dump for debugging
118             return;
119         }
120     }
121     if (NSExtraRefCount(self) == 1)
122         [self dealloc];
123     else
124         NSDecrementExtraRefCountWasZero(self);
125 }
126
127 - (id)retain
128 {
129     extern BOOL __autoreleaseEnableRetainRemove;
130
131     if (__autoreleaseEnableRetainRemove) {
132         if ([NSAutoreleasePool retainAutoreleasedObject:self]) {
133             NSLog(@"retained autoreleased object ..");
134             return self;
135         }
136     }
137     
138     NSIncrementExtraRefCount(self);
139     return self;
140 }
141
142 @end
143
144 #endif // !LIB_FOUNDATION_BOEHM_GC
145
146 #endif // defined(LIB_FOUNDATION_LIBRARY)
147
148 void __link_NSAutoreleasePool_misc(void) {
149   __link_NSAutoreleasePool_misc();
150 }