]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/profile/InstrProfilingBuffer.c
Merge clang trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / profile / InstrProfilingBuffer.c
1 /*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
2 |*
3 |*                     The LLVM Compiler Infrastructure
4 |*
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
7 |*
8 \*===----------------------------------------------------------------------===*/
9
10 #include "InstrProfiling.h"
11 #include "InstrProfilingInternal.h"
12
13 COMPILER_RT_VISIBILITY
14 uint64_t __llvm_profile_get_size_for_buffer(void) {
15   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
16   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
17   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
18   const uint64_t *CountersEnd = __llvm_profile_end_counters();
19   const char *NamesBegin = __llvm_profile_begin_names();
20   const char *NamesEnd = __llvm_profile_end_names();
21
22   return __llvm_profile_get_size_for_buffer_internal(
23       DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
24 }
25
26 COMPILER_RT_VISIBILITY
27 uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
28                                       const __llvm_profile_data *End) {
29   intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
30   return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
31          sizeof(__llvm_profile_data);
32 }
33
34 COMPILER_RT_VISIBILITY
35 uint64_t __llvm_profile_get_size_for_buffer_internal(
36     const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
37     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
38     const char *NamesBegin, const char *NamesEnd) {
39   /* Match logic in __llvm_profile_write_buffer(). */
40   const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
41   const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
42   return sizeof(__llvm_profile_header) +
43          (__llvm_profile_get_data_size(DataBegin, DataEnd) *
44           sizeof(__llvm_profile_data)) +
45          (CountersEnd - CountersBegin) * sizeof(uint64_t) + NamesSize + Padding;
46 }
47
48 COMPILER_RT_VISIBILITY
49 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {
50   BufferWriter->Write = lprofBufferWriter;
51   BufferWriter->WriterCtx = Buffer;
52 }
53
54 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
55   ProfDataWriter BufferWriter;
56   initBufferWriter(&BufferWriter, Buffer);
57   return lprofWriteData(&BufferWriter, 0, 0);
58 }
59
60 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
61     char *Buffer, const __llvm_profile_data *DataBegin,
62     const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
63     const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
64   ProfDataWriter BufferWriter;
65   initBufferWriter(&BufferWriter, Buffer);
66   return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,
67                             CountersEnd, 0, NamesBegin, NamesEnd, 0);
68 }