Commit 77c1169dd5367a15bf52a983ce803cccf8022059

Thomas de Grivel 2024-05-10T16:13:37

wip cow

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
diff --git a/libc3/bool.c b/libc3/bool.c
index 6eea312..1823d46 100644
--- a/libc3/bool.c
+++ b/libc3/bool.c
@@ -33,7 +33,8 @@ bool * bool_init_cast (bool *b, const s_sym * const *type,
   case TAG_CHARACTER: *b = (bool) tag->data.character;         return b;
   case TAG_COMPLEX:   *b = ! complex_is_zero(tag->data.complex);
                                                                return b;
-  case TAG_COW: return bool_init_cast(b, type, cow_rw(tag->data.cow));
+  case TAG_COW:
+    return bool_init_cast(b, type, cow_read_only(tag->data.cow));
   case TAG_F32:       *b = (bool) tag->data.f32;               return b;
   case TAG_F64:       *b = (bool) tag->data.f64;               return b;
   case TAG_F128:      *b = (bool) tag->data.f128;              return b;
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 3b08e8c..5a5d060 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -917,7 +917,7 @@ sw buf_inspect_cow (s_buf *buf, const s_cow *cow)
   buf_save_init(buf, &save);
   if ((r = buf_write_1(buf, "cow ")) < 0)
     goto restore;
-  if ((r = buf_inspect_tag(buf, cow_ro(cow))) < 0)
+  if ((r = buf_inspect_tag(buf, cow_read_only(cow))) < 0)
     goto restore;
   result += r;
   r = result;
@@ -934,7 +934,7 @@ sw buf_inspect_cow_size (const s_cow *cow)
   sw r;
   sw result;
   result = strlen("cow ");
-  if ((r = buf_inspect_tag_size(cow_ro(cow))) < 0)
+  if ((r = buf_inspect_tag_size(cow_read_only(cow))) < 0)
     return r;
   result += r;
   return result;
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 8923827..ded5e55 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -1201,7 +1201,7 @@ sw buf_parse_cow (s_buf *buf, s_cow *cow)
   if ((r = buf_ignore_spaces(buf)) <= 0)
     goto restore;
   result += r;
-  if ((r = buf_parse_tag(buf, &tmp.r)) <= 0)
+  if ((r = buf_parse_tag(buf, cow_read_write(&tmp))) <= 0)
     goto clean;
   result += r;
   *cow = tmp;
diff --git a/libc3/compare.c b/libc3/compare.c
index 16a2eea..f168b78 100644
--- a/libc3/compare.c
+++ b/libc3/compare.c
@@ -151,9 +151,8 @@ s8 compare_complex (const s_complex *a, const s_complex *b)
 u8 compare_cow (const s_cow *a, const s_cow *b)
 {
   u8 r;
-  if ((r = compare_tag(&a->r, &b->r)) ||
-      (r = compare_bool(a->w_is_set, b->w_is_set)) ||
-      (r = compare_tag(&a->w, &b->w)))
+  if ((r = compare_sym(a->type, b->type)) ||
+      (r = compare_list(a->list, b->list)))
     return r;
   return 0;
 }
diff --git a/libc3/cow.c b/libc3/cow.c
index 376befa..0e6fe35 100644
--- a/libc3/cow.c
+++ b/libc3/cow.c
@@ -13,13 +13,13 @@
 #include "assert.h"
 #include "alloc.h"
 #include "cow.h"
+#include "list.h"
 #include "tag.h"
 
 void cow_clean (s_cow *cow)
 {
   assert(cow);
-  tag_clean(&cow->r);
-  tag_clean(&cow->w);
+  list_delete_all(cow->list);
 }
 
 void cow_delete (s_cow *cow)
@@ -31,75 +31,108 @@ void cow_delete (s_cow *cow)
 
 s_cow * cow_freeze (s_cow *cow)
 {
+  s_list *tmp;
   assert(cow);
-  if (cow->w_is_set) {
-    tag_clean(&cow->r);
-    tag_init_copy(&cow->r, &cow->w);
-    cow->w_is_set = false;
-    tag_clean(&cow->w);
-  }
+  assert(cow->list);
+  tmp = list_new_tag_copy(&cow->list->tag, cow->list);
+  if (! tmp)
+    return NULL;
+  cow->list = tmp;
   return cow;
 }
 
-s_cow * cow_freeze_tag (s_cow *cow, const s_tag *src)
+s_cow * cow_freeze_copy (s_cow *cow, const s_tag *src)
 {
+  s_list *tmp;
   assert(cow);
-  assert(src);
-  cow->w_is_set = false;
-  tag_clean(&cow->w);
-  tag_clean(&cow->r);
-  if (! tag_init_copy(&cow->r, src))
+  assert(cow->list);
+  tmp = list_new_tag_copy(src, list_next(cow->list));
+  if (! tmp)
     return NULL;
+  cow->list->next.type = TAG_LIST;
+  cow->list->next.data.list = tmp;
   return cow;
 }
 
-s_cow * cow_init (s_cow *cow)
+s_cow * cow_init (s_cow *cow, const s_sym *type)
 {
   s_cow tmp = {0};
   assert(cow);
+  assert(type);
+  tmp.type = type;
+  tmp.list = list_new(NULL);
   *cow = tmp;
   return cow;
 }
 
 s_cow * cow_init_1 (s_cow *cow, const char *utf8)
 {
-  assert(cow);
-  assert(utf8);
-  (void) cow;
-  (void) utf8;
-  err_puts("cow_init_1: FIXME: not implemented");
-  assert(! "cow_init_1: FIXME: not implemented");
-  return NULL;
+  s_buf buf;
+  uw len;
+  sw r;
+  len = strlen(p);
+  buf_init(&buf, false, len, (char *) p); // buf is read-only
+  buf.wpos = len;
+  r = buf_parse_cow(&buf, cow);
+  if (r < 0 || (uw) r != len) {
+    err_write_1("invalid cow: \"");
+    err_write_1(p);
+    err_write_1("\", ");
+    err_inspect_uw(&len);
+    err_write_1(" != ");
+    err_inspect_sw(&r);
+    assert(! "invalid cow");
+    return NULL;
+  }
+  return tag;
 }
 
 s_cow * cow_init_cast (s_cow *cow, const s_sym * const *type,
                        const s_tag *tag)
 {
-  assert(cow);
-  assert(type);
+  void *data;
+  s_tag tmp;
   assert(tag);
-  (void) type;
-  if (tag->type == TAG_COW)
-    return cow_init_copy(cow, tag->data.cow);
-  return cow_freeze_tag(cow, tag);
+  assert(type);
+  assert(src);
+  if (*type == &g_sym_Cow) {
+    err_puts("cow_init_cast: cannot cast to Cow");
+    assert(! "cow_init_cast: cannot cast to Cow");
+    return NULL;
+  }
+  if (! sym_to_tag_type(*type, &tmp.type))
+    return NULL;
+  if (! tag_to_pointer(&tmp.list->tag, *type, &data))
+    return NULL;
+  if (! data_init_cast(data, type, src))
+    return NULL;
+  *tag = tmp;
+  return tag;
 }
 
 s_cow * cow_init_copy (s_cow *cow, const s_cow *src)
 {
+  s_cow tmp = {0};
   assert(cow);
   assert(src);
-  if (! tag_init_copy(&cow->r, &src->r) ||
-      ! tag_init_copy(&cow->w, &src->w))
+  tmp.type = src->type;
+  tmp.list = list_new_copy(src->list);
+  if (! tmp.list)
     return NULL;
+  *cow = tmp;
   return cow;
 }
-
+  
 s_cow * cow_init_tag_copy (s_cow *cow, const s_tag *src)
 {
+  assert(cow);
+  assert(src);
   s_cow tmp = {0};
   assert(cow);
   assert(src);
-  if (! tag_init_copy(&tmp.r, src))
+  tmp.type = src->type;
+  tmp.list = list_new_tag_copy(src);
+  if (! tmp.list)
     return NULL;
   *cow = tmp;
   return cow;
@@ -107,13 +140,22 @@ s_cow * cow_init_tag_copy (s_cow *cow, const s_tag *src)
 
 s_str * cow_inspect (const s_cow *cow, s_str *dest)
 {
-  assert(cow);
+  s_buf buf;
+  sw size;
+  assert(tag);
   assert(dest);
-  (void) cow;
-  (void) dest;
-  err_puts("cow_inspect: FIXME: not implemented");
-  assert(! "cow_inspect: FIXME: not implemented");
-  return NULL;
+  if ((size = buf_inspect_cow_size(cow)) < 0) {
+    err_puts("tag_inspect: size error");
+    assert(! "tag_inspect: size error");
+    return NULL;
+  }
+  buf_init_alloc(&buf, size);
+  if (buf_inspect_cow(&buf, cow) != size) {
+    err_puts("tag_inspect: inspect error");
+    assert(! "tag_inspect: inspect error");
+    return NULL;
+  }
+  return buf_to_str(&buf, dest);
 }
 
 s_cow * cow_new (void)
@@ -122,15 +164,29 @@ s_cow * cow_new (void)
   cow = alloc(sizeof(s_cow));
   if (! cow)
     return NULL;
-  cow_init(cow);
+  if (! cow_init(cow)) {
+    free(cow);
+    return NULL;
+  }
+  return cow;
+}
+
+s_cow * cow_new_1 (const char *utf8)
+{
+  s_cow *cow;
+  cow = alloc(sizeof(s_cow));
+  if (! cow)
+    return NULL;
+  if (! cow_init_1(cow, utf8)) {
+    free(cow);
+    return NULL;
+  }
   return cow;
 }
 
 s_cow * cow_new_cast (const s_sym * const *type, const s_tag *tag)
 {
   s_cow *cow;
-  assert(type);
-  assert(tag);
   cow = alloc(sizeof(s_cow));
   if (! cow)
     return NULL;
@@ -144,7 +200,6 @@ s_cow * cow_new_cast (const s_sym * const *type, const s_tag *tag)
 s_cow * cow_new_copy (const s_cow *src)
 {
   s_cow *cow;
-  assert(src);
   cow = alloc(sizeof(s_cow));
   if (! cow)
     return NULL;
@@ -155,41 +210,50 @@ s_cow * cow_new_copy (const s_cow *src)
   return cow;
 }
 
-const s_tag * cow_ro (const s_cow *cow)
+const s_tag * cow_read_only (const s_cow *cow)
 {
   assert(cow);
-  if (cow->w_is_set) {
-    // FIXME: get a read lock ?
-    return &cow->w;
-  }
-  return &cow->r;
+  assert(cow->list);
+  if (! list_next(cow->list))
+    if (! cow_freeze(cow))
+      return NULL;
+  return &list_next(cow->list)->tag;
 }
 
-s_tag * cow_rw (s_cow *cow)
+s_tag * cow_read_write (s_cow *cow)
 {
-  if (! cow->w_is_set &&
-      ! cow_thaw(cow))
-    return NULL;
-  return &cow->w;
+  assert(cow);
+  assert(cow->list);
+  return &cow->list->tag;
 }
 
 s_cow * cow_thaw (s_cow *cow)
 {
+  s_tag tmp;
   assert(cow);
-  tag_clean(&cow->w);
-  if (! tag_init_copy(&cow->w, &cow->r))
+  assert(cow->list);
+  assert(list_next(cow->list));
+  if (! list_next(cow->list)) {
+    err_puts("cow_thaw: nothing to thaw");
+    assert(! "cow_thaw: nothing to thaw");
+    return NULL;
+  }
+  if (! tag_init_copy(&tmp, &list_next(cow->list)->tag))
     return NULL;
-  cow->w_is_set = true;
+  tag_clean(&cow->list->tag);
+  cow->list->tag = tmp;
   return cow;
 }
 
-s_cow * cow_thaw_tag (s_cow *cow, const s_tag *src)
+s_cow * cow_thaw_copy (s_cow *cow, const s_tag *src)
 {
+  s_tag tmp;
   assert(cow);
+  assert(cow->list);
   assert(src);
-  tag_clean(&cow->w);
-  if (! tag_init_copy(&cow->w, src))
+  if (! tag_init_copy(&tmp, src))
     return NULL;
-  cow->w_is_set = true;
+  tag_clean(&cow->list->tag);
+  cow->list->tag = tmp;
   return cow;
 }
diff --git a/libc3/cow.h b/libc3/cow.h
index 173b9ba..4d4b92a 100644
--- a/libc3/cow.h
+++ b/libc3/cow.h
@@ -14,16 +14,14 @@
  * @file cow.h
  * @brief Copy on write is a software technique that allows you to have
  * read-only access to some memory but still be able to modify it. The
- * read-only data is copied to newly allocated memory when modified.
+ * read-only data is copied to a writable memory area to allow for
+ * modification and can then be turned into read-only memory.
  *
- * You can cow_freeze the writable version into read-only memory which
- * cleans the previous read-only value and you must be sure to not
- * access it anymore.
- *
- * Call cow_thaw to restore the writable memory to a copy of the
- * read-only version.
- *
- * Call cow_rw to get a writable s_tag pointer.
+ * Usage :
+ *   - cow_init() 
+ *   - cow_rw() returns a writable tag pointer
+ *   - cow_freeze() freeze the writable memory area into a read-only one
+ *   - cow_ro() returns the last read-only version that was frozen
  */
 #ifndef LIBC3_COW_H
 #define LIBC3_COW_H
@@ -42,17 +40,19 @@ s_cow * cow_init_tag_copy (s_cow *cow, const s_tag *src);
 /* Heap-allocation functions. Call cow_delete after use. */
 void    cow_delete (s_cow *cow);
 s_cow * cow_new (void);
+s_cow * cow_new_1 (const char *utf8);
 s_cow * cow_new_cast (const s_sym * const *type, const s_tag *tag);
 s_cow * cow_new_copy (const s_cow *src);
 
 /* Observers. */
 s_str *       cow_inspect (const s_cow *cow, s_str *dest);
-const s_tag * cow_ro (const s_cow *cow);
+const s_tag * cow_read_only (const s_cow *cow);
+s_tag *       cow_read_write (s_cow *cow);
 
 /* Operators. */
 s_cow * cow_freeze (s_cow *cow);
 s_cow * cow_freeze_copy (s_cow *cow, const s_tag *src);
-s_tag * cow_rw (s_cow *cow);
 s_cow * cow_thaw (s_cow *cow);
+s_cow * cow_thaw_copy (s_cow *cow, const s_tag *src);
 
 #endif /* LIBC3_COW_H */
diff --git a/libc3/types.h b/libc3/types.h
index 5b8bc11..0463fcf 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -224,6 +224,11 @@ struct buf_save {
   uw wpos;
 };
 
+struct cow {
+  const s_sym *type;
+  s_list *list;
+};
+
 struct fact {
   const s_tag *subject;
   const s_tag *predicate;
@@ -505,12 +510,6 @@ struct complex {
   s_tag y;
 };
 
-struct cow {
-  s_tag r;
-  s_tag w;
-  bool w_is_set;
-};
-
 struct error_handler
 {
   s_list *backtrace;