mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
implement latest OSX changes to NSProcessInfo
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@27032 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
353b1d75c7
commit
b3207e1550
5 changed files with 170 additions and 39 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
|||
2008-10-13 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/NSPage.m: Update to use NSUInteger and to cope with large
|
||||
address space in windows.
|
||||
* Headers/Foundation/NSObjCRuntime.h: typedef NSUInteger earlier
|
||||
* Headers/Foundation/NSZone.h: Update to use NSUInteger.
|
||||
* Source/NSProcessInfo.m; implement new MacOS-x 5 methods based on
|
||||
Fred's suggestion and Scotts code as fallback and wiht a version
|
||||
for mswindows too.
|
||||
|
||||
2008-10-11 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/NSUserDefaults.m: implement KVC methods to get and set values
|
||||
|
|
|
@ -31,15 +31,18 @@
|
|||
#import <GNUstepBase/GSVersionMacros.h>
|
||||
#import <GNUstepBase/preface.h>
|
||||
#import <GNUstepBase/GSConfig.h>
|
||||
|
||||
/* These typedefs must be in place before GSObjCRuntime.h is imported.
|
||||
*/
|
||||
typedef gsaddr NSInteger;
|
||||
typedef gsuaddr NSUInteger;
|
||||
|
||||
#import <GNUstepBase/GSObjCRuntime.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef gsaddr NSInteger;
|
||||
typedef gsuaddr NSUInteger;
|
||||
|
||||
#if OS_API_VERSION(100500,GS_API_LATEST)
|
||||
GS_EXPORT NSString *NSStringFromProtocol(Protocol *aProtocol);
|
||||
GS_EXPORT Protocol *NSProtocolFromString(NSString *aProtocolName);
|
||||
|
|
|
@ -434,23 +434,23 @@ GS_ZONE_SCOPE struct NSZoneStats NSZoneStats (NSZone *zone)
|
|||
#endif /* GS_WITH_GC */
|
||||
|
||||
|
||||
GS_EXPORT unsigned NSPageSize (void) __attribute__ ((const));
|
||||
GS_EXPORT NSUInteger NSPageSize (void) __attribute__ ((const));
|
||||
|
||||
GS_EXPORT unsigned NSLogPageSize (void) __attribute__ ((const));
|
||||
GS_EXPORT NSUInteger NSLogPageSize (void) __attribute__ ((const));
|
||||
|
||||
GS_EXPORT unsigned NSRoundDownToMultipleOfPageSize (unsigned bytes)
|
||||
GS_EXPORT NSUInteger NSRoundDownToMultipleOfPageSize (NSUInteger bytes)
|
||||
__attribute__ ((const));
|
||||
|
||||
GS_EXPORT unsigned NSRoundUpToMultipleOfPageSize (unsigned bytes)
|
||||
GS_EXPORT NSUInteger NSRoundUpToMultipleOfPageSize (NSUInteger bytes)
|
||||
__attribute__ ((const));
|
||||
|
||||
GS_EXPORT unsigned NSRealMemoryAvailable (void);
|
||||
GS_EXPORT NSUInteger NSRealMemoryAvailable (void);
|
||||
|
||||
GS_EXPORT void* NSAllocateMemoryPages (unsigned bytes);
|
||||
GS_EXPORT void* NSAllocateMemoryPages (NSUInteger bytes);
|
||||
|
||||
GS_EXPORT void NSDeallocateMemoryPages (void *ptr, unsigned bytes);
|
||||
GS_EXPORT void NSDeallocateMemoryPages (void *ptr, NSUInteger bytes);
|
||||
|
||||
GS_EXPORT void NSCopyMemoryPages (const void *src, void *dest, unsigned bytes);
|
||||
GS_EXPORT void NSCopyMemoryPages (const void *src, void *dest, NSUInteger bytes);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
|
|
@ -72,27 +72,27 @@ getpagesize(void)
|
|||
|
||||
/* Cache the size of a memory page here, so we don't have to make the
|
||||
getpagesize() system call repeatedly. */
|
||||
static unsigned ns_page_size = 0;
|
||||
static NSUInteger ns_page_size = 0;
|
||||
|
||||
/**
|
||||
* Return the number of bytes in a memory page.
|
||||
*/
|
||||
unsigned
|
||||
NSUInteger
|
||||
NSPageSize (void)
|
||||
{
|
||||
if (!ns_page_size)
|
||||
ns_page_size = (unsigned) getpagesize ();
|
||||
ns_page_size = getpagesize ();
|
||||
return ns_page_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return log base 2 of the number of bytes in a memory page.
|
||||
*/
|
||||
unsigned
|
||||
NSUInteger
|
||||
NSLogPageSize (void)
|
||||
{
|
||||
unsigned tmp_page_size = NSPageSize();
|
||||
unsigned log = 0;
|
||||
NSUInteger tmp_page_size = NSPageSize();
|
||||
NSUInteger log = 0;
|
||||
|
||||
while (tmp_page_size >>= 1)
|
||||
log++;
|
||||
|
@ -103,10 +103,10 @@ NSLogPageSize (void)
|
|||
* Round bytes down to the nearest multiple of the memory page size,
|
||||
* and return it.
|
||||
*/
|
||||
unsigned
|
||||
NSRoundDownToMultipleOfPageSize (unsigned bytes)
|
||||
NSUInteger
|
||||
NSRoundDownToMultipleOfPageSize (NSUInteger bytes)
|
||||
{
|
||||
unsigned a = NSPageSize();
|
||||
NSUInteger a = NSPageSize();
|
||||
|
||||
return (bytes / a) * a;
|
||||
}
|
||||
|
@ -115,10 +115,10 @@ NSRoundDownToMultipleOfPageSize (unsigned bytes)
|
|||
* Round bytes up to the nearest multiple of the memory page size,
|
||||
* and return it.
|
||||
*/
|
||||
unsigned
|
||||
NSRoundUpToMultipleOfPageSize (unsigned bytes)
|
||||
NSUInteger
|
||||
NSRoundUpToMultipleOfPageSize (NSUInteger bytes)
|
||||
{
|
||||
unsigned a = NSPageSize();
|
||||
NSUInteger a = NSPageSize();
|
||||
|
||||
return ((bytes % a) ? ((bytes / a + 1) * a) : bytes);
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ NSRoundUpToMultipleOfPageSize (unsigned bytes)
|
|||
/**
|
||||
* Return the number of bytes of real (physical) memory available.
|
||||
*/
|
||||
unsigned
|
||||
NSUInteger
|
||||
NSRealMemoryAvailable ()
|
||||
{
|
||||
#if __linux__
|
||||
|
@ -138,18 +138,19 @@ NSRealMemoryAvailable ()
|
|||
|
||||
if ((sysinfo(&info)) != 0)
|
||||
return 0;
|
||||
return (unsigned) info.freeram;
|
||||
return info.freeram;
|
||||
#elif defined(__MINGW32__)
|
||||
MEMORYSTATUS memory;
|
||||
MEMORYSTATUSEX memory;
|
||||
|
||||
GlobalMemoryStatus(&memory);
|
||||
return (unsigned)memory.dwAvailPhys;
|
||||
memory.dwLength = sizeof(memory);
|
||||
GlobalMemoryStatusEx(&memory);
|
||||
return memory.ullAvailPhys;
|
||||
#elif defined(__BEOS__)
|
||||
system_info info;
|
||||
|
||||
if (get_system_info(&info) != B_OK)
|
||||
return 0;
|
||||
return (unsigned)(info.max_pages - info.used_pages) * B_PAGE_SIZE;
|
||||
return (info.max_pages - info.used_pages) * B_PAGE_SIZE;
|
||||
#else
|
||||
fprintf (stderr, "NSRealMemoryAvailable() not implemented.\n");
|
||||
return 0;
|
||||
|
@ -161,7 +162,7 @@ NSRealMemoryAvailable ()
|
|||
* pointer on failure).
|
||||
*/
|
||||
void *
|
||||
NSAllocateMemoryPages (unsigned bytes)
|
||||
NSAllocateMemoryPages (NSUInteger bytes)
|
||||
{
|
||||
void *where;
|
||||
#if __mach__
|
||||
|
@ -184,7 +185,7 @@ NSAllocateMemoryPages (unsigned bytes)
|
|||
* NSAllocateMemoryPages() function.
|
||||
*/
|
||||
void
|
||||
NSDeallocateMemoryPages (void *ptr, unsigned bytes)
|
||||
NSDeallocateMemoryPages (void *ptr, NSUInteger bytes)
|
||||
{
|
||||
#if __mach__
|
||||
vm_deallocate (mach_task_self (), ptr, bytes);
|
||||
|
@ -198,7 +199,7 @@ NSDeallocateMemoryPages (void *ptr, unsigned bytes)
|
|||
* The value bytes specifies the length of the data copied.
|
||||
*/
|
||||
void
|
||||
NSCopyMemoryPages (const void *src, void *dest, unsigned bytes)
|
||||
NSCopyMemoryPages (const void *src, void *dest, NSUInteger bytes)
|
||||
{
|
||||
#if __mach__
|
||||
kern_return_t r;
|
||||
|
|
|
@ -1185,27 +1185,144 @@ static void determineOperatingSystem()
|
|||
|
||||
- (void) setProcessName: (NSString *)newName
|
||||
{
|
||||
if (newName && [newName length]) {
|
||||
[_gnu_processName autorelease];
|
||||
_gnu_processName = [newName copyWithZone: [self zone]];
|
||||
}
|
||||
if (newName && [newName length])
|
||||
{
|
||||
[_gnu_processName autorelease];
|
||||
_gnu_processName = [newName copyWithZone: [self zone]];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
- (NSUInteger) processorCount
|
||||
{
|
||||
return 0; // FIXME
|
||||
static NSUInteger procCount = 0;
|
||||
static BOOL beenHere = NO;
|
||||
|
||||
if (beenHere == NO)
|
||||
{
|
||||
#if defined(__MINGW32__)
|
||||
SYSTEM_INFO info;
|
||||
|
||||
GetSystemInfo(&info);
|
||||
return info.dwNumberOfProcessors;
|
||||
#elif defined(_SC_NPROCESSORS_CONF)
|
||||
procCount = sysconf(_SC_NPROCESSORS_CONF);
|
||||
#elif defined(HAVE_PROCFS)
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
|
||||
if ([fileManager fileExistsAtPath: @"/proc/cpuinfo"])
|
||||
{
|
||||
NSString *cpuInfo;
|
||||
NSArray *a;
|
||||
unsigned i;
|
||||
|
||||
cpuInfo = [NSString stringWithContentsOfFile: @"/proc/cpuinfo"];
|
||||
a = [cpuInfo componentsSeparatedByCharactersInSet:
|
||||
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
// syntax is processor : #
|
||||
// count up each one
|
||||
for (i = 0; i < [a count]; ++i)
|
||||
{
|
||||
if ([[a objectAtIndex: i] isEqualToString: @"processor"])
|
||||
{
|
||||
if (((i+1) < [a count])
|
||||
&& [[a objectAtIndex: i+1] isEqualToString: @":"])
|
||||
{
|
||||
procCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
#warning "no known way to determine number of processors on this system"
|
||||
#endif
|
||||
|
||||
beenHere = YES;
|
||||
if (procCount == 0)
|
||||
{
|
||||
NSLog(@"Cannot determine processor count.");
|
||||
}
|
||||
}
|
||||
return procCount;
|
||||
}
|
||||
|
||||
- (NSUInteger) activeProcessorCount
|
||||
{
|
||||
return 0; // FIXME
|
||||
#if defined(__MINGW32__)
|
||||
SYSTEM_INFO info;
|
||||
int index;
|
||||
int count = 0;
|
||||
|
||||
GetSystemInfo(&info);
|
||||
for (index = 0; index < 32; index++)
|
||||
{
|
||||
if (info.dwActiveProcessorMask & (1<<index))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
#elif defined(_SC_NPROCESSORS_ONLN)
|
||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#else
|
||||
return [self processorCount];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (unsigned long long) physicalMemory
|
||||
{
|
||||
return 0; // FIXME
|
||||
static NSUInteger availMem = 0;
|
||||
static BOOL beenHere = NO;
|
||||
|
||||
if (beenHere == NO)
|
||||
{
|
||||
#if defined(__MINGW32__)
|
||||
MEMORYSTATUSEX memory;
|
||||
|
||||
memory.dwLength = sizeof(memory);
|
||||
GlobalMemoryStatusEx(&memory);
|
||||
return memory.ullTotalPhys;
|
||||
#elif defined(_SC_PHYS_PAGES)
|
||||
availMem = sysconf(_SC_PHYS_PAGES) * NSPageSize();
|
||||
#elif defined(HAVE_PROCFS)
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
|
||||
if ([fileManager fileExistsAtPath: @"/proc/meminfo"])
|
||||
{
|
||||
NSString *memInfo;
|
||||
NSString *s;
|
||||
NSArray *a;
|
||||
NSRange r;
|
||||
|
||||
memInfo = [NSString stringWithContentsOfFile: @"/proc/meminfo"];
|
||||
r = [memInfo rangeOfString: @"MemTotal:"];
|
||||
|
||||
if (r.location == NSNotFound)
|
||||
{
|
||||
NSLog(@"Cannot determine amount of physical memory.");
|
||||
return 0;
|
||||
}
|
||||
s = [[memInfo substringFromIndex: (r.location + r.length)]
|
||||
stringByTrimmingCharactersInSet:
|
||||
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
a = [s componentsSeparatedByString: @" "];
|
||||
s = [a objectAtIndex: 0];
|
||||
availMem = (NSUInteger)[s longLongValue];
|
||||
availMem *= NSPageSize();
|
||||
}
|
||||
#else
|
||||
#warning "no known way to determine amount of memory on this system"
|
||||
#endif
|
||||
|
||||
beenHere = YES;
|
||||
if (availMem == 0)
|
||||
{
|
||||
NSLog(@"Cannot determine amount of physical memory.");
|
||||
}
|
||||
}
|
||||
return availMem;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSProcessInfo (GNUstep)
|
||||
|
|
Loading…
Reference in a new issue