1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 00:07:35 +00:00

LibCore: Make ProcessStatisticsReader return results in a Vector

The HashMap API was overkill and made using this less ergonomic than
it should be.
This commit is contained in:
Andreas Kling 2021-05-23 11:08:32 +02:00
parent a345a1f4a1
commit a1e133cc6b
12 changed files with 79 additions and 83 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -17,7 +17,7 @@ namespace Core {
HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
{
if (proc_all_file) {
if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) {
@ -32,7 +32,7 @@ Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_a
}
}
HashMap<pid_t, Core::ProcessStatistics> map;
Vector<Core::ProcessStatistics> processes;
auto file_contents = proc_all_file->read_all();
auto json = JsonValue::from_string(file_contents);
@ -93,13 +93,13 @@ Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_a
// and synthetic data last
process.username = username_from_uid(process.uid);
map.set(process.pid, process);
processes.append(move(process));
});
return map;
return processes;
}
Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
{
RefPtr<Core::File> proc_all_file;
return get_all(proc_all_file);

View file

@ -1,12 +1,11 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/String.h>
#include <LibCore/File.h>
#include <unistd.h>
@ -67,8 +66,8 @@ struct ProcessStatistics {
class ProcessStatisticsReader {
public:
static Optional<HashMap<pid_t, Core::ProcessStatistics>> get_all(RefPtr<Core::File>&);
static Optional<HashMap<pid_t, Core::ProcessStatistics>> get_all();
static Optional<Vector<Core::ProcessStatistics>> get_all(RefPtr<Core::File>&);
static Optional<Vector<Core::ProcessStatistics>> get_all();
private:
static String username_from_uid(uid_t);