]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSAutoreleasePool+misc.m
fixed incompatibility of new logging framework with OGo
[sope] / sope-core / NGExtensions / FdExt.subproj / NSAutoreleasePool+misc.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #import "common.h"
24 #import "NSAutoreleasePool+misc.h"
25
26 #if defined(LIB_FOUNDATION_LIBRARY)
27
28 BOOL __autoreleaseEnableRetainRemove = NO;
29
30 #if !LIB_FOUNDATION_BOEHM_GC
31
32 @implementation NSAutoreleasePool(misc)
33
34 // retain/remove check
35
36 + (void)enableRetainRemove:(BOOL)enable {
37     __autoreleaseEnableRetainRemove = enable;
38 }
39
40 + (NSAutoreleasePool *)findReleasingPoolForObject:(id)_obj {
41     NSAutoreleasePool *pool = nil;
42     
43     for (pool = [self defaultPool]; pool; pool = pool->parentPool) {
44         NSAutoreleasePoolChunk *ch;
45         int i;
46
47         for (ch = pool->firstChunk; ch; ch = ch->next) {
48             for (i = 0; i < ch->used; i++) {
49                 if (ch->objects[i] == _obj)
50                     return pool;
51             }
52         }
53         //if ([pool doesReleaseObject:_obj])
54         //    return pool;
55     }
56     return nil;
57 }
58
59 + (BOOL)retainAutoreleasedObject:(id)_obj {
60     register NSAutoreleasePool *pool = nil;
61     
62     for (pool = [self defaultPool]; pool; pool = pool->parentPool) {
63         register NSAutoreleasePoolChunk *ch;
64         register int i;
65
66         for (ch = pool->firstChunk; ch; ch = ch->next) {
67             for (i = 0; i < ch->used; i++) {
68                 if (ch->objects[i] == _obj) {
69                     ch->objects[i] = nil;
70                     return YES;
71                 }
72             }
73         }
74         //if ([pool doesReleaseObject:_obj])
75         //    return pool;
76     }
77     return NO;
78 }
79
80 - (BOOL)retainAutoreleasedObject:(id)_obj {
81     NSAutoreleasePoolChunk *ch;
82     int i;
83
84     for (ch = firstChunk; ch; ch = ch->next) {
85         for (i = 0; i < ch->used; i++) {
86             if (ch->objects[i] == _obj) {
87                 ch->objects[i] = nil;
88                 return YES;
89             }
90         }
91     }
92     return NO;
93 }
94
95 @end
96
97 @implementation NSObject(RC)
98
99 - (oneway void)release
100 {
101 #if BUILD_libFoundation_DLL && defined(__WIN32__)
102     extern __declspec(dllimport) BOOL __autoreleaseEnableCheck;
103 #else
104     extern BOOL __autoreleaseEnableCheck;
105 #endif
106
107     // check if retainCount is Ok
108     if (__autoreleaseEnableCheck) {
109         unsigned int toCome = [NSAutoreleasePool autoreleaseCountForObject:self];
110         if (toCome+1 > [self retainCount]) {
111             NSLog(@"Release[0x%08X<%@>] release check for object %@ "
112                   @"has %d references "
113                   @"and %d pending calls to release in autorelease pools\n", 
114                   (unsigned)self, NSStringFromClass([self class]),
115                   self,
116                   [self retainCount], toCome);
117             NSLog(@"  description='%@'", [self description]);
118             abort(); // core dump for debugging
119             return;
120         }
121     }
122     if (NSExtraRefCount(self) == 1)
123         [self dealloc];
124     else
125         NSDecrementExtraRefCountWasZero(self);
126 }
127
128 - (id)retain
129 {
130     extern BOOL __autoreleaseEnableRetainRemove;
131
132     if (__autoreleaseEnableRetainRemove) {
133         if ([NSAutoreleasePool retainAutoreleasedObject:self]) {
134             NSLog(@"retained autoreleased object ..");
135             return self;
136         }
137     }
138     
139     NSIncrementExtraRefCount(self);
140     return self;
141 }
142
143 @end
144
145 #endif // !LIB_FOUNDATION_BOEHM_GC
146
147 #endif // defined(LIB_FOUNDATION_LIBRARY)
148
149 void __link_NSAutoreleasePool_misc(void) {
150   __link_NSAutoreleasePool_misc();
151 }