• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBPROFILE_PROFILE_PROFILE_HELPERS_H_
18 #define ART_LIBPROFILE_PROFILE_PROFILE_HELPERS_H_
19 
20 #include <unistd.h>
21 
22 #include <vector>
23 
24 #include "base/globals.h"
25 
26 namespace art {
27 
28 // Returns true if all the bytes were successfully written to the file descriptor.
WriteBuffer(int fd,const uint8_t * buffer,size_t byte_count)29 inline bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) {
30   while (byte_count > 0) {
31     int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count));
32     if (bytes_written == -1) {
33       return false;
34     }
35     byte_count -= bytes_written;  // Reduce the number of remaining bytes.
36     buffer += bytes_written;  // Move the buffer forward.
37   }
38   return true;
39 }
40 
41 // Add the string bytes to the buffer.
AddStringToBuffer(std::vector<uint8_t> * buffer,const std::string & value)42 inline void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) {
43   buffer->insert(buffer->end(), value.begin(), value.end());
44 }
45 
46 // Insert each byte, from low to high into the buffer.
47 template <typename T>
AddUintToBuffer(std::vector<uint8_t> * buffer,T value)48 inline void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) {
49   for (size_t i = 0; i < sizeof(T); i++) {
50     buffer->push_back((value >> (i * kBitsPerByte)) & 0xff);
51   }
52 }
53 
54 }  // namespace art
55 
56 #endif  // ART_LIBPROFILE_PROFILE_PROFILE_HELPERS_H_
57