-
Notifications
You must be signed in to change notification settings - Fork 0
/
xepl.cc
5735 lines (4540 loc) · 128 KB
/
xepl.cc
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
// SPDX: AGPL-3.0-only
/* XEPL Operating Environment - Copyright (c) 2024 Keith Edwin Robbins
Project Name: XEPL Operating Environment
File Name: xepl.cc
Author: Keith Edwin Robbins
Release date: May 10, 2024
Website: https://xepl.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For more information about the AGPL, please visit:
https://www.gnu.org/licenses/agpl-3.0.html
*/
#include "xepl.h"
/// This detects C++ memory leaks
std::atomic_size_t XEPL::num_total_news{0};
std::atomic_size_t XEPL::num_total_dels{0};
void* operator new ( size_t _size )
{
++XEPL::num_total_news;
return malloc ( _size );
}
void operator delete ( void* _ptr ) throw()
{
if ( !_ptr )
return;
++XEPL::num_total_dels;
free ( _ptr );
}
XEPL::MemoryCounts::~MemoryCounts()
{
size_t leaking_allocations = num_total_news-num_total_dels;
if ( std_ostream && num_total_news && ( Show_Memory_Counts || leaking_allocations ) )
{
String news_string;
Long_Commafy(num_total_news, &news_string);
*std_ostream << "Others: " << std::setw( 12 ) << news_string << " :" << std::endl;
}
if ( leaking_allocations )
std::cerr << " ---LEAKING new/delete: " << leaking_allocations << '\n' << std::flush;
}
XEPL::MemoryCounts::MemoryCounts ( std::ostream* _ostream )
: std_ostream ( _ostream )
{
num_total_dels = 0;
num_total_news = 0;
}
// 8888ba.88ba dP
// 88 `8b `8b 88
// 88 88 88 dP dP d8888P .d8888b. dP. .dP
// 88 88 88 88 88 88 88ooood8 `8bd8'
// 88 88 88 88. .88 88 88. ... .d88b.
// dP dP dP `88888P' dP `88888P' dP' `dP
//
XEPL::Mutex::Mutex()
: mutex() {}
XEPL::MutexScope::~MutexScope ( void )
{
if ( mutex )
mutex->mutex.unlock();
}
XEPL::MutexScope::MutexScope ( Mutex* _mutex )
: mutex ( _mutex )
{
if ( mutex )
mutex->mutex.lock();
}
// d888888P dP dP
// 88 88 88
// 88 88d888b. 88d888b. .d8888b. .d8888b. .d888b88
// 88 88' `88 88' `88 88ooood8 88' `88 88' `88
// 88 88 88 88 88. ... 88. .88 88. .88
// dP dP dP dP `88888P' `88888P8 `88888P8
//
XEPL::Thread::Thread ( Lobe* _lobe, Semaphore* _semaphore )
: std_thread ( nullptr )
, lobe ( _lobe )
, semaphore_rest ( _semaphore )
{}
void XEPL::Thread::Bury_Child()
{
if ( std_thread )
{
std_thread->join();
std_thread->~thread();
delete std_thread;
std_thread=nullptr;
lobe = nullptr;
}
}
void XEPL::Thread::Concieve_Child( Semaphore* _semaphore )
{
std_thread = ::new std::thread ( [_semaphore] ( Thread* _thread )
{
tlsLobe = _thread->lobe;
Backpack memory_backpack;
ShortTerms short_term_memories;
tlsLobe->Main_Loop( _semaphore );
}, this );
}
// .d88888b dP
// 88. "' 88
// `Y88888b. .d8888b. 88d8b.d8b. .d8888b. 88d888b. 88d888b. .d8888b. 88d888b. .d8888b.
// `8b 88ooood8 88'`88'`88 88' `88 88' `88 88' `88 88' `88 88' `88 88ooood8
// d8' .8P 88. ... 88 88 88 88. .88 88. .88 88 88 88. .88 88 88. ...
// Y88888P `88888P' dP dP dP `88888P8 88Y888P' dP dP `88888P' dP `88888P'
// 88
// dP
XEPL::Semaphore::Semaphore()
: std::condition_variable()
, std::mutex()
{}
void XEPL::Semaphore::Give ( void )
{
std::unique_lock<std::mutex> lock ( *this );
notify_one();
}
std::ostream& operator<< (std::ostream&, XEPL::Cord* );
std::ostream& operator<< (std::ostream&, XEPL::Cord& );
XEPL::String::~String ( void ) {}
XEPL::String::String ( void )
: CppString()
{}
XEPL::String::String ( const CppString* _string )
: CppString()
{
if ( _string )
assign ( *_string );
}
XEPL::String::String ( const String* _string )
: CppString()
{
if ( _string )
assign ( *_string );
}
XEPL::String::String ( Text* _string )
: CppString()
{
if ( _string )
assign ( _string );
}
XEPL::String::String ( Text* _string, size_t _length )
: CppString()
{
if ( _string && _length )
assign ( _string, _length );
}
std::ostream& operator<< (std::ostream& os, XEPL::Cord* str)
{
return os << str->c_str();
}
std::ostream& operator<< (std::ostream& os, XEPL::Cord& str)
{
return os << str.c_str();
}
// 888888ba dP dP
// 88 `8b 88 88
// a88aaaa8P' .d8888b. .d8888b. 88 .dP 88d888b. .d8888b. .d8888b. 88 .dP
// 88 `8b. 88' `88 88' `"" 88888" 88' `88 88' `88 88' `"" 88888"
// 88 .88 88. .88 88. ... 88 `8b. 88. .88 88. .88 88. ... 88 `8b.
// 88888888P `88888P8 `88888P' dP `YP 88Y888P' `88888P8 `88888P' dP `YP
// 88
// dP
XEPL::Backpack::~Backpack()
{
heap->~HeapOfPools();
free ( heap );
tlsHeap = nullptr;
}
XEPL::Backpack::Backpack()
: heap ( new ( malloc ( sizeof ( HeapOfPools ) ) ) HeapOfPools() )
{
tlsHeap = heap;
}
// 888888ba dP
// 88 `8b 88
// a88aaaa8P' .d8888b. .d8888b. dP dP .d8888b. 88 .d8888b. 88d888b.
// 88 `8b. 88ooood8 88' `"" 88 88 88' `"" 88 88ooood8 88' `88
// 88 88 88. ... 88. ... 88. .88 88. ... 88 88. ... 88
// dP dP `88888P' `88888P' `8888P88 `88888P' dP `88888P' dP
// .88
// d8888P
void* XEPL::Recycler::operator new ( size_t size )
{
if ( HeapOfPools* heap = tlsHeap )
return heap->Get_Block ( size );
return ::operator new ( size );
}
void XEPL::Recycler::operator delete ( void* p )
{
if ( HeapOfPools* heap = tlsHeap )
heap->Recycle_Block ( p );
else
::operator delete ( p );
}
// dP dP .88888. .8888b 888888ba dP
// 88 88 d8' `8b 88 " 88 `8b 88
// 88aaaaa88a .d8888b. .d8888b. 88d888b. 88 88 88aaa a88aaaa8P' .d8888b. .d8888b. 88 .d8888b.
// 88 88 88ooood8 88' `88 88' `88 88 88 88 88 88' `88 88' `88 88 Y8ooooo.
// 88 88 88. ... 88. .88 88. .88 Y8. .8P 88 88 88. .88 88. .88 88 88
// dP dP `88888P' `88888P8 88Y888P' `8888P' dP dP `88888P' `88888P' dP `88888P'
// 88
// dP
thread_local class XEPL::HeapOfPools* XEPL::tlsHeap = nullptr;
XEPL::HeapOfPools::~HeapOfPools()
{
total_biggies_out += count_biggies_out;
total_biggies_in += count_biggies_in;
if ( largest_biggie < largest_biggie_out )
largest_biggie = largest_biggie_out; // may overwrite on race
for ( long index = 1; index <= Memory::maxPoolIndex; ++index )
{
pool_of_blocks[index]->~PoolOfBlocks();
free ( pool_of_blocks[index] );
}
}
XEPL::HeapOfPools::HeapOfPools()
: pool_of_blocks ()
, count_biggies_out ( 0 )
, count_biggies_in ( 0 )
, largest_biggie_out ( 0 )
{
for ( long index = 1; index <= Memory::maxPoolIndex; ++index )
{
void* ptr = malloc ( sizeof ( PoolOfBlocks ) );
pool_of_blocks[index] = ( new ( ptr ) PoolOfBlocks ( index ) );
}
pool_of_blocks[0] = nullptr;
}
void* XEPL::HeapOfPools::Get_Block ( size_t _size )
{
auto block_id = _size/Memory::poolWidth;
if ( block_id <= Memory::maxPoolIndex )
{
if ( !block_id )
block_id = 1;
return pool_of_blocks[block_id]->Get_Or_Malloc();
}
++count_biggies_out;
if ( largest_biggie_out < _size )
largest_biggie_out = _size;
const size_t block_size = _size + sizeof ( BlockHeader );
BlockHeader* block = static_cast<BlockHeader*> ( malloc ( block_size ) );
block->pool_index = 0;
return block + 1;
}
void XEPL::HeapOfPools::Recycle_Block ( void* _ptr )
{
if ( !_ptr )
return;
BlockHeader* returned_block = static_cast<BlockHeader*> ( _ptr )-1;
if ( returned_block->pool_index )
pool_of_blocks[returned_block->pool_index]->Catch_Or_Free ( returned_block );
else
{
++count_biggies_in;
free ( returned_block );
}
}
// 888888ba dP .88888. .8888b 888888ba dP dP
// 88 `8b 88 d8' `8b 88 " 88 `8b 88 88
// a88aaaa8P' .d8888b. .d8888b. 88 88 88 88aaa a88aaaa8P' 88 .d8888b. .d8888b. 88 .dP .d8888b.
// 88 88' `88 88' `88 88 88 88 88 88 `8b. 88 88' `88 88' `"" 88888" Y8ooooo.
// 88 88. .88 88. .88 88 Y8. .8P 88 88 .88 88 88. .88 88. ... 88 `8b. 88
// dP `88888P' `88888P' dP `8888P' dP 88888888P dP `88888P' `88888P' dP `YP `88888P'
//
XEPL::PoolOfBlocks::~PoolOfBlocks( void )
{
total_mallocs += blocks_malloced;
total_cached += blocks_cached;
total_freed += blocks_freed;
total_held += blocks_holding;
pool_mallocs [ pool_index ] += blocks_malloced;
pool_cached [ pool_index ] += blocks_cached;
pool_freed [ pool_index ] += blocks_freed;
pool_held [ pool_index ] += blocks_holding;
while ( BlockHeader* block = head_block )
{
head_block=block->next_block;
free ( block );
}
}
XEPL::PoolOfBlocks::PoolOfBlocks ( long _index )
: head_block ( nullptr )
, pool_index ( _index )
, block_size ( ( _index+1 )*Memory::poolWidth )
, blocks_malloced ( 0 )
, blocks_cached ( 0 )
, blocks_freed ( 0 )
, blocks_holding ( 0 )
{}
void XEPL::PoolOfBlocks::Catch_Or_Free ( BlockHeader* _block )
{
if ( blocks_holding > blocks_cached || blocks_holding > blocks_malloced )
{
++blocks_freed;
free ( _block );
}
else
{
++blocks_holding;
_block->next_block = head_block;
head_block = _block;
}
}
void* XEPL::PoolOfBlocks::Get_Or_Malloc( void )
{
BlockHeader* block = head_block;
if ( block )
{
--blocks_holding;
++blocks_cached;
head_block = block->next_block;
}
else
{
++blocks_malloced;
block = static_cast<BlockHeader*> ( malloc ( block_size ) );
}
block->pool_index = pool_index;
return block+1;
}
// required for operation
std::atomic_size_t XEPL::pool_mallocs[] = {0};
std::atomic_size_t XEPL::pool_cached[] = {0};
std::atomic_size_t XEPL::pool_freed[] = {0};
std::atomic_size_t XEPL::pool_held[] = {0};
// optional for observation
std::atomic_size_t XEPL::total_mallocs ( 0 );
std::atomic_size_t XEPL::total_cached ( 0 );
std::atomic_size_t XEPL::total_freed ( 0 );
std::atomic_size_t XEPL::total_held ( 0 );
std::atomic_size_t XEPL::total_biggies_out ( 0 );
std::atomic_size_t XEPL::total_biggies_in ( 0 );
std::atomic_size_t XEPL::largest_biggie ( 0 );
// 888888ba dP a88888b. dP
// 88 `8b 88 d8' `88 88
// a88aaaa8P' .d8888b. .d8888b. dP dP .d8888b. 88 .d8888b. 88d888b. 88 .d8888b. dP dP 88d888b. d8888P .d8888b.
// 88 `8b. 88ooood8 88' `"" 88 88 88' `"" 88 88ooood8 88' `88 88 88' `88 88 88 88' `88 88 Y8ooooo.
// 88 88 88. ... 88. ... 88. .88 88. ... 88 88. ... 88 Y8. .88 88. .88 88. .88 88 88 88 88
// dP dP `88888P' `88888P' `8888P88 `88888P' dP `88888P' dP Y88888P' `88888P' `88888P' dP dP dP `88888P'
// .88
// d8888P
XEPL::RecycleCounts::~RecycleCounts()
{
long biggies_out = total_biggies_out;
long biggies_in = total_biggies_in;
long leaking = total_mallocs - total_freed - total_held;
if ( std_ostream && ( Show_Memory_Counts || leaking ) )
{
const long width = 13;
*std_ostream << "\nCounter: Total : ";
for ( long index = 1; index <= Memory::maxPoolIndex; ++index )
*std_ostream << std::setw ( width ) << ( index+1 )*Memory::poolWidth << " ";
Print_Pool_Counts ( std_ostream, "Mallocs: ", total_mallocs, pool_mallocs, width );
Print_Pool_Counts ( std_ostream, "Freed: ", total_freed, pool_freed, width );
Print_Pool_Counts ( std_ostream, "Cached: ", total_cached, pool_cached, width );
Print_Pool_Counts ( std_ostream, "Held: ", total_held, pool_held, width );
String counts;
Long_Commafy ( biggies_out, &counts );
*std_ostream << "\nBiggies: " << std::setw( width ) << counts;
counts.assign(" : Largest: " );
Long_In_Bytes ( largest_biggie, &counts );
*std_ostream << counts << std::endl;
}
if ( leaking )
std::cerr << " ***LEAKING " << leaking << " Recycled Allocations: " << std::endl;
if ( biggies_in-biggies_out )
std::cerr << " ***LEAKING " << biggies_in-biggies_out << " Biggie Allocations: " << std::endl;
}
XEPL::RecycleCounts::RecycleCounts ( std::ostream* _ostream )
: std_ostream ( _ostream )
{
total_mallocs = 0;
total_cached = 0;
total_freed = 0;
total_held = 0;
total_biggies_out = 0;
total_biggies_in = 0;
largest_biggie = 0;
}
void XEPL::PoolOfBlocks::Report( String* _into )
{
_into->append( std::to_string(block_size) ).append( ":(" );
_into->append( std::to_string(blocks_malloced)).append(",");
_into->append( std::to_string(blocks_freed) ).append(",");
_into->append( std::to_string(blocks_cached) ).append(",");
_into->append( std::to_string(blocks_holding) ).append(") ");;
}
void XEPL::HeapOfPools::Report(String* _into )
{
_into->append("[");
_into->append( std::to_string(largest_biggie_out) ).append( "," );
_into->append( std::to_string(count_biggies_out) ).append( "/" );
_into->append( std::to_string(count_biggies_in) ).append("] ");
for ( long index = 1; index <= Memory::maxPoolIndex; ++index )
pool_of_blocks[index]->Report( _into );
}
void XEPL::Recycler::Report_Heap( String* _into )
{
tlsHeap->Report( _into );
}
void XEPL::RecycleCounts::Print_Pool_Counts ( std::ostream* _report, Text* _label, const std::atomic_size_t& _total, std::atomic_size_t* _array, int _width )
{
long mega_sum = 0;
String counts;
*_report << "\n" << _label << std::setw ( _width ) << *Long_Commafy ( _total, &counts ) << " : ";
for ( long index = 1; index <= Memory::maxPoolIndex; ++index )
{
counts.clear();
*_report << std::setw ( _width ) << *Long_Commafy ( _array[index], &counts ) << " ";
mega_sum += ( index+1 )*Memory::poolWidth * _array[index];
}
counts.clear();
*_report << std::setw ( _width+3 ) << *Long_In_Bytes ( mega_sum, &counts );
}
// a88888b. dP
// d8' `88 88
// 88 .d8888b. dP dP 88d888b. d8888P .d8888b. 88d888b. .d8888b.
// 88 88' `88 88 88 88' `88 88 88ooood8 88' `88 Y8ooooo.
// Y8. .88 88. .88 88. .88 88 88 88 88. ... 88 88
// Y88888P' `88888P' `88888P' dP dP dP `88888P' dP `88888P'
//
/// per thread counter descriptions
const char* XEPL::Counters::Counter_Descriptions = "\
genes:\
traits:\
lobes:\
neurons:\
dispatched:\
rests:\
actions:\
wakes:\
";
void XEPL::Counters::Add ( Counters* _counters )
{
long record_size = sizeof ( Counters );
long number_of_counters = record_size/sizeof ( long );
Counter* scounter = (Counter*) ( _counters );
Counter* tcounter = (Counter*) ( this );
while ( number_of_counters-- )
*tcounter+++=*scounter++;
}
void XEPL::Counters::Report ( String* _string )
{
String long_label ( Counter_Descriptions );
String lhs;
Counter* tcounter = (Counter*) ( this );
_string->append( "" );
while ( Split_ch_lhs ( &long_label, ':', &lhs ) )
{
_string->append ( lhs ).append ( ": " );
_string->append ( std::to_string ( *tcounter ) ).append ( " " );
tcounter++;
}
}
void XEPL::Counters::Final_Report()
{
String long_label ( Counter_Descriptions );
String lhs;
Counter* tcounter = (Counter*) ( this );
String count;
while ( Split_ch_lhs ( &long_label, ':', &lhs ) )
{
count.clear();
Long_Commafy ( *tcounter++, &count );
std::cout << std::setw ( 12 ) << lhs << std::setw ( 15 )
<< count << '\n';
}
std::cout << std::flush;
}
// .d888888 dP
// d8' 88 88
// 88aaaaa88a d8888P .d8888b. 88d8b.d8b.
// 88 88 88 88' `88 88'`88'`88
// 88 88 88 88. .88 88 88 88
// 88 88 dP `88888P' dP dP dP
//
XEPL::Atom::~Atom( void )
{}
void XEPL::Atom::Release( void )
{
if ( --retain_count == 0 )
delete this;
}
// a88888b. dP oo
// d8' `88 88
// 88 88d888b. .d8888b. dP 88d888b.
// 88 88' `88 88' `88 88 88' `88
// Y8. .88 88 88 88. .88 88 88 88
// Y88888P' dP dP `88888P8 dP dP dP
//
XEPL::Chain::~Chain()
{
if ( head_bond )
{
MutexScope lock_chain ( chain_lock );
Bond* bond = head_bond;
while ( bond )
{
Bond* next = bond->next_bond;
delete bond;
bond = next;
}
}
if ( is_my_lock )
delete chain_lock;
}
XEPL::Chain::Chain ( bool _locked )
: chain_lock ( nullptr )
, head_bond ( nullptr )
, tail_bond ( nullptr )
, is_my_lock ( _locked )
{
if ( is_my_lock )
chain_lock = new Mutex();
}
XEPL::Chain::Chain ( Atom* _atom )
: chain_lock ( nullptr )
, head_bond ( new Bond ( _atom, nullptr ) )
, tail_bond ( head_bond )
, is_my_lock ( false )
{}
XEPL::Bond* XEPL::Chain::Add_Atom ( Atom* _atom )
{
MutexScope lock_chain ( chain_lock );
Bond* fresh_bond = new Bond ( _atom, tail_bond );
if ( !head_bond )
head_bond = fresh_bond;
else
tail_bond->next_bond = fresh_bond;
tail_bond = fresh_bond;
return fresh_bond;
}
bool XEPL::Chain::Pull_Atom ( Atom** _atom )
{
MutexScope lock_chain ( chain_lock );
if ( head_bond )
{
Bond* last_head = head_bond;
*_atom = head_bond->atom;
head_bond->atom = nullptr;
head_bond = head_bond->next_bond;
delete last_head;
if ( head_bond )
head_bond->prev_bond = nullptr;
else
tail_bond = nullptr;
return true;
}
return false;
}
bool XEPL::Chain::Remove_Atom ( Atom* _atom )
{
MutexScope lock_chain ( chain_lock );
Bond* bond = head_bond;
while ( bond && bond->atom != _atom )
bond=bond->next_bond;
if ( !bond )
return !!head_bond;
if ( bond == head_bond )
head_bond = bond->next_bond;
else
bond->prev_bond->next_bond = bond->next_bond;
if ( bond == tail_bond )
tail_bond = bond->prev_bond;
else
bond->next_bond->prev_bond = bond->prev_bond;
delete bond;
return !head_bond;
}
void XEPL::Chain::Remove_Bond ( Bond* _bond )
{
MutexScope lock_chain ( chain_lock );
if ( _bond->prev_bond )
_bond->prev_bond->next_bond = _bond->next_bond;
else
head_bond = _bond->next_bond;
if ( _bond->next_bond )
_bond->next_bond->prev_bond = _bond->prev_bond;
else
tail_bond = _bond->prev_bond;
delete _bond;
}
// a88888b. dP dP
// d8' `88 88 88
// 88 .d8888b. 88 88
// 88 88ooood8 88 88
// Y8. .88 88. ... 88 88
// Y88888P' `88888P' dP dP
//
XEPL::Cell::~Cell()
{
delete cell_name;
}
XEPL::Cell::Cell ( Cord* _cord )
: Atom()
, cell_name ( new Cord ( _cord ) )
{}
XEPL::Cell::Cell ( Text* _chars )
: Atom()
, cell_name ( new Cord ( _chars ) )
{}
// a88888b. dP
// d8' `88 88
// 88 .d8888b. 88d888b. d8888P .d8888b. dP. .dP
// 88 88' `88 88' `88 88 88ooood8 `8bd8'
// Y8. .88 88. .88 88 88 88. ... .d88b.
// Y88888P' `88888P' dP dP `88888P' dP' `dP
//
XEPL::Cortex* XEPL::cortex = nullptr;
void XEPL::Cortex::Close_Cortex( void )
{
if( !host_lobe->index_link )
return;
host_lobe->Lobe_Dying();
host_lobe->index_link->Release();
host_lobe->indicies.Unstack( host_lobe );
host_lobe->index_link = nullptr;
}
XEPL::Cortex::~Cortex()
{
Close_Cortex();
host_lobe->Release();
host_lobe = nullptr;
tlsLobe = nullptr;
delete keywords_map;
delete operators_map;
delete render_map;
delete mutual_map;
delete commands_map;
cortex = nullptr;
if ( Show_Counters )
final_counters.Final_Report();
}
XEPL::Cortex::Cortex ( Text* _name, std::ostream& _ostream )
: memory_counts ( &_ostream )
, recycle_counts ( &_ostream )
, memory_backpack ()
, keywords_map ( new KeywordsMap() )
, operators_map ( new OperatorsMap() )
, commands_map ( new CommandsMap() )
, mutual_map ( new MutualsMap() )
, render_map ( new RenderMap() )
, host_lobe ( tlsLobe = new Lobe ( _name ) )
, short_term_memories()
, final_counters ()
{
cortex = this;
Set_Thread_Name ( host_lobe->cpp_thread, _name );
host_lobe->Attach();
host_lobe->indicies.Stack(host_lobe, new Gene ( nullptr, "Index", host_lobe->cell_name ));
host_lobe->Lobe_Born();
}
void XEPL::Cortex::Register_Command ( Text* _chars, Command _command )
{
if ( !_chars )
return;
String name_cord ( _chars );
auto [it, noob] = commands_map->insert_or_assign ( name_cord, _command );
if (!noob)
ErrorReport error_report("Replaced command: ", &name_cord );
TRACE( "New_Command", nullptr, &name_cord);
}
bool XEPL::Cortex::Did_Command ( Text* _chars )
{
if ( !_chars || !*_chars )
return false;
String command_name ( _chars );
{
String cmd;
String opt;
Split_ch_lhs_rhs ( &command_name, ' ', &cmd, &opt );
auto it = commands_map->find ( cmd );
if ( it != commands_map->end() )
{
( *it->second ) ( &opt );
return true;
}
}
if ( tlsLobe->Performed_Method ( &command_name, nullptr ) )
return true;
ErrorReport error_report( "Command not understood: ", _chars );
return false;
}
bool XEPL::Cortex::Did_Dot_Tag ( Nucleus* _nucleus, Gene* _call_gene )
{
String neuron_name;
String method_name;
Neuron* target_neuron = nullptr;
Split_ch_lhs_rhs ( _call_gene->cell_name, '.', &neuron_name, &method_name );
if ( _nucleus->Host()->Find_Neuron ( &neuron_name, &target_neuron ) )
return target_neuron->Performed_Method ( &method_name, _call_gene );
return false;
}
void XEPL::Cortex::Register_Keyword ( Text* _chars, Keyword _keyword )
{
if (!_chars)
return;
Cord name_cord(_chars);
auto [it, noob] = keywords_map->insert_or_assign(name_cord, _keyword);
if (!noob)
ErrorReport error_report("Replaced keyword: ", &name_cord);
TRACE( "New_Keyword", nullptr, &name_cord);
}
bool XEPL::Cortex::Did_Keyword ( Nucleus* _nucleus, Gene* _call_gene )
{
auto it = keywords_map->find ( *_call_gene->cell_name );
if ( it == keywords_map->end() )
return false;
auto host = _nucleus->Host();
String param;
String* param_string = ¶m;
XEPL::String content;
if ( _call_gene->Copy_Content(&content) )
{
if ( *content.c_str() == '{' )
Script ( host, _call_gene, ¶m );
else
param_string = &content;
}
if ( Show_Trace )
{
String trace_string;
_call_gene->Print_Into(&trace_string, 1);
TRACE( "DO_Keyword", _nucleus->Host(), &trace_string, param_string );
}
it->second ( _nucleus->Host(), _call_gene, param_string );
return true;
}
XEPL::Gene* XEPL::Cortex::Locate_Gene ( Nucleus* _nucleus, Cord* _gene_name )
{
if ( auto ephemerals = tlsLobe->ephemerals )
{
auto it = ephemerals->find ( *_gene_name );
if ( it != ephemerals->end() )
return it->second;
}
Nucleus* current_nucleus = _nucleus;
Gene* located_gene = nullptr;
Gene* vitals = nullptr;
while ( current_nucleus )
{
if ( current_nucleus->observer->Get_First_Gene("Vitals", &vitals ) )
if ( vitals->inner_genes && ( vitals->Get_First_Gene ( _gene_name, &located_gene ) ) )
return located_gene;
current_nucleus = current_nucleus->parent_neuron;
}
auto it = cortex->mutual_map->find ( *_gene_name );
if ( it != cortex->mutual_map->end() )
return it->second ( _nucleus );
return nullptr;
}
XEPL::Neuron* XEPL::Cortex::Locate_Neuron ( Nucleus* _nucleus, Cord* _cord, char _split_char )
{
Neuron* neuron = nullptr;
size_t spot = _cord->find ( _split_char );
if ( spot == _cord->npos )
{
_nucleus->Host()->Find_Neuron ( _cord, &neuron );
return neuron;
}
Cord* remaining_path = _cord;
String neuron_name;
String neuron_root;
neuron = cortex->host_lobe;
while ( neuron && Split_ch_lhs_rhs ( remaining_path, _split_char, &neuron_root, &neuron_name ) )
{
if ( !neuron->Find_Neuron ( &neuron_root, &neuron ) )
break;
remaining_path = &neuron_name;
}
if ( neuron )
neuron->Find_Neuron ( &neuron_name, &neuron );
return neuron;
}
XEPL::Axon* XEPL::Cortex::Locate_Axon ( Nucleus* _nucleus, Cord* _cord, char _split_char )
{
if ( !_cord )
return nullptr;
Axon* located_axon = nullptr;
String axon_name;
String path_string;
if ( Split_ch_lhs_rhs ( _cord, _split_char, &path_string, &axon_name ) )
{
Neuron* located_neuron = nullptr;
if ( _nucleus->Host()->Find_Neuron ( &path_string, &located_neuron ) )
located_neuron->Get_Axon ( &axon_name, &located_axon );
}
else
_nucleus->Host()->Hunt_Axon ( &axon_name, &located_axon );
return located_axon;
}