universal7885: parts-aidl: Concat swap related code to one file

Also run clang-format
This commit is contained in:
roynatech2544 2022-11-09 18:54:35 +09:00
commit 5aab0c64ed
4 changed files with 106 additions and 102 deletions

View file

@ -5,7 +5,6 @@ cc_binary {
"FlashLight.cpp", "FlashLight.cpp",
"Display.cpp", "Display.cpp",
"Swap.cpp", "Swap.cpp",
"SwapHelpers.cpp",
"SmartCharge.cpp", "SmartCharge.cpp",
"service.cpp", "service.cpp",
], ],

View file

@ -1,4 +1,4 @@
// Copyright (C) 2021 Eureka Team // Copyright (C) 2022 Eureka Team
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -13,15 +13,98 @@
// limitations under the License. // limitations under the License.
#include "Swap.h" #include "Swap.h"
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <sys/stat.h>
#include <sys/swap.h> #include <sys/swap.h>
#include <sys/types.h>
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
#include <vector>
extern int mkswap(const char *filename); namespace {
extern void mkfile(int filesize, const char *name);
/* XXX This needs to be obtained from kernel headers. See b/9336527 */
struct linux_swap_header {
char bootbits[1024]; /* Space for disklabel etc. */
u_int32_t version;
u_int32_t last_page;
u_int32_t nr_badpages;
unsigned char sws_uuid[16];
unsigned char sws_volume[16];
u_int32_t padding[117];
u_int32_t badpages[1];
};
void mkfile(int filesize, const char *path) {
std::vector<char> empty(1024, 0);
std::ofstream ofs(std::string(path), std::ios::binary | std::ios::out);
for (int i = 0; i < 1024 * filesize; i++) {
ofs.write(empty.data(), empty.size());
}
}
#define MAGIC_SWAP_HEADER "SWAPSPACE2"
#define MAGIC_SWAP_HEADER_LEN 10
#define MIN_PAGES 10
int mkswap(const char *filename) {
int err = 0;
int fd;
ssize_t len;
off_t swap_size;
int pagesize;
struct linux_swap_header sw_hdr;
fd = open(filename, O_WRONLY | O_CLOEXEC);
if (fd < 0) {
err = errno;
return err;
}
pagesize = getpagesize();
/* Determine the length of the swap file */
swap_size = lseek(fd, 0, SEEK_END);
if (swap_size < MIN_PAGES * pagesize) {
err = -ENOSPC;
goto err;
}
if (lseek(fd, 0, SEEK_SET)) {
err = errno;
goto err;
}
memset(&sw_hdr, 0, sizeof(sw_hdr));
sw_hdr.version = 1;
sw_hdr.last_page = (swap_size / pagesize) - 1;
len = write(fd, &sw_hdr, sizeof(sw_hdr));
if (len != sizeof(sw_hdr)) {
err = errno;
goto err;
}
/* Write the magic header */
if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) {
err = errno;
goto err;
}
len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN);
if (len != MAGIC_SWAP_HEADER_LEN) {
err = errno;
goto err;
}
if (fsync(fd) < 0) {
err = errno;
goto err;
}
err:
close(fd);
return err;
}
} // namespace
constexpr const char *SWAP_PATH = "/data/swap/swapfile"; constexpr const char *SWAP_PATH = "/data/swap/swapfile";
@ -29,9 +112,7 @@ namespace aidl::vendor::eureka::hardware::parts {
static std::mutex thread_lock; static std::mutex thread_lock;
static inline bool swapfile_exist(void) { static inline bool swapfile_exist(void) { return access(SWAP_PATH, F_OK) == 0; }
return access(SWAP_PATH, F_OK) == 0;
}
static void makeFile(int32_t mSwapSize) { static void makeFile(int32_t mSwapSize) {
const std::lock_guard<std::mutex> lock(thread_lock); const std::lock_guard<std::mutex> lock(thread_lock);
@ -40,7 +121,8 @@ static void makeFile(int32_t mSwapSize) {
} }
::ndk::ScopedAStatus SwapOnData::makeSwapFile(int32_t size) { ::ndk::ScopedAStatus SwapOnData::makeSwapFile(int32_t size) {
if (swapfile_exist()) return ::ndk::ScopedAStatus::ok(); if (swapfile_exist())
return ::ndk::ScopedAStatus::ok();
std::thread makefile_thread(makeFile, size); std::thread makefile_thread(makeFile, size);
makefile_thread.detach(); makefile_thread.detach();
@ -53,7 +135,8 @@ static void rmswap(void) {
} }
::ndk::ScopedAStatus SwapOnData::removeSwapFile(void) { ::ndk::ScopedAStatus SwapOnData::removeSwapFile(void) {
if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok(); if (!swapfile_exist())
return ::ndk::ScopedAStatus::ok();
std::thread rmswap_thread(rmswap); std::thread rmswap_thread(rmswap);
rmswap_thread.detach(); rmswap_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
@ -61,12 +144,15 @@ static void rmswap(void) {
static void swapon_func(cb_t cb) { static void swapon_func(cb_t cb) {
const std::lock_guard<std::mutex> lock(thread_lock); const std::lock_guard<std::mutex> lock(thread_lock);
int res = swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK); int res =
if (cb != nullptr) cb->respondToBool(res == 0); swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
if (cb != nullptr)
cb->respondToBool(res == 0);
} }
::ndk::ScopedAStatus SwapOnData::setSwapOn(cb_t cb) { ::ndk::ScopedAStatus SwapOnData::setSwapOn(cb_t cb) {
if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok(); if (!swapfile_exist())
return ::ndk::ScopedAStatus::ok();
std::thread swapon_thread(swapon_func, cb); std::thread swapon_thread(swapon_func, cb);
swapon_thread.detach(); swapon_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
@ -78,18 +164,19 @@ static void swapoff_func(void) {
} }
::ndk::ScopedAStatus SwapOnData::setSwapOff() { ::ndk::ScopedAStatus SwapOnData::setSwapOff() {
if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok(); if (!swapfile_exist())
return ::ndk::ScopedAStatus::ok();
std::thread swapoff_thread(swapoff_func); std::thread swapoff_thread(swapoff_func);
swapoff_thread.detach(); swapoff_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus SwapOnData::isMutexLocked(bool *_aidl_return) { ::ndk::ScopedAStatus SwapOnData::isMutexLocked(bool *_aidl_return) {
if (thread_lock.try_lock()) { if (thread_lock.try_lock()) {
thread_lock.unlock(); thread_lock.unlock();
*_aidl_return = false; *_aidl_return = false;
} else { } else {
*_aidl_return = true; *_aidl_return = true;
} }
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }

View file

@ -18,7 +18,7 @@
namespace aidl::vendor::eureka::hardware::parts { namespace aidl::vendor::eureka::hardware::parts {
using cb_t = const std::shared_ptr<IBoolCallback>&; using cb_t = const std::shared_ptr<IBoolCallback> &;
struct SwapOnData : public BnSwapOnData { struct SwapOnData : public BnSwapOnData {
// Methods from ::aidl::vendor::eureka::hardware::parts::ISwapOnData // Methods from ::aidl::vendor::eureka::hardware::parts::ISwapOnData
@ -29,4 +29,4 @@ struct SwapOnData : public BnSwapOnData {
::ndk::ScopedAStatus setSwapOff(void); ::ndk::ScopedAStatus setSwapOff(void);
::ndk::ScopedAStatus isMutexLocked(bool *_aidl_return); ::ndk::ScopedAStatus isMutexLocked(bool *_aidl_return);
}; };
} // namespace vendor::eureka::hardware::parts::V1_0 } // namespace aidl::vendor::eureka::hardware::parts

View file

@ -1,82 +0,0 @@
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <sys/stat.h>
#include <sys/swap.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
/* XXX This needs to be obtained from kernel headers. See b/9336527 */
struct linux_swap_header {
char bootbits[1024]; /* Space for disklabel etc. */
u_int32_t version;
u_int32_t last_page;
u_int32_t nr_badpages;
unsigned char sws_uuid[16];
unsigned char sws_volume[16];
u_int32_t padding[117];
u_int32_t badpages[1];
};
void mkfile(int filesize, const char *path) {
std::vector<char> empty(1024, 0);
std::ofstream ofs(std::string(path), std::ios::binary | std::ios::out);
for (int i = 0; i < 1024 * filesize; i++) {
ofs.write(&empty[0], empty.size());
}
}
#define MAGIC_SWAP_HEADER "SWAPSPACE2"
#define MAGIC_SWAP_HEADER_LEN 10
#define MIN_PAGES 10
int mkswap(const char *filename) {
int err = 0;
int fd;
ssize_t len;
off_t swap_size;
int pagesize;
struct linux_swap_header sw_hdr;
fd = open(filename, O_WRONLY | O_CLOEXEC);
if (fd < 0) {
err = errno;
return err;
}
pagesize = getpagesize();
/* Determine the length of the swap file */
swap_size = lseek(fd, 0, SEEK_END);
if (swap_size < MIN_PAGES * pagesize) {
err = -ENOSPC;
goto err;
}
if (lseek(fd, 0, SEEK_SET)) {
err = errno;
goto err;
}
memset(&sw_hdr, 0, sizeof(sw_hdr));
sw_hdr.version = 1;
sw_hdr.last_page = (swap_size / pagesize) - 1;
len = write(fd, &sw_hdr, sizeof(sw_hdr));
if (len != sizeof(sw_hdr)) {
err = errno;
goto err;
}
/* Write the magic header */
if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) {
err = errno;
goto err;
}
len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN);
if (len != MAGIC_SWAP_HEADER_LEN) {
err = errno;
goto err;
}
if (fsync(fd) < 0) {
err = errno;
goto err;
}
err:
close(fd);
return err;
}