mirror of
https://github.com/gnustep/libs-base.git
synced 2025-06-01 09:02:01 +00:00
minor optimisation
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38534 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
ef1f8107eb
commit
78ed7c4608
1 changed files with 44 additions and 33 deletions
|
@ -66,7 +66,11 @@ static table_entry* the_table = 0;
|
||||||
|
|
||||||
static BOOL debug_allocation = NO;
|
static BOOL debug_allocation = NO;
|
||||||
|
|
||||||
static GSLazyRecursiveLock *uniqueLock = nil;
|
static NSRecursiveLock *uniqueLock = nil;
|
||||||
|
static SEL doLockSel = 0;
|
||||||
|
static SEL unLockSel = 0;
|
||||||
|
static IMP doLockImp = 0;
|
||||||
|
static IMP unLockImp = 0;
|
||||||
|
|
||||||
static const char* _GSDebugAllocationList(BOOL difference);
|
static const char* _GSDebugAllocationList(BOOL difference);
|
||||||
static const char* _GSDebugAllocationListAll(void);
|
static const char* _GSDebugAllocationListAll(void);
|
||||||
|
@ -79,6 +83,9 @@ static void (*_GSDebugAllocationAddFunc)(Class c, id o)
|
||||||
static void (*_GSDebugAllocationRemoveFunc)(Class c, id o)
|
static void (*_GSDebugAllocationRemoveFunc)(Class c, id o)
|
||||||
= _GSDebugAllocationRemove;
|
= _GSDebugAllocationRemove;
|
||||||
|
|
||||||
|
#define doLock() (*doLockImp)(uniqueLock, doLockSel)
|
||||||
|
#define unLock() (*unLockImp)(uniqueLock, unLockSel)
|
||||||
|
|
||||||
@interface GSDebugAlloc : NSObject
|
@interface GSDebugAlloc : NSObject
|
||||||
+ (void) initialize;
|
+ (void) initialize;
|
||||||
@end
|
@end
|
||||||
|
@ -86,7 +93,11 @@ static void (*_GSDebugAllocationRemoveFunc)(Class c, id o)
|
||||||
@implementation GSDebugAlloc
|
@implementation GSDebugAlloc
|
||||||
+ (void) initialize
|
+ (void) initialize
|
||||||
{
|
{
|
||||||
uniqueLock = [GSLazyRecursiveLock new];
|
uniqueLock = [NSRecursiveLock new];
|
||||||
|
doLockSel = @selector(lock);
|
||||||
|
unLockSel = @selector(unlock);
|
||||||
|
doLockImp = [uniqueLock methodForSelector: doLockSel];
|
||||||
|
unLockImp = [uniqueLock methodForSelector: unLockSel];
|
||||||
[[NSObject leakAt: &uniqueLock] release];
|
[[NSObject leakAt: &uniqueLock] release];
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
@ -95,7 +106,7 @@ void
|
||||||
GSSetDebugAllocationFunctions(void (*newAddObjectFunc)(Class c, id o),
|
GSSetDebugAllocationFunctions(void (*newAddObjectFunc)(Class c, id o),
|
||||||
void (*newRemoveObjectFunc)(Class c, id o))
|
void (*newRemoveObjectFunc)(Class c, id o))
|
||||||
{
|
{
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
|
|
||||||
if (newAddObjectFunc && newRemoveObjectFunc)
|
if (newAddObjectFunc && newRemoveObjectFunc)
|
||||||
{
|
{
|
||||||
|
@ -109,7 +120,7 @@ GSSetDebugAllocationFunctions(void (*newAddObjectFunc)(Class c, id o),
|
||||||
_GSDebugAllocationRemoveFunc = _GSDebugAllocationRemove;
|
_GSDebugAllocationRemoveFunc = _GSDebugAllocationRemove;
|
||||||
}
|
}
|
||||||
|
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
BOOL
|
||||||
|
@ -137,7 +148,7 @@ GSDebugAllocationRecordObjects(Class c, BOOL newState)
|
||||||
{
|
{
|
||||||
if (the_table[i].class == c)
|
if (the_table[i].class == c)
|
||||||
{
|
{
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
oldState = (YES == the_table[i].is_recording) ? YES : NO;
|
oldState = (YES == the_table[i].is_recording) ? YES : NO;
|
||||||
if (newState)
|
if (newState)
|
||||||
{
|
{
|
||||||
|
@ -156,13 +167,13 @@ GSDebugAllocationRecordObjects(Class c, BOOL newState)
|
||||||
the_table[i].recorded_tags[j] = nil;
|
the_table[i].recorded_tags[j] = nil;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return oldState;
|
return oldState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (YES == newState)
|
if (YES == newState)
|
||||||
{
|
{
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
if (num_classes >= table_size)
|
if (num_classes >= table_size)
|
||||||
{
|
{
|
||||||
int more = table_size + 128;
|
int more = table_size + 128;
|
||||||
|
@ -172,7 +183,7 @@ GSDebugAllocationRecordObjects(Class c, BOOL newState)
|
||||||
|
|
||||||
if (tmp == 0)
|
if (tmp == 0)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
if (the_table)
|
if (the_table)
|
||||||
|
@ -194,7 +205,7 @@ GSDebugAllocationRecordObjects(Class c, BOOL newState)
|
||||||
the_table[num_classes].num_recorded_objects = 0;
|
the_table[num_classes].num_recorded_objects = 0;
|
||||||
the_table[num_classes].stack_size = 0;
|
the_table[num_classes].stack_size = 0;
|
||||||
num_classes++;
|
num_classes++;
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
}
|
}
|
||||||
return oldState;
|
return oldState;
|
||||||
}
|
}
|
||||||
|
@ -222,7 +233,7 @@ _GSDebugAllocationAdd(Class c, id o)
|
||||||
{
|
{
|
||||||
if (the_table[i].class == c)
|
if (the_table[i].class == c)
|
||||||
{
|
{
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
the_table[i].count++;
|
the_table[i].count++;
|
||||||
the_table[i].total++;
|
the_table[i].total++;
|
||||||
if (the_table[i].count > the_table[i].peak)
|
if (the_table[i].count > the_table[i].peak)
|
||||||
|
@ -242,7 +253,7 @@ _GSDebugAllocationAdd(Class c, id o)
|
||||||
more * sizeof(id));
|
more * sizeof(id));
|
||||||
if (tmp == 0)
|
if (tmp == 0)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,7 +262,7 @@ _GSDebugAllocationAdd(Class c, id o)
|
||||||
if (tmp1 == 0)
|
if (tmp1 == 0)
|
||||||
{
|
{
|
||||||
NSZoneFree(NSDefaultMallocZone(), tmp);
|
NSZoneFree(NSDefaultMallocZone(), tmp);
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,11 +291,11 @@ _GSDebugAllocationAdd(Class c, id o)
|
||||||
[the_table[i].num_recorded_objects] = nil;
|
[the_table[i].num_recorded_objects] = nil;
|
||||||
the_table[i].num_recorded_objects++;
|
the_table[i].num_recorded_objects++;
|
||||||
}
|
}
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
if (num_classes >= table_size)
|
if (num_classes >= table_size)
|
||||||
{
|
{
|
||||||
unsigned int more = table_size + 128;
|
unsigned int more = table_size + 128;
|
||||||
|
@ -294,7 +305,7 @@ _GSDebugAllocationAdd(Class c, id o)
|
||||||
|
|
||||||
if (tmp == 0)
|
if (tmp == 0)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return; /* Argh */
|
return; /* Argh */
|
||||||
}
|
}
|
||||||
if (the_table)
|
if (the_table)
|
||||||
|
@ -316,7 +327,7 @@ _GSDebugAllocationAdd(Class c, id o)
|
||||||
the_table[num_classes].num_recorded_objects = 0;
|
the_table[num_classes].num_recorded_objects = 0;
|
||||||
the_table[num_classes].stack_size = 0;
|
the_table[num_classes].stack_size = 0;
|
||||||
num_classes++;
|
num_classes++;
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,7 +383,7 @@ GSDebugAllocationClassList()
|
||||||
size_t siz;
|
size_t siz;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
|
|
||||||
siz = sizeof(Class) * (num_classes + 1);
|
siz = sizeof(Class) * (num_classes + 1);
|
||||||
ans = NSZoneMalloc(NSDefaultMallocZone(), siz);
|
ans = NSZoneMalloc(NSDefaultMallocZone(), siz);
|
||||||
|
@ -383,7 +394,7 @@ GSDebugAllocationClassList()
|
||||||
}
|
}
|
||||||
ans[num_classes] = NULL;
|
ans[num_classes] = NULL;
|
||||||
|
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
|
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
@ -398,10 +409,10 @@ GSDebugAllocationList(BOOL changeFlag)
|
||||||
{
|
{
|
||||||
return "Debug allocation system is not active!\n";
|
return "Debug allocation system is not active!\n";
|
||||||
}
|
}
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
ans = _GSDebugAllocationList(changeFlag);
|
ans = _GSDebugAllocationList(changeFlag);
|
||||||
d = [NSData dataWithBytes: ans length: strlen(ans) + 1];
|
d = [NSData dataWithBytes: ans length: strlen(ans) + 1];
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return (const char*)[d bytes];
|
return (const char*)[d bytes];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -491,10 +502,10 @@ GSDebugAllocationListAll()
|
||||||
{
|
{
|
||||||
return "Debug allocation system is not active!\n";
|
return "Debug allocation system is not active!\n";
|
||||||
}
|
}
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
ans = _GSDebugAllocationListAll();
|
ans = _GSDebugAllocationListAll();
|
||||||
d = [NSData dataWithBytes: ans length: strlen(ans)+1];
|
d = [NSData dataWithBytes: ans length: strlen(ans)+1];
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return (const char*)[d bytes];
|
return (const char*)[d bytes];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,7 +586,7 @@ _GSDebugAllocationRemove(Class c, id o)
|
||||||
{
|
{
|
||||||
id tag = nil;
|
id tag = nil;
|
||||||
|
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
the_table[i].count--;
|
the_table[i].count--;
|
||||||
if (the_table[i].is_recording)
|
if (the_table[i].is_recording)
|
||||||
{
|
{
|
||||||
|
@ -610,7 +621,7 @@ _GSDebugAllocationRemove(Class c, id o)
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
[tag release];
|
[tag release];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -630,7 +641,7 @@ GSDebugAllocationTagRecordedObject(id object, id tag)
|
||||||
{
|
{
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
|
|
||||||
for (i = 0; i < num_classes; i++)
|
for (i = 0; i < num_classes; i++)
|
||||||
{
|
{
|
||||||
|
@ -644,7 +655,7 @@ GSDebugAllocationTagRecordedObject(id object, id tag)
|
||||||
|| the_table[i].is_recording == NO
|
|| the_table[i].is_recording == NO
|
||||||
|| the_table[i].num_recorded_objects == 0)
|
|| the_table[i].num_recorded_objects == 0)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -658,7 +669,7 @@ GSDebugAllocationTagRecordedObject(id object, id tag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return AUTORELEASE(o);
|
return AUTORELEASE(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -674,7 +685,7 @@ GSDebugAllocationListRecordedObjects(Class c)
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
[uniqueLock lock];
|
doLock();
|
||||||
|
|
||||||
for (i = 0; i < num_classes; i++)
|
for (i = 0; i < num_classes; i++)
|
||||||
{
|
{
|
||||||
|
@ -686,19 +697,19 @@ GSDebugAllocationListRecordedObjects(Class c)
|
||||||
|
|
||||||
if (i == num_classes)
|
if (i == num_classes)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (the_table[i].is_recording == NO)
|
if (the_table[i].is_recording == NO)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (the_table[i].num_recorded_objects == 0)
|
if (the_table[i].num_recorded_objects == 0)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return [NSArray array];
|
return [NSArray array];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -706,7 +717,7 @@ GSDebugAllocationListRecordedObjects(Class c)
|
||||||
the_table[i].num_recorded_objects * sizeof(id));
|
the_table[i].num_recorded_objects * sizeof(id));
|
||||||
if (tmp == 0)
|
if (tmp == 0)
|
||||||
{
|
{
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -724,7 +735,7 @@ GSDebugAllocationListRecordedObjects(Class c)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Then, we bravely unlock the lock */
|
/* Then, we bravely unlock the lock */
|
||||||
[uniqueLock unlock];
|
unLock();
|
||||||
|
|
||||||
/* Only then we create an array with them - this is now safe as we
|
/* Only then we create an array with them - this is now safe as we
|
||||||
* have copied the objects out, unlocked, and retained them. */
|
* have copied the objects out, unlocked, and retained them. */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue