forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryView.cs
More file actions
726 lines (586 loc) · 27.2 KB
/
MemoryView.cs
File metadata and controls
726 lines (586 loc) · 27.2 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Numerics;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using System.Collections.Generic;
using System.Threading;
namespace IronPython.Runtime {
[PythonType("memoryview")]
public sealed class MemoryView : ICodeFormattable, IWeakReferenceable {
private const int MaximumDimensions = 64;
private IBufferProtocol _buffer;
private readonly int _start;
private readonly int? _end;
private readonly int _step;
private readonly string _format;
private readonly PythonTuple _shape;
private readonly int _itemsize;
private int? _storedHash;
private WeakRefTracker _tracker;
// Variable to determine whether this memoryview is aligned
// with and has the same type as the underlying buffer. This
// allows us to fast-path by getting the specific item instead
// of having to convert to and from bytes.
private readonly bool _matchesBuffer;
public MemoryView(IBufferProtocol @object) {
_buffer = @object;
_step = 1;
_format = _buffer.Format;
_itemsize = (int)_buffer.ItemSize;
_matchesBuffer = true;
var shape = _buffer.GetShape(_start, _end);
if (shape == null) {
_shape = null;
}
_shape = new PythonTuple(shape);
}
public MemoryView(MemoryView @object) :
this(@object._buffer, @object._start, @object._end, @object._step, @object._format, @object._shape) { }
internal MemoryView(IBufferProtocol @object, int start, int? end, int step, string format, PythonTuple shape) {
_buffer = @object;
_format = format;
_shape = shape;
_start = start;
_end = end;
_step = step;
if (!TypecodeOps.TryGetTypecodeWidth(format, out _itemsize)) {
_itemsize = (int) _buffer.ItemSize;
}
_matchesBuffer = _format == _buffer.Format && _start % itemsize == 0;
}
private void CheckBuffer() {
if (_buffer == null) throw PythonOps.ValueError("operation forbidden on released memoryview object");
}
private int numberOfElements() {
if (_end != null) {
return (_end.Value - _start) / (_itemsize * _step);
}
return _buffer.ItemCount * (int)_buffer.ItemSize / (_itemsize * _step);
}
public int __len__() {
CheckBuffer();
return Converter.ConvertToInt32(shape[0]);
}
public object obj {
get {
CheckBuffer();
return _buffer;
}
}
public void release(CodeContext /*!*/ context) {
_buffer = null;
}
public object __enter__() {
CheckBuffer();
return this;
}
public void __exit__(CodeContext/*!*/ context, params object[] excinfo) {
release(context);
}
public string format {
get {
CheckBuffer();
return _format ?? _buffer.Format;
}
}
public BigInteger itemsize {
get {
CheckBuffer();
return _itemsize;
}
}
public BigInteger ndim {
get {
CheckBuffer();
return (_shape?.__len__()) ?? _buffer.NumberDimensions;
}
}
public bool @readonly {
get {
CheckBuffer();
return _buffer.ReadOnly;
}
}
public PythonTuple shape {
get {
CheckBuffer();
return _shape;
}
}
public PythonTuple strides {
get {
CheckBuffer();
return _buffer.Strides;
}
}
public PythonTuple suboffsets {
get {
CheckBuffer();
return _buffer.SubOffsets ?? PythonTuple.EMPTY;
}
}
public Bytes tobytes() {
CheckBuffer();
if (_matchesBuffer && _step == 1) {
return _buffer.ToBytes(_start / _itemsize, _end / _itemsize);
}
byte[] bytes = getByteRange(_start, numberOfElements() * _itemsize);
if (_step == 1) {
return Bytes.Make(bytes);
}
// getByteRange() doesn't care about our _step, so if we have one
// that isn't 1, we will need to get rid of any bytes we don't care
// about and potentially adjust for a reversed memoryview.
byte[] stridedBytes = new byte[bytes.Length / _itemsize];
for (int indexStrided = 0; indexStrided < stridedBytes.Length; indexStrided += _itemsize) {
int indexInBytes = indexStrided * _step;
for (int j = 0; j < _itemsize; j++) {
stridedBytes[indexStrided + j] = bytes[indexInBytes + j];
}
}
return Bytes.Make(stridedBytes);
}
public PythonList tolist() {
CheckBuffer();
if (_matchesBuffer && _step == 1) {
return _buffer.ToList(_start / _itemsize, _end / _itemsize);
}
int length = numberOfElements();
object[] elements = new object[length];
for (int i = 0; i < length; i++) {
elements[i] = getAtFlatIndex(i);
}
return PythonList.FromArrayNoCopy(elements);
}
public MemoryView cast(object format) {
return cast(format, null);
}
public MemoryView cast(object format, [NotNull]object shape) {
if (!(format is string formatAsString)) {
throw PythonOps.TypeError("memoryview: format argument must be a string");
}
if (_step != 1) {
throw PythonOps.TypeError("memoryview: casts are restricted to C-contiguous views");
}
if ((shape != null || ndim != 0) && this.shape.Contains(0)) {
throw PythonOps.TypeError("memoryview: cannot cast view with zeros in shape or strides");
}
PythonTuple shapeAsTuple = null;
if (shape != null) {
if (!(shape is PythonList) && !(shape is PythonTuple)) {
throw PythonOps.TypeError("shape must be a list or a tuple");
}
shapeAsTuple = PythonOps.MakeTupleFromSequence(shape);
int newNDim = shapeAsTuple.Count;
if (newNDim > MaximumDimensions) {
throw PythonOps.TypeError("memoryview: number of dimensions must not exceed {0}", MaximumDimensions);
}
if (ndim != 1 && newNDim != 1) {
throw PythonOps.TypeError("memoryview: cast must be 1D -> ND or ND -> 1D");
}
}
int newItemsize;
if (!TypecodeOps.TryGetTypecodeWidth(formatAsString, out newItemsize)) {
throw PythonOps.ValueError(
"memoryview: destination format must be a native single character format prefixed with an optional '@'");
}
bool thisIsBytes = this.format == "B" || this.format == "b" || this.format == "c";
bool otherIsBytes = formatAsString == "B" || formatAsString == "b" || formatAsString == "c";
if (!thisIsBytes && !otherIsBytes) {
throw PythonOps.TypeError("memoryview: cannot cast between two non-byte formats");
}
int length = numberOfElements();
if (length % newItemsize != 0) {
throw PythonOps.TypeError("memoryview: length is not a multiple of itemsize");
}
int newLength = length * _itemsize / newItemsize;
if (shapeAsTuple != null) {
int lengthGivenShape = 1;
for (int i = 0; i < shapeAsTuple.Count; i++) {
lengthGivenShape *= Converter.ConvertToInt32(shapeAsTuple[i]);
}
if (lengthGivenShape != newLength) {
throw PythonOps.TypeError("memoryview: product(shape) * itemsize != buffer size");
}
}
return new MemoryView(_buffer, _start, _end, _step, formatAsString, shapeAsTuple ?? PythonOps.MakeTuple(newLength));
}
private byte[] unpackBytes(string format, object o) {
if (TypecodeOps.TryGetBytes(format, o, out byte[] bytes)) {
return bytes;
} else if (o is Bytes b) {
return b.UnsafeByteArray; // CData returns a bytes object for its type
} else {
throw PythonOps.NotImplementedError("No conversion for type {0} to byte array", PythonOps.GetPythonTypeName(o));
}
}
private object packBytes(string format, byte[] bytes, int offset, int itemsize) {
if (TypecodeOps.TryGetFromBytes(format, bytes, offset, out object result))
return result;
else {
byte[] obj = new byte[itemsize];
for (int i = 0; i < obj.Length; i++) {
obj[i] = bytes[offset + i];
}
return Bytes.Make(obj);
}
}
private void setByteRange(int startByte, byte[] toWrite) {
string bufferTypeCode = _buffer.Format;
int bufferItemSize = (int)_buffer.ItemSize;
// Because memoryviews can be cast to bytes, sliced, and then
// cast to a different format, we have no guarantee of being aligned
// with the underlying buffer.
int startAlignmentOffset = startByte % bufferItemSize;
int endAlignmentOffset = (startByte + toWrite.Length) % bufferItemSize;
int indexInBuffer = startByte / bufferItemSize;
// Special case: when the bytes we set fall within the boundary
// of a single item, we have to worry about both the start and
// end offsets
if (startAlignmentOffset + toWrite.Length < bufferItemSize) {
byte[] existingBytes = unpackBytes(bufferTypeCode, _buffer.GetItem(indexInBuffer));
for (int i = 0; i < toWrite.Length; i++) {
existingBytes[i + startAlignmentOffset] = toWrite[i];
}
_buffer.SetItem(indexInBuffer, packBytes(bufferTypeCode, existingBytes, 0, bufferItemSize));
return;
}
// If we aren't aligned at the start, we have to preserve the first x bytes as
// they already are in the buffer, and overwrite the last (size - x) bytes
if (startAlignmentOffset != 0) {
byte[] existingBytes = unpackBytes(bufferTypeCode, _buffer.GetItem(indexInBuffer));
for (int i = startAlignmentOffset; i < existingBytes.Length; i++) {
existingBytes[i] = toWrite[i - startAlignmentOffset];
}
_buffer.SetItem(indexInBuffer, packBytes(bufferTypeCode, existingBytes, 0, bufferItemSize));
indexInBuffer++;
}
for (int i = startAlignmentOffset; i + bufferItemSize <= toWrite.Length; i += bufferItemSize, indexInBuffer++) {
_buffer.SetItem(indexInBuffer, packBytes(bufferTypeCode, toWrite, i, bufferItemSize));
}
// Likewise at the end, we may have to overwrite the first x bytes, but
// preserve the last (size - x) bytes
if (endAlignmentOffset != 0) {
byte[] existingBytes = unpackBytes(bufferTypeCode, _buffer.GetItem(indexInBuffer));
for (int i = 0; i < endAlignmentOffset; i++) {
existingBytes[i] = toWrite[toWrite.Length - startAlignmentOffset + i];
}
_buffer.SetItem(indexInBuffer, packBytes(bufferTypeCode, existingBytes, 0, bufferItemSize));
}
}
private byte[] getByteRange(int startByte, int length) {
string bufferTypeCode = _buffer.Format;
int bufferItemsize = (int)_buffer.ItemSize;
byte[] bytes = new byte[length];
int startAlignmentOffset = startByte % bufferItemsize;
int indexInBuffer = startByte / bufferItemsize;
for (int i = -startAlignmentOffset; i < length; i += bufferItemsize, indexInBuffer++) {
byte[] currentBytes = unpackBytes(bufferTypeCode, _buffer.GetItem(indexInBuffer));
for (int j = 0; j < currentBytes.Length; j++) {
// Because we don't have a guarantee that we are aligned with the
// the buffer's data, we may potentially read extra bits to the left
// and write of what we want, so we must ignore these bytes.
if (j + i < 0) {
continue;
}
if (j + i >= bytes.Length) {
continue;
}
bytes[i + j] = currentBytes[j];
}
}
return bytes;
}
/// <summary>
/// Treats the memoryview as if it were a flattened array, instead
/// of having multiple dimensions. So, for a memoryview with the shape
/// (2,2,2), retrieving at index 6 would be equivalent to getting at the
/// index (1,1,0).
/// </summary>
private object getAtFlatIndex(int index) {
if (_matchesBuffer) {
return _buffer.GetItem((_start / _itemsize) + (index * _step));
}
int firstByteIndex = _start + index * _itemsize * _step;
object result = packBytes(format, getByteRange(firstByteIndex, _itemsize), 0, (int)_buffer.ItemSize);
return PythonOps.ConvertToPythonPrimitive(result);
}
private void setAtFlatIndex(int index, object value) {
switch (format) {
case "d": // double
case "f": // float
double convertedValueDouble = 0;
if (!Converter.TryConvertToDouble(value, out convertedValueDouble)) {
throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", format);
}
value = convertedValueDouble;
break;
case "c": // char
case "b": // signed byte
case "B": // unsigned byte
case "u": // unicode char
case "h": // signed short
case "H": // unsigned short
case "i": // signed int
case "I": // unsigned int
case "l": // signed long
case "L": // unsigned long
case "q": // signed long long
case "P": // pointer
case "Q": // unsigned long long
if (!PythonOps.IsNumericObject(value)) {
throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", format);
}
if (TypecodeOps.CausesOverflow(value, format)) {
throw PythonOps.ValueError("memoryview: invalid value for format '{0}'", format);
}
if (format == "Q") {
value = Converter.ConvertToUInt64(value);
} else {
value = Converter.ConvertToInt64(value);
}
break;
default:
break; // This could be a variety of types, let the _buffer decide
}
if (_matchesBuffer) {
_buffer.SetItem((_start / _itemsize) + (index * _step), value);
return;
}
int firstByteIndex = _start + index * _itemsize * _step;
setByteRange(firstByteIndex, unpackBytes(format, value));
}
public object this[int index] {
get {
CheckBuffer();
index = PythonOps.FixIndex(index, __len__());
if (ndim > 1) {
throw PythonOps.NotImplementedError("multi-dimensional sub-views are not implemented");
}
return getAtFlatIndex(index);
}
set {
CheckBuffer();
if (_buffer.ReadOnly) {
throw PythonOps.TypeError("cannot modify read-only memory");
}
index = PythonOps.FixIndex(index, __len__());
if (ndim > 1) {
throw PythonOps.NotImplementedError("multi-dimensional sub-views are not implemented");
}
setAtFlatIndex(index, value);
}
}
public void __delitem__(int index) {
CheckBuffer();
if (_buffer.ReadOnly) {
throw PythonOps.TypeError("cannot modify read-only memory");
}
throw PythonOps.TypeError("cannot delete memory");
}
public void __delitem__([NotNull]Slice slice) {
CheckBuffer();
if (_buffer.ReadOnly) {
throw PythonOps.TypeError("cannot modify read-only memory");
}
throw PythonOps.TypeError("cannot delete memory");
}
public object this[[NotNull]Slice slice] {
get {
CheckBuffer();
int start, stop, step;
FixSlice(slice, __len__(), out start, out stop, out step);
List<int> dimensions = new List<int>();
// When a multidimensional memoryview is sliced, the slice
// applies to only the first dimension. Therefore, other
// dimensions are inherited.
dimensions.Add((stop - start) / step);
// In a 1-dimensional memoryview, the difference in bytes
// between the position of mv[x] and mv[x + 1] is guaranteed
// to be just the size of the data. For multidimensional
// memoryviews, we must worry about the width of all the other
// dimensions for the difference between mv[(x, y, z...)] and
// mv[(x + 1, y, z...)]
int firstIndexWidth = _itemsize;
for (int i = 1; i < shape.__len__(); i++) {
int dimensionWidth = Converter.ConvertToInt32(shape[i]);
dimensions.Add(dimensionWidth);
firstIndexWidth *= dimensionWidth;
}
int newStart = _start + start * firstIndexWidth;
int newEnd = _start + stop * firstIndexWidth;
int newStep = _step * step;
PythonTuple newShape = PythonOps.MakeTupleFromSequence(dimensions);
return new MemoryView(_buffer, newStart, newEnd, newStep, format, newShape);
}
set {
CheckBuffer();
if (_buffer.ReadOnly) {
throw PythonOps.TypeError("cannot modify read-only memory");
}
int start, stop, step;
FixSlice(slice, __len__(), out start, out stop, out step);
slice = new Slice(start, stop, step);
int newLen = PythonOps.Length(value);
if (stop - start != newLen) {
throw PythonOps.ValueError("cannot resize memory view");
}
slice.DoSliceAssign(SliceAssign, __len__(), value);
}
}
private void SliceAssign(int index, object value) {
setAtFlatIndex(index, value);
}
/// <summary>
/// MemoryView slicing is somewhat different and more restricted than
/// standard slicing.
/// </summary>
private static void FixSlice(Slice slice, int len, out int start, out int stop, out int step) {
slice.indices(len, out start, out stop, out step);
if (stop < start && step >= 0) {
// wrapped iteration is interpreted as empty slice
stop = start;
}
}
public object this[[NotNull]PythonTuple index] {
get {
CheckBuffer();
return getAtFlatIndex(GetFlatIndex(index));
}
set {
CheckBuffer();
setAtFlatIndex(GetFlatIndex(index), value);
}
}
/// <summary>
/// Gets the "flat" index from the access of a tuple as if the
/// multidimensional tuple were layed out in contiguous memory.
/// </summary>
private int GetFlatIndex(PythonTuple tuple) {
int flatIndex = 0;
int tupleLength = tuple.Count;
int ndim = (int)this.ndim;
int firstOutOfRangeIndex = -1;
bool allInts = true;
bool allSlices = true;
// A few notes about the ordering of operations here:
// 1) CPython checks the types of the objects in the tuple
// first, then the dimensions, then finally for the range.
// Because we do a range check while we go through the tuple,
// we have to remember that we had something out of range
// 2) CPython checks for a multislice tuple, then for all ints,
// and throws an invalid slice key otherwise. We again try to
// do this in one pass, so we remember whether we've seen an int
// and whether we've seen a slice
for (int i = 0; i < tupleLength; i++) {
object indexObject = tuple[i];
if (Converter.TryConvertToInt32(indexObject, out int indexValue)) {
allSlices = false;
// If we have a "bad" tuple, we no longer care
// about the resulting flat index, but still need
// to check the rest of the tuple in case it has a
// non-int value
if (i >= ndim || firstOutOfRangeIndex > -1) {
continue;
}
int dimensionWidth = (int)shape[i];
// If we have an out of range exception, that will only
// be thrown if the tuple length is correct, so we have to
// defer throwing to later
if (!PythonOps.TryFixIndex(indexValue, dimensionWidth, out indexValue)) {
firstOutOfRangeIndex = i;
continue;
}
flatIndex *= dimensionWidth;
flatIndex += indexValue;
} else if (indexObject is Slice) {
allInts = false;
} else {
throw PythonOps.TypeError("memoryview: invalid slice key");
}
}
if (!allInts) {
if (allSlices) {
throw PythonOps.NotImplementedError("multi-dimensional slicing is not implemented");
} else {
throw PythonOps.TypeError("memoryview: invalid slice key");
}
}
if (tupleLength < ndim) {
throw PythonOps.NotImplementedError("sub-views are not implemented");
}
if (tupleLength > ndim) {
throw PythonOps.TypeError("cannot index {0}-dimension view with {1}-element tuple", ndim, tupleLength);
}
if (firstOutOfRangeIndex != -1) {
PythonOps.IndexError("index out of bounds on dimension {0}", firstOutOfRangeIndex + 1);
}
return flatIndex;
}
public int __hash__(CodeContext context) {
if (_storedHash != null) {
return _storedHash.Value;
}
if (!@readonly) {
throw PythonOps.ValueError("cannot hash writable memoryview object");
}
if (format != "B" && format != "b" && format != "c") {
throw PythonOps.ValueError("memoryview: hashing is restricted to formats 'B', 'b' or 'c'");
}
_storedHash = tobytes().GetHashCode();
return _storedHash.Value;
}
public bool __eq__(CodeContext/*!*/ context, [NotNull]MemoryView value) {
if (_buffer == null) {
return value._buffer == null;
}
return tobytes().Equals(value.tobytes());
}
public bool __eq__(CodeContext/*!*/ context, [NotNull]IBufferProtocol value) => __eq__(context, new MemoryView(value));
[return: MaybeNotImplemented]
public object __eq__(CodeContext/*!*/ context, object value) => NotImplementedType.Value;
public bool __ne__(CodeContext/*!*/ context, [NotNull]MemoryView value) => !__eq__(context, value);
public bool __ne__(CodeContext/*!*/ context, [NotNull]IBufferProtocol value) => !__eq__(context, value);
[return: MaybeNotImplemented]
public object __ne__(CodeContext/*!*/ context, object value) => NotImplementedType.Value;
public bool __lt__(CodeContext/*!*/ context, object value) {
throw PythonOps.TypeError("'<' not supported between instances of '{0}' and '{1}'",
PythonOps.GetPythonTypeName(this), PythonOps.GetPythonTypeName(value));
}
public bool __le__(CodeContext/*!*/ context, object value) {
throw PythonOps.TypeError("'<=' not supported between instances of '{0}' and '{1}'",
PythonOps.GetPythonTypeName(this), PythonOps.GetPythonTypeName(value));
}
public bool __gt__(CodeContext/*!*/ context, object value) {
throw PythonOps.TypeError("'>' not supported between instances of '{0}' and '{1}'",
PythonOps.GetPythonTypeName(this), PythonOps.GetPythonTypeName(value));
}
public bool __ge__(CodeContext/*!*/ context, object value) {
throw PythonOps.TypeError("'>=' not supported between instances of '{0}' and '{1}'",
PythonOps.GetPythonTypeName(this), PythonOps.GetPythonTypeName(value));
}
#region ICodeFormattable Members
public string __repr__(CodeContext context) {
if (_buffer == null) {
return String.Format("<released memory at {0}>", PythonOps.Id(this));
}
return String.Format("<memory at {0}>", PythonOps.Id(this));
}
#endregion
#region IWeakReferenceable Members
WeakRefTracker IWeakReferenceable.GetWeakRef() {
return _tracker;
}
bool IWeakReferenceable.SetWeakRef(WeakRefTracker value) {
return Interlocked.CompareExchange(ref _tracker, value, null) == null;
}
void IWeakReferenceable.SetFinalizer(WeakRefTracker value) {
_tracker = value;
}
#endregion
}
}