update_engine: Move glib related utils to glib_utils.h.
This patch move the remaining glib.h functions used from utils.h to
glib_utils.h.
BUG=chromium:499886
TEST=emerge-link update_engine
Change-Id: Ia727cf3d4ee29fb33fa4db500ca83ea37d23a335
Reviewed-on: https://chromium-review.googlesource.com/281446
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Trybot-Ready: Alex Deymo <deymo@chromium.org>
diff --git a/glib_utils.cc b/glib_utils.cc
index 3371da4..625d8d1 100644
--- a/glib_utils.cc
+++ b/glib_utils.cc
@@ -24,5 +24,14 @@
return message;
}
+gchar** StringVectorToGStrv(const std::vector<string> &vec_str) {
+ GPtrArray *p = g_ptr_array_new();
+ for (const string& str : vec_str) {
+ g_ptr_array_add(p, g_strdup(str.c_str()));
+ }
+ g_ptr_array_add(p, nullptr);
+ return reinterpret_cast<gchar**>(g_ptr_array_free(p, FALSE));
+}
+
} // namespace utils
} // namespace chromeos_update_engine
diff --git a/glib_utils.h b/glib_utils.h
index 0c941ca..97020cb 100644
--- a/glib_utils.h
+++ b/glib_utils.h
@@ -6,6 +6,7 @@
#define UPDATE_ENGINE_GLIB_UTILS_H_
#include <string>
+#include <vector>
#include <glib.h>
@@ -16,6 +17,29 @@
// object and resets error to null.
std::string GetAndFreeGError(GError** error);
+// Converts a vector of strings to a NUL-terminated array of
+// strings. The resulting array should be freed with g_strfreev()
+// when are you done with it.
+gchar** StringVectorToGStrv(const std::vector<std::string>& vec_str);
+
+// A base::FreeDeleter that frees memory using g_free(). Useful when
+// integrating with GLib since it can be used with std::unique_ptr to
+// automatically free memory when going out of scope.
+struct GLibFreeDeleter {
+ inline void operator()(void* ptr) const {
+ g_free(static_cast<gpointer>(ptr));
+ }
+};
+
+// A base::FreeDeleter that frees memory using g_strfreev(). Useful
+// when integrating with GLib since it can be used with std::unique_ptr to
+// automatically free memory when going out of scope.
+struct GLibStrvFreeDeleter {
+ inline void operator()(void* ptr) const {
+ g_strfreev(static_cast<gchar**>(ptr));
+ }
+};
+
} // namespace utils
} // namespace chromeos_update_engine
diff --git a/p2p_manager.cc b/p2p_manager.cc
index 8d519fa..c1a2738 100644
--- a/p2p_manager.cc
+++ b/p2p_manager.cc
@@ -245,7 +245,7 @@
may_be_running_ = true; // Unless successful, we must be conservative.
vector<string> args = configuration_->GetInitctlArgs(should_be_running);
- unique_ptr<gchar*, GLibStrvFreeDeleter> argv(
+ unique_ptr<gchar*, utils::GLibStrvFreeDeleter> argv(
utils::StringVectorToGStrv(args));
if (!g_spawn_sync(nullptr, // working_directory
argv.get(),
@@ -260,7 +260,8 @@
<< ": " << utils::GetAndFreeGError(&error);
return false;
}
- unique_ptr<gchar, GLibFreeDeleter> standard_error_deleter(standard_error);
+ unique_ptr<gchar, utils::GLibFreeDeleter> standard_error_deleter(
+ standard_error);
if (!WIFEXITED(exit_status)) {
LOG(ERROR) << "Error spawning '" << utils::StringVectorToString(args)
diff --git a/utils.cc b/utils.cc
index 03f642a..f36cbec 100644
--- a/utils.cc
+++ b/utils.cc
@@ -40,7 +40,6 @@
#include <chromeos/data_encoding.h>
#include <chromeos/key_value_store.h>
#include <chromeos/message_loops/message_loop.h>
-#include <glib.h>
#include "update_engine/clock_interface.h"
#include "update_engine/constants.h"
@@ -676,7 +675,7 @@
TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY &&
num_retries < kUnmountMaxNumOfRetries);
- g_usleep(kUnmountRetryIntervalInMicroseconds);
+ usleep(kUnmountRetryIntervalInMicroseconds);
}
return true;
}
@@ -1503,15 +1502,6 @@
return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us);
}
-gchar** StringVectorToGStrv(const vector<string> &vec_str) {
- GPtrArray *p = g_ptr_array_new();
- for (const string& str : vec_str) {
- g_ptr_array_add(p, g_strdup(str.c_str()));
- }
- g_ptr_array_add(p, nullptr);
- return reinterpret_cast<gchar**>(g_ptr_array_free(p, FALSE));
-}
-
string StringVectorToString(const vector<string> &vec_str) {
string str = "[";
for (vector<string>::const_iterator i = vec_str.begin();
diff --git a/utils.h b/utils.h
index f33c011..bd3fda6 100644
--- a/utils.h
+++ b/utils.h
@@ -19,7 +19,6 @@
#include <base/posix/eintr_wrapper.h>
#include <base/time/time.h>
#include <chromeos/secure_blob.h>
-#include <glib.h>
#include "metrics/metrics_library.h"
#include "update_engine/action.h"
@@ -41,11 +40,6 @@
// down.
base::Time TimeFromStructTimespec(struct timespec *ts);
-// Converts a vector of strings to a NUL-terminated array of
-// strings. The resulting array should be freed with g_strfreev()
-// when are you done with it.
-gchar** StringVectorToGStrv(const std::vector<std::string> &vec_str);
-
// Formats |vec_str| as a string of the form ["<elem1>", "<elem2>"].
// Does no escaping, only use this for presentation in error messages.
std::string StringVectorToString(const std::vector<std::string> &vec_str);
@@ -577,24 +571,6 @@
DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter);
};
-// A base::FreeDeleter that frees memory using g_free(). Useful when
-// integrating with GLib since it can be used with std::unique_ptr to
-// automatically free memory when going out of scope.
-struct GLibFreeDeleter : public base::FreeDeleter {
- inline void operator()(void *ptr) const {
- g_free(reinterpret_cast<gpointer>(ptr));
- }
-};
-
-// A base::FreeDeleter that frees memory using g_strfreev(). Useful
-// when integrating with GLib since it can be used with std::unique_ptr to
-// automatically free memory when going out of scope.
-struct GLibStrvFreeDeleter : public base::FreeDeleter {
- inline void operator()(void *ptr) const {
- g_strfreev(reinterpret_cast<gchar**>(ptr));
- }
-};
-
} // namespace chromeos_update_engine
#define TEST_AND_RETURN_FALSE_ERRNO(_x) \