summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2019-10-25 20:04:37 +0000
committerevergreen <evergreen@mongodb.com>2019-10-25 20:04:37 +0000
commitc801d7cca46f020a08d38e0e541d8e981b8649e0 (patch)
treeab2ee3adf9dd8e1c5001b4334b5ed75d4c8c3497
parenta54df9e45d85a670bc4dc40339d76347865fab69 (diff)
downloadmongo-c801d7cca46f020a08d38e0e541d8e981b8649e0.tar.gz
SERVER-44133 Refactor kms memory allocation to header file
-rw-r--r--src/mongo/shell/kms_aws.cpp51
-rw-r--r--src/mongo/util/kms_message_support.h68
2 files changed, 69 insertions, 50 deletions
diff --git a/src/mongo/shell/kms_aws.cpp b/src/mongo/shell/kms_aws.cpp
index 34b185a4f62..911269b8e11 100644
--- a/src/mongo/shell/kms_aws.cpp
+++ b/src/mongo/shell/kms_aws.cpp
@@ -42,6 +42,7 @@
#include "mongo/shell/kms.h"
#include "mongo/shell/kms_gen.h"
#include "mongo/util/base64.h"
+#include "mongo/util/kms_message_support.h"
#include "mongo/util/log.h"
#include "mongo/util/net/hostandport.h"
#include "mongo/util/net/sock.h"
@@ -54,56 +55,6 @@ namespace mongo {
namespace {
/**
- * Free kms_request_t
- */
-struct kms_request_tFree {
- void operator()(kms_request_t* p) noexcept {
- if (p) {
- ::kms_request_destroy(p);
- }
- }
-};
-
-using UniqueKmsRequest = std::unique_ptr<kms_request_t, kms_request_tFree>;
-
-/**
- * Free kms_response_parser_t
- */
-struct kms_response_parser_tFree {
- void operator()(kms_response_parser_t* p) noexcept {
- if (p) {
- ::kms_response_parser_destroy(p);
- }
- }
-};
-
-using UniqueKmsResponseParser = std::unique_ptr<kms_response_parser_t, kms_response_parser_tFree>;
-
-/**
- * Free kms_response_t
- */
-struct kms_response_tFree {
- void operator()(kms_response_t* p) noexcept {
- if (p) {
- ::kms_response_destroy(p);
- }
- }
-};
-
-using UniqueKmsResponse = std::unique_ptr<kms_response_t, kms_response_tFree>;
-
-/**
- * Free kms_char_buffer
- */
-struct kms_char_free {
- void operator()(char* x) {
- kms_request_free_string(x);
- }
-};
-
-using UniqueKmsCharBuffer = std::unique_ptr<char, kms_char_free>;
-
-/**
* Make a request to a AWS HTTP endpoint.
*
* Does not maintain a persistent HTTP connection.
diff --git a/src/mongo/util/kms_message_support.h b/src/mongo/util/kms_message_support.h
new file mode 100644
index 00000000000..c1af4e9c1f5
--- /dev/null
+++ b/src/mongo/util/kms_message_support.h
@@ -0,0 +1,68 @@
+/**
+ * Copyright (C) 2019-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include <kms_message/kms_message.h>
+#include <memory>
+
+namespace mongo {
+namespace kms_message_support_detail {
+
+template <typename T>
+using kms_message_deleter_func = void(T*);
+
+template <typename T, kms_message_deleter_func<T> DelFunc>
+struct KMSMessageDeleter {
+ void operator()(T* ptr) {
+ if (ptr) {
+ DelFunc(ptr);
+ }
+ }
+};
+
+template <typename T, kms_message_deleter_func<T> DelFunc>
+using kms_message_unique_ptr = std::unique_ptr<T, KMSMessageDeleter<T, DelFunc>>;
+
+} // namespace kms_message_support_detail
+
+/**
+ * Unique pointers to various types from the kms_message library.
+ */
+using UniqueKmsRequest =
+ kms_message_support_detail::kms_message_unique_ptr<kms_request_t, kms_request_destroy>;
+using UniqueKmsResponseParser =
+ kms_message_support_detail::kms_message_unique_ptr<kms_response_parser_t,
+ kms_response_parser_destroy>;
+using UniqueKmsResponse =
+ kms_message_support_detail::kms_message_unique_ptr<kms_response_t, kms_response_destroy>;
+using UniqueKmsCharBuffer =
+ kms_message_support_detail::kms_message_unique_ptr<char, kms_request_free_string>;
+
+} // namespace mongo