~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

« back to all changes in this revision

Viewing changes to Source/NSGarbageCollector.m

Tags: upstream-1.20.0
ImportĀ upstreamĀ versionĀ 1.20.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** Implementation for NSGarbageCollector for GNUStep
 
2
   Copyright (C) 2009 Free Software Foundation, Inc.
 
3
 
 
4
   Written by:  Richard Frith-Macdonald <rfm@gnu.org>
 
5
   Created: Jan 2009
 
6
 
 
7
   This file is part of the GNUstep Base Library.
 
8
 
 
9
   This library is free software; you can redistribute it and/or
 
10
   modify it under the terms of the GNU Lesser General Public
 
11
   License as published by the Free Software Foundation; either
 
12
   version 2 of the License, or (at your option) any later version.
 
13
 
 
14
   This library is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
   Library General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU Lesser General Public
 
20
   License along with this library; if not, write to the Free
 
21
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
22
   Boston, MA 02111 USA.
 
23
 
 
24
   */
 
25
 
 
26
#import "common.h"
 
27
#import "Foundation/NSGarbageCollector.h"
 
28
 
 
29
static NSGarbageCollector       *collector = nil;
 
30
static unsigned                 disabled = 0;
 
31
 
 
32
#if     GS_WITH_GC
 
33
 
 
34
#include <gc.h>
 
35
 
 
36
#import "Foundation/NSLock.h"
 
37
#import "Foundation/NSHashTable.h"
 
38
static NSLock           *lock = nil;
 
39
static NSHashTable      *uncollectable = 0;
 
40
#endif
 
41
 
 
42
@implementation NSGarbageCollector
 
43
 
 
44
+ (id) defaultCollector
 
45
{
 
46
  return collector;
 
47
}
 
48
 
 
49
#if     GS_WITH_GC
 
50
+ (void) initialize
 
51
{
 
52
  collector = [self alloc];
 
53
  lock = [NSLock new];
 
54
}
 
55
#endif
 
56
 
 
57
- (void) collectIfNeeded
 
58
{
 
59
#if     GS_WITH_GC
 
60
  GC_collect_a_little();
 
61
#endif
 
62
  return;
 
63
}
 
64
 
 
65
- (void) collectExhaustively
 
66
{
 
67
#if     GS_WITH_GC
 
68
  GC_gcollect();
 
69
#endif
 
70
  return;
 
71
}
 
72
 
 
73
- (void) disable
 
74
{
 
75
#if     GS_WITH_GC
 
76
  [lock lock];
 
77
  GC_disable();
 
78
  disabled++;
 
79
  [lock unlock];
 
80
#endif
 
81
  return;
 
82
}
 
83
 
 
84
- (void) disableCollectorForPointer: (void *)ptr
 
85
{
 
86
#if     GS_WITH_GC
 
87
  [lock lock];
 
88
  if (uncollectable == 0)
 
89
    {
 
90
      uncollectable = NSCreateHashTable(NSOwnedPointerHashCallBacks, 0);
 
91
    }
 
92
  NSHashInsertIfAbsent(uncollectable, ptr);
 
93
  [lock unlock];
 
94
#endif
 
95
  return;
 
96
}
 
97
 
 
98
- (void) enable
 
99
{
 
100
#if     GS_WITH_GC
 
101
  [lock lock];
 
102
  if (disabled)
 
103
    {
 
104
      GC_enable();
 
105
      disabled--;
 
106
    }
 
107
  [lock unlock];
 
108
#endif
 
109
  return;
 
110
}
 
111
 
 
112
- (void) enableCollectorForPointer: (void *)ptr
 
113
{
 
114
#if     GS_WITH_GC
 
115
  [lock lock];
 
116
  if (uncollectable != 0)
 
117
    {
 
118
      NSHashRemove(uncollectable, ptr);
 
119
    }
 
120
  [lock unlock];
 
121
#endif
 
122
  return;
 
123
}
 
124
 
 
125
- (id) init
 
126
{
 
127
  if (self != collector)
 
128
    {
 
129
      [self dealloc];
 
130
      self = nil;
 
131
    }
 
132
  return self;
 
133
}
 
134
 
 
135
- (BOOL) isCollecting
 
136
{
 
137
  return NO;
 
138
}
 
139
 
 
140
- (BOOL) isEnabled
 
141
{
 
142
  if (disabled)
 
143
    {
 
144
      return NO;
 
145
    }
 
146
  return YES;
 
147
}
 
148
 
 
149
- (NSZone*) zone
 
150
{
 
151
  return NSDefaultMallocZone();
 
152
}
 
153
@end
 
154