forked from telosprotocol/xbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmem.h
executable file
·2243 lines (2054 loc) · 99.3 KB
/
xmem.h
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
// Copyright (c) 2018-2020 Telos Foundation & contributors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <stdlib.h>
#include <memory.h>
#include <list>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include "xbase.h"
#include "xatom.h"
#include "xint.h"
namespace top
{
namespace base
{
class xbuffer_t;
class xcontext_t;
/////////////////////////////////////thread-memory-pool support//////////////////////////////////////////////////
//Note:xmalloc and xfree are paired,but has same memory layout as system lib,so actually it is ok use ::free(xmalloc()) or xfree(::malloc());
//But one thing must be konw: xmalloc may alloc more bytes than want,caller must record the actually alloced sizes from param.
//xmalloc may carry out the actually allocated memory by parameter "nSize"
//Note: xmem_alloc has better perforamnce when alloc <= 65535 bytes
void* xmalloc(xcontext_t& _context,int32_t & nSize); //try malloc from thread-local-pool if less than 64KB,then try from system malloc
void* xmalloc(xcontext_t& _context,int32_t & nSize,int32_t & thread_id); //get current thread_id as well,just for performance reason
void xfree(xcontext_t& _context,void* pPtr,const int32_t nSize = 0); //to return memory to pool,need carry the correct size(know it from xmalloc) , caller may also pass 0 for nSize if dont remember the actual nSize--which may free memory immediately instead of return back to pool
//return how many actual bytes will allocted for input_Size
int32_t xsizeof(xcontext_t& _context,int32_t input_Size);
//query the cached memory for current thread
int32_t xdbg_get_cached_memory(xcontext_t& _context);
//clear the cached meory at current thread,return the total cached size
int32_t xdbg_clear_cached_memory(xcontext_t& _context);
/////////////////////////////////////thread-memory-pool support//////////////////////////////////////////////////
/////////////////////////////////////x_mem_handle//////////////////////////////////////////////////////////////
//lowest 5 bit of flags
enum enum_xmem_handle_flag
{
//default it is not shared,once reference > 1 ->shared mode, reference == 1 -->non-share mode
enum_xmem_handle_flag_freed = 0, //debug purpose, flag the memory is already freed
enum_xmem_handle_flag_shared = 1, //once memory ptr shared,any write must do clone_on_write
enum_xmem_handle_flag_locked = 2, //once memory ptr locked,any write must do clone_on_write
};
//4bit for header alloc policy
enum enum_xmem_alloc_type
{
enum_xmem_alloc_type_invalid = 0, //uninited and invalid
enum_xmem_alloc_type_xmalloc = 1, //content memory from thread-local-storage
enum_xmem_alloc_type_sysmalloc = 2, //c malloc
enum_xmem_alloc_type_new = 3, //c++ new[]
enum_xmem_alloc_type_static = 4, //persist and static memory(like global data) and dont need free
enum_xmem_alloc_type_from_owner = 5, //data from object self; if object live , memory is live
enum_xmem_alloc_type_temp = 6, //stack calloc or other non-persist memory,which means needs not free but need use soon
enum_xmem_alloc_policy_max = 7 //only 7 memory policy allowed
};
//the following api are safe for multiple-thread write
xmem_handle xmem_handle_alloc(xcontext_t & _context,const uint32_t target_size); //malloc memory as our own memory pool with customized memory layout
//please get final capacity by xmem_handle_capacity after xmem_handle_alloc where need occupy 20+ bytes to manage from pMemPtr
xmem_handle xmem_handle_alloc(xcontext_t & _context,const uint8_t* ptr_mem,const int32_t capacity,enum_xmem_alloc_type mem_type);//ptr_mem should not share with other mem_handle ,otherwise it may cause multiple-thread problem;note: ptr_mem is takeover by xmem_handle
int32_t xmem_handle_addref(xcontext_t & _context,xmem_handle handle); //add reference for the memory ptr
int32_t xmem_handle_release(xcontext_t & _context,xmem_handle handle); //reduce reference, if 0 just free the memory really
void xmem_handle_set_flag(xmem_handle handle,enum_xmem_handle_flag eflag);
bool xmem_handle_test_flag(xmem_handle handle,enum_xmem_handle_flag eflag); //test whether flag is set
uint8_t* xmem_handle_data(xmem_handle handle); //return orignal data ptr
int32_t xmem_handle_reference(xmem_handle handle); //return current reference count
uint8_t xmem_handle_type(xmem_handle handle); //return the type associated with mem ptr,refer enum_xmem_alloc_type
int32_t xmem_handle_capacity(xmem_handle handle); //return the allocated size for this ptr
bool xmem_handle_writeable(xmem_handle handle); //test whether writable
struct xmemh_t
{
friend class xpacket_t;
friend class xsocket_t;
friend class udp_t;
friend class xudp_t;
friend class tcp_t;
friend class xtcp_t;
public:
xmemh_t(xcontext_t & _context); //advance use case, that init as empty then copy data from others
explicit xmemh_t(xcontext_t & _context,xmem_handle handle);
//as default use ju_mem_alloc to alloc memory from thread-local-storage
explicit xmemh_t(xcontext_t & _context,uint32_t nInitCapacity);
explicit xmemh_t(xcontext_t & _context,uint32_t nInitOffset,uint32_t nInitCapacity);
//nCapacity must be at least 16 bytes ,allow caller pass in the pre-alloc memory,
//both are just copy ptr and set share flag
xmemh_t(xcontext_t & _context,const xmemh_t & obj);
xmemh_t & operator = (const xmemh_t & obj);
~xmemh_t(); //no virtual function, so sub-object force cast to xmemh_t* and delete xmemh_t* ,which maybe cause memory leak
protected://advance purpose and use carefully
//Note: allow caller pass in the pre-alloc memory(ptr_mem takeover by xmem_h)
//note: xmemh_t may overwrite the first 24 bytes of ptr_mem
xmemh_t(xcontext_t & _context,const uint8_t * ptr_mem,uint32_t nInitOffset,uint32_t nInitCapacity,enum_xmem_alloc_type mem_type);
protected: //disable new/delete operator to force copy or pass by reference
void* operator new(size_t size);
void operator delete(void *p);
public: //basic use case
bool empty() const; //test whether has data to read
uint8_t * data() const; //point to real data position(already apply the front_offset)
uint8_t * back() const; //point to next available position to write/push back data
int32_t size() const; //how many bytes are stored
int32_t front_offset() const; //front offset from root(0)
int32_t back_offset() const; //back offset from root(0), [front,back) is the data range
int32_t capacity() const; //malloc memory
int32_t spare_size() const {return (capacity() - back_offset());} //spare size to write in from back
int32_t set_max_capacity(const int32_t new_max_size);//as default max_capacity is 4MByte for mobile; return -1 if have error
//Note: write to the next avaiable data position like FIFO, just change offset if pPtr is NULL
//Note: ask_mem_barrier support single-thread write, and another single thread read safe
int32_t push_front(uint8_t* pPtr, const uint32_t nSize);
int32_t push_back(uint8_t* pPtr, const uint32_t nSize);
//Note: just modify front/back offset but without content chagne ,so treat pop as read operation
//pop the data at front end,caller decide how many bytes need pop(actually just adjust offset)
//return how many bytes are popped out
int32_t pop_front(const uint32_t nSize);
int32_t pop_back(const uint32_t nSize); //pop the data at back end
int32_t pop_front(uint8_t* buffer,const uint32_t size_to_read); //pop data to passed in buffer at front
int32_t pop_back(uint8_t* buffer,const uint32_t size_to_read); //pop data to passed in buffer at back
int32_t pop_front(xbuffer_t & block,const uint32_t size_to_read);
int32_t pop_back(xbuffer_t & block,const uint32_t size_to_read);
int32_t pop_front(std::string & content,const uint32_t size_to_read);
int32_t pop_back(std::string & content,const uint32_t size_to_read);
//as multiple-thread safe, not provide api to unlock anymore
bool lock(); //once lock,any write must do clone_on_write,that provider thread safe for multiple writer
bool is_readonly(); //test two condition by lock and shared
bool is_locked() const; //test whether memory is locked or not
bool is_shared() const; //test whether shared memory or not
bool is_temp() const; //test whether the memory is from stack or other temp memory
uint32_t get_alloc_type() const;
public: //basic serialize support as little-endian format
int32_t operator << (const bool value)
{
reserved_push_back(sizeof(int8_t));
int8_t * pPoint = (int8_t*)(back());
*pPoint = (int8_t)value;
push_back(NULL, sizeof(int8_t));
return sizeof(int8_t);
}
#ifdef __XSTRICT_64BIT_ADDRESS_ACCESS___
template<class T>
int32_t operator << (const T & value) //T must be natvie type like int8/int16/int32/int64/uint8/uint16/uint32/uint64
{
reserved_push_back(sizeof(T));
push_back((uint8_t*)&value, sizeof(T));
return sizeof(T);
}
#else
int32_t operator << (const char & value) //write in data as most performance way
{
reserved_push_back(sizeof(int8_t));
int8_t * pPoint = (int8_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(int8_t));
return sizeof(int8_t);
}
int32_t operator << (const int8_t & value) //write in data as most performance way
{
reserved_push_back(sizeof(int8_t));
int8_t * pPoint = (int8_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(int8_t));
return sizeof(int8_t);
}
int32_t operator << (const int16_t & value)
{
reserved_push_back(sizeof(int16_t));
int16_t * pPoint = (int16_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(int16_t));
return sizeof(int16_t);
}
int32_t operator << (const int32_t & value)
{
reserved_push_back(sizeof(int32_t));
int32_t * pPoint = (int32_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(int32_t));
return sizeof(int32_t);
}
int32_t operator << (const int64_t & value)
{
reserved_push_back(sizeof(int64_t));
int64_t * pPoint = (int64_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(int64_t));
return sizeof(int64_t);
}
int32_t operator << (const uint8_t & value)
{
reserved_push_back(sizeof(uint8_t));
uint8_t * pPoint = (uint8_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(uint8_t));
return sizeof(uint8_t);
}
int32_t operator << (const uint16_t & value)
{
reserved_push_back(sizeof(uint16_t));
uint16_t * pPoint = (uint16_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(uint16_t));
return sizeof(uint16_t);
}
int32_t operator << (const uint32_t & value)
{
reserved_push_back(sizeof(uint32_t));
uint32_t * pPoint = (uint32_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(uint32_t));
return sizeof(uint32_t);
}
int32_t operator << (const uint64_t & value)
{
reserved_push_back(sizeof(uint64_t));
uint64_t * pPoint = (uint64_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(uint64_t));
return sizeof(uint64_t);
}
#endif //end of __XSTRICT_64BIT_ADDRESS_ACCESS___
//GCC 4.7.1+ or Clang support extend 128bit type
#if defined(__LINUX_PLATFORM__) || defined(__MAC_PLATFORM__)
int32_t operator << (const __uint128_t & value)
{
reserved_push_back(sizeof(__uint128_t));
__uint128_t * pPoint = (__uint128_t*)(back());
*pPoint = value;
push_back(NULL, sizeof(__uint128_t));
return sizeof(__uint128_t);
}
#endif
template<int predefine_bits>
int32_t operator << (const xuint_t<predefine_bits> & value) //write in data
{
const uint32_t data_size = predefine_bits/8;
reserved_push_back(data_size);
if(data_size > 0)
{
push_back(value.data(),data_size);
return (data_size);
}
else
{
return 0;
}
}
int32_t operator << (const std::string & value) //write in data
{
const uint32_t nStrSize = (uint32_t)value.size();
reserved_push_back(nStrSize + sizeof(uint32_t));
*this << nStrSize;
if(nStrSize > 0)
{
push_back((uint8_t*)value.data(),nStrSize);
return (nStrSize + sizeof(uint32_t));
}
else
{
return sizeof(uint32_t);
}
}
//Note:T must be natvie type like std::string/int8/int16/int32/int64/uint8/uint16/uint32/uint64
template<typename T>
int32_t operator << (const std::vector<T> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back(sizeof(T) * item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
for(auto & it : container)
*this << it;
return (size() - begin_size);
}
//optimization std::vector<char>
int32_t operator << (const std::vector<char> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back(sizeof(char) * item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
push_back((uint8_t*)&container[0],item_size);
return (size() - begin_size);
}
int32_t operator << (const std::vector<signed char> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back(sizeof(char) * item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
push_back((uint8_t*)&container[0],item_size);
return (size() - begin_size);
}
int32_t operator << (const std::vector<unsigned char> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back(sizeof(char) * item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
push_back((uint8_t*)&container[0],item_size);
return (size() - begin_size);
}
template<typename T>
int32_t operator << (const std::list<T> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back(sizeof(T) * item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
for(auto & it : container)
*this << it;
return (size() - begin_size);
}
template<typename T>
int32_t operator << (const std::set<T> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back(sizeof(T) * item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
for(auto & it : container)
*this << it;
return (size() - begin_size);
}
template<typename KEY_T,typename VALUE_T>
int32_t operator << (const std::map<KEY_T,VALUE_T> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back((sizeof(KEY_T) + sizeof(VALUE_T))* item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
for(auto & it : container)
{
*this << it.first;
*this << it.second;
}
return (size() - begin_size);
}
template<typename KEY_T,typename VALUE_T>
int32_t operator << (const std::unordered_map<KEY_T,VALUE_T> & container) //write in data as most performance way
{
const int begin_size = size();
const uint32_t item_size = (uint32_t)container.size();
reserved_push_back((sizeof(KEY_T) + sizeof(VALUE_T))* item_size + sizeof(uint32_t)); //at least reserved the amount
*this << item_size;
for(auto & it : container)
{
*this << it.first;
*this << it.second;
}
return (size() - begin_size);
}
int32_t operator << (const xbuffer_t & value); //write in data
int32_t operator >> (bool & value) //read out data
{
if(size() < (int32_t)sizeof(int8_t))
{
xerror("xmemh_t >> bool fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
int8_t * pPoint = (int8_t*)(data());
value = (bool)(*pPoint);
pop_front(sizeof(int8_t));
return sizeof(int8_t);
}
#ifdef __XSTRICT_64BIT_ADDRESS_ACCESS___
template<class T>
int32_t operator >> (T & value) //T must be native type like int8/int16/int32/int64/uint8/uint16/uint32/uint64
{
if(size() < (int32_t)sizeof(T))
{
xerror("xmemh_t >> T(%d) fail as unenough data(%d)",sizeof(T),size());
throw enum_xerror_code_bad_packet;
return 0;
}
memcpy(&value,data(),sizeof(T));
pop_front(sizeof(T));
return sizeof(T);
}
#else
int32_t operator >> (char & value) //read out data
{
if(size() < (int32_t)sizeof(char))
{
xerror("xmemh_t >> char fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
char * pPoint = (char*)(data());
value = *pPoint;
pop_front(sizeof(char));
return sizeof(char);
}
int32_t operator >> (int8_t & value) //read out data
{
if(size() < (int32_t)sizeof(int8_t))
{
xerror("xmemh_t >> int8_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
int8_t * pPoint = (int8_t*)(data());
value = *pPoint;
pop_front(sizeof(int8_t));
return sizeof(int8_t);
}
int32_t operator >> (int16_t & value) //read out data
{
if(size() < (int32_t)sizeof(int16_t))
{
xerror("xmemh_t >> int16_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
int16_t * pPoint = (int16_t*)(data());
value = *pPoint;
pop_front(sizeof(int16_t));
return sizeof(int16_t);
}
int32_t operator >> (int32_t & value) //read out data
{
if(size() < (int32_t)sizeof(int32_t))
{
xerror("xmemh_t >> int32_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
int32_t * pPoint = (int32_t*)(data());
value = *pPoint;
pop_front(sizeof(int32_t));
return sizeof(int32_t);
}
int32_t operator >> (int64_t & value) //read out data
{
if(size() < (int32_t)sizeof(int64_t))
{
xerror("xmemh_t >> int64_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
int64_t * pPoint = (int64_t*)(data());
value = *pPoint;
pop_front(sizeof(int64_t));
return sizeof(int64_t);
}
int32_t operator >> (uint8_t & value) //read out data
{
if(size() < (int32_t)sizeof(uint8_t))
{
xerror("xmemh_t >> uint8_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
uint8_t * pPoint = (uint8_t*)(data());
value = *pPoint;
pop_front(sizeof(uint8_t));
return sizeof(uint8_t);
}
int32_t operator >> (uint16_t & value) //read out data
{
if(size() < (int32_t)sizeof(uint16_t))
{
xerror("xmemh_t >> uint16_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
uint16_t * pPoint = (uint16_t*)(data());
value = *pPoint;
pop_front(sizeof(uint16_t));
return sizeof(uint16_t);
}
int32_t operator >> (uint32_t & value) //read out data
{
if(size() < (int32_t)sizeof(uint32_t))
{
xerror("xmemh_t >> uint32_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
uint32_t * pPoint = (uint32_t*)(data());
value = *pPoint;
pop_front(sizeof(uint32_t));
return sizeof(uint32_t);
}
int32_t operator >> (uint64_t & value) //read out data
{
if(size() < (int32_t)sizeof(uint64_t))
{
xerror("xmemh_t >> uint64_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
uint64_t * pPoint = (uint64_t*)(data());
value = *pPoint;
pop_front(sizeof(uint64_t));
return sizeof(uint64_t);
}
#endif //end of __XSTRICT_64BIT_ADDRESS_ACCESS___
//GCC 4.7.1+ or Clang support extend 128bit type
#if defined(__LINUX_PLATFORM__) || defined(__MAC_PLATFORM__)
int32_t operator >> (__uint128_t & value) //read out data
{
if(size() < (int32_t)sizeof(__uint128_t))
{
xerror("xmemh_t >> __uint128_t fail as unenough data(%d)",size());
throw enum_xerror_code_bad_packet;
return 0;
}
__uint128_t * pPoint = (__uint128_t*)(data());
value = *pPoint;
pop_front(sizeof(__uint128_t));
return sizeof(__uint128_t);
}
#endif
template<int _predefine_bits_>
int32_t operator >> (xuint_t<_predefine_bits_> & value) //read out data
{
uint32_t memory_size = _predefine_bits_ / 8; //convert bits to bytes
if((uint32_t)size() < memory_size)
{
xerror("xmemh_t >> xuint_t<%d> fail as unenough data(%d)",_predefine_bits_,size());
throw enum_xerror_code_bad_packet;
return 0;
}
memcpy(value.data(),data(),memory_size);
pop_front(memory_size);
return (memory_size);
}
int32_t operator >> (std::string & value) //read out data
{
uint32_t nStrSize = 0;
int32_t nRet = *this >> nStrSize;
if(0 == nRet) //dont have any data
{
xassert(nRet > 0);
throw enum_xerror_code_bad_packet;
return 0;
}
if(0 == nStrSize) //empty string
{
value.clear();
return sizeof(uint32_t);
}
if((uint32_t)size() < nStrSize)
{
xerror("xmemh_t >> string(%d) fail as unenough data(%d)",nStrSize,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
value.assign((char*)data(),nStrSize);
pop_front(nStrSize);
return (nStrSize + sizeof(uint32_t));
}
//Note:T must be natvie type like std::string/int8/int16/int32/int64/uint8/uint16/uint32/uint64
template<typename T>
int32_t operator >> (std::vector<T> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
const uint32_t ask_size = item_size;
if((uint32_t)size() < ask_size)
{
xerror("xmemh_t >> std::vector<T>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
//for safety, using slow way to read out
for(uint32_t i = 0; i < item_size; ++i)
{
T _item;
*this >> _item;
container.emplace_back(_item);
}
return (begin_size - size());
}
//optimization std::vector<char>
int32_t operator >> (std::vector<char> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
if((uint32_t)size() < item_size * sizeof(char))
{
xerror("xmemh_t >> std::vector<char>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
const int org_container_size = (int)container.size();
container.resize(org_container_size + item_size); //safe to resize now
memcpy(&container[org_container_size],data(),item_size); //copy whole data to container
pop_front(item_size);
return (begin_size - size());
}
int32_t operator >> (std::vector<signed char> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
if((uint32_t)size() < item_size * sizeof(char))
{
xerror("xmemh_t >> std::vector<signed char>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
const int org_container_size = (int)container.size();
container.resize(org_container_size + item_size); //safe to resize now
memcpy(&container[org_container_size],data(),item_size); //copy whole data to container
pop_front(item_size);
return (begin_size - size());
}
int32_t operator >> (std::vector<unsigned char> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
if((uint32_t)size() < item_size * sizeof(char))
{
xerror("xmemh_t >> std::vector<unsigned char>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
const int org_container_size = (int)container.size();
container.resize(org_container_size + item_size); //safe to resize now
memcpy(&container[org_container_size],data(),item_size); //copy whole data to container
pop_front(item_size);
return (begin_size - size());
}
template<typename T>
int32_t operator >> (std::list<T> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
const uint32_t ask_size = item_size;
if((uint32_t)size() < ask_size)
{
xerror("xmemh_t >> std::list<T>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
//for safety, using slow way to read out
for(uint32_t i = 0; i < item_size; ++i)
{
T _item;
*this >> _item;
container.emplace_back(_item);
}
return (begin_size - size());
}
template<typename T>
int32_t operator >> (std::set<T> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
const uint32_t ask_size = item_size;
if((uint32_t)size() < ask_size)
{
xerror("xmemh_t >> std::set<T>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
//for safety, using slow way to read out
for(uint32_t i = 0; i < item_size; ++i)
{
T _item;
*this >> _item;
container.emplace(_item);
}
return (begin_size - size());
}
template<typename KEY_T,typename VALUE_T>
int32_t operator >> (std::map<KEY_T,VALUE_T> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
const uint32_t ask_size = (2)* item_size;//minimal size
if((uint32_t)size() < ask_size)
{
xerror("xmemh_t >> std::map<KEY,VALUE>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
//for safety, using slow way to read out
for(uint32_t i = 0; i < item_size; ++i)
{
std::pair<KEY_T,VALUE_T> _item;
*this >> _item.first;
*this >> _item.second;
container.emplace(_item);
}
return (begin_size - size());
}
template<typename KEY_T,typename VALUE_T>
int32_t operator >> (std::unordered_map<KEY_T,VALUE_T> & container) //write in data as most performance way
{
const int begin_size = size();
uint32_t item_size = 0;
*this >> item_size;
const uint32_t ask_size = (2)* item_size;//minimal size
if((uint32_t)size() < ask_size)
{
xerror("xmemh_t >> std::unordered_map<KEY,VALUE>(%d) fail as unenough data(%d)",item_size,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint32_t);
}
//for safety, using slow way to read out
for(uint32_t i = 0; i < item_size; ++i)
{
std::pair<KEY_T,VALUE_T> _item;
*this >> _item.first;
*this >> _item.second;
container.emplace(_item);
}
return (begin_size - size());
}
int32_t operator >> (xbuffer_t & value); //read out data
//to save space and increase safty, here provide function for short string(max as 64K)
int32_t read_short_string(std::string & value) //read out data
{
uint16_t nStrSize = 0;
int32_t nRet = *this >> nStrSize;
if(0 == nRet) //dont have any data
{
xassert(nRet > 0);
throw enum_xerror_code_bad_packet;
return 0;
}
if(0 == nStrSize) //empty string
{
value.clear();
return sizeof(uint16_t);
}
if((uint32_t)size() < nStrSize)
{
xerror("xmem_h >> short string(%d) fail as unenough data(%d)",nStrSize,size());
throw enum_xerror_code_bad_packet;
return sizeof(uint16_t);
}
value.assign((char*)data(),nStrSize);
pop_front(nStrSize);
return (nStrSize + sizeof(uint16_t));
}
//to save space and increase safty, here provide function for short string(max as 64K)
int32_t write_short_string(const std::string & value) //write in data
{
xdbgassert(value.size() < 65536);
const uint16_t nStrSize = (uint16_t)value.size();
reserved_push_back(nStrSize + sizeof(uint16_t));
*this << nStrSize;
if(nStrSize > 0)
{
push_back((uint8_t*)value.data(),nStrSize);
return (nStrSize + sizeof(uint16_t));
}
else
{
return sizeof(uint16_t);
}
}
public: //advance use case
bool copy_from(const xmemh_t & source); //just copy and shared with both
bool move_from(xmemh_t & source); //copy and reset the source,after move source is already empty
bool attach_from(xmemh_t & source); //replace own by source
//attach is just very light reference and share the object,no-copy/move
bool attach_from(xmem_handle handle,int32_t frontoffset = 0,int32_t backoffset = 0);
//note:data are invalid and lost after init.provide this init to mattch pipe ' requirement.
bool init(){return close();} //init actually same as close,it cose first then init as empty
bool close(); //allow sub class close and init again
//note:data are invalid and lost after reset,but memory is keep there. and initialize offset by default if init_offset < 0
bool reset(const int init_offset = -1); //similar as init but reset try to reuse the used memory for next time,use carefuly
//on_demand create the mem handle if empty, or extend back area to match new bigger capacity without modify rawdata
//if current capacity already more than ask_capacity,nothing to do
bool reserve_back(int32_t ask_writing_size_at_back);//reserve should and may keep original data when expand memory size
protected:
//use carefully, it close existing and reinit,(ptr_mem takeover by xmem_h)
bool init(const uint8_t * pMemPtr,int32_t nInitOffset,int32_t nCapacity,enum_xmem_alloc_type mem_type);
//attach raw buffer(memory) to mem_handle,use very carefully
bool attach(const uint8_t * raw_buffer,uint32_t raw_buffer_size);
//use carefully and internal layer use only, it explore the inside mem handle
xmem_handle raw_mem_handle() const {return _mem_handle;}
protected:
int32_t set_front_offset(const int32_t offset);
int32_t set_back_offset(const int32_t offset);
bool reserved_push_front(const uint32_t nPushBufSize,uint32_t extend_size = 512);
bool reserved_push_back(const uint32_t nPushBufSize,uint32_t extend_size = 512);
private:
bool _is_allow_write_direct() const; //test whether can write directly or must clone-on-write
protected:
uint32_t _front_offset; //front data offset
uint32_t _back_offset; //back offset of data[front_offset,backoffset)
xmem_handle _mem_handle; //point to allocated to write/read area
int32_t _max_capacity; //max size is allowed to alloc,as default it is 4MByte
xcontext_t* _ptr_context; //point to global context object
};
//xautoblock_t preload bytes from stack
template<int __preload_local_buff_size__>
class xautomemh_t : public xmemh_t
{
enum
{
enum_align_to_8bytes = __preload_local_buff_size__ + (8 - __preload_local_buff_size__%8) + 24,//at least 32 bytes
enum_convert_to_8byte_count = enum_align_to_8bytes >> 3
};
public:
xautomemh_t(xcontext_t & _context)
:xmemh_t(_context,(uint8_t*)local_buf,(enum_align_to_8bytes >> 3),enum_align_to_8bytes,enum_xmem_alloc_type_from_owner)
{
}
explicit xautomemh_t(xcontext_t & _context,const uint32_t init_offset)
:xmemh_t(_context,(uint8_t*)local_buf, (init_offset < __preload_local_buff_size__) ? init_offset : (enum_align_to_8bytes >> 3),enum_align_to_8bytes,enum_xmem_alloc_type_from_owner)
{
}
//note: use carefully, xautomemh_t reference the raw data buffer instead of internal memory,
//note: raw_data_buf must be valid until xautomemh_t close
explicit xautomemh_t(xcontext_t & _context,const uint8_t* _data_buffer, const uint32_t _data_buff_size,uint32_t front_offset,uint32_t back_offset)
:xmemh_t(_context,(uint8_t*)local_buf,0,enum_align_to_8bytes,enum_xmem_alloc_type_from_owner)
{
if( (back_offset > front_offset) && (back_offset <= _data_buff_size) ) //_data_buf point raw data
{
attach(_data_buffer,_data_buff_size);
set_front_offset(front_offset);
set_back_offset(back_offset);
}
else //_data_buffer just a memory without raw data
{
init(_data_buffer, front_offset, _data_buff_size, enum_xmem_alloc_type_temp);
}
}
~xautomemh_t()
{
}
void reinit(const uint32_t init_offset = (enum_align_to_8bytes >> 3))//clean alloced memory and reinit by local buffer
{
xmemh_t::init((uint8_t*)local_buf,(init_offset < __preload_local_buff_size__) ? init_offset : (enum_align_to_8bytes >> 3),enum_align_to_8bytes,enum_xmem_alloc_type_from_owner);
}
private:
xautomemh_t();
xautomemh_t(const xautomemh_t &);
xautomemh_t & operator = (const xautomemh_t &);
private:
int64_t local_buf[enum_convert_to_8byte_count];
};
/////////////////////////////////////x_mem_handle//////////////////////////////////////////////////////////////
//Note: xrbuffer_t is a block-based link-buffer that support one signle thread write and single thread read at same time
//xbuffer_t on-demand linked mulitple blocks automatically
template<int _BLOCK_BYTES_COUNT_ = 8000> //each block has 8KB as default
class xrbuffer_t
{
public:
xrbuffer_t(xcontext_t & _context)
{
m_ptr_context = &_context;
int32_t alloc_size = sizeof(block_t);
front_block = xmalloc(_context, alloc_size);
front_block->alloc_size = alloc_size;
front_block->next = NULL;
front_offset = 0;
end_block = front_block;
back_block = end_block;
back_offset = 0;
free_block = 0;
total_size = 0;
xassert (front_block != 0);
}
~xrbuffer_t()
{
while(front_block != NULL)
{
block_t *copy = front_block;
front_block = front_block->next;
if(copy != NULL)
{
xfree(*get_context(),copy,copy->alloc_size);
}
}
block_t * others = free_block.exchange(0);
if(others != NULL)
{
xfree(*get_context(),others,others->alloc_size);
}
}
inline int32_t size() {return total_size;}
inline int32_t get_block_size(){return _BLOCK_BYTES_COUNT_;}
//only allow one single thread call front at any time
uint8_t* front(int & size) //must be test empty() first then allow call front
{
size = _BLOCK_BYTES_COUNT_ - front_offset;
return (front_block->data + front_offset);
}
uint8_t* back(int & size)//how many bytes is continusely to read
{
size = _BLOCK_BYTES_COUNT_ - back_offset;
return (back_block->data + back_offset);
}
int32_t pop_front(uint8_t* data,const int32_t total_count)//only allow one single thread call pop_front at any time
{
if(empty())
return 0;
const int32_t total_allow_read = std::min(size(),total_count);
if(0 == total_allow_read)
return 0;
int32_t readed_count = 0;
while(readed_count < total_allow_read)
{
if(front_offset < _BLOCK_BYTES_COUNT_)//block range[0,_BLOCK_BYTES_COUNT_),left range[front_offset,_BLOCK_BYTES_COUNT_)
{
const int32_t allow_read_block = std::min( (_BLOCK_BYTES_COUNT_- front_offset), (total_allow_read - readed_count));
memcpy(data + readed_count,front_block->data + front_offset,allow_read_block);
front_offset += allow_read_block;
readed_count += allow_read_block;
}
if(front_offset == _BLOCK_BYTES_COUNT_)
{
block_t * current_block = front_offset;