Dealloc of shared memory patches to NSHData (Richard Frith-Macdonald, Sep 16)

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@2427 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Adam Fedor 1997-09-16 15:47:15 +00:00
parent 4a99922d47
commit 8066576c56
3 changed files with 69 additions and 17 deletions

View file

@ -1,3 +1,12 @@
Mon Sep 15 21:27:00 1997 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* src/NSHData.m: Fixed a couple of bugs occurring when allocation of
shared memory segments failed. Also added class methods to query
and set the size of the blocks in which shared memory is allocated.
* src/include/NSHData.h: Added class methods for query and set of size
of shared memory allocation blocks.
Sat Sep 13 10:05:57 1997 Scott Christley <scottc@net-community.com>
* The GNU tools for building a DLL on Windows only allow functions

View file

@ -103,7 +103,10 @@ typedef enum {
The write operations have no effect on an NSData but work as expected
for NSMutableData objects.
*/
+ (void) setVMThreshold:(unsigned int)size;
+ (void) setVMChunk:(int)size;
+ (void) setVMThreshold:(int)size;
+ (int) vmChunk;
+ (int) vmThreshold;
- (void) close;
- (void) flushStream;
@ -166,12 +169,12 @@ typedef enum {
- (void) setStreamEofPosition: (unsigned)i;
- (void) setStreamPosition: (unsigned)i;
- (void) setStreamPosition: (unsigned)i seekMode: (seek_mode_t)mode;
- (void) setVMThreshold:(int)size;
- (char*) streamBuffer; /* Returns null for an NSData object. */
- (unsigned) streamBufferLength;
- (unsigned) streamEofPosition;
- (unsigned) streamPosition;
- (unsigned int) vmThreshold;
/* The following write operations have no effect on an NSData object. */
- (int) writeByte: (unsigned char)b;
@ -179,6 +182,8 @@ typedef enum {
- (int) writeFormat: (NSString*)format, ...;
- (int) writeFormat: (NSString*)format arguments: (va_list)arg;
- (void) writeLine: (NSString*)l;
- (int) vmThreshold;
@end
@interface NSHMutableData : NSHData
@ -200,7 +205,7 @@ typedef enum {
- (void) resetBytesInRange: (NSRange)aRange;
- (void) setData:(NSData*)other;
- (void) setLength:(unsigned int)length;
- (void) setVMThreshold:(unsigned int)size;
- (void) setVMThreshold:(int)size;
- (void) serializeAlignedBytesLength: (unsigned int)length;
- (void) serializeDataAt: (const void*)data
@ -215,6 +220,7 @@ typedef enum {
count: (unsigned int)numInts
atIndex: (unsigned int)location;
- (int) vmThreshold;
- (void) increaseCapacityBy:(unsigned int)length;
@end

View file

@ -111,12 +111,11 @@ o_vscanf (void *stream,
* is set from nsdata_vm_threshold when the object is created.
* This can be modified by using the [-setVMThreshold:] method.
*
* Shared memory is allocated in LARGE chunks. Perhapos the chunk size
* should be a class variable?
* Shared memory is allocated in LARGE chunks. This size can be adjusted
* using the [+setVMChunk:] method.
*/
static int nsdata_vm_threshold = 2048; /* Use shared mem for big buffer. */
#define VM_CHUNK 262144 /* 256 Kbyte chunks */
static int nsdata_vm_chunk = 262144;
/* Making these nested functions (which is what I'd like to do) is
crashing the va_arg stuff in vscanf(). Why? */
@ -782,9 +781,36 @@ static void unchar_func(void *s, int c)
/*
* GNUstep extensions to NSData (for Streaming)
*/
+ (void) setVMThreshold:(unsigned int)s
+ (void) setVMChunk:(int)newValue
{
nsdata_vm_threshold = s;
if (newValue < 256)
{
newValue = 256;
}
if (newValue % 256)
{
newValue = ((newValue >> 8) + 1) << 8;
}
nsdata_vm_chunk = newValue;
}
+ (void) setVMThreshold:(int)newValue
{
if (newValue < 256)
{
newValue = 256;
}
nsdata_vm_threshold = newValue;
}
+ (int)vmChunk
{
return nsdata_vm_chunk;
}
+ (int)vmThreshold
{
return nsdata_vm_threshold;
}
- (void) close
@ -875,6 +901,7 @@ static void unchar_func(void *s, int c)
close(fd);
if (buffer == MAP_FAILED)
{
buffer = 0;
[self dealloc];
return nil;
}
@ -892,8 +919,8 @@ static void unchar_func(void *s, int c)
struct shmid_ds buf;
if ([self isWritable])
if (s % VM_CHUNK)
s = ((s / VM_CHUNK) + 1) * VM_CHUNK;
if (s % nsdata_vm_chunk)
s = ((s / nsdata_vm_chunk) + 1) * nsdata_vm_chunk;
m = shmget(IPC_PRIVATE, s, IPC_CREAT|VM_ACCESS);
if (m == -1) /* Created memory? */
{
@ -902,8 +929,9 @@ static void unchar_func(void *s, int c)
}
buffer = shmat(m, 0, 0);
shmctl(m, IPC_RMID, &buf); /* Mark for later deletion. */
if ((int)buffer == -1) /* Attached memory? */
if (buffer == (char*)-1)
{
buffer = 0;
[self dealloc];
return nil;
}
@ -926,8 +954,9 @@ static void unchar_func(void *s, int c)
return nil;
}
buffer = shmat(m, 0, 0);
if (buffer == 0)
if (buffer == (char*)-1)
{
buffer = 0;
[self dealloc]; /* Unable to attach to memory. */
return nil;
}
@ -1136,7 +1165,12 @@ static void unchar_func(void *s, int c)
return position;
}
- (unsigned int)vmThreshold
- (void)setVMThreshold: (int)size
{
// Do nothing.
}
- (int)vmThreshold
{
return nsdata_vm_threshold;
}
@ -1518,7 +1552,8 @@ static void unchar_func(void *s, int c)
int shmid;
char* b;
if (s % VM_CHUNK) s = ((s / VM_CHUNK) + 1) * VM_CHUNK;
if (s % nsdata_vm_chunk)
s = ((s / nsdata_vm_chunk) + 1) * nsdata_vm_chunk;
shmid = shmget(IPC_PRIVATE, s, IPC_CREAT|VM_ACCESS);
if (shmid == -1) /* Created memory? */
[NSException raise:NSMallocException
@ -1556,8 +1591,10 @@ static void unchar_func(void *s, int c)
[super setStreamEofPosition:i];
}
- (void) setVMThreshold:(unsigned int)s
- (void) setVMThreshold:(int)s
{
if (s < 256)
s = 256;
vm_threshold = s;
/* Force change in memory allocation if appropriate. */
[self setStreamBufferCapacity:size];
@ -1568,7 +1605,7 @@ static void unchar_func(void *s, int c)
return buffer;
}
- (unsigned int)vmThreshold
- (int)vmThreshold
{
return vm_threshold;
}