File size: 4,794 Bytes
f7ccd08
 
 
32b4893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{ SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
   Licensing: see LICENSE in the repository root. }

{ TensorArray: Array/buffer management with Arc-like reference counting.
  Mimics Rust Arc<Vec<T>> semantics using explicit reference counting.

  Semantic invariants:
    - Physical storage is strictly contiguous 1D array
    - Reference counting prevents use-after-free
    - Clone operation increments refcount without copying data
    - Drop operation decrements refcount, frees when reaching 0
}

unit tensor_array;

{$MODE FPC}

interface

uses
  buffer;

type
  { Arc-wrapped buffer }
  TTensorArray = record
    { Underlying buffer with refcount }
    buffer: PBuffer;

    { Current generation of this reference }
    generation: uint32;
  end;

  PTensorArray = ^TTensorArray;

{ Create new tensor array with given size }
function CreateTensorArray(size: uint32): TTensorArray;

{ Clone a tensor array (increment refcount, share buffer) }
function CloneTensorArray(const arr: TTensorArray): TTensorArray;

{ Drop a tensor array (decrement refcount) }
procedure DropTensorArray(var arr: TTensorArray);

{ Get element at index (with bounds checking) }
function GetElement(const arr: TTensorArray; index: uint32): single;

{ Set element at index (requires exclusive ownership) }
procedure SetElement(var arr: TTensorArray; index: uint32; value: single);

{ Get pointer to buffer data }
function GetDataPtr(const arr: TTensorArray): ^single;

{ Get size of tensor array }
function GetSize(const arr: TTensorArray): uint32;

{ Check if exclusively owned }
function IsExclusivelyOwned(const arr: TTensorArray): boolean;

{ Check if refcount > 1 (shared) }
function IsShared(const arr: TTensorArray): boolean;

{ Validate array consistency }
function IsValidArray(const arr: TTensorArray): boolean;

{ Clone on write: if refcount > 1, allocate new buffer }
procedure CopyOnWrite(var arr: TTensorArray);

{ Get reference count }
function GetRefCount(const arr: TTensorArray): uint32;

implementation

function CreateTensorArray(size: uint32): TTensorArray;
var
  buf: PBuffer;
begin
  buf := AllocateBuffer(size);
  if buf = nil then
  begin
    result.buffer := nil;
    result.generation := 0;
  end
  else
  begin
    result.buffer := buf;
    result.generation := buf^.generation;
  end;
end;

function CloneTensorArray(const arr: TTensorArray): TTensorArray;
var
  buf: PBuffer;
begin
  if arr.buffer = nil then
  begin
    result.buffer := nil;
    result.generation := 0;
  end
  else
  begin
    buf := arr.buffer;
    CloneBuffer(buf);
    result.buffer := buf;
    result.generation := buf^.generation;
  end;
end;

procedure DropTensorArray(var arr: TTensorArray);
begin
  ReleaseBuffer(arr.buffer);
  arr.generation := 0;
end;

function GetElement(const arr: TTensorArray; index: uint32): single;
begin
  if (arr.buffer = nil) or (index >= arr.buffer^.size) then
    result := 0.0
  else
    result := arr.buffer^.data^[index];
end;

procedure SetElement(var arr: TTensorArray; index: uint32; value: single);
begin
  if (arr.buffer <> nil) and (index < arr.buffer^.size) then
  begin
    arr.buffer^.data^[index] := value;
    arr.generation := arr.buffer^.generation;
  end;
end;

function GetDataPtr(const arr: TTensorArray): ^single;
begin
  if arr.buffer = nil then
    result := nil
  else
    result := arr.buffer^.data;
end;

function GetSize(const arr: TTensorArray): uint32;
begin
  if arr.buffer = nil then
    result := 0
  else
    result := arr.buffer^.size;
end;

function IsExclusivelyOwned(const arr: TTensorArray): boolean;
begin
  if arr.buffer = nil then
    result := false
  else
    result := IsExclusivelyOwned(arr.buffer);
end;

function IsShared(const arr: TTensorArray): boolean;
begin
  if arr.buffer = nil then
    result := false
  else
    result := (arr.buffer^.refcount > 1);
end;

function IsValidArray(const arr: TTensorArray): boolean;
begin
  if arr.buffer = nil then
    result := false
  else
    result := (arr.buffer^.size > 0) and
              (arr.buffer^.data <> nil) and
              (arr.generation = arr.buffer^.generation);
end;

procedure CopyOnWrite(var arr: TTensorArray);
var
  new_buf: PBuffer;
begin
  if arr.buffer = nil then
    exit;

  if arr.buffer^.refcount <= 1 then
    exit; { Already exclusive }

  { Allocate new buffer and copy data }
  new_buf := AllocateBuffer(arr.buffer^.size);
  if new_buf = nil then
    exit;

  CopyBufferData(arr.buffer, 0, new_buf, 0, arr.buffer^.size);

  { Release old buffer }
  ReleaseBuffer(arr.buffer);

  { Adopt new buffer }
  arr.buffer := new_buf;
  arr.generation := new_buf^.generation;
end;

function GetRefCount(const arr: TTensorArray): uint32;
begin
  if arr.buffer = nil then
    result := 0
  else
    result := arr.buffer^.refcount;
end;

end.