/Users/deen/code/yugabyte-db/src/yb/common/ybc-internal.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | #include "yb/common/ybc-internal.h" |
13 | | |
14 | | #include "yb/util/status.h" |
15 | | #include "yb/util/status_format.h" |
16 | | |
17 | | namespace yb { |
18 | | |
19 | | namespace { |
20 | | YBCPAllocFn g_palloc_fn = nullptr; |
21 | | YBCCStringToTextWithLenFn g_cstring_to_text_with_len_fn = nullptr; |
22 | | } // anonymous namespace |
23 | | |
24 | 2.55k | void YBCSetPAllocFn(YBCPAllocFn palloc_fn) { |
25 | 2.55k | CHECK_NOTNULL(palloc_fn); |
26 | 2.55k | g_palloc_fn = palloc_fn; |
27 | 2.55k | } |
28 | | |
29 | 53.6k | void* YBCPAlloc(size_t size) { |
30 | 53.6k | CHECK_NOTNULL(g_palloc_fn); |
31 | 53.6k | return g_palloc_fn(size); |
32 | 53.6k | } |
33 | | |
34 | 2.55k | void YBCSetCStringToTextWithLenFn(YBCCStringToTextWithLenFn fn) { |
35 | 2.55k | CHECK_NOTNULL(fn); |
36 | 2.55k | g_cstring_to_text_with_len_fn = fn; |
37 | 2.55k | } |
38 | | |
39 | 16.3M | void* YBCCStringToTextWithLen(const char* c, int size) { |
40 | 16.3M | CHECK_NOTNULL(g_cstring_to_text_with_len_fn); |
41 | 16.3M | return g_cstring_to_text_with_len_fn(c, size); |
42 | 16.3M | } |
43 | | |
44 | 2.55k | YBCStatus ToYBCStatus(const Status& status) { |
45 | 2.55k | return status.RetainStruct(); |
46 | 2.55k | } |
47 | | |
48 | 76.9M | YBCStatus ToYBCStatus(Status&& status) { |
49 | 76.9M | return status.DetachStruct(); |
50 | 76.9M | } |
51 | | |
52 | 53.6k | void FreeYBCStatus(YBCStatus status) { |
53 | | // Create Status object that receives control over provided status, so it will be destoyed with |
54 | | // yb_status. |
55 | 53.6k | Status yb_status(status, AddRef::kFalse); |
56 | 53.6k | } |
57 | | |
58 | 27.5M | YBCStatus YBCStatusOK() { |
59 | 27.5M | return nullptr; |
60 | 27.5M | } |
61 | | |
62 | 0 | YBCStatus YBCStatusNotSupport(const string& feature_name) { |
63 | 0 | if (feature_name.empty()) { |
64 | 0 | return ToYBCStatus(STATUS(NotSupported, "Feature is not supported")); |
65 | 0 | } else { |
66 | 0 | return ToYBCStatus(STATUS_FORMAT(NotSupported, "Feature '$0' not supported", feature_name)); |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | 30 | const char* YBCPAllocStdString(const std::string& s) { |
71 | 30 | const size_t len = s.size(); |
72 | 30 | char* result = reinterpret_cast<char*>(YBCPAlloc(len + 1)); |
73 | 30 | memcpy(result, s.c_str(), len); |
74 | 30 | result[len] = 0; |
75 | 30 | return result; |
76 | 30 | } |
77 | | |
78 | | } // namespace yb |