Byte alignment in ACE CDRS

Source: Internet
Author: User

Byte alignment in 12 ace CDRS

Everyone should know that there is a byte alignment problem in the middle of the computer. When the CPU accesses the memory, access from a specific address can generally speed up, for example, on a 32-bit machine, if a 32-bit integer is placed on an address that can be divided by 32 modulo to 0, it only needs to be accessed once. If not, it may need to be accessed twice. However, this requires that some data start from a specific address, rather than discharge data sequentially (there will be some idle addresses in the middle), which is byte alignment.

The estimation of ace cdrs is also to speed up, so that byte alignment is also used by default in CDR encoding. Therefore, during the CDR codec process of ACE, it is recommended that the input parameter addresses comply with the byte alignment rules. Otherwise, encoding/decoding errors may occur.

The ace_outputcdr constructor calls a function mb_align to adjust the input address parameters to become address alignment addresses. However, the adjustment function ace_ptr_align_binary does not know what to consider. Instead, it uses the ace_cdr: max_alignment (64bit, 8 bytpes in length) as the parameter address instead of the machine's alignment length. The internal address of ace_outputcdr is aligned by 8 bytes, but ace_inputcdr does not change the internal address to an address equal to 64 or 0, instead, it is only adjusted to the address where the value of 32 (on a 32-bit machine) is equal to 0.

 

Void

Ace_cdr: mb_align (ace_message_block * MB)

{

# If! Defined (ace_cdr_ignore_alignment)

// If the byte alignment mode is used, the maximum alignment mode is used to adjust the memory. Adjusted to the address where the value of 64 is equal to 0.

Char * const start = ace_ptr_align_binary (MB-> base (),

Ace_cdr: max_alignment );

# Else

......

}

You can use a simple piece of code to test and find this problem.

 

Char * tmp_buffer = new char [2048];

// Use an alignment parameter as the address of ace_inputcdr and ace_outputcdr,

Char * tmp_data = tmp_buffer + 1;

 

// Output_cdr adjusted the default alignment starting address to 8 bytes

Ace_outputcdr output_cdr (tmp_data, 512 );

Ace_inputcdr input_cdr (tmp_data, 512 );

 

Ace_cdr: ulong cdr_long = 123;

Bool Bret = false;

//

Bret = output_cdr.write_ulong (cdr_long );

// Cdr_long is not equal to 123, but an error is invalid.

Bret = input_cdr.read_ulong (cdr_long );

 

 

In fact, if the codec BUFF adopts the same alignment mode, there should be no problems in theory. At most, there is a gap to be filled for alignment, but this can increase the CPU efficiency, it is also a good thing. However, the address of ACE_OutputCDR is changed. But it may lead to inconsistent codec BUFFER. I cannot be sure whether this is an error or the author has his own considerations.

This problem reaches 5.6.1 Still exist. I have submitted a problem report.

Of course, there is a way to solve this problem. Define the macro ACE_CDR_IGNORE_ALIGNMENT [note]. If this macro is defined, ACE will not use byte alignment to process CDR encoding. Using this method, the space occupied by encoding will be compressed, but the efficiency may be lower (in fact, it may not be necessary because it will take some time to calculate for byte alignment ),

 

[Note] ACE does not know why to use two macros without byte alignment in the Code. One is to use ACE_CDR_IGNORE_ALIGNMENT in the CDR_Base.h CDR_Base.cpp file, macro ACE_LACKS_CDR_ALIGNMENT used on CDR_Stream.cpp and CDR_Stream.h files.

I usually define both macros.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.