strarr.h
Go to the documentation of this file.
1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BLOATY_TESTS_STRARR_H_
16 #define BLOATY_TESTS_STRARR_H_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 // For constructing arrays of strings in the slightly peculiar format
23 // required by execve().
24 class StrArr {
25  public:
26  explicit StrArr(const std::vector<std::string>& strings)
27  : size_(strings.size()), array_(new char*[size_ + 1]) {
28  array_[size_] = NULL;
29  for (size_t i = 0; i < strings.size(); i++) {
30  // Can't use c_str() directly because array_ is not const char*.
31  array_[i] = strdup(strings[i].c_str());
32  }
33  }
34 
35  ~StrArr() {
36  // unique_ptr frees the array of pointers but not the pointed-to strings.
37  for (int i = 0; i < size_; i++) {
38  free(array_[i]);
39  }
40  }
41 
42  char **get() const { return array_.get(); }
43 
44  size_t size() const { return size_; }
45 
46  private:
47  size_t size_;
48  // Can't use vector<char*> because execve() takes ptr to non-const array.
49  std::unique_ptr<char*[]> array_;
50 };
51 
52 #endif // BLOATY_TESTS_STRARR_H_
StrArr::~StrArr
~StrArr()
Definition: strarr.h:35
StrArr::get
char ** get() const
Definition: strarr.h:42
StrArr::array_
std::unique_ptr< char *[]> array_
Definition: strarr.h:49
StrArr::size_
size_t size_
Definition: strarr.h:47
env.new
def new
Definition: env.py:51
StrArr::size
size_t size() const
Definition: strarr.h:44
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
StrArr::StrArr
StrArr(const std::vector< std::string > &strings)
Definition: strarr.h:26
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
strdup
#define strdup(ptr)
Definition: acountry.c:55
StrArr
Definition: strarr.h:24


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:25