Minor tidying

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3100 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-10-22 10:02:39 +00:00
parent 6868e8ada7
commit 4568fd4427
3 changed files with 94 additions and 184 deletions

View file

@ -112,7 +112,7 @@
- (Class) classForArchiver; - (Class) classForArchiver;
- (Class) classForCoder; - (Class) classForCoder;
- (Class) classForPortCoder; - (Class) classForPortCoder;
- (id) replacementObjectForArchiver: (NSCoder*)anEncoder; - (id) replacementObjectForArchiver: (NSArchiver*)anEncoder;
- (id) replacementObjectForCoder: (NSCoder*)anEncoder; - (id) replacementObjectForCoder: (NSCoder*)anEncoder;
- (id) replacementObjectForPortCoder: (NSPortCoder*)anEncoder; - (id) replacementObjectForPortCoder: (NSPortCoder*)anEncoder;

View file

@ -553,7 +553,7 @@ static BOOL double_release_check_enabled = NO;
return [self classForCoder]; return [self classForCoder];
} }
- (id) replacementObjectForArchiver: (NSCoder*)anArchiver - (id) replacementObjectForArchiver: (NSArchiver*)anArchiver
{ {
return [self replacementObjectForCoder: (NSCoder*)anArchiver]; return [self replacementObjectForCoder: (NSCoder*)anArchiver];
} }

View file

@ -77,7 +77,7 @@
/* Define to turn off assertions. */ /* Define to turn off assertions. */
#define DEBUG 0 #define NDEBUG 1
#include <config.h> #include <config.h>
@ -113,7 +113,6 @@
#define NF_HEAD sizeof(nf_block) #define NF_HEAD sizeof(nf_block)
typedef struct _ffree_free_link ff_link; typedef struct _ffree_free_link ff_link;
typedef struct _nfree_chunk_struct nf_chunk;
typedef struct _nfree_block_struct nf_block; typedef struct _nfree_block_struct nf_block;
typedef struct _ffree_block_struct ff_block; typedef struct _ffree_block_struct ff_block;
typedef struct _ffree_zone_struct ffree_zone; typedef struct _ffree_zone_struct ffree_zone;
@ -137,20 +136,6 @@ struct _nfree_block_struct
char padding[ALIGN - ((NFBPAD % ALIGN) ? (NFBPAD % ALIGN) : ALIGN)]; char padding[ALIGN - ((NFBPAD % ALIGN) ? (NFBPAD % ALIGN) : ALIGN)];
}; };
struct _nfree_chunk_unpadded
{
size_t cur;
size_t max;
};
#define NFCPAD sizeof(struct _nfree_chunk_unpadded)
struct _nfree_chunk_struct
{
size_t cur;
size_t max;
char padding[ALIGN - ((NFCPAD % ALIGN) ? (NFCPAD % ALIGN) : ALIGN)];
};
struct _ffree_block_unpadded { struct _ffree_block_unpadded {
size_t size; size_t size;
struct _ffree_block_struct *next; struct _ffree_block_struct *next;
@ -336,6 +321,7 @@ struct _nfree_zone_struct
/* Linked list of blocks in decreasing order of free space, /* Linked list of blocks in decreasing order of free space,
except maybe for the first block. */ except maybe for the first block. */
nf_block *blocks; nf_block *blocks;
size_t use;
}; };
@ -395,8 +381,7 @@ NSZone* __nszone_private_hidden_default_zone = &default_zone;
/* /*
* Lists of zones to be used to determine if a pointer is in a zone. * Lists of zones to be used to determine if a pointer is in a zone.
*/ */
static NSZone *live_zones = 0; static NSZone *zone_list = 0;
static NSZone *dead_zones = 0;
inline NSZone* inline NSZone*
NSZoneFromPointer(void *ptr) NSZoneFromPointer(void *ptr)
@ -404,29 +389,35 @@ NSZoneFromPointer(void *ptr)
NSZone *zone; NSZone *zone;
if (ptr == 0) return 0; if (ptr == 0) return 0;
/*
* See if we can find the zone in our list of all zones.
*/
[gnustep_global_lock lock]; [gnustep_global_lock lock];
/* for (zone = zone_list; zone != 0; zone = zone->next) {
* See if we can find the zone in our list of all active zones.
*/
for (zone = live_zones; zone != 0; zone = zone->next) {
if ((zone->lookup)(zone, ptr) == YES) { if ((zone->lookup)(zone, ptr) == YES) {
[gnustep_global_lock unlock]; break;
return zone;
}
}
/*
* Also check our list of 'dead' zones - ones that have been recycled
* but still contain memory that is in use and has to be freed before
* we can finally destroy the zone properly.
*/
for (zone = dead_zones; zone != 0; zone = zone->next) {
if ((zone->lookup)(zone, ptr) == YES) {
[gnustep_global_lock unlock];
return zone;
} }
} }
[gnustep_global_lock unlock]; [gnustep_global_lock unlock];
return __nszone_private_hidden_default_zone; return (zone == 0) ? __nszone_private_hidden_default_zone : zone;
}
static inline void
destroy_zone(NSZone* zone)
{
if (zone_list == zone)
zone_list = zone->next;
else
{
NSZone *ptr = zone_list;
while (ptr->next != zone)
ptr = ptr->next;
if (ptr)
ptr->next = zone->next;
}
objc_free((void*)zone);
} }
static void* static void*
@ -748,15 +739,13 @@ frecycle (NSZone *zone)
[name release]; [name release];
} }
if (frecycle1(zone) == YES) if (frecycle1(zone) == YES)
objc_free((void*)zone); destroy_zone(zone);
else else
{ {
zone->malloc = rmalloc; zone->malloc = rmalloc;
zone->realloc = rrealloc; zone->realloc = rrealloc;
zone->free = rffree; zone->free = rffree;
zone->recycle = rrecycle; zone->recycle = rrecycle;
zone->next = dead_zones;
dead_zones = zone;
} }
[gnustep_global_lock unlock]; [gnustep_global_lock unlock];
} }
@ -765,22 +754,10 @@ static void
rffree (NSZone *zone, void *ptr) rffree (NSZone *zone, void *ptr)
{ {
ffree(zone, ptr); ffree(zone, ptr);
[gnustep_global_lock lock];
if (frecycle1(zone)) if (frecycle1(zone))
{ destroy_zone(zone);
[gnustep_global_lock lock]; [gnustep_global_lock unlock];
if (dead_zones == zone)
dead_zones = zone->next;
else
{
NSZone *ptr = dead_zones;
while (ptr->next != zone)
ptr = ptr->next;
ptr->next = zone->next;
}
objc_free((void*)zone);
[gnustep_global_lock unlock];
}
} }
@ -1266,27 +1243,27 @@ static void*
nmalloc (NSZone *zone, size_t size) nmalloc (NSZone *zone, size_t size)
{ {
nfree_zone *zptr = (nfree_zone*)zone; nfree_zone *zptr = (nfree_zone*)zone;
size_t chunksize = roundupto(size, ALIGN);
size_t freesize;
void *chunkhead;
nf_block *block;
size_t top; size_t top;
size_t chunksize = roundupto(size+NBSZ, ALIGN);
nf_chunk *chunkhead;
objc_mutex_lock(zptr->lock); objc_mutex_lock(zptr->lock);
top = zptr->blocks->top; block = zptr->blocks;
/* No need to worry about (block == NULL), since a nonfreeable zone top = block->top;
always starts with a block. */ freesize = block->size-top;
if (zptr->blocks->size-top >= chunksize) if (freesize >= chunksize)
{ {
chunkhead = (nf_chunk*)((void*)(zptr->blocks)+top); chunkhead = (void*)(block)+top;
zptr->blocks->top += chunksize; block += chunksize;
} }
else else
{ {
size_t freesize = zptr->blocks->size-top; nf_block *preblock;
nf_block *block, *preblock;
/* First, get the block list in decreasing free size order. */ /* First, get the block list in decreasing free size order. */
preblock = NULL; preblock = NULL;
block = zptr->blocks;
while ((block->next != NULL) while ((block->next != NULL)
&& (freesize < block->next->size-block->next->top)) && (freesize < block->next->size-block->next->top))
{ {
@ -1321,13 +1298,12 @@ nmalloc (NSZone *zone, size_t size)
block->top = NF_HEAD; block->top = NF_HEAD;
zptr->blocks = block; zptr->blocks = block;
} }
chunkhead = (nf_chunk*)((void*)block+zptr->blocks->top); chunkhead = (void*)block+block->top;
zptr->blocks->top += chunksize; block->top += chunksize;
} }
chunkhead->cur = size; zptr->use++;
chunkhead->max = chunksize - NBSZ;
objc_mutex_unlock(zptr->lock); objc_mutex_unlock(zptr->lock);
return (void*)&chunkhead[1]; return chunkhead;
} }
/* Return the blocks to the default zone, then deallocate mutex, and /* Return the blocks to the default zone, then deallocate mutex, and
@ -1336,48 +1312,19 @@ static BOOL
nrecycle1 (NSZone *zone) nrecycle1 (NSZone *zone)
{ {
nfree_zone *zptr = (nfree_zone*)zone; nfree_zone *zptr = (nfree_zone*)zone;
nf_block *nextblock;
nf_block *block;
objc_mutex_lock(zptr->lock); objc_mutex_lock(zptr->lock);
block = zptr->blocks; if (zptr->use == 0)
while (block != NULL)
{ {
nf_chunk *chunk = (nf_chunk*)((void*)(block)+NF_HEAD); nf_block *nextblock;
nf_chunk *top = (nf_chunk*)((void*)(block)+block->top); nf_block *block = zptr->blocks;
BOOL isEmpty = NO;
nextblock = block->next; while (block != NULL)
if (chunk >= top)
isEmpty = YES;
if (chunk->max == 0)
{ {
nf_chunk *tmp = (nf_chunk*)((void*)(chunk)+chunk->cur); nextblock = block->next;
objc_free(block);
while (tmp < top && tmp->max == 0) block = nextblock;
{
chunk->cur += tmp->cur;
tmp = (nf_chunk*)((void*)(chunk)+chunk->cur);
}
if (tmp >= top)
isEmpty = YES;
} }
if (isEmpty)
{
if (zptr->blocks == block)
zptr->blocks = block->next;
else
{
nf_block *tmp = zptr->blocks;
while (tmp->next != block)
tmp = tmp->next;
tmp->next = block->next;
}
objc_free(block);
}
block = nextblock;
} }
objc_mutex_unlock(zptr->lock); objc_mutex_unlock(zptr->lock);
if (zptr->blocks == 0) if (zptr->blocks == 0)
@ -1400,15 +1347,13 @@ nrecycle (NSZone *zone)
[name release]; [name release];
} }
if (nrecycle1(zone) == YES) if (nrecycle1(zone) == YES)
objc_free((void*)zone); destroy_zone(zone);
else else
{ {
zone->malloc = rmalloc; zone->malloc = rmalloc;
zone->realloc = rrealloc; zone->realloc = rrealloc;
zone->free = rnfree; zone->free = rnfree;
zone->recycle = rrecycle; zone->recycle = rrecycle;
zone->next = dead_zones;
dead_zones = zone;
} }
[gnustep_global_lock unlock]; [gnustep_global_lock unlock];
} }
@ -1416,27 +1361,34 @@ nrecycle (NSZone *zone)
static void* static void*
nrealloc (NSZone *zone, void *ptr, size_t size) nrealloc (NSZone *zone, void *ptr, size_t size)
{ {
if (ptr == 0) nfree_zone *zptr = (nfree_zone*)zone;
return nmalloc(zone, size); void *tmp = nmalloc(zone, size);
else
if (ptr != 0)
{ {
nf_chunk *old = &((nf_chunk*)ptr)[-1]; objc_mutex_lock(zptr->lock);
if (tmp)
if (old->max >= size)
{ {
old->cur = size; nf_block *block;
return ptr; size_t old = 0;
}
else for (block = zptr->blocks; block != NULL; block = block->next) {
{ if (ptr >= (void*)block && ptr < ((void*)block)+block->size) {
void *tmp = nmalloc(zone, size); old = ((void*)block)+block->size - ptr;
break;
if (tmp) }
memcpy(tmp, ptr, old->cur); }
nfree(zone, ptr); /* Book keeping */ if (old > 0)
return tmp; {
if (size < old)
old = size;
memcpy(tmp, ptr, old);
}
} }
zptr->use--;
objc_mutex_unlock(zptr->lock);
} }
return tmp;
} }
/* /*
@ -1448,39 +1400,24 @@ nrealloc (NSZone *zone, void *ptr, size_t size)
static void static void
nfree (NSZone *zone, void *ptr) nfree (NSZone *zone, void *ptr)
{ {
if (ptr) nfree_zone *zptr = (nfree_zone*)zone;
{
nf_chunk *old = &((nf_chunk*)ptr)[-1];
if (old->max >= old->cur) objc_mutex_lock(zptr->lock);
{ zptr->use--;
old->cur = old->max + NBSZ; objc_mutex_unlock(zptr->lock);
old->max = 0;
}
else
[NSException raise: NSMallocException
format: @"Attempt to free freed memory"];
}
} }
static void static void
rnfree (NSZone *zone, void *ptr) rnfree (NSZone *zone, void *ptr)
{ {
nfree_zone *zptr = (nfree_zone*)zone;
nfree(zone, ptr); nfree(zone, ptr);
if (nrecycle1(zone)) if (zptr->use == 0)
{ {
[gnustep_global_lock lock]; [gnustep_global_lock lock];
if (dead_zones == zone) nrecycle1(zone);
dead_zones = zone->next; destroy_zone(zone);
else
{
NSZone *ptr = dead_zones;
while (ptr->next != zone)
ptr = ptr->next;
ptr->next = zone->next;
}
objc_free((void*)zone);
[gnustep_global_lock unlock]; [gnustep_global_lock unlock];
} }
} }
@ -1684,6 +1621,7 @@ NSCreateZone (size_t start, size_t gran, BOOL canFree)
zone->common.name = nil; zone->common.name = nil;
zone->lock = objc_mutex_allocate(); zone->lock = objc_mutex_allocate();
zone->blocks = objc_malloc(startsize); zone->blocks = objc_malloc(startsize);
zone->use = 0;
if (zone->blocks == NULL) if (zone->blocks == NULL)
{ {
objc_mutex_deallocate(zone->lock); objc_mutex_deallocate(zone->lock);
@ -1700,8 +1638,8 @@ NSCreateZone (size_t start, size_t gran, BOOL canFree)
} }
[gnustep_global_lock lock]; [gnustep_global_lock lock];
newZone->next = live_zones; newZone->next = zone_list;
live_zones = newZone; zone_list = newZone;
[gnustep_global_lock unlock]; [gnustep_global_lock unlock];
return newZone; return newZone;
@ -1745,36 +1683,8 @@ NSZoneRealloc (NSZone *zone, void *ptr, size_t size)
inline void inline void
NSRecycleZone (NSZone *zone) NSRecycleZone (NSZone *zone)
{ {
if (!zone || zone == NSDefaultMallocZone()) if (zone == 0)
{ zone == NSDefaultMallocZone();
(zone->recycle)(zone);
return;
}
[gnustep_global_lock lock];
if (live_zones = zone) {
live_zones = zone->next;
zone->next = 0;
}
else {
NSZone *ptr = live_zones;
while (ptr && ptr->next != zone) {
ptr = ptr->next;
}
if (ptr == 0) {
if (zone->name != nil)
[NSException raise: NSMallocException
format: @"Zone %s not available for recycling",
[zone->name cString]];
else
[NSException raise: NSMallocException
format: @"Zone not available for recycling"];
}
ptr->next = zone->next;
zone->next = 0;
}
[gnustep_global_lock unlock];
(zone->recycle)(zone); (zone->recycle)(zone);
} }