forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSObjectPointer.m
141 lines (111 loc) · 3.32 KB
/
FSObjectPointer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* FSObjectPointer.m Copyright (c) 2004-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSObjectPointer.h"
#import "FSPointer.h"
#import "FSPointerPrivate.h"
#import "FScriptFunctions.h"
#import "Numberprivate.h"
#import "FSNumber.h"
#import <objc/runtime.h>
#import "FSArray.h"
/*
void FSObjectPointer_autoreleaseAll(FSObjectPointer *s) // Would have been simpler to implement as a method, but here I want to experiment about not poluting the method's space of user objects
{
#ifdef __LP64__
size_t *countPtr;
if (s == nil) return;
object_getInstanceVariable(s, "count", &(void*)countPtr);
for (NSUInteger i = 0; i < *countPtr; i++)
{
[((id *)[s cPointer])[i] autorelease];
}
#else
if (s == nil) return;
struct FSObjectPointerDef {@defs(FSObjectPointer)} *self = (struct FSObjectPointerDef *)s;
for (NSUInteger i = 0; i < self->count; i++)
{
[((id *)self->cPointer)[i] autorelease];
}
#endif
}
void FSObjectPointer_retainAll(FSObjectPointer *s) // Would have been simpler to implement as a method, but here I want to experiment about not poluting the method's space of user objects
{
#ifdef __LP64__
void *countPtr;
if (s == nil) return;
object_getInstanceVariable(s, "count", &countPtr);
for (NSUInteger i = 0; i < *(size_t *)countPtr; i++)
{
[((id *)[s cPointer])[i] retain];
}
#else
if (s == nil) return;
struct FSObjectPointerDef {@defs(FSObjectPointer)} *self = (struct FSObjectPointerDef *)s;
for (NSUInteger i = 0; i < self->count; i++)
{
[((id *)self->cPointer)[i] retain];
}
#endif
}
*/
@implementation FSObjectPointer
- (void) autoreleaseAll
{
for (NSUInteger i = 0; i < count; i++)
{
[((id*)cPointer)[i] autorelease];
}
}
- (void) dealloc
{
for (size_t i = 0; i < count; i++) [((id *)cPointer)[i] release];
free(cPointer);
[super dealloc];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"FSObjectPointer to %p", cPointer];
}
- (id) initWithCPointer:(void *)p count:(size_t)c // designated initializer
{
if (self = [super initWithCPointer:p])
{
count = c;
}
return self;
}
- (id) initWithCPointer:(void *)p
{
return [self initWithCPointer:p count:0];
}
- (void) retainAll
{
for (NSUInteger i = 0; i < count; i++)
{
[((id*)cPointer)[i] retain];
}
}
- (NSArray *)memoryContent
{
if (count <= 20000) return [FSArray arrayWithObjects:cPointer count:count]; // The current method is invoked by the BB to display memory content. We don't want to compute and display things too big, for performance reasons.
else return nil;
}
/////////// User methods
- (id)at:(id)i
{
FSPointer_validateDereferencingWithSelector_index(self, _cmd, i);
size_t index = [i doubleValue];
if (index >= count) [NSException raise:NSRangeException format:@"pointer dereferencing: index out of bounds in method \"at:\""];
return ((id *)cPointer)[index];
}
- (id)at:(id)i put:(id)elem
{
FSPointer_validateDereferencingWithSelector_index(self, _cmd, i);
size_t index = [i doubleValue];
if (index >= count) [NSException raise:NSRangeException format:@"pointer dereferencing: index out of bounds in method \"at:put:\""];
[elem retain];
[((id *)cPointer)[index] release];
((id *)cPointer)[index] = elem;
return elem;
}
@end