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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
|
#include "ace/Message_Block.h"
#if !defined (__ACE_INLINE__)
#include "ace/Message_Block.inl"
#endif /* __ACE_INLINE__ */
#include "ace/Guard_T.h"
#include "ace/Log_Category.h"
#include "ace/Malloc_Base.h"
#include "ace/OS_NS_string.h"
//#define ACE_ENABLE_TIMEPROBES
#include "ace/Timeprobe.h"
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
ACE_ALLOC_HOOK_DEFINE (ACE_Message_Block)
#if defined (ACE_ENABLE_TIMEPROBES)
static const char *ACE_MB_Timeprobe_Description[] =
{
"Message_Block::init_i - enter",
"Message_Block::init_i - leave",
"Message_Block::init_i - db alloc",
"Message_Block::init_i - db ctor",
"Data_Block::ctor[1] - enter",
"Data_Block::ctor[1] - leave",
"Data_Block::ctor[2] - enter",
"Data_Block::ctor[2] - leave",
"Data_Block::clone - enter",
"Data_Block::clone - leave"
};
enum
{
ACE_MESSAGE_BLOCK_INIT_I_ENTER = 3000,
ACE_MESSAGE_BLOCK_INIT_I_LEAVE,
ACE_MESSAGE_BLOCK_INIT_I_DB_ALLOC,
ACE_MESSAGE_BLOCK_INIT_I_DB_CTOR,
ACE_DATA_BLOCK_CTOR1_ENTER,
ACE_DATA_BLOCK_CTOR1_LEAVE,
ACE_DATA_BLOCK_CTOR2_ENTER,
ACE_DATA_BLOCK_CTOR2_LEAVE,
ACE_DATA_BLOCK_CLONE_ENTER,
ACE_DATA_BLOCK_CLONE_LEAVE
};
// Setup Timeprobes
ACE_TIMEPROBE_EVENT_DESCRIPTIONS (ACE_MB_Timeprobe_Description,
ACE_MESSAGE_BLOCK_INIT_I_ENTER);
#endif /* ACE_ENABLE_TIMEPROBES */
void
ACE_Message_Block::data_block (ACE_Data_Block *db)
{
ACE_TRACE ("ACE_Message_Block::data_block");
if (ACE_BIT_DISABLED (this->flags_,
ACE_Message_Block::DONT_DELETE)
&& this->data_block_ != 0)
this->data_block_->release ();
this->data_block_ = db;
// Set the read and write pointers in the <Message_Block> to point
// to the buffer in the <ACE_Data_Block>.
this->rd_ptr (this->data_block ()->base ());
this->wr_ptr (this->data_block ()->base ());
}
int
ACE_Message_Block::copy (const char *buf, size_t n)
{
ACE_TRACE ("ACE_Message_Block::copy");
/*size_t len = static_cast<size_t> (this->end () - this->wr_ptr ());*/
// Note that for this to work correct, end () *must* be >= mark ().
size_t len = this->space ();
if (len < n)
{
errno = ENOSPC;
return -1;
}
else
{
(void) ACE_OS::memcpy (this->wr_ptr (),
buf,
n);
this->wr_ptr (n);
return 0;
}
}
int
ACE_Message_Block::copy (const char *buf)
{
ACE_TRACE ("ACE_Message_Block::copy");
/* size_t len = static_cast<size_t> (this->end () - this->wr_ptr ()); */
// Note that for this to work correct, end() *must* be >= wr_ptr().
size_t len = this->space ();
size_t buflen = ACE_OS::strlen (buf) + 1;
if (len < buflen)
{
errno = ENOSPC;
return -1;
}
else
{
(void) ACE_OS::memcpy (this->wr_ptr (),
buf,
buflen);
this->wr_ptr (buflen);
return 0;
}
}
int
ACE_Message_Block::crunch ()
{
if (this->rd_ptr_ != 0)
{
if (this->rd_ptr_ > this->wr_ptr_)
return -1;
size_t const len = this->length ();
(void) ACE_OS::memmove (this->base (),
this->rd_ptr (),
len);
this->rd_ptr (this->base ());
this->wr_ptr (this->base () + len);
}
return 0;
}
void
ACE_Data_Block::dump () const
{
#if defined (ACE_HAS_DUMP)
ACE_TRACE ("ACE_Data_Block::dump");
ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
ACELIB_DEBUG ((LM_DEBUG,
ACE_TEXT ("-----( Data Block )-----\n")
ACE_TEXT ("type_ = %d\n")
ACE_TEXT ("cur_size_ = %u\n")
ACE_TEXT ("max_size_ = %u\n")
ACE_TEXT ("flags_ = %u\n")
ACE_TEXT ("base_ = %@\n")
ACE_TEXT ("locking_strategy_ = %u\n")
ACE_TEXT ("reference_count_ = %u\n")
ACE_TEXT ("---------------------------\n"),
this->type_,
this->cur_size_,
this->max_size_,
this->flags_,
this->base_,
this->locking_strategy_,
this->reference_count_));
this->allocator_strategy_->dump ();
ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}
void
ACE_Message_Block::dump () const
{
#if defined (ACE_HAS_DUMP)
ACE_TRACE ("ACE_Message_Block::dump");
ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
ACELIB_DEBUG ((LM_DEBUG,
ACE_TEXT ("-----( Message Block )-----\n")
ACE_TEXT ("priority_ = %d\n")
ACE_TEXT ("next_ = %@\n")
ACE_TEXT ("prev_ = %@\n")
ACE_TEXT ("cont_ = %@\n")
ACE_TEXT ("rd_ptr_ = %@\n")
ACE_TEXT ("wr_ptr_ = %@\n")
ACE_TEXT ("---------------------------\n"),
this->priority_,
this->next_,
this->prev_,
this->cont_,
this->rd_ptr_,
this->wr_ptr_));
this->data_block ()->dump ();
ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}
int
ACE_Data_Block::reference_count () const
{
if (this->locking_strategy_)
{
// We need to acquire the lock before retrieving the count
ACE_GUARD_RETURN (ACE_Lock, ace_mon, *this->locking_strategy_, 0);
return this->reference_count_i ();
}
return this->reference_count_i ();
}
int
ACE_Data_Block::size (size_t length)
{
ACE_TRACE ("ACE_Data_Block::size");
if (length <= this->max_size_)
this->cur_size_ = length;
else
{
// We need to resize!
char *buf = 0;
ACE_ALLOCATOR_RETURN (buf,
(char *) this->allocator_strategy_->malloc (length),
-1);
ACE_OS::memcpy (buf,
this->base_,
this->cur_size_);
if (ACE_BIT_DISABLED (this->flags_,
ACE_Message_Block::DONT_DELETE))
this->allocator_strategy_->free ((void *) this->base_);
else
// We now assume ownership.
ACE_CLR_BITS (this->flags_,
ACE_Message_Block::DONT_DELETE);
this->max_size_ = length;
this->cur_size_ = length;
this->base_ = buf;
}
return 0;
}
int
ACE_Message_Block::size (size_t length)
{
ACE_TRACE ("ACE_Message_Block::size");
// Resize the underlying <ACE_Data_Block>.
if (this->data_block ()->size (length) == -1)
return -1;
return 0;
}
void
ACE_Message_Block::total_size_and_length (size_t &mb_size,
size_t &mb_length) const
{
ACE_TRACE ("ACE_Message_Block::total_size_and_length");
for (const ACE_Message_Block *i = this;
i != 0;
i = i->cont ())
{
mb_size += i->size ();
mb_length += i->length ();
}
}
size_t
ACE_Message_Block::total_size () const
{
ACE_TRACE ("ACE_Message_Block::total_size");
size_t size = 0;
for (const ACE_Message_Block *i = this;
i != 0;
i = i->cont ())
size += i->size ();
return size;
}
size_t
ACE_Message_Block::total_length () const
{
ACE_TRACE ("ACE_Message_Block::total_length");
size_t length = 0;
for (const ACE_Message_Block *i = this;
i != 0;
i = i->cont ())
length += i->length ();
return length;
}
size_t
ACE_Message_Block::total_capacity () const
{
ACE_TRACE ("ACE_Message_Block::total_capacity");
size_t size = 0;
for (const ACE_Message_Block *i = this;
i != 0;
i = i->cont ())
size += i->capacity ();
return size;
}
ACE_Data_Block::ACE_Data_Block ()
: type_ (ACE_Message_Block::MB_DATA),
cur_size_ (0),
max_size_ (0),
flags_ (ACE_Message_Block::DONT_DELETE),
base_ (0),
allocator_strategy_ (0),
locking_strategy_ (0),
reference_count_ (1),
data_block_allocator_ (0)
{
ACE_TRACE ("ACE_Data_Block::ACE_Data_Block");
ACE_FUNCTION_TIMEPROBE (ACE_DATA_BLOCK_CTOR1_ENTER);
ACE_ALLOCATOR (this->allocator_strategy_,
ACE_Allocator::instance ());
ACE_ALLOCATOR (this->data_block_allocator_,
ACE_Allocator::instance ());
}
ACE_Data_Block::ACE_Data_Block (size_t size,
ACE_Message_Block::ACE_Message_Type msg_type,
const char *msg_data,
ACE_Allocator *allocator_strategy,
ACE_Lock *locking_strategy,
ACE_Message_Block::Message_Flags flags,
ACE_Allocator *data_block_allocator)
: type_ (msg_type),
cur_size_ (0), // Reset later if memory alloc'd ok
max_size_ (0),
flags_ (flags),
base_ (const_cast <char *> (msg_data)),
allocator_strategy_ (allocator_strategy),
locking_strategy_ (locking_strategy),
reference_count_ (1),
data_block_allocator_ (data_block_allocator)
{
ACE_TRACE ("ACE_Data_Block::ACE_Data_Block");
ACE_FUNCTION_TIMEPROBE (ACE_DATA_BLOCK_CTOR2_ENTER);
// If the user didn't pass one in, let's use the
// <ACE_Allocator::instance>.
if (this->allocator_strategy_ == 0)
ACE_ALLOCATOR (this->allocator_strategy_,
ACE_Allocator::instance ());
if (this->data_block_allocator_ == 0)
ACE_ALLOCATOR (this->data_block_allocator_,
ACE_Allocator::instance ());
if (msg_data == 0)
{
ACE_ALLOCATOR (this->base_,
(char *) this->allocator_strategy_->malloc (size));
#if defined (ACE_INITIALIZE_MEMORY_BEFORE_USE)
(void) ACE_OS::memset (this->base_,
'\0',
size);
#endif /* ACE_INITIALIZE_MEMORY_BEFORE_USE */
}
// ACE_ALLOCATOR returns on alloc failure but we cant throw, so setting
// the size to 0 (i.e. "bad bit") ...
if (this->base_ == 0)
{
size = 0;
}
// The memory is legit, whether passed in or allocated, so set
// the size.
this->cur_size_ = this->max_size_ = size;
}
ACE_Message_Block::ACE_Message_Block (const char *data,
size_t size,
unsigned long priority)
: flags_ (0),
data_block_ (0)
{
ACE_TRACE ("ACE_Message_Block::ACE_Message_Block");
if (this->init_i (size, // size
MB_DATA, // type
0, // cont
data, // data
0, // allocator
0, // locking strategy
ACE_Message_Block::DONT_DELETE, // flags
priority, // priority
ACE_Time_Value::zero, // execution time
ACE_Time_Value::max_time, // absolute time of deadline
0, // data block
0, // data_block allocator
0) == -1) // message_block allocator
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Message_Block")));
}
ACE_Message_Block::ACE_Message_Block (ACE_Allocator *message_block_allocator)
: flags_ (0),
data_block_ (0)
{
ACE_TRACE ("ACE_Message_Block::ACE_Message_Block");
if (this->init_i (0, // size
MB_DATA, // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
ACE_Message_Block::DONT_DELETE, // flags
0, // priority
ACE_Time_Value::zero, // execution time
ACE_Time_Value::max_time, // absolute time of deadline
0, // data block
0, // data_block allocator
message_block_allocator) == -1) // message_block allocator
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Message_Block")));
}
ACE_Message_Block::ACE_Message_Block (size_t size,
ACE_Message_Type msg_type,
ACE_Message_Block *msg_cont,
const char *msg_data,
ACE_Allocator *allocator_strategy,
ACE_Lock *locking_strategy,
unsigned long priority,
const ACE_Time_Value &execution_time,
const ACE_Time_Value &deadline_time,
ACE_Allocator *data_block_allocator,
ACE_Allocator *message_block_allocator)
:flags_ (0),
data_block_ (0)
{
ACE_TRACE ("ACE_Message_Block::ACE_Message_Block");
if (this->init_i (size,
msg_type,
msg_cont,
msg_data,
allocator_strategy,
locking_strategy,
msg_data ? ACE_Message_Block::DONT_DELETE : 0,
priority,
execution_time,
deadline_time,
0, // data block
data_block_allocator,
message_block_allocator) == -1)
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Message_Block")));
}
int
ACE_Message_Block::init (size_t size,
ACE_Message_Type msg_type,
ACE_Message_Block *msg_cont,
const char *msg_data,
ACE_Allocator *allocator_strategy,
ACE_Lock *locking_strategy,
unsigned long priority,
const ACE_Time_Value &execution_time,
const ACE_Time_Value &deadline_time,
ACE_Allocator *data_block_allocator,
ACE_Allocator *message_block_allocator)
{
ACE_TRACE ("ACE_Message_Block::init");
return this->init_i (size,
msg_type,
msg_cont,
msg_data,
allocator_strategy,
locking_strategy,
msg_data ? ACE_Message_Block::DONT_DELETE : 0,
priority,
execution_time,
deadline_time,
0, // data block
data_block_allocator,
message_block_allocator);
}
int
ACE_Message_Block::init (const char *data,
size_t size)
{
ACE_TRACE ("ACE_Message_Block::init");
// Should we also initialize all the other fields, as well?
return this->init_i (size, // size
MB_DATA, // type
0, // cont
data, // data
0, // allocator
0, // locking strategy
ACE_Message_Block::DONT_DELETE, // flags
0, // priority
ACE_Time_Value::zero, // execution time
ACE_Time_Value::max_time, // absolute time of deadline
0, // data block
0, // data_block allocator
0); // message_block allocator
}
ACE_Message_Block::ACE_Message_Block (size_t size,
ACE_Message_Type msg_type,
ACE_Message_Block *msg_cont,
const char *msg_data,
ACE_Allocator *allocator_strategy,
ACE_Lock *locking_strategy,
Message_Flags flags,
unsigned long priority,
const ACE_Time_Value &execution_time,
const ACE_Time_Value &deadline_time,
ACE_Data_Block *db,
ACE_Allocator *data_block_allocator,
ACE_Allocator *message_block_allocator)
: flags_ (0),
data_block_ (0)
{
ACE_TRACE ("ACE_Message_Block::ACE_Message_Block");
if (this->init_i (size,
msg_type,
msg_cont,
msg_data,
allocator_strategy,
locking_strategy,
flags,
priority,
execution_time,
deadline_time,
db,
data_block_allocator,
message_block_allocator) == -1)
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Message_Block")));
}
ACE_Message_Block::ACE_Message_Block (ACE_Data_Block *data_block,
ACE_Message_Block::Message_Flags flags,
ACE_Allocator *message_block_allocator)
: flags_ (flags),
data_block_ (0)
{
ACE_TRACE ("ACE_Message_Block::ACE_Message_Block");
if (this->init_i (0, // size
MB_NORMAL, // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
0, // flags
0, // priority
ACE_Time_Value::zero, // execution time
ACE_Time_Value::max_time, // absolute time of deadline
data_block, // data block
data_block->data_block_allocator (),
message_block_allocator) == -1)
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Message_Block")));
}
ACE_Message_Block::ACE_Message_Block (const ACE_Message_Block &mb,
size_t align)
:flags_ (0),
data_block_ (0)
{
ACE_TRACE ("ACE_Message_Block::ACE_Message_Block");
if (ACE_BIT_DISABLED (mb.flags_,
ACE_Message_Block::DONT_DELETE))
{
if (this->init_i (0, // size
MB_NORMAL, // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
0, // flags
0, // priority
ACE_Time_Value::zero, // execution time
ACE_Time_Value::max_time, // absolute time of deadline
mb.data_block ()->duplicate (), // data block
mb.data_block ()->data_block_allocator (),
mb.message_block_allocator_) == -1)
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Message_Block")));
#if !defined (ACE_LACKS_CDR_ALIGNMENT)
// Align ourselves
char *start = ACE_ptr_align_binary (this->base (),
align);
#else
char *start = this->base ();
#endif /* ACE_LACKS_CDR_ALIGNMENT */
// Set our rd & wr pointers
this->rd_ptr (start);
this->wr_ptr (start);
}
else
{
if (this->init_i (0, // size
MB_NORMAL, // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
0, // flags
0, // priority
ACE_Time_Value::zero, // execution time
ACE_Time_Value::max_time, // absolute time of deadline
mb.data_block ()->clone_nocopy (),// data block
mb.data_block ()->data_block_allocator (),
mb.message_block_allocator_) == -1)
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_Message_Block")));
#if !defined (ACE_LACKS_CDR_ALIGNMENT)
// Align ourselves
char *start = ACE_ptr_align_binary (this->base (),
align);
#else
char *start = this->base ();
#endif /* ACE_LACKS_CDR_ALIGNMENT */
// Set our rd & wr pointers
this->rd_ptr (start);
this->wr_ptr (start);
#if !defined (ACE_LACKS_CDR_ALIGNMENT)
// Get the alignment offset of the incoming ACE_Message_Block
start = ACE_ptr_align_binary (mb.base (),
align);
#else
start = mb.base ();
#endif /* ACE_LACKS_CDR_ALIGNMENT */
// Actual offset for the incoming message block assuming that it
// is also aligned to the same "align" byte
size_t const wr_offset = mb.wr_ptr_ - (start - mb.base ());
// Copy wr_offset amount of data in to <this->data_block>
(void) ACE_OS::memcpy (this->wr_ptr (),
start,
wr_offset);
// Dont move the write pointer, just leave it to the application
// to do what it wants
}
#if defined (ACE_LACKS_CDR_ALIGNMENT)
ACE_UNUSED_ARG (align);
#endif /* ACE_LACKS_CDR_ALIGNMENT */
}
int
ACE_Message_Block::init_i (size_t size,
ACE_Message_Type msg_type,
ACE_Message_Block *msg_cont,
const char *msg_data,
ACE_Allocator *allocator_strategy,
ACE_Lock *locking_strategy,
Message_Flags flags,
unsigned long priority,
const ACE_Time_Value &execution_time,
const ACE_Time_Value &deadline_time,
ACE_Data_Block *db,
ACE_Allocator *data_block_allocator,
ACE_Allocator *message_block_allocator)
{
ACE_TRACE ("ACE_Message_Block::init_i");
ACE_FUNCTION_TIMEPROBE (ACE_MESSAGE_BLOCK_INIT_I_ENTER);
this->rd_ptr_ = 0;
this->wr_ptr_ = 0;
this->priority_ = priority;
#if defined (ACE_HAS_TIMED_MESSAGE_BLOCKS)
this->execution_time_ = execution_time;
this->deadline_time_ = deadline_time;
#else
ACE_UNUSED_ARG (execution_time);
ACE_UNUSED_ARG (deadline_time);
#endif /* ACE_HAS_TIMED_MESSAGE_BLOCKS */
this->cont_ = msg_cont;
this->next_ = 0;
this->prev_ = 0;
this->message_block_allocator_ = message_block_allocator;
if (this->data_block_ != 0)
{
this->data_block_->release ();
this->data_block_ = 0;
}
if (db == 0)
{
if (data_block_allocator == 0)
ACE_ALLOCATOR_RETURN (data_block_allocator,
ACE_Allocator::instance (),
-1);
ACE_TIMEPROBE (ACE_MESSAGE_BLOCK_INIT_I_DB_ALLOC);
// Allocate the <ACE_Data_Block> portion, which is reference
// counted.
ACE_NEW_MALLOC_RETURN (db,
static_cast<ACE_Data_Block *> (
data_block_allocator->malloc (sizeof (ACE_Data_Block))),
ACE_Data_Block (size,
msg_type,
msg_data,
allocator_strategy,
locking_strategy,
flags,
data_block_allocator),
-1);
ACE_TIMEPROBE (ACE_MESSAGE_BLOCK_INIT_I_DB_CTOR);
// Message block initialization may fail, while the construction
// succeds. Since ACE may throw no exceptions, we have to do a
// separate check and clean up, like this:
if (db != 0 && db->size () < size)
{
db->ACE_Data_Block::~ACE_Data_Block(); // placement destructor ...
data_block_allocator->free (db); // free ...
errno = ENOMEM;
return -1;
}
}
// Reset the data_block_ pointer.
this->data_block (db);
return 0;
}
ACE_Data_Block::~ACE_Data_Block ()
{
// Sanity check...
ACE_ASSERT (this->reference_count_ <= 1);
// Just to be safe...
this->reference_count_ = 0;
if (ACE_BIT_DISABLED (this->flags_,
ACE_Message_Block::DONT_DELETE))
{
this->allocator_strategy_->free ((void *) this->base_);
this->base_ = 0;
}
}
ACE_Data_Block *
ACE_Data_Block::release_i ()
{
ACE_TRACE ("ACE_Data_Block::release_i");
ACE_ASSERT (this->reference_count_ > 0);
ACE_Data_Block *result = 0;
// decrement reference count
--this->reference_count_;
if (this->reference_count_ == 0)
// this will cause deletion of this
result = 0;
else
result = this;
return result;
}
ACE_Data_Block *
ACE_Data_Block::release_no_delete (ACE_Lock *lock)
{
ACE_TRACE ("ACE_Data_Block::release_no_delete");
ACE_Data_Block *result = 0;
ACE_Lock *lock_to_be_used = 0;
// Check if we were passed in a lock
if (lock != 0)
{
// Make sure that the lock passed in and our lock are the same
if (lock == this->locking_strategy_)
// In this case no locking is required.
lock_to_be_used = 0;
// The lock passed in does not match our lock
else
// Lock to be used is our lock
lock_to_be_used = this->locking_strategy_;
}
// This is the case when no lock was passed in
else
{
// Lock to be used is our lock
lock_to_be_used = this->locking_strategy_;
}
// If there's a locking strategy then we need to acquire the lock
// before decrementing the count.
if (lock_to_be_used != 0)
{
ACE_GUARD_RETURN (ACE_Lock, ace_mon, *lock_to_be_used, 0);
result = this->release_i ();
}
else
{
result = this->release_i ();
}
return result;
}
ACE_Data_Block *
ACE_Data_Block::release (ACE_Lock *lock)
{
ACE_TRACE ("ACE_Data_Block::release");
ACE_Allocator *allocator = this->data_block_allocator_;
ACE_Data_Block *result = this->release_no_delete (lock);
// We must delete this outside the scope of the locking_strategy_
// since otherwise we'd be trying to "release" through a deleted
// pointer!
if (result == 0)
ACE_DES_FREE_THIS (allocator->free,
ACE_Data_Block);
return result;
}
ACE_Message_Block *
ACE_Message_Block::release ()
{
ACE_TRACE ("ACE_Message_Block::release");
// We want to hold the data block in a temporary variable because we
// invoked "delete this;" at some point, so using this->data_block_
// could be a bad idea.
ACE_Data_Block *tmp = this->data_block ();
// This flag is set to 1 when we have to destroy the data_block
int destroy_dblock = 0;
ACE_Lock *lock = 0;
// Do we have a valid data block
if (this->data_block ())
{
// Grab the lock that belongs to my data block
lock = this->data_block ()->locking_strategy ();
// if we have a lock
if (lock != 0)
{
// One guard for all
ACE_GUARD_RETURN (ACE_Lock, ace_mon, *lock, 0);
// Call non-guarded release with @a lock
destroy_dblock = this->release_i (lock);
}
// This is the case when we have a valid data block but no lock
else
// Call non-guarded release with no lock
destroy_dblock = this->release_i (0);
}
else
// This is the case when we don't even have a valid data block
destroy_dblock = this->release_i (0);
if (destroy_dblock != 0)
{
ACE_Allocator *allocator = tmp->data_block_allocator ();
ACE_DES_FREE (tmp,
allocator->free,
ACE_Data_Block);
}
return 0;
}
int
ACE_Message_Block::release_i (ACE_Lock *lock)
{
ACE_TRACE ("ACE_Message_Block::release_i");
// Free up all the continuation messages.
if (this->cont_)
{
ACE_Message_Block *mb = this->cont_;
ACE_Message_Block *tmp = 0;
do
{
tmp = mb;
mb = mb->cont_;
tmp->cont_ = 0;
ACE_Data_Block *db = tmp->data_block ();
if (tmp->release_i (lock) != 0)
{
ACE_Allocator *allocator = db->data_block_allocator ();
ACE_DES_FREE (db,
allocator->free,
ACE_Data_Block);
}
}
while (mb);
this->cont_ = 0;
}
int result = 0;
if (ACE_BIT_DISABLED (this->flags_,
ACE_Message_Block::DONT_DELETE) &&
this->data_block ())
{
if (this->data_block ()->release_no_delete (lock) == 0)
result = 1;
this->data_block_ = 0;
}
// We will now commit suicide: this object *must* have come from the
// allocator given.
if (this->message_block_allocator_ == 0)
delete this;
else
{
ACE_Allocator *allocator = this->message_block_allocator_;
ACE_DES_FREE_THIS (allocator->free,
ACE_Message_Block);
}
return result;
}
/* static */ ACE_Message_Block *
ACE_Message_Block::release (ACE_Message_Block *mb)
{
ACE_TRACE ("ACE_Message_Block::release");
if (mb != 0)
return mb->release ();
else
return 0;
}
ACE_Message_Block::~ACE_Message_Block ()
{
ACE_TRACE ("ACE_Message_Block::~ACE_Message_Block");
if (ACE_BIT_DISABLED (this->flags_,
ACE_Message_Block::DONT_DELETE)&&
this->data_block ())
this->data_block ()->release ();
this->prev_ = 0;
this->next_ = 0;
this->cont_ = 0;
}
ACE_Data_Block *
ACE_Data_Block::duplicate ()
{
ACE_TRACE ("ACE_Data_Block::duplicate");
// Create a new <ACE_Message_Block>, but share the <base_> pointer
// data (i.e., don't copy that).
if (this->locking_strategy_)
{
// We need to acquire the lock before incrementing the count.
ACE_GUARD_RETURN (ACE_Lock, ace_mon, *this->locking_strategy_, 0);
++this->reference_count_;
}
else
++this->reference_count_;
return this;
}
#if defined (ACE_HAS_TIMED_MESSAGE_BLOCKS)
#define ACE_EXECUTION_TIME this->execution_time_
#define ACE_DEADLINE_TIME this->deadline_time_
#else
#define ACE_EXECUTION_TIME ACE_Time_Value::zero
#define ACE_DEADLINE_TIME ACE_Time_Value::max_time
#endif /* ACE_HAS_TIMED_MESSAGE_BLOCKS */
ACE_Message_Block *
ACE_Message_Block::duplicate () const
{
ACE_TRACE ("ACE_Message_Block::duplicate");
ACE_Message_Block *nb_top = 0;
ACE_Message_Block *nb = 0;
const ACE_Message_Block *current = this;
// Increment the reference counts of all the continuation messages.
while (current)
{
ACE_Message_Block* cur_dup = 0;
// Create a new <ACE_Message_Block> that contains unique copies of
// the message block fields, but a reference counted duplicate of
// the <ACE_Data_Block>.
// If there is no allocator, use the standard new and delete calls.
if (current->message_block_allocator_ == 0)
ACE_NEW_NORETURN (cur_dup,
ACE_Message_Block (0, // size
ACE_Message_Type (0), // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
0, // flags
current->priority_, // priority
ACE_EXECUTION_TIME,
ACE_DEADLINE_TIME,
// Get a pointer to a
// "duplicated" <ACE_Data_Block>
// (will simply increment the
// reference count).
current->data_block ()->duplicate (),
current->data_block ()->data_block_allocator (),
current->message_block_allocator_));
else // Otherwise, use the message_block_allocator passed in.
ACE_NEW_MALLOC_NORETURN (cur_dup,
static_cast<ACE_Message_Block*> (
current->message_block_allocator_->malloc (sizeof (ACE_Message_Block))),
ACE_Message_Block (0, // size
ACE_Message_Type (0), // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
0, // flags
current->priority_, // priority
ACE_EXECUTION_TIME,
ACE_DEADLINE_TIME,
// Get a pointer to a
// "duplicated" <ACE_Data_Block>
// (will simply increment the
// reference count).
current->data_block ()->duplicate (),
current->data_block ()->data_block_allocator (),
current->message_block_allocator_));
// If allocation failed above, release everything done so far and return NULL
if (cur_dup == 0)
{
if (nb_top != 0)
{
nb_top->release ();
}
return 0;
}
// Set the read and write pointers in the new <Message_Block> to the
// same relative offset as in the existing <Message_Block>. Note
// that we are assuming that the data_block()->base() pointer
// doesn't change when it's duplicated.
cur_dup->rd_ptr (current->rd_ptr_);
cur_dup->wr_ptr (current->wr_ptr_);
if (!nb)
{
/* First in the list: set leading pointers */
nb_top = nb = cur_dup;
}
else
{
/* Continuing on: append to nb and walk down the list */
nb->cont_ = cur_dup;
nb = nb->cont_;
}
current = current->cont_;
}
return nb_top;
}
ACE_Message_Block *
ACE_Message_Block::duplicate (const ACE_Message_Block *mb)
{
ACE_TRACE ("ACE_Message_Block::duplicate");
if (mb == 0)
return 0;
else
return mb->duplicate ();
}
ACE_Data_Block *
ACE_Data_Block::clone (ACE_Message_Block::Message_Flags mask) const
{
ACE_TRACE ("ACE_Data_Block::clone");
ACE_Data_Block *nb = this->clone_nocopy (mask);
// Copy all of the payload memory into the new object. The new block
// was allocated with max_size_ (and, thus, it's cur_size_ is the same
// as max_size_). Maintain the same "has been written" boundary in the
// new block by only copying cur_size_ bytes.
if (nb != 0)
{
ACE_OS::memcpy (nb->base_,
this->base_,
this->cur_size_);
}
return nb;
}
ACE_Data_Block *
ACE_Data_Block::clone_nocopy (ACE_Message_Block::Message_Flags mask,
size_t max_size) const
{
ACE_FUNCTION_TIMEPROBE(ACE_DATA_BLOCK_CLONE_ENTER);
ACE_TRACE ("ACE_Data_Block::clone_nocopy");
// You always want to clear this one to prevent memory leaks but you
// might add some others later.
const ACE_Message_Block::Message_Flags always_clear =
ACE_Message_Block::DONT_DELETE;
const size_t newsize =
max_size == 0 ? this->max_size_ : max_size;
ACE_Data_Block *nb = 0;
ACE_NEW_MALLOC_RETURN (nb,
static_cast<ACE_Data_Block*> (
this->data_block_allocator_->malloc (sizeof (ACE_Data_Block))),
ACE_Data_Block (newsize, // size
this->type_, // type
0, // data
this->allocator_strategy_, // allocator
this->locking_strategy_, // locking strategy
this->flags_, // flags
this->data_block_allocator_),
0);
// Message block initialization may fail while the construction
// succeds. Since as a matter of policy, ACE may throw no
// exceptions, we have to do a separate check like this.
if (nb != 0 && nb->size () < newsize)
{
nb->ACE_Data_Block::~ACE_Data_Block(); // placement destructor ...
this->data_block_allocator_->free (nb); // free ...
errno = ENOMEM;
return 0;
}
// Set new flags minus the mask...
nb->clr_flags (mask | always_clear);
return nb;
}
ACE_Message_Block *
ACE_Message_Block::clone (Message_Flags mask) const
{
ACE_TRACE ("ACE_Message_Block::clone");
const ACE_Message_Block *old_message_block = this;
ACE_Message_Block *new_message_block = 0;
ACE_Message_Block *new_previous_message_block = 0;
ACE_Message_Block *new_root_message_block = 0;
do
{
// Get a pointer to a "cloned"<ACE_Data_Block> (will copy the
// values rather than increment the reference count).
ACE_Data_Block *db = old_message_block->data_block ()->clone (mask);
if (db == 0)
return 0;
if(old_message_block->message_block_allocator_ == 0)
{
ACE_NEW_RETURN (new_message_block,
ACE_Message_Block (0, // size
ACE_Message_Type (0), // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
0, // flags
old_message_block->priority_, // priority
ACE_EXECUTION_TIME, // execution time
ACE_DEADLINE_TIME, // absolute time to deadline
// Get a pointer to a
// "duplicated"<ACE_Data_Block>
// (will simply increment the
// reference count).
db,
db->data_block_allocator (),
old_message_block->message_block_allocator_),
0);
}
else
{
// This is the ACE_NEW_MALLOC macro with the return check removed.
// We need to do it this way because if it fails we need to release
// the cloned data block that was created above. If we used
// ACE_NEW_MALLOC_RETURN, there would be a memory leak because the
// above db pointer would be left dangling.
new_message_block = static_cast<ACE_Message_Block*> (old_message_block->message_block_allocator_->malloc (sizeof (ACE_Message_Block)));
if (new_message_block != 0)
new (new_message_block) ACE_Message_Block (0, // size
ACE_Message_Type (0), // type
0, // cont
0, // data
0, // allocator
0, // locking strategy
0, // flags
old_message_block->priority_, // priority
ACE_EXECUTION_TIME, // execution time
ACE_DEADLINE_TIME, // absolute time to deadline
db,
db->data_block_allocator (),
old_message_block->message_block_allocator_);
}
if (new_message_block == 0)
{
db->release ();
return 0;
}
// Set the read and write pointers in the new <Message_Block> to the
// same relative offset as in the existing <Message_Block>.
new_message_block->rd_ptr (old_message_block->rd_ptr_);
new_message_block->wr_ptr (old_message_block->wr_ptr_);
// save the root message block to return
if (new_root_message_block == 0)
new_root_message_block = new_message_block;
if (new_previous_message_block != 0)
// we're a continuation of the previous block, add ourself to its chain
new_previous_message_block->cont_ = new_message_block;
new_previous_message_block = new_message_block;
old_message_block = old_message_block->cont ();
}
while (old_message_block != 0);
return new_root_message_block;
}
// This is private.
ACE_Message_Block &
ACE_Message_Block::operator= (const ACE_Message_Block &)
{
ACE_TRACE ("ACE_Message_Block::operator=");
return *this;
}
void
ACE_Data_Block::base (char *msg_data,
size_t msg_length,
ACE_Message_Block::Message_Flags msg_flags)
{
if (ACE_BIT_DISABLED (this->flags_,
ACE_Message_Block::DONT_DELETE))
this->allocator_strategy_->free (this->base_);
this->max_size_ = msg_length;
this->cur_size_ = msg_length;
this->base_ = msg_data;
this->flags_ = msg_flags;
}
ACE_END_VERSIONED_NAMESPACE_DECL
|