Index: compiler-rt/trunk/lib/profile/InstrProfiling.h =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfiling.h +++ compiler-rt/trunk/lib/profile/InstrProfiling.h @@ -131,4 +131,8 @@ /*! \brief Get the version of the file format. */ uint64_t __llvm_profile_get_version(void); +/*! \brief Get the number of entries in the profile data section. */ +uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin, + const __llvm_profile_data *End); + #endif /* PROFILE_INSTRPROFILING_H_ */ Index: compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c +++ compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c @@ -23,7 +23,13 @@ DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd); } -#define PROFILE_RANGE_SIZE(Range) (Range##End - Range##Begin) +COMPILER_RT_VISIBILITY +uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin, + const __llvm_profile_data *End) { + intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End; + return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) / + sizeof(__llvm_profile_data); +} COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_size_for_buffer_internal( @@ -31,11 +37,12 @@ const uint64_t *CountersBegin, const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) { /* Match logic in __llvm_profile_write_buffer(). */ - const uint64_t NamesSize = PROFILE_RANGE_SIZE(Names) * sizeof(char); + const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char); const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); return sizeof(__llvm_profile_header) + - PROFILE_RANGE_SIZE(Data) * sizeof(__llvm_profile_data) + - PROFILE_RANGE_SIZE(Counters) * sizeof(uint64_t) + NamesSize + Padding; + (__llvm_profile_get_data_size(DataBegin, DataEnd) * + sizeof(__llvm_profile_data)) + + (CountersEnd - CountersBegin) * sizeof(uint64_t) + NamesSize + Padding; } COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) { Index: compiler-rt/trunk/lib/profile/InstrProfilingValue.c =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfilingValue.c +++ compiler-rt/trunk/lib/profile/InstrProfilingValue.c @@ -142,8 +142,8 @@ if (!ValueDataSize) return NULL; - ValueDataArray = - (ValueProfData **)calloc(DataEnd - DataBegin, sizeof(void *)); + ValueDataArray = (ValueProfData **)calloc( + __llvm_profile_get_data_size(DataBegin, DataEnd), sizeof(void *)); if (!ValueDataArray) PROF_OOM_RETURN("Failed to write value profile data "); Index: compiler-rt/trunk/lib/profile/InstrProfilingWriter.c =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfilingWriter.c +++ compiler-rt/trunk/lib/profile/InstrProfilingWriter.c @@ -144,7 +144,7 @@ const char *NamesBegin, const char *NamesEnd) { /* Calculate size of sections. */ - const uint64_t DataSize = DataEnd - DataBegin; + const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); const uint64_t CountersSize = CountersEnd - CountersBegin; const uint64_t NamesSize = NamesEnd - NamesBegin; const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);