Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -286,10 +286,12 @@ 4B674404163C395900EB1E59 /* OFMutex.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6743FB163C395900EB1E59 /* OFMutex.m */; }; 4B674405163C395900EB1E59 /* OFRecursiveMutex.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6743FC163C395900EB1E59 /* OFRecursiveMutex.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B674406163C395900EB1E59 /* OFRecursiveMutex.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6743FD163C395900EB1E59 /* OFRecursiveMutex.m */; }; 4B674407163C395900EB1E59 /* OFTLSKey.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6743FE163C395900EB1E59 /* OFTLSKey.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B674408163C395900EB1E59 /* OFTLSKey.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6743FF163C395900EB1E59 /* OFTLSKey.m */; }; + 4B745BA5168B25E600A6C20E /* OFSystemInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4B745BA6168B25E600A6C20E /* OFSystemInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B745BA4168B25E600A6C20E /* OFSystemInfo.m */; }; 4B7FF3B4133CED6200000324 /* OFConditionStillWaitingException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7FF3B2133CED6100000324 /* OFConditionStillWaitingException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B7FF3B5133CED6200000324 /* OFConditionStillWaitingException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FF3B3133CED6100000324 /* OFConditionStillWaitingException.m */; }; 4B837D7916829C5F007A3E83 /* block.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B837D7716829C5F007A3E83 /* block.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B837D7A16829C5F007A3E83 /* instance.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B837D7816829C5F007A3E83 /* instance.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B83F0F4142FDEFD00E4A821 /* OFStreamObserver_kqueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B83F0F2142FDEFD00E4A821 /* OFStreamObserver_kqueue.h */; }; @@ -687,10 +689,12 @@ 4B6EF67F1235358D0076B512 /* PropertiesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PropertiesTests.m; path = tests/PropertiesTests.m; sourceTree = SOURCE_ROOT; }; 4B6EF6801235358D0076B512 /* TestsAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestsAppDelegate.h; path = tests/TestsAppDelegate.h; sourceTree = SOURCE_ROOT; }; 4B6EF6811235358D0076B512 /* TestsAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestsAppDelegate.m; path = tests/TestsAppDelegate.m; sourceTree = SOURCE_ROOT; }; 4B6EF684123535B60076B512 /* TestPlugin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestPlugin.m; path = tests/plugin/TestPlugin.m; sourceTree = SOURCE_ROOT; }; 4B6EF685123535C80076B512 /* test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = test.m; path = tests/objc_sync/test.m; sourceTree = SOURCE_ROOT; }; + 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSystemInfo.h; path = src/OFSystemInfo.h; sourceTree = ""; }; + 4B745BA4168B25E600A6C20E /* OFSystemInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSystemInfo.m; path = src/OFSystemInfo.m; sourceTree = ""; }; 4B7FF3B2133CED6100000324 /* OFConditionStillWaitingException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFConditionStillWaitingException.h; path = src/exceptions/OFConditionStillWaitingException.h; sourceTree = ""; }; 4B7FF3B3133CED6100000324 /* OFConditionStillWaitingException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFConditionStillWaitingException.m; path = src/exceptions/OFConditionStillWaitingException.m; sourceTree = ""; }; 4B837D7716829C5F007A3E83 /* block.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = block.h; path = src/block.h; sourceTree = ""; }; 4B837D7816829C5F007A3E83 /* instance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = instance.h; path = src/instance.h; sourceTree = ""; }; 4B83F0F2142FDEFD00E4A821 /* OFStreamObserver_kqueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFStreamObserver_kqueue.h; path = src/OFStreamObserver_kqueue.h; sourceTree = ""; }; @@ -1177,10 +1181,12 @@ 4BF1BCCB11C9663F0025511F /* OFString+XMLEscaping.m */, 4BF1BCCC11C9663F0025511F /* OFString+XMLUnescaping.h */, 4BF1BCCD11C9663F0025511F /* OFString+XMLUnescaping.m */, 4B552550147AA5DB0003BF47 /* OFString_UTF8.h */, 4B552551147AA5DB0003BF47 /* OFString_UTF8.m */, + 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */, + 4B745BA4168B25E600A6C20E /* OFSystemInfo.m */, 4B6799811099E7C50041064A /* OFTCPSocket.h */, 4B6799821099E7C50041064A /* OFTCPSocket.m */, 4BD653C3143B8489006182F0 /* OFTCPSocket+SOCKS5.h */, 4BD653C4143B8489006182F0 /* OFTCPSocket+SOCKS5.m */, 4B6799831099E7C50041064A /* OFThread.h */, @@ -1399,10 +1405,11 @@ 4B5CF8F914940BD2007AA324 /* OFString+JSONValue.h in Headers */, 4BB25E8A139C388A00F574EA /* OFString+Serialization.h in Headers */, 4B3D23DA1337FCB000DD29B8 /* OFString+URLEncoding.h in Headers */, 4B3D23DB1337FCB000DD29B8 /* OFString+XMLEscaping.h in Headers */, 4B3D23DC1337FCB000DD29B8 /* OFString+XMLUnescaping.h in Headers */, + 4B745BA5168B25E600A6C20E /* OFSystemInfo.h in Headers */, 4B3D23DD1337FCB000DD29B8 /* OFTCPSocket.h in Headers */, 4B3D23DE1337FCB000DD29B8 /* OFThread.h in Headers */, 4B9361A81511000C00DCD16B /* OFThreadPool.h in Headers */, 4B325EDF1605F3A0007836CA /* OFTimer.h in Headers */, 4B674407163C395900EB1E59 /* OFTLSKey.h in Headers */, @@ -1752,10 +1759,11 @@ 4BB25E8B139C388A00F574EA /* OFString+Serialization.m in Sources */, 4B3D23A81337FC0D00DD29B8 /* OFString+URLEncoding.m in Sources */, 4B3D23A91337FC0D00DD29B8 /* OFString+XMLEscaping.m in Sources */, 4B3D23AA1337FC0D00DD29B8 /* OFString+XMLUnescaping.m in Sources */, 4B552555147AA5DB0003BF47 /* OFString_UTF8.m in Sources */, + 4B745BA6168B25E600A6C20E /* OFSystemInfo.m in Sources */, 4B3D23AB1337FC0D00DD29B8 /* OFTCPSocket.m in Sources */, 4BD653C6143B8489006182F0 /* OFTCPSocket+SOCKS5.m in Sources */, 4B3D23AC1337FC0D00DD29B8 /* OFThread.m in Sources */, 4B9361A91511000C00DCD16B /* OFThreadPool.m in Sources */, 4B325EE01605F3A0007836CA /* OFTimer.m in Sources */, Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -51,10 +51,11 @@ OFString+JSONValue.m \ OFString+Serialization.m \ OFString+URLEncoding.m \ OFString+XMLEscaping.m \ OFString+XMLUnescaping.m \ + OFSystemInfo.m \ OFTCPSocket.m \ OFTimer.m \ OFURL.m \ OFXMLAttribute.m \ OFXMLCDATA.m \ Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -25,10 +25,11 @@ #import "OFFile.h" #import "OFURL.h" #import "OFHTTPClient.h" #import "OFHTTPRequest.h" #import "OFXMLElement.h" +#import "OFSystemInfo.h" #import "OFHTTPRequestFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfMemoryException.h" @@ -110,17 +111,18 @@ mode: @"rb"]; itemSize = 1; @try { - char *buffer = [self allocMemoryWithSize: of_pagesize]; + size_t pageSize = [OFSystemInfo pageSize]; + char *buffer = [self allocMemoryWithSize: pageSize]; while (![file isAtEndOfStream]) { size_t length; length = [file readIntoBuffer: buffer - length: of_pagesize]; + length: pageSize]; [self addItems: buffer count: length]; } [self freeMemory: buffer]; @@ -581,11 +583,11 @@ size_t newSize, lastPageByte; if (SIZE_MAX - count < 1 || count + 1 > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - lastPageByte = of_pagesize - 1; + lastPageByte = [OFSystemInfo pageSize] - 1; newSize = ((count + 1) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) items = [self resizeMemory: items size: newSize]; @@ -602,11 +604,11 @@ size_t newSize, lastPageByte; if (count_ > SIZE_MAX - count || count + count_ > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - lastPageByte = of_pagesize - 1; + lastPageByte = [OFSystemInfo pageSize] - 1; newSize = ((count + count_) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) items = [self resizeMemory: items size: newSize]; @@ -625,11 +627,11 @@ if (count_ > SIZE_MAX - count || index > count || count + count_ > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - lastPageByte = of_pagesize - 1; + lastPageByte = [OFSystemInfo pageSize] - 1; newSize = ((count + count_) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) items = [self resizeMemory: items size: newSize]; @@ -653,11 +655,11 @@ memmove(items + range.location * itemSize, items + (range.location + range.length) * itemSize, (count - range.location - range.length) * itemSize); count -= range.length; - lastPageByte = of_pagesize - 1; + lastPageByte = [OFSystemInfo pageSize] - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) items = [self resizeMemory: items size: newSize]; @@ -670,11 +672,11 @@ if (count == 0) return; count--; - lastPageByte = of_pagesize - 1; + lastPageByte = [OFSystemInfo pageSize] - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) { @try { items = [self resizeMemory: items Index: src/OFDate.m ================================================================== --- src/OFDate.m +++ src/OFDate.m @@ -30,10 +30,11 @@ #import "OFDictionary.h" #import "OFXMLElement.h" #ifdef OF_THREADS # import "OFMutex.h" #endif +#import "OFSystemInfo.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" @@ -512,10 +513,11 @@ - (OFString*)dateStringWithFormat: (OFConstantString*)format { OFString *ret; time_t seconds_ = (time_t)seconds; struct tm tm; + size_t pageSize; char *buffer; if (seconds_ != floor(seconds)) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; @@ -540,14 +542,15 @@ [mutex unlock]; } # endif #endif - buffer = [self allocMemoryWithSize: of_pagesize]; + pageSize = [OFSystemInfo pageSize]; + buffer = [self allocMemoryWithSize: pageSize]; @try { - if (!strftime(buffer, of_pagesize, [format UTF8String], &tm)) + if (!strftime(buffer, pageSize, [format UTF8String], &tm)) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; ret = [OFString stringWithUTF8String: buffer]; } @finally { @@ -560,10 +563,11 @@ - (OFString*)localDateStringWithFormat: (OFConstantString*)format { OFString *ret; time_t seconds_ = (time_t)seconds; struct tm tm; + size_t pageSize; char *buffer; if (seconds_ != floor(seconds)) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; @@ -588,14 +592,15 @@ [mutex unlock]; } # endif #endif - buffer = [self allocMemoryWithSize: of_pagesize]; + pageSize = [OFSystemInfo pageSize]; + buffer = [self allocMemoryWithSize: pageSize]; @try { - if (!strftime(buffer, of_pagesize, [format UTF8String], &tm)) + if (!strftime(buffer, pageSize, [format UTF8String], &tm)) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; ret = [OFString stringWithUTF8String: buffer]; } @finally { Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -46,10 +46,11 @@ #ifdef OF_THREADS # import "threading.h" #endif #import "OFDate.h" #import "OFApplication.h" +#import "OFSystemInfo.h" #import "OFChangeDirectoryFailedException.h" #import "OFChangeFileModeFailedException.h" #import "OFChangeFileOwnerFailedException.h" #import "OFCreateDirectoryFailedException.h" @@ -496,22 +497,24 @@ void *pool = objc_autoreleasePoolPush(); BOOL override; OFFile *sourceFile = nil; OFFile *destinationFile = nil; char *buffer; + size_t pageSize; if ([self directoryExistsAtPath: destination]) { OFString *filename = [source lastPathComponent]; destination = [OFString stringWithPath: destination, filename, nil]; } override = [self fileExistsAtPath: destination]; + pageSize = [OFSystemInfo pageSize]; - if ((buffer = malloc(of_pagesize)) == NULL) + if ((buffer = malloc(pageSize)) == NULL) @throw [OFOutOfMemoryException exceptionWithClass: self - requestedSize: of_pagesize]; + requestedSize: pageSize]; @try { sourceFile = [OFFile fileWithPath: source mode: @"rb"]; destinationFile = [OFFile fileWithPath: destination @@ -519,11 +522,11 @@ while (![sourceFile isAtEndOfStream]) { size_t length; length = [sourceFile readIntoBuffer: buffer - length: of_pagesize]; + length: pageSize]; [destinationFile writeBuffer: buffer length: length]; } #if !defined(_WIN32) && !defined(_PSP) Index: src/OFHTTPClient.m ================================================================== --- src/OFHTTPClient.m +++ src/OFHTTPClient.m @@ -24,10 +24,11 @@ #import "OFString.h" #import "OFURL.h" #import "OFTCPSocket.h" #import "OFDictionary.h" #import "OFDataArray.h" +#import "OFSystemInfo.h" #import "OFHTTPRequestFailedException.h" #import "OFInvalidEncodingException.h" #import "OFInvalidFormatException.h" #import "OFInvalidServerReplyException.h" @@ -129,10 +130,11 @@ OFString *key, *object, *contentLengthHeader; int status; const char *type = NULL; size_t contentLength = 0; BOOL chunked; + size_t pageSize; char *buffer; size_t bytesReceived; if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"]) @throw [OFUnsupportedProtocolException @@ -359,11 +361,12 @@ if (contentLength > SIZE_MAX) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; } - buffer = [self allocMemoryWithSize: of_pagesize]; + pageSize = [OFSystemInfo pageSize]; + buffer = [self allocMemoryWithSize: pageSize]; bytesReceived = 0; @try { if (chunked) { for (;;) { void *pool2 = objc_autoreleasePoolPush(); @@ -394,12 +397,12 @@ (contentLengthHeader != nil && contentLength >= bytesReceived)) break; while (toRead > 0) { - size_t length = (toRead < of_pagesize - ? toRead : of_pagesize); + size_t length = (toRead < pageSize + ? toRead : pageSize); length = [sock readIntoBuffer: buffer length: length]; if ([delegate respondsToSelector: @@ -438,11 +441,11 @@ while (![sock isAtEndOfStream]) { void *pool2; length = [sock readIntoBuffer: buffer - length: of_pagesize]; + length: pageSize]; pool2 = objc_autoreleasePoolPush(); if ([delegate respondsToSelector: @selector(client:didReceiveData:length: Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -885,11 +885,9 @@ #ifdef __cplusplus extern "C" { #endif extern id of_alloc_object(Class class_, size_t extraSize, size_t extraAlignment, void **extra); -extern size_t of_pagesize; -extern size_t of_num_cpus; extern uint32_t of_hash_seed; #ifdef __cplusplus } #endif Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -14,26 +14,18 @@ * file. */ #include "config.h" -#define __NO_EXT_QNX - #include #include #include -#include - #include #include -#ifdef __QNX__ -# include -#endif - #import "OFObject.h" #import "OFTimer.h" #import "OFThread.h" #import "OFRunLoop.h" #import "OFAutoreleasePool.h" @@ -97,12 +89,10 @@ static struct { Class isa; } alloc_failed_exception; -size_t of_pagesize; -size_t of_num_cpus; uint32_t of_hash_seed; #if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__) static void uncaught_exception_handler(id exception) @@ -260,30 +250,10 @@ #ifdef HAVE_OBJC_ENUMERATIONMUTATION objc_setEnumerationMutationHandler(enumeration_mutation_handler); #endif -#if defined(_WIN32) - SYSTEM_INFO si; - GetSystemInfo(&si); - of_pagesize = si.dwPageSize; - of_num_cpus = si.dwNumberOfProcessors; -#elif defined(__QNX__) - if ((of_pagesize = sysconf(_SC_PAGESIZE)) < 1) - of_pagesize = 4096; - of_num_cpus = _syspage_ptr->num_cpu; -#else -# ifdef _SC_PAGESIZE - if ((of_pagesize = sysconf(_SC_PAGESIZE)) < 1) -# endif - of_pagesize = 4096; -# ifdef _SC_NPROCESSORS_CONF - if ((of_num_cpus = sysconf(_SC_NPROCESSORS_CONF)) < 1) -# endif - of_num_cpus = 1; -#endif - #if defined(OF_HAVE_ARC4RANDOM) of_hash_seed = arc4random(); #elif defined(OF_HAVE_RANDOM) struct timeval t; gettimeofday(&t, NULL); Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -32,10 +32,11 @@ #endif #import "OFStream.h" #import "OFString.h" #import "OFDataArray.h" +#import "OFSystemInfo.h" #import "OFRunLoop.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFSetOptionFailedException.h" @@ -512,21 +513,23 @@ } - (OFDataArray*)readDataArrayTillEndOfStream { OFDataArray *dataArray; + size_t pageSize; char *buffer; dataArray = [OFDataArray dataArray]; - buffer = [self allocMemoryWithSize: of_pagesize]; + pageSize = [OFSystemInfo pageSize]; + buffer = [self allocMemoryWithSize: pageSize]; @try { while (![self isAtEndOfStream]) { size_t length; length = [self readIntoBuffer: buffer - length: of_pagesize]; + length: pageSize]; [dataArray addItems: buffer count: length]; } } @finally { [self freeMemory: buffer]; @@ -561,11 +564,11 @@ return ret; } - (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding { - size_t i, bufferLength, retLength; + size_t i, pageSize, bufferLength, retLength; char *retCString, *buffer, *newCache; OFString *ret; /* Look if there's a line or \0 in our cache */ if (!waitingForDelimiter && cache != NULL) { @@ -594,12 +597,13 @@ return ret; } } } - /* Read and see if we get a newline or \0 */ - buffer = [self allocMemoryWithSize: of_pagesize]; + /* Read and see if we got a newline or \0 */ + pageSize = [OFSystemInfo pageSize]; + buffer = [self allocMemoryWithSize: pageSize]; @try { if ([self lowlevelIsAtEndOfStream]) { if (cache == NULL) { waitingForDelimiter = NO; @@ -622,11 +626,11 @@ waitingForDelimiter = NO; return ret; } bufferLength = [self lowlevelReadIntoBuffer: buffer - length: of_pagesize]; + length: pageSize]; /* Look if there's a newline or \0 */ for (i = 0; i < bufferLength; i++) { if OF_UNLIKELY (buffer[i] == '\n' || buffer[i] == '\0') { @@ -763,11 +767,11 @@ - (OFString*)tryReadTillDelimiter: (OFString*)delimiter encoding: (of_string_encoding_t)encoding { const char *delimiterCString; - size_t i, j, delimiterLength, bufferLength, retLength; + size_t i, j, delimiterLength, pageSize, bufferLength, retLength; char *retCString, *buffer, *newCache; OFString *ret; delimiterCString = [delimiter cStringUsingEncoding: encoding]; delimiterLength = [delimiter lengthOfBytesUsingEncoding: encoding]; @@ -807,12 +811,13 @@ return ret; } } } - /* Read and see if we get a delimiter or \0 */ - buffer = [self allocMemoryWithSize: of_pagesize]; + /* Read and see if we got a delimiter or \0 */ + pageSize = [OFSystemInfo pageSize]; + buffer = [self allocMemoryWithSize: pageSize]; @try { if ([self lowlevelIsAtEndOfStream]) { if (cache == NULL) { waitingForDelimiter = NO; @@ -830,11 +835,11 @@ waitingForDelimiter = NO; return ret; } bufferLength = [self lowlevelReadIntoBuffer: buffer - length: of_pagesize]; + length: pageSize]; /* Look if there's a delimiter or \0 */ for (i = 0; i < bufferLength; i++) { if (buffer[i] != delimiterCString[j++]) j = 0; ADDED src/OFSystemInfo.h Index: src/OFSystemInfo.h ================================================================== --- src/OFSystemInfo.h +++ src/OFSystemInfo.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFObject.h" + +/*! + * @brief A class for querying information about the system. + */ +@interface OFSystemInfo: OFObject +/*! + * @brief Returns the size of a page. + * + * @return The size of a page + */ ++ (size_t)pageSize; + +/*! + * @brief Returns the number of CPUs installed in the system. + * + * A CPU with multiple cores counts as multiple CPUs. + * + * @return The number of CPUs installed in the system + */ ++ (size_t)numberOfCPUs; +@end ADDED src/OFSystemInfo.m Index: src/OFSystemInfo.m ================================================================== --- src/OFSystemInfo.m +++ src/OFSystemInfo.m @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#define __NO_EXT_QNX + +#import "OFSystemInfo.h" + +#include + +#include + +#ifdef __QNX__ +# include +#endif + +static size_t pageSize; +static size_t numberOfCPUs; + +@implementation OFSystemInfo ++ (void)initialize +{ + if (self != [OFSystemInfo class]) + return; + +#if defined(_WIN32) + SYSTEM_INFO si; + GetSystemInfo(&si); + pageSize = si.dwPageSize; + numberOfCPUs = si.dwNumberOfProcessors; +#elif defined(__QNX__) + if ((pageSize = sysconf(_SC_PAGESIZE)) < 1) + pageSize = 4096; + numberOfCPUs = _syspage_ptr->num_cpu; +#else +# ifdef _SC_PAGESIZE + if ((pageSize = sysconf(_SC_PAGESIZE)) < 1) +# endif + numberOfCPUs = 4096; +# ifdef _SC_NPROCESSORS_CONF + if ((numberOfCPUs = sysconf(_SC_NPROCESSORS_CONF)) < 1) +# endif + numberOfCPUs = 1; +#endif +} + ++ alloc +{ + [self doesNotRecognizeSelector: _cmd]; + abort(); +} + ++ (size_t)pageSize +{ + return pageSize; +} + ++ (size_t)numberOfCPUs +{ + return numberOfCPUs; +} +@end Index: src/OFThreadPool.m ================================================================== --- src/OFThreadPool.m +++ src/OFThreadPool.m @@ -19,10 +19,11 @@ #import "OFThreadPool.h" #import "OFArray.h" #import "OFList.h" #import "OFThread.h" #import "OFCondition.h" +#import "OFSystemInfo.h" #import "autorelease.h" @interface OFThreadPoolJob: OFObject { @@ -252,11 +253,11 @@ return [[[self alloc] initWithSize: size] autorelease]; } - init { - return [self initWithSize: of_num_cpus]; + return [self initWithSize: [OFSystemInfo numberOfCPUs]]; } - initWithSize: (size_t)size_ { self = [super init]; Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -26,10 +26,11 @@ #import "OFDictionary.h" #import "OFDataArray.h" #import "OFXMLAttribute.h" #import "OFStream.h" #import "OFFile.h" +#import "OFSystemInfo.h" #import "OFInitializationFailedException.h" #import "OFMalformedXMLException.h" #import "OFUnboundNamespaceException.h" @@ -277,16 +278,17 @@ length: [string UTF8StringLength]]; } - (void)parseStream: (OFStream*)stream { - char *buffer = [self allocMemoryWithSize: of_pagesize]; + size_t pageSize = [OFSystemInfo pageSize]; + char *buffer = [self allocMemoryWithSize: pageSize]; @try { while (![stream isAtEndOfStream]) { size_t length = [stream readIntoBuffer: buffer - length: of_pagesize]; + length: pageSize]; [self parseBuffer: buffer length: length]; } } @finally { Index: src/autorelease.m ================================================================== --- src/autorelease.m +++ src/autorelease.m @@ -18,10 +18,11 @@ #include #include #import "OFObject.h" +#import "OFSystemInfo.h" #ifndef OF_COMPILER_TLS # import "threading.h" #endif #import "macros.h" @@ -92,14 +93,15 @@ id *objects = of_tlskey_get(objectsKey); size_t size = (size_t)(uintptr_t)of_tlskey_get(sizeKey); #endif if (objects == NULL) { - OF_ENSURE((objects = malloc(of_pagesize)) != NULL); + size = [OFSystemInfo pageSize]; + + OF_ENSURE((objects = malloc(size)) != NULL); top = objects; - size = of_pagesize; #ifndef OF_COMPILER_TLS OF_ENSURE(of_tlskey_set(objectsKey, objects)); OF_ENSURE(of_tlskey_set(sizeKey, (void*)(uintptr_t)size)); #endif @@ -106,11 +108,11 @@ } if ((uintptr_t)top >= (uintptr_t)objects + size) { ptrdiff_t diff = top - objects; - size += of_pagesize; + size += [OFSystemInfo pageSize]; OF_ENSURE((objects = realloc(objects, size)) != NULL); #ifndef OF_COMPILER_TLS OF_ENSURE(of_tlskey_set(objectsKey, objects)); OF_ENSURE(of_tlskey_set(sizeKey, (void*)(uintptr_t)size)); Index: tests/OFStreamTests.m ================================================================== --- tests/OFStreamTests.m +++ tests/OFStreamTests.m @@ -18,10 +18,11 @@ #include #import "OFStream.h" #import "OFString.h" +#import "OFSystemInfo.h" #import "OFAutoreleasePool.h" #import "TestsAppDelegate.h" static OFString *module = @"OFStream"; @@ -39,10 +40,12 @@ } - (size_t)lowlevelReadIntoBuffer: (void*)buffer length: (size_t)size { + size_t pageSize = [OFSystemInfo pageSize]; + switch (state) { case 0: if (size < 1) return 0; @@ -49,18 +52,18 @@ memcpy(buffer, "f", 1); state++; return 1; case 1: - if (size < of_pagesize) + if (size < pageSize) return 0; memcpy(buffer, "oo\n", 3); - memset((char*)buffer + 3, 'X', of_pagesize - 3); + memset((char*)buffer + 3, 'X', pageSize - 3); state++; - return of_pagesize; + return pageSize; } return 0; } @end @@ -67,20 +70,21 @@ @implementation TestsAppDelegate (OFStreamTests) - (void)streamTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + size_t pageSize = [OFSystemInfo pageSize]; StreamTester *t = [[[StreamTester alloc] init] autorelease]; OFString *str; char *cstr; - cstr = [t allocMemoryWithSize: of_pagesize - 2]; - memset(cstr, 'X', of_pagesize - 3); - cstr[of_pagesize - 3] = '\0'; + cstr = [t allocMemoryWithSize: pageSize - 2]; + memset(cstr, 'X', pageSize - 3); + cstr[pageSize - 3] = '\0'; TEST(@"-[readLine]", [[t readLine] isEqual: @"foo"] && - [(str = [t readLine]) length] == of_pagesize - 3 && + [(str = [t readLine]) length] == pageSize - 3 && !strcmp([str UTF8String], cstr)) [pool drain]; } @end