-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathneo-c.h
More file actions
7524 lines (6288 loc) · 174 KB
/
Copy pathneo-c.h
File metadata and controls
7524 lines (6288 loc) · 174 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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef NEO_C_H
#define NEO_C_H
#define __BEGIN_DECLS
#define __END_DECLS
#undef __cplusplus
#ifdef __STDC__
#define __P(protos) protos
#else
#define __P(protos) ()
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE _GNU_SOURCE
#endif
#define ALLOCATED_MAGIC_NUM 177783
#define nullptr ((void*)0)
typedef char*% string;
#if defined(__MINUX__)
#define UNIX 1
#elif defined(__BAREMETAL__) && defined(__NEO_BARE_HOST_SYSCALLS__) && defined(__linux__) && defined(__x86_64__)
#define UNIX 1
#elif defined(__BAREMETAL__) && defined(__NEO_BARE_HOST_SYSCALLS__) && defined(__APPLE__)
#define UNIX 1
#elif defined(__BAREMETAL__)
#elif defined(__PICO__)
#elif defined(__M5STACK__)
#else
#define UNIX 1
#endif
///////////////////////////////////////////////////////////////////////////
// BARE METAL
///////////////////////////////////////////////////////////////////////////
#if defined(__BAREMETAL__)
#include "neo-c-libc.h"
using neo-c;
using unsafe;
///////////////////////////////////////////////////////////////////////////
// PICO
///////////////////////////////////////////////////////////////////////////
#elif defined(__PICO__)
c_include {#define _GNU_SOURCE}
c_include {#include "stdarg.h"}
c_include {#include "stdlib.h"}
c_include {#include "stdint.h"}
c_include {#include "string.h"}
c_include {#include "stdio.h"}
c_include {#include "ctype.h"}
c_include {#include "wchar.h"}
c_include {#include "pico/stdlib.h"}
c_include {#include "pico/stdio.h"}
c_include {#include "pico/time.h"}
c_include {#include "hardware/irq.h"}
c_include {#include "hardware/timer.h"}
c_include {#include "hardware/uart.h"}
c_include {#include "pico/mutex.h"}
c_include {#include "pico/multicore.h"}
#define MUTEX_INITIALIZER (mutex_t){ .locked = false, .core = NULL }
#ifndef NULL
#define NULL ((void*)0)
#endif
typedef __builtin_va_list va_list;
using neo-c;
using unsafe;
///////////////////////////////////////////////////////////////////////////
// M5STACK
///////////////////////////////////////////////////////////////////////////
#elif defined(__M5STACK__)
c_include {#include <M5Stack.h>}
typedef __builtin_va_list va_list;
#ifndef NULL
#define NULL ((void*)0)
#endif
using neo-c;
using unsafe;
///////////////////////////////////////////////////////////////////////////
// UNIX
///////////////////////////////////////////////////////////////////////////
#else
using C;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#undef va_start
#define va_start(ap, last) __builtin_va_start(ap, last)
#include <limits.h>
#include <locale.h>
#include <errno.h>
#include <assert.h>
#include <stdbool.h>
#include <wchar.h>
#include <stdint.h>
#if defined(__NEO_NATIVE_BACKTRACE__) && (defined(__linux__) || defined(__APPLE__))
#include <execinfo.h>
#include <dlfcn.h>
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
using neo-c;
using unsafe;
#endif
///////////////////////////////////////////////////////////////////////////
// PREVIOUS DEFINITIONS
///////////////////////////////////////////////////////////////////////////
struct buffer
{
char*% buf;
int len;
int size;
};
uniq buffer*% buffer*::initialize(buffer*% self);
uniq void buffer*::finalize(buffer* self);
uniq buffer*% buffer*::clone(buffer* self);
uniq bool buffer*::equals(buffer* left, buffer* right);
uniq buffer* buffer*::append_str(buffer* self, const char* mem);
uniq buffer* buffer*::append(buffer* self, const char* mem, size_t size);
uniq string xsprintf(const char* msg, ...);
uniq string char*::to_string(const char* self);
uniq string int::to_string(int self);
uniq unsigned int bool::get_hash_key(bool value);
uniq unsigned int _Bool::get_hash_key(bool value);
uniq unsigned int char::get_hash_key(char value);
uniq unsigned int short::get_hash_key(short int value);
uniq unsigned int int::get_hash_key(int value);
uniq unsigned int long::get_hash_key(long value);
uniq unsigned int size_t::get_hash_key(size_t value);
uniq unsigned int float::get_hash_key(float value);
uniq unsigned int double::get_hash_key(double value);
uniq unsigned int char*::get_hash_key(const char* value);
uniq unsigned int string::get_hash_key(char* value);
uniq unsigned int void*::get_hash_key(void* value);
uniq string char*::substring(const char* str, int head, int tail);
uniq buffer* buffer*::append_format(buffer* self, const char* msg, ...);
uniq string buffer*::to_string(buffer* self);
uniq string char*::to_string(const char* self);
uniq string double::to_string(double self);
uniq string float::to_string(float self);
uniq string size_t::to_string(size_t self);
uniq string long::to_string(long self);
uniq string int::to_string(int self);
uniq string short::to_string(short self);
uniq string char::to_string(char self);
uniq string bool::to_string(bool self);
uniq string _Bool::to_string(bool self);
uniq bool string::equals(char* self, const char* right);
#ifdef __NEO_MICRO_RAM_8K__
#define COME_STACKFRAME_MAX 2
#else
#define COME_STACKFRAME_MAX 8
#endif
#define COME_STACKFRAME_SNAME_MAX 8
struct neo_frame {
neo_frame *prev;
char* fun_name;
unsigned long frame_id;
};
using unsafe {
uniq __thread neo_frame* neo_current_frame = (void*)0;
uniq __thread unsigned long neo_frame_id = 0;
}
uniq _norecord bool neo_frame_is_alive(unsigned long frame_id)
{
neo_frame* f = neo_current_frame;
while(f) {
if(f->frame_id == frame_id) {
return true;
}
f = f->prev;
}
return false;
}
uniq void neo_print_native_backtrace_symbol(void* addr)
{
#if defined(__NEO_NATIVE_BACKTRACE__) && (defined(__linux__) || defined(__APPLE__)) && !defined(__BAREMETAL__) && !defined(__NEO_MICRO__)
Dl_info info;
if(dladdr(addr, &info) && info.dli_fname) {
char command[2048];
char line[2048];
FILE* fp;
#if defined(__APPLE__)
snprintf(command, sizeof(command), "atos -o '%s' -l 0x%lx 0x%lx 2>/dev/null",
info.dli_fname, (unsigned long)(uintptr_t)info.dli_fbase,
(unsigned long)(uintptr_t)addr);
#else
snprintf(command, sizeof(command), "addr2line -f -p -e '%s' 0x%lx 2>/dev/null",
info.dli_fname,
(unsigned long)((uintptr_t)addr - (uintptr_t)info.dli_fbase));
#endif
fp = popen(command, "r");
if(fp) {
if(fgets(line, sizeof(line), fp) && strncmp(line, "??", 2) != 0) {
printf("%s", line);
pclose(fp);
return;
}
pclose(fp);
}
#if defined(__linux__)
snprintf(command, sizeof(command), "addr2line -f -p -e '%s' 0x%lx 2>/dev/null",
info.dli_fname, (unsigned long)(uintptr_t)addr);
fp = popen(command, "r");
if(fp) {
if(fgets(line, sizeof(line), fp) && strncmp(line, "??", 2) != 0) {
printf("%s", line);
pclose(fp);
return;
}
pclose(fp);
}
#endif
if(info.dli_sname) {
printf("%s (%p)\n", info.dli_sname, addr);
}
else {
printf("%s (%p)\n", info.dli_fname, addr);
}
}
else {
printf("%p\n", addr);
}
#else
printf("%p\n", addr);
#endif
}
uniq bool neo_print_native_backtrace()
{
#if defined(__NEO_NATIVE_BACKTRACE__) && (defined(__linux__) || defined(__APPLE__)) && !defined(__BAREMETAL__) && !defined(__NEO_MICRO__)
void* frames[64];
int n = backtrace(frames, 64);
int i;
if(n <= 0) {
return false;
}
puts("native backtrace:");
for(i=1; i<n; i++) {
printf(" #%d ", i-1);
neo_print_native_backtrace_symbol(frames[i]);
}
return true;
#else
return false;
#endif
}
uniq void stackframe()
{
if(neo_print_native_backtrace()) {
return;
}
neo_frame *f = neo_current_frame;
while(f) {
char* fun_name = f->fun_name;
printf("%s\n", fun_name);
f = f->prev;
}
}
bool come_is_alive(void* mem);
uniq void stackframe2(void* mem)
{
if(come_is_alive(mem) && mem) {
#ifndef __NEO_MICRO_RAM_8K__
sMemHeader* it = (sMemHeader*)((char*)mem - sizeof(size_t) - sizeof(size_t) - sizeof(sMemHeader));
printf("allocated at %s %d #%d. type is %s.\n", it->sname, it->sline, it->id, it->class_name);
#endif
}
if(neo_print_native_backtrace()) {
return;
}
neo_frame *f = neo_current_frame;
while(f) {
char* fun_name = f->fun_name;
printf("%s\n", fun_name);
f = f->prev;
}
}
uniq bool die(const char* msg, char* sname=__caller_sname__, int sline=__caller_line__)
{
puts(s"\{sname} \{sline} : " + msg);
stackframe();
exit(4);
return false;
}
//////////////////////////////
/// HEAP
//////////////////////////////
struct sMemHeader
{
#ifdef __NEO_MICRO_RAM_8K__
size_t size;
size_t compiletime_size;
size_t alloc_size;
#else
long size;
long compiletime_size;
long alloc_size;
#endif
int32_t allocated; /// ALLOCATED_MAGIC_NUM
int alive;
struct sMemHeader* next;
struct sMemHeader* prev;
struct sMemHeader* free_next;
#if defined(__NEO_MEMLEAK_STACKTRACE__) && !defined(__NEO_MICRO_RAM_8K__)
char* fun_name[COME_STACKFRAME_MAX];
#endif
#ifndef __NEO_MICRO_RAM_8K__
const char* class_name;
const char* sname;
int sline;
int id;
#endif
};
using unsafe {
uniq sMemHeader* gAllocMem = (void*)0;
uniq sMemHeader* gFreeMem = (void*)0;
}
uniq void come_memleak_checker()
{
using unsafe;
sMemHeader* it = gAllocMem;
int n = 0;
while(it) {
n++;
#ifdef __NEO_MICRO_RAM_8K__
printf("#%d allocation\n", n);
#else
printf("#%d ", n);
if(it->class_name) {
printf("%p (%s) %s %d: ", (char*)it + sizeof(sMemHeader) + sizeof(size_t) + sizeof(size_t), it->class_name, it->sname, it->sline);
}
#ifdef __NEO_MEMLEAK_STACKTRACE__
for(int i=0; i<COME_STACKFRAME_MAX; i++) {
if(it->fun_name[i]) {
printf("%s, ", it->fun_name[i]);
}
}
#endif
puts("");
#endif
it = it->next;
}
if(n > 0) printf("%d memory leaks. %d alloc, %d free.\n", n, gNumAlloc, gNumFree);
}
uniq void* alloc_from_pages(size_t size)
{
using unsafe;
sMemHeader* it = gFreeMem;
sMemHeader* it_prev = null;
while(it) {
if(size <= it->alloc_size) {
if(it_prev == null) {
gFreeMem = null;
}
else {
it_prev->free_next = it->free_next;
}
memset(it, 0, size);
return it;
}
it_prev = it;
it = it->free_next;
}
sMemHeader* it = (sMemHeader*)calloc(1, size);
if(it == null) {
return null;
}
it->alloc_size = size;
return it;
}
uniq void come_free_mem_of_heap_pool(void* mem)
{
using unsafe;
if(mem) {
sMemHeader* it = (sMemHeader*)((char*)mem - sizeof(sMemHeader));
if(it->allocated != ALLOCATED_MAGIC_NUM) {
return;
}
it->allocated = 0;
sMemHeader* prev_it = it->prev;
sMemHeader* next_it = it->next;
if(gAllocMem == it) {
gAllocMem = next_it;
if(gAllocMem) {
gAllocMem->prev = null;
}
}
else {
if(prev_it) {
prev_it->next = next_it;
}
if(next_it) {
next_it->prev = prev_it;
}
}
size_t size = it->size;
it->free_next = gFreeMem;
gFreeMem = it;
it->alive = 0;
/*
free(it);
*/
gNumFree++;
}
}
uniq void* come_alloc_mem_from_heap_pool(size_t compiletime_size, size_t size, const char* sname=null, int sline=0, int id=0, const char* class_name="")
{
using unsafe;
size_t size2 = size + sizeof(sMemHeader);
#ifdef __32BIT_CPU__
size2 = (size2 + 3 & ~0x3);
#elif defined(__16BIT_CPU__)
size2 = (size2 + 1 & ~0x1);
#else
size2 = (size2 + 7 & ~0x7);
#endif
void* result = alloc_from_pages(size2);
if(result == null) {
puts("out of memory");
stackframe();
exit(5);
return null;
}
sMemHeader* it = result;
it->allocated = ALLOCATED_MAGIC_NUM;
it->compiletime_size = compiletime_size;
it->size = size2;
it->free_next = NULL;
it->alive = 1;
#if defined(__NEO_MEMLEAK_STACKTRACE__) && !defined(__NEO_MICRO_RAM_8K__)
int n = 0;
neo_frame *f = neo_current_frame;
while(f && n < COME_STACKFRAME_MAX) {
char* fun_name = f->fun_name;
it.fun_name[n] = fun_name;
n++;
f = f->prev;
}
#endif
#ifndef __NEO_MICRO_RAM_8K__
it->next = gAllocMem;
it->prev = null;
it->class_name = class_name;
it->sname = sname;
it->sline = sline;
it->id = id;
#endif
if(gAllocMem) {
gAllocMem->prev = it;
}
gAllocMem = it;
gNumAlloc++;
return (char*)result + sizeof(sMemHeader);
}
uniq char* come_dynamic_typeof(void* mem)
{
using unsafe;
if(!come_is_alive(mem)) {
puts("invalid heap object");
stackframe2(mem);
exit(3);
}
sMemHeader* it = (sMemHeader*)((char*)mem - sizeof(size_t) - sizeof(size_t) - sizeof(sMemHeader));
if(it->allocated != ALLOCATED_MAGIC_NUM) {
printf("invalid heap object(%p)(1)\n", it);
stackframe2(mem);
exit(2);
}
#ifdef __NEO_MICRO_RAM_8K__
return "";
#else
return (char*)it->class_name;
#endif
}
uniq size_t dynamic_sizeof(void* mem)
{
using unsafe;
if(!come_is_alive(mem)) {
puts("invalid heap object");
stackframe2(mem);
exit(3);
}
sMemHeader* it = (sMemHeader*)((char*)mem - sizeof(size_t) - sizeof(size_t) - sizeof(sMemHeader));
if(it->allocated != ALLOCATED_MAGIC_NUM) {
printf("invalid heap object(%p)(1)\n", it);
stackframe2(mem);
exit(2);
}
size_t size = it->compiletime_size;
return size;
}
uniq int gNumAlloc = 0;
uniq int gNumFree = 0;
uniq void* come_calloc(size_t count, size_t size, const char* sname=null, int sline=0, int id=0, const char* class_name="")
{
using unsafe;
char* mem = come_alloc_mem_from_heap_pool(count*size, sizeof(size_t)+sizeof(size_t)+count*size, sname, sline, id, class_name);
size_t* ref_count = (size_t*)mem;
*ref_count = 0;
size_t* size2 = (size_t*)(mem + sizeof(size_t));
*size2 = size*count + sizeof(size_t) + sizeof(size_t);
return mem + sizeof(size_t) + sizeof(size_t);
}
uniq bool come_is_alive(void* mem)
{
if(mem == null) {
return false;
}
sMemHeader* it = (sMemHeader*)((char*)mem - sizeof(size_t) - sizeof(size_t) - sizeof(sMemHeader));
return it->alive;
}
uniq void come_free(void* mem)
{
using unsafe;
if(mem == NULL) {
return;
}
if(!come_is_alive(mem)) {
puts("invalid heap object");
stackframe2(mem);
exit(3);
}
size_t* ref_count = (size_t*)((char*)mem - sizeof(size_t) - sizeof(size_t));
come_free_mem_of_heap_pool((char*)ref_count);
}
uniq void* come_memdup(void* block, char* sname=null, int sline=0, int id=0, const char* class_name=null)
{
using unsafe;
if(block == null) {
return null;
}
if(!come_is_alive(block)) {
puts(s"invalid heap object. \{sname} \{sline} #\{id}");
stackframe2(block);
exit(3);
}
char* mem = (char*)block - sizeof(size_t) - sizeof(size_t);
size_t* size_p = (size_t*)(mem + sizeof(size_t));
size_t size = *size_p - sizeof(size_t) - sizeof(size_t);
void* result = come_calloc(1, size, sname, sline, id, class_name);
memcpy(result, block, size);
return result;
}
uniq void* come_increment_ref_count(void* mem, char* sname, int sline, int id)
{
using unsafe;
if(mem == NULL) {
return mem;
}
if(!come_is_alive(mem)) {
puts(s"invalid heap object \{sname} \{sline} #\{id}");
stackframe2(mem);
exit(3);
}
size_t* ref_count = (size_t*)((char*)mem - sizeof(size_t) - sizeof(size_t));
(*ref_count)++;
return mem;
}
uniq void* come_print_ref_count(void* mem)
{
using unsafe;
if(mem == NULL) {
return mem;
}
if(!come_is_alive(mem)) {
puts("invalid heap object");
stackframe();
exit(3);
}
size_t* ref_count = (size_t*)((char*)mem - sizeof(size_t) - sizeof(size_t));
printf("ref_count %ld\n", *ref_count);
return mem;
}
uniq int come_get_ref_count(void* mem)
{
using unsafe;
if(mem == NULL) {
return 0;
}
if(!come_is_alive(mem)) {
puts("invalid heap object");
stackframe();
exit(3);
}
size_t* ref_count = (size_t*)((char*)mem - sizeof(size_t) - sizeof(size_t));
return *ref_count;
}
uniq void* come_decrement_ref_count(void* mem, void* protocol_fun, void* protocol_obj, bool no_decrement, bool no_free, void* result_obj, char* sname, int sline, int id)
{
using unsafe;
if(result_obj) {
if(mem == result_obj) {
return mem;
}
}
if(mem == NULL) {
return NULL;
}
if(!come_is_alive(mem)) {
puts(s"invalid heap object \{sname} \{sline} #\{id}");
stackframe2(mem);
exit(3);
}
long* ref_count = (long*)((char*)mem - sizeof(size_t) - sizeof(size_t));
if(!no_decrement) {
(*ref_count)--;
}
long count = *ref_count;
if(!no_free && count <= 0) {
if(protocol_obj && protocol_fun) {
void (*finalizer)(void*) = (void (*)(void*))protocol_fun;
finalizer(protocol_obj);
come_free(protocol_obj);
}
come_free(mem);
return NULL;
}
return mem;
}
uniq void come_call_finalizer(void* fun, void* mem, void* protocol_fun, void* protocol_obj, int call_finalizer_only, int no_decrement, int no_free, void* result_obj, char* sname, int sline, int id)
{
using unsafe;
if(result_obj) {
if(mem == result_obj) {
return;
}
}
if(mem == NULL) {
return;
}
if(call_finalizer_only) {
if(fun) {
if(protocol_obj && protocol_fun) {
void (*finalizer)(void*) = (void (*)(void*))protocol_fun;
finalizer(protocol_obj);
}
void (*finalizer)(void*) = (void (*)(void*))fun;
finalizer(mem);
}
else {
if(protocol_obj && protocol_fun) {
void (*finalizer)(void*) = (void (*)(void*))protocol_fun;
finalizer(protocol_obj);
}
}
}
else {
if(!come_is_alive(mem)) {
puts(s"invalid heap object. \{sname} \{sline} #\{id}");
stackframe2(mem);
exit(3);
}
long* ref_count = (long*)((char*)mem - sizeof(size_t) - sizeof(size_t));
if(!no_decrement) {
(*ref_count)--;
}
long count = *ref_count;
if(!no_free && count <= 0) {
if(mem) {
if(fun) {
if(protocol_obj && protocol_fun) {
void (*finalizer)(void*) = (void (*)(void*))protocol_fun;
finalizer(protocol_obj);
come_free(protocol_obj);
}
if(fun) {
void (*finalizer)(void*) = (void (*)(void*))fun;
finalizer(mem);
}
}
else {
if(protocol_obj && protocol_fun) {
void (*finalizer)(void*) = (void (*)(void*))protocol_fun;
finalizer(protocol_obj);
come_free(protocol_obj);
}
}
come_free(mem);
}
}
}
}
uniq void xassert(const char* msg, bool test)
{
printf("%s...", msg);
if(!test) {
puts("false");
stackframe();
exit(2);
}
puts("ok");
}
uniq void* come_null_checker(void* mem, const char* sname, int sline, int id)
{
if(mem) {
return mem;
}
puts(s"null pointer exception \{sname} \{sline} #\{id}");
stackframe();
exit(1);
}
uniq void* come_heap_checker(void* mem, const char* sname, int sline, int id)
{
if(mem) {
if(come_is_alive(mem)) {
return mem;
}
else {
puts(s"heap pointer exception \{sname} \{sline} #\{id}");
stackframe();
exit(1);
}
}
puts(s"null pointer exception \{sname} \{sline} \{id}");
stackframe();
exit(1);
}
uniq string __builtin_string(const char* str, char* sname=__caller_sname__, int sline=__caller_line__)
{
if(str == null) {
return null;
}
int len = strlen(str) + 1;
char* result = come_calloc(1, sizeof(char)*(len), sname, sline, 0, "string");
//char*% result = new char[len];
strncpy(result, str, len);
return dummy_heap result;
}
#ifndef UNIX
uniq void stackframe() version 2
{
inherit();
}
uniq void* come_calloc(size_t count, size_t size, const char* sname=null, int sline=0, int id=0, const char* class_name="") version 2
{
return inherit(count, size, sname, sline, id, class_name);
}
uniq void come_free(void* mem) version 2
{
inherit(mem);
}
#endif
//////////////////////////////
// ref
//////////////////////////////
struct ref<T>
{
T^ p;
bool global;
bool heap;
bool local;
unsigned long frame_id;
void* heaptop;
};
impl ref<T>
{
ref<T>*% initialize(ref<T>*% self, T^ p, bool global_, bool heap_, bool local_, unsigned long frame_id) {
if(!ispointer(T) || p == null) {
puts(s"ref is pointer and not null");
stackframe2(self);
exit(2);
}
self.p = p;
self.global = global_;
self.heap = heap_;
self.local = local_;
self.frame_id = frame_id;
self.heaptop = p;
return self;
}
_norecord T^ unwrap(ref<T>* self) {
using unsafe;
if(self == null) {
puts("null pointer exception. self is null");
stackframe();
exit(2);
}
if(self.local) {
if(!neo_frame_is_alive(self.frame_id)) {
puts("refferenced stack object is vanished");
stackframe2(self);
exit(127);
}
}
if(self.heap) {
if(!come_is_alive(self.heaptop)) {
puts("refferenced heap object is vanished");
stackframe2(self);
exit(127);
}
}
return self.p;
}
_norecord T] operator_derefference(ref<T>* self)
{
using unsafe;
if(self == null) {
puts("null pointer exception. self is null");
stackframe();
exit(2);
}
if(self.local) {
if(!neo_frame_is_alive(self.frame_id)) {
puts("refferenced stack object is vanished");
stackframe2(self);
exit(127);
}
}
if(self.heap) {
if(!come_is_alive(self.heaptop)) {
puts("refferenced heap object is vanished");
stackframe2(self);
exit(127);
}
}
return *self.p;
}
_norecord void operator_store_derefference(ref<T>* self, T] item)
{
using unsafe;
if(self == null) {
puts("null pointer exception. self is null");
stackframe();
exit(2);
}
if(self.local) {
if(!neo_frame_is_alive(self.frame_id)) {
puts("refferenced stack object is vanished");
stackframe2(self);
exit(127);
}
}
if(self.heap) {
if(!come_is_alive(self.heaptop)) {
puts("refferenced heap object is vanished");
stackframe2(self);
exit(127);
}
}