(+enableRelease:, +enableDoubleReleaseCheck:,

+setPoolCountThreshhold:): New methods.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@354 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Andrew McCallum 1995-04-14 15:01:24 +00:00
parent 622565d761
commit 03659389e9

View file

@ -27,6 +27,7 @@
#include <objects/collhash.h>
#include <objects/eltfuncs.h>
#include <objects/objc-malloc.h>
#include <limits.h>
/* Doesn't handle multi-threaded stuff.
Doesn't handle exceptions. */
@ -39,10 +40,60 @@
static NSAutoreleasePool *current_pool = nil;
/* When this is `NO', autoreleased objects are never actually sent a
`release' message. Memory use grows, and grows, and... */
static BOOL autorelease_enabled = YES;
/* When this is `YES', every call to addObject, checks to make sure
isn't being set up to release itself too many times. */
static BOOL double_release_check_enabled = NO;
/* When the released_count gets over this value, we call error:.
In the future, I may change this to raise an exception or call
a function instead. */
static unsigned pool_count_warning_threshhold = UINT_MAX;
#define DEFAULT_SIZE 64
@implementation NSAutoreleasePool
/* This method not in OpenStep */
- parentAutoreleasePool
{
return parent;
}
/* This method not in OpenStep */
- (unsigned) autoreleaseCount
{
return released_count;
}
/* This method not in OpenStep */
- (unsigned) autoreleaseCountForObject: anObject
{
unsigned count = 0;
int i;
for (i = 0; i < released_count; i++)
if (released[i] == anObject)
count++;
return count;
}
/* This method not in OpenStep */
+ (unsigned) autoreleaseCountForObject: anObject
{
unsigned count;
id pool = current_pool;
while (pool)
{
count += [pool autoreleaseCountForObject:anObject];
pool = [pool parentAutoreleasePool];
}
return count;
}
+ currentPool
{
return current_pool;
@ -55,6 +106,20 @@ static NSAutoreleasePool *current_pool = nil;
- (void) addObject: anObj
{
if (!autorelease_enabled)
return;
if (double_release_check_enabled)
{
unsigned release_count = [[self class] autoreleaseCountForObject:anObj];
unsigned retain_count = [anObj retainCount];
if (release_count > retain_count + 1)
[self error:"Autorelease would release object too many times."];
}
if (released_count >= pool_count_warning_threshhold)
[self error:"AutoreleasePool count threshhold exceeded."];
released_count++;
if (released_count == released_size)
{
@ -105,4 +170,19 @@ static NSAutoreleasePool *current_pool = nil;
return self;
}
+ (void) enableRelease: (BOOL)enable
{
autorelease_enabled = enable;
}
+ (void) enableDoubleReleaseCheck: (BOOL)enable
{
double_release_check_enabled = enable;
}
+ (void) setPoolCountThreshhold: (unsigned)c
{
pool_count_warning_threshhold = c;
}
@end