/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_memctx.cc
Line | Count | Source |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. 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 distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | //-------------------------------------------------------------------------------------------------- |
15 | | |
16 | | #include "yb/yql/pggate/pg_memctx.h" |
17 | | |
18 | | #include "yb/util/status.h" |
19 | | |
20 | | #include "yb/yql/pggate/pg_tabledesc.h" |
21 | | |
22 | | namespace yb { |
23 | | namespace pggate { |
24 | | |
25 | 678k | PgMemctx::PgMemctx() { |
26 | 678k | } |
27 | | |
28 | 678k | PgMemctx::~PgMemctx() { |
29 | 678k | Clear(); |
30 | 678k | } |
31 | | |
32 | | namespace { |
33 | | // Table of memory contexts. |
34 | | // - Although defined in Yugabyte, this table is owned and managed by Postgres process. |
35 | | // Other processes cannot control "pggate::PgMemctx" to avoid memory violations. |
36 | | // - Table "postgres_process_memctxs" is to help releasing the references to PgStatement when |
37 | | // Postgres Process (a C program) is exiting. |
38 | | // - Transaction layer and Postgres BOTH hold references to PgStatement objects, and those |
39 | | // PgGate objects wouldn't be destroyed unless both layers release their references or both |
40 | | // layers are terminated. |
41 | | std::unordered_map<PgMemctx *, PgMemctx::SharedPtr> postgres_process_memctxs; |
42 | | } // namespace |
43 | | |
44 | 677k | PgMemctx *PgMemctx::Create() { |
45 | 677k | auto memctx = std::make_shared<PgMemctx>(); |
46 | 677k | postgres_process_memctxs[memctx.get()] = memctx; |
47 | 677k | return memctx.get(); |
48 | 677k | } |
49 | | |
50 | 668k | Status PgMemctx::Destroy(PgMemctx *handle) { |
51 | 668k | if (handle) { |
52 | 668k | SCHECK(postgres_process_memctxs.find(handle) != postgres_process_memctxs.end(), |
53 | 668k | InternalError, "Invalid memory context handle"); |
54 | 668k | postgres_process_memctxs.erase(handle); |
55 | 668k | } |
56 | 668k | return Status::OK(); |
57 | 668k | } |
58 | | |
59 | 3.95M | Status PgMemctx::Reset(PgMemctx *handle) { |
60 | 3.96M | if (handle3.95M ) { |
61 | 3.96M | SCHECK(postgres_process_memctxs.find(handle) != postgres_process_memctxs.end(), |
62 | 3.96M | InternalError, "Invalid memory context handle"); |
63 | 3.96M | handle->Clear(); |
64 | 3.96M | } |
65 | 3.95M | return Status::OK(); |
66 | 3.95M | } |
67 | | |
68 | 8.80M | void PgMemctx::Register(Registrable *obj) { |
69 | 8.80M | registered_objects_.push_back(*obj); |
70 | 8.80M | obj->memctx_ = this; |
71 | 8.80M | } |
72 | | |
73 | 7.61M | void PgMemctx::Destroy(Registrable *obj) { |
74 | 7.61M | auto& objs = obj->memctx_->registered_objects_; |
75 | 7.61M | objs.erase_and_dispose(objs.iterator_to(*obj), std::default_delete<Registrable>()); |
76 | 7.61M | } |
77 | | |
78 | 4.63M | void PgMemctx::Clear() { |
79 | 4.63M | tabledesc_map_.clear(); |
80 | 4.63M | registered_objects_.clear_and_dispose(std::default_delete<Registrable>()); |
81 | 4.63M | } |
82 | | |
83 | 2.40M | void PgMemctx::Cache(size_t hash_id, const PgTableDescPtr &table_desc) { |
84 | | // Add table descriptor to table. |
85 | 2.40M | tabledesc_map_[hash_id] = table_desc; |
86 | 2.40M | } |
87 | | |
88 | 16.4M | void PgMemctx::GetCache(size_t hash_id, PgTableDesc **handle) { |
89 | | // Read table descriptor to table. |
90 | 16.4M | const auto iter = tabledesc_map_.find(hash_id); |
91 | 16.4M | if (iter == tabledesc_map_.end()) { |
92 | 2.40M | *handle = nullptr; |
93 | 14.0M | } else { |
94 | 14.0M | *handle = iter->second.get(); |
95 | 14.0M | } |
96 | 16.4M | } |
97 | | |
98 | 6.07k | void ClearGlobalPgMemctxMap() { |
99 | 6.07k | postgres_process_memctxs.clear(); |
100 | 6.07k | } |
101 | | |
102 | | } // namespace pggate |
103 | | } // namespace yb |