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#include <string.h>
14
15COMPILER_RT_VISIBILITY
16uint64_t __llvm_profile_get_size_for_buffer(void) {
17  const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
18  const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
19  const uint64_t *CountersBegin = __llvm_profile_begin_counters();
20  const uint64_t *CountersEnd = __llvm_profile_end_counters();
21  const char *NamesBegin = __llvm_profile_begin_names();
22  const char *NamesEnd = __llvm_profile_end_names();
23
24  return __llvm_profile_get_size_for_buffer_internal(
25      DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
26}
27
28#define PROFILE_RANGE_SIZE(Range) (Range##End - Range##Begin)
29
30COMPILER_RT_VISIBILITY
31uint64_t __llvm_profile_get_size_for_buffer_internal(
32    const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
33    const uint64_t *CountersBegin, const uint64_t *CountersEnd,
34    const char *NamesBegin, const char *NamesEnd) {
35  /* Match logic in __llvm_profile_write_buffer(). */
36  const uint64_t NamesSize = PROFILE_RANGE_SIZE(Names) * sizeof(char);
37  const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
38  return sizeof(__llvm_profile_header) +
39         PROFILE_RANGE_SIZE(Data) * sizeof(__llvm_profile_data) +
40         PROFILE_RANGE_SIZE(Counters) * sizeof(uint64_t) + NamesSize + Padding;
41}
42
43/* The buffer writer is reponsponsible in keeping writer state
44 * across the call.
45 */
46static uint32_t bufferWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
47                             void **WriterCtx) {
48  uint32_t I;
49  char **Buffer = (char **)WriterCtx;
50  for (I = 0; I < NumIOVecs; I++) {
51    size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
52    memcpy(*Buffer, IOVecs[I].Data, Length);
53    *Buffer += Length;
54  }
55  return 0;
56}
57
58COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
59  return llvmWriteProfData(bufferWriter, Buffer, 0, 0);
60}
61
62COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
63    char *Buffer, const __llvm_profile_data *DataBegin,
64    const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
65    const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
66  return llvmWriteProfDataImpl(bufferWriter, Buffer, DataBegin, DataEnd,
67                               CountersBegin, CountersEnd, 0, 0, NamesBegin,
68                               NamesEnd);
69}
70