From 9ce6f3cc46de489621329c22eb0c5e25a5580a3e Mon Sep 17 00:00:00 2001 From: rfm Date: Mon, 12 Jan 2009 21:35:51 +0000 Subject: [PATCH] Add new class from macos-x git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@27585 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 8 ++ Headers/Foundation/Foundation.h | 1 + Headers/Foundation/NSGarbageCollector.h | 90 ++++++++++++++++ Source/GNUmakefile | 2 + Source/NSGarbageCollector.m | 137 ++++++++++++++++++++++++ 5 files changed, 238 insertions(+) create mode 100644 Headers/Foundation/NSGarbageCollector.h create mode 100644 Source/NSGarbageCollector.m diff --git a/ChangeLog b/ChangeLog index 9617507ae..2e6713ce2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2009-01-12 Richard Frith-Macdonald + + * Source/GNUmakefile: + * Headers/Foundation/NSGarbageCollector.h: + * Source/NSGarbageCollector.m: + * Headers/Foundation/Foundation.h: + Add new MacOS-X class. + 2009-01-12 Richard Frith-Macdonald * Source/NSSocketPort.m: diff --git a/Headers/Foundation/Foundation.h b/Headers/Foundation/Foundation.h index e36579245..276b77738 100644 --- a/Headers/Foundation/Foundation.h +++ b/Headers/Foundation/Foundation.h @@ -65,6 +65,7 @@ #import #import #import +#import #import #import #import diff --git a/Headers/Foundation/NSGarbageCollector.h b/Headers/Foundation/NSGarbageCollector.h new file mode 100644 index 000000000..6bf7ff012 --- /dev/null +++ b/Headers/Foundation/NSGarbageCollector.h @@ -0,0 +1,90 @@ +/** Interface for NSGarbageCollector for GNUStep + Copyright (C) 2009 Free Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Created: Jan 2009 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + + AutogsdocSource: NSGarbageCollector.m + + */ + +#ifndef _NSGarbageCollector_h_GNUSTEP_BASE_INCLUDE +#define _NSGarbageCollector_h_GNUSTEP_BASE_INCLUDE +#import + +#if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST) + +#import + +#if defined(__cplusplus) +extern "C" { +#endif + + +@interface NSGarbageCollector : NSObject + +/** Returns the garbage collector instance ... there is only one.
+ * Returns nil if the process is not using garbage collection. + */ ++ (id) defaultCollector; + +/** Collects some memory. + */ +- (void) collectIfNeeded; + +/** Collects all collectable memory. + */ +- (void) collectExhaustively; + +/** Disables garbage collection until a corresponding call to -enable is made. + */ +- (void) disable; + +/** Disables collection for the area of memory pointed at. + */ +- (void) disableCollectorForPointer: (void *)ptr; + +/** Enables garbage collection prevously disabled by a calle to -disable + */ +- (void) enable; + +/** Enables collection for the area of memory pointed at. + */ +- (void) enableCollectorForPointer: (void *)ptr; + +/** Returns yes if there is a garbage collection progress. + */ +- (BOOL) isCollecting; + +/** Retunrs YES if garbage collecting is currently enabled. + */ +- (BOOL) isEnabled; + +/** Returns a zone for holding non-collectable pointers.
+ */ +- (NSZone*) zone; +@end + +#if defined(__cplusplus) +} +#endif + +#endif +#endif diff --git a/Source/GNUmakefile b/Source/GNUmakefile index eac87618b..eb79ab7f8 100644 --- a/Source/GNUmakefile +++ b/Source/GNUmakefile @@ -194,6 +194,7 @@ NSException.m \ NSFileHandle.m \ NSFileManager.m \ NSFormatter.m \ +NSGarbageCollector.m \ NSGeometry.m \ NSHashTable.m \ NSHost.m \ @@ -336,6 +337,7 @@ NSExpression.h \ NSFileHandle.h \ NSFileManager.h \ NSFormatter.h \ +NSGarbageCollector.h \ NSGeometry.h \ NSHashTable.h \ NSHost.h \ diff --git a/Source/NSGarbageCollector.m b/Source/NSGarbageCollector.m new file mode 100644 index 000000000..db3573aac --- /dev/null +++ b/Source/NSGarbageCollector.m @@ -0,0 +1,137 @@ +/** Implementation for NSGarbageCollector for GNUStep + Copyright (C) 2009 Free Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Created: Jan 2009 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + + */ + +#import "Foundation/NSGarbageCollector.h" +#import "Foundation/NSLock.h" +#import "Foundation/NSZone.h" + +#if GS_WITH_GC + +#include + +#endif + +@implementation NSGarbageCollector + +static NSGarbageCollector *collector = nil; +static NSLock *lock = nil; +static unsigned disabled = 0; + ++ (id) defaultCollector +{ + return collector; +} + +#if GS_WITH_GC ++ (void) initialize +{ + collector = [self alloc]; + lock = [NSLock new]; +} +#endif + +- (void) collectIfNeeded +{ +#if GS_WITH_GC + GC_collect_a_little(); +#endif + return; +} + +- (void) collectExhaustively +{ +#if GS_WITH_GC + GC_gcollect(); +#endif + return; +} + +- (void) disable +{ +#if GS_WITH_GC + [lock lock]; + GC_disable(); + disabled++; + [lock unlock]; +#endif + return; +} + +- (void) disableCollectorForPointer: (void *)ptr +{ + // FIXME + return; +} + +- (void) enable +{ +#if GS_WITH_GC + [lock lock]; + if (disabled) + { + GC_enable(); + disabled--; + } + [lock unlock]; +#endif + return; +} + +- (void) enableCollectorForPointer: (void *)ptr +{ + // FIXME + return; +} + +- (id) init +{ + if (self != collector) + { + [self dealloc]; + self = nil; + } + return self; +} + +- (BOOL) isCollecting +{ + return NO; +} + +- (BOOL) isEnabled +{ + if (disabled) + { + return NO; + } + return YES; +} + +- (NSZone*) zone +{ + return GSAtomicMallocZone(); +} +@end +