Memory allocation update.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@14662 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2002-10-07 13:46:22 +00:00
parent 5ed508ae34
commit 2b7ef209d6
3 changed files with 57 additions and 16 deletions

View file

@ -1,3 +1,10 @@
2002-10-07 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSData.m: Update logging after checks for results of memory
allocation.
* Source/NSTimeZone.m: Don't abort on failure to allocate memory ...
just print a log and continue returning a null pointer.
2002-10-06 Richard Frith-Macdonald <rfm@gnu.org> 2002-10-06 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSData.m: readContentsOfFile() fix use of atomic memory * Source/NSData.m: readContentsOfFile() fix use of atomic memory

View file

@ -181,9 +181,9 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone)
tmp = NSZoneMalloc(zone, fileLength); tmp = NSZoneMalloc(zone, fileLength);
if (tmp == 0) if (tmp == 0)
{ {
NSWarnFLog(@"Malloc failed for file (%s) of length %d - %s",
thePath, fileLength, GSLastErrorStr(errno));
CloseHandle(fh); CloseHandle(fh);
NSLog(@"Malloc failed for file (%s) of length %d - %s",
thePath, fileLength, GSLastErrorStr(errno));
return NO; return NO;
} }
if (!ReadFile(fh, tmp, fileLength, &got, 0)) if (!ReadFile(fh, tmp, fileLength, &got, 0))
@ -275,7 +275,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone)
} }
if (tmp == 0) if (tmp == 0)
{ {
NSWarnFLog(@"Malloc failed for file (%s) of length %d - %s", NSLog(@"Malloc failed for file (%s) of length %d - %s",
thePath, fileLength + c, GSLastErrorStr(errno)); thePath, fileLength + c, GSLastErrorStr(errno));
goto failure; goto failure;
} }
@ -288,7 +288,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone)
tmp = NSZoneMalloc(zone, fileLength); tmp = NSZoneMalloc(zone, fileLength);
if (tmp == 0) if (tmp == 0)
{ {
NSWarnFLog(@"Malloc failed for file (%s) of length %d - %s", NSLog(@"Malloc failed for file (%s) of length %d - %s",
thePath, fileLength, GSLastErrorStr(errno)); thePath, fileLength, GSLastErrorStr(errno));
goto failure; goto failure;
} }
@ -312,9 +312,13 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone)
*/ */
failure: failure:
if (tmp != 0) if (tmp != 0)
NSZoneFree(zone, tmp); {
NSZoneFree(zone, tmp);
}
if (theFile != 0) if (theFile != 0)
fclose(theFile); {
fclose(theFile);
}
return NO; return NO;
} }
@ -555,6 +559,11 @@ failure:
if (bufferSize > 0) if (bufferSize > 0)
{ {
ptr = NSZoneMalloc(NSDefaultMallocZone(), bufferSize); ptr = NSZoneMalloc(NSDefaultMallocZone(), bufferSize);
if (ptr == 0)
{
DESTROY(self);
return nil;
}
memcpy(ptr, aBuffer, bufferSize); memcpy(ptr, aBuffer, bufferSize);
} }
return [self initWithBytesNoCopy: ptr return [self initWithBytesNoCopy: ptr
@ -775,8 +784,10 @@ failure:
buffer = NSZoneMalloc([self zone], aRange.length); buffer = NSZoneMalloc([self zone], aRange.length);
#endif #endif
if (buffer == 0) if (buffer == 0)
[NSException raise: NSMallocException {
format: @"No memory for subdata of NSData object"]; [NSException raise: NSMallocException
format: @"No memory for subdata of NSData object"];
}
[self getBytes: buffer range: aRange]; [self getBytes: buffer range: aRange];
return [NSData dataWithBytesNoCopy: buffer length: aRange.length]; return [NSData dataWithBytesNoCopy: buffer length: aRange.length];
@ -1163,6 +1174,11 @@ failure:
#else #else
*(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len); *(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len);
#endif #endif
if (*(char**)data == 0)
{
[NSException raise: NSMallocException
format: @"out of memory to deserialize bytes"];
}
} }
[self deserializeBytes: *(char**)data [self deserializeBytes: *(char**)data
@ -1230,6 +1246,11 @@ failure:
#else #else
*(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len); *(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len);
#endif #endif
if (*(char**)data == 0)
{
[NSException raise: NSMallocException
format: @"out of memory to deserialize bytes"];
}
[self deserializeDataAt: *(char**)data [self deserializeDataAt: *(char**)data
ofObjCType: type ofObjCType: type
atCursor: cursor atCursor: cursor
@ -2365,6 +2386,11 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
#else #else
*(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len+1); *(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len+1);
#endif #endif
if (*(char**)data == 0)
{
[NSException raise: NSMallocException
format: @"out of memory to deserialize bytes"];
}
} }
getBytes(*(void**)data, bytes, len, length, cursor); getBytes(*(void**)data, bytes, len, length, cursor);
(*(char**)data)[len] = '\0'; (*(char**)data)[len] = '\0';
@ -2428,6 +2454,11 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
*(char**)data = (char*)NSZoneMalloc(NSDefaultMallocZone(), len); *(char**)data = (char*)NSZoneMalloc(NSDefaultMallocZone(), len);
#else #else
*(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len); *(char**)data = (char*)NSZoneMalloc(GSAtomicMallocZone(), len);
if (*(char**)data == 0)
{
[NSException raise: NSMallocException
format: @"out of memory to deserialize bytes"];
}
#endif #endif
[self deserializeDataAt: *(char**)data [self deserializeDataAt: *(char**)data
ofObjCType: type ofObjCType: type
@ -3312,7 +3343,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return; return;
} }
default: default:
[NSException raise: NSGenericException [NSException raise: NSMallocException
format: @"Unknown type to serialize - '%s'", type]; format: @"Unknown type to serialize - '%s'", type];
} }
} }
@ -3399,6 +3430,11 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
zone = GSObjCZone(self); zone = GSObjCZone(self);
#endif #endif
tmp = NSZoneMalloc(zone, size); tmp = NSZoneMalloc(zone, size);
if (tmp == 0)
{
[NSException raise: NSMallocException
format: @"Unable to set data capacity to '%d'", size];
}
memcpy(tmp, bytes, capacity < size ? capacity : size); memcpy(tmp, bytes, capacity < size ? capacity : size);
} }
else else

View file

@ -96,17 +96,15 @@
#include <Foundation/NSLock.h> #include <Foundation/NSLock.h>
/* /*
* Try to get more memory - the normal process has failed. * Try to get more memory - the normal process has failed.
* If we can't do anything, bomb out. * If we can't do anything, just return a null pointer.
* Try to do some logging if possible.
*/ */
void * void *
GSOutOfMemory(size_t size, BOOL retry) GSOutOfMemory(size_t size, BOOL retry)
{ {
/* fprintf(stderr, "GSOutOfMemory ... wanting %u bytes.\n", size);
* It would be nice to raise an exception - but how can we if there is return 0;
* no memory available?
*/
abort();
} }
#if GS_WITH_GC == 0 #if GS_WITH_GC == 0