Prefer C++17 std::any instead of void *

This avoids unsafe casts and manual memory allocation.
This commit is contained in:
Andrew Gaul 2024-11-06 11:18:59 -08:00
parent 2852445113
commit 6b8670d7a3
9 changed files with 55 additions and 47 deletions

View File

@ -1232,9 +1232,9 @@ bool S3fsCurl::SetIPResolveType(const char* value)
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameter
// cppcheck-suppress constParameterCallback
bool S3fsCurl::UploadMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
bool S3fsCurl::UploadMultipartPostCallback(S3fsCurl* s3fscurl, std::any param)
{
if(!s3fscurl || param){ // this callback does not need a parameter
if(!s3fscurl){
return false;
}
@ -1244,9 +1244,9 @@ bool S3fsCurl::UploadMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameter
// cppcheck-suppress constParameterCallback
bool S3fsCurl::MixMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
bool S3fsCurl::MixMultipartPostCallback(S3fsCurl* s3fscurl, std::any param)
{
if(!s3fscurl || param){ // this callback does not need a parameter
if(!s3fscurl){
return false;
}
@ -4385,9 +4385,9 @@ bool S3fsCurl::UploadMultipartPostComplete()
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameter
// cppcheck-suppress constParameterCallback
bool S3fsCurl::CopyMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
bool S3fsCurl::CopyMultipartPostCallback(S3fsCurl* s3fscurl, std::any param)
{
if(!s3fscurl || param){ // this callback does not need a parameter
if(!s3fscurl){
return false;
}

View File

@ -21,6 +21,7 @@
#ifndef S3FS_CURL_H_
#define S3FS_CURL_H_
#include <any>
#include <atomic>
#include <cstdint>
#include <curl/curl.h>
@ -244,9 +245,9 @@ class S3fsCurl
static size_t UploadReadCallback(void *ptr, size_t size, size_t nmemb, void *userp);
static size_t DownloadWriteCallback(void* ptr, size_t size, size_t nmemb, void* userp);
static bool UploadMultipartPostCallback(S3fsCurl* s3fscurl, void* param);
static bool CopyMultipartPostCallback(S3fsCurl* s3fscurl, void* param);
static bool MixMultipartPostCallback(S3fsCurl* s3fscurl, void* param);
static bool UploadMultipartPostCallback(S3fsCurl* s3fscurl, std::any param);
static bool CopyMultipartPostCallback(S3fsCurl* s3fscurl, std::any param);
static bool MixMultipartPostCallback(S3fsCurl* s3fscurl, std::any param);
static std::unique_ptr<S3fsCurl> UploadMultipartPostRetryCallback(S3fsCurl* s3fscurl);
static std::unique_ptr<S3fsCurl> CopyMultipartPostRetryCallback(S3fsCurl* s3fscurl);
static std::unique_ptr<S3fsCurl> MixMultipartPostRetryCallback(S3fsCurl* s3fscurl);

View File

@ -90,16 +90,16 @@ S3fsMultiRetryCallback S3fsMultiCurl::SetRetryCallback(S3fsMultiRetryCallback fu
return old;
}
void* S3fsMultiCurl::SetSuccessCallbackParam(void* param)
std::any S3fsMultiCurl::SetSuccessCallbackParam(std::any param)
{
void* old = pSuccessCallbackParam;
std::any old = pSuccessCallbackParam;
pSuccessCallbackParam = param;
return old;
}
void* S3fsMultiCurl::SetNotFoundCallbackParam(void* param)
std::any S3fsMultiCurl::SetNotFoundCallbackParam(std::any param)
{
void* old = pNotFoundCallbackParam;
std::any old = pNotFoundCallbackParam;
pNotFoundCallbackParam = param;
return old;
}

View File

@ -21,6 +21,7 @@
#ifndef S3FS_CURL_MULTI_H_
#define S3FS_CURL_MULTI_H_
#include <any>
#include <future>
#include <memory>
#include <mutex>
@ -35,8 +36,8 @@
class S3fsCurl;
typedef std::vector<std::unique_ptr<S3fsCurl>> s3fscurllist_t;
typedef bool (*S3fsMultiSuccessCallback)(S3fsCurl* s3fscurl, void* param); // callback for succeed multi request
typedef bool (*S3fsMultiNotFoundCallback)(S3fsCurl* s3fscurl, void* param); // callback for succeed multi request
typedef bool (*S3fsMultiSuccessCallback)(S3fsCurl* s3fscurl, std::any param); // callback for succeed multi request
typedef bool (*S3fsMultiNotFoundCallback)(S3fsCurl* s3fscurl, std::any param); // callback for succeed multi request
typedef std::unique_ptr<S3fsCurl> (*S3fsMultiRetryCallback)(S3fsCurl* s3fscurl); // callback for failure and retrying
//----------------------------------------------
@ -54,8 +55,8 @@ class S3fsMultiCurl
S3fsMultiSuccessCallback SuccessCallback;
S3fsMultiNotFoundCallback NotFoundCallback;
S3fsMultiRetryCallback RetryCallback;
void* pSuccessCallbackParam;
void* pNotFoundCallbackParam;
std::any pSuccessCallbackParam;
std::any pNotFoundCallbackParam;
std::mutex completed_tids_lock;
std::vector<std::thread::id> completed_tids GUARDED_BY(completed_tids_lock);
@ -80,8 +81,8 @@ class S3fsMultiCurl
S3fsMultiSuccessCallback SetSuccessCallback(S3fsMultiSuccessCallback function);
S3fsMultiNotFoundCallback SetNotFoundCallback(S3fsMultiNotFoundCallback function);
S3fsMultiRetryCallback SetRetryCallback(S3fsMultiRetryCallback function);
void* SetSuccessCallbackParam(void* param);
void* SetNotFoundCallbackParam(void* param);
std::any SetSuccessCallbackParam(std::any param);
std::any SetNotFoundCallbackParam(std::any param);
bool Clear() { return ClearEx(true); }
bool SetS3fsCurlObject(std::unique_ptr<S3fsCurl> s3fscurl);
int Request();

View File

@ -50,10 +50,14 @@ int PseudoFdInfo::opt_max_threads = -1;
//
// Worker function for uploading
//
void* PseudoFdInfo::MultipartUploadThreadWorker(void* arg)
void* PseudoFdInfo::MultipartUploadThreadWorker(std::any arg)
{
std::unique_ptr<pseudofdinfo_thparam> pthparam(static_cast<pseudofdinfo_thparam*>(arg));
if(!pthparam || !(pthparam->ppseudofdinfo)){
if(arg.type() != typeid(pseudofdinfo_thparam)){
return reinterpret_cast<void*>(-EIO);
}
auto thparam = std::any_cast<pseudofdinfo_thparam>(arg);
auto *pthparam = &thparam;
if(!pthparam->ppseudofdinfo){
return reinterpret_cast<void*>(-EIO);
}
S3FS_PRN_INFO3("Upload Part Thread [tpath=%s][start=%lld][size=%lld][part=%d]", pthparam->path.c_str(), static_cast<long long>(pthparam->start), static_cast<long long>(pthparam->size), pthparam->part_num);
@ -454,27 +458,26 @@ bool PseudoFdInfo::ParallelMultipartUpload(const char* path, const mp_part_list_
}
// make parameter for my thread
auto* thargs = new pseudofdinfo_thparam;
thargs->ppseudofdinfo = this;
thargs->path = SAFESTRPTR(path);
thargs->upload_id = tmp_upload_id;
thargs->upload_fd = tmp_upload_fd;
thargs->start = iter->start;
thargs->size = iter->size;
thargs->is_copy = is_copy;
thargs->part_num = iter->part_num;
thargs->petag = petag;
pseudofdinfo_thparam thargs;
thargs.ppseudofdinfo = this;
thargs.path = SAFESTRPTR(path);
thargs.upload_id = tmp_upload_id;
thargs.upload_fd = tmp_upload_fd;
thargs.start = iter->start;
thargs.size = iter->size;
thargs.is_copy = is_copy;
thargs.part_num = iter->part_num;
thargs.petag = petag;
// make parameter for thread pool
thpoolman_param ppoolparam;
ppoolparam.args = thargs;
ppoolparam.args = std::move(thargs);
ppoolparam.psem = &uploaded_sem;
ppoolparam.pfunc = PseudoFdInfo::MultipartUploadThreadWorker;
// setup instruction
if(!ThreadPoolMan::Instruct(ppoolparam)){
S3FS_PRN_ERR("failed setup instruction for uploading.");
delete thargs;
return false;
}

View File

@ -21,6 +21,7 @@
#ifndef S3FS_FDCACHE_FDINFO_H_
#define S3FS_FDCACHE_FDINFO_H_
#include <any>
#include <memory>
#include <mutex>
#include <string>
@ -74,7 +75,7 @@ class PseudoFdInfo
Semaphore uploaded_sem; // use a semaphore to trigger an upload completion like event flag
private:
static void* MultipartUploadThreadWorker(void* arg);
static void* MultipartUploadThreadWorker(std::any arg);
bool Clear();
void CloseUploadFd();

View File

@ -125,7 +125,7 @@ static int check_object_access(const char* path, int mask, struct stat* pstbuf);
static int check_object_owner(const char* path, struct stat* pstbuf);
static int check_parent_object_access(const char* path, int mask);
static int get_local_fent(AutoFdEntity& autoent, FdEntity **entity, const char* path, int flags = O_RDONLY, bool is_load = false);
static bool multi_head_callback(S3fsCurl* s3fscurl, void* param);
static bool multi_head_callback(S3fsCurl* s3fscurl, std::any param);
static std::unique_ptr<S3fsCurl> multi_head_retry_callback(S3fsCurl* s3fscurl);
static int readdir_multi_head(const char* path, const S3ObjList& head, void* buf, fuse_fill_dir_t filler);
static int list_bucket(const char* path, S3ObjList& head, const char* delimiter, bool check_content_only = false);
@ -3123,7 +3123,7 @@ static int s3fs_opendir(const char* _path, struct fuse_file_info* fi)
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameterCallback
static bool multi_head_callback(S3fsCurl* s3fscurl, void* param)
static bool multi_head_callback(S3fsCurl* s3fscurl, std::any param)
{
if(!s3fscurl){
return false;
@ -3141,8 +3141,8 @@ static bool multi_head_callback(S3fsCurl* s3fscurl, void* param)
if(use_wtf8){
bpath = s3fs_wtf8_decode(bpath);
}
if(param){
auto* pcbparam = reinterpret_cast<SyncFiller*>(param);
if(param.type() == typeid(SyncFiller*)){
auto* pcbparam = std::any_cast<SyncFiller*>(param);
struct stat st;
if(StatCache::getStatCacheData()->GetStat(saved_path, &st)){
pcbparam->Fill(bpath.c_str(), &st, 0);
@ -3152,6 +3152,7 @@ static bool multi_head_callback(S3fsCurl* s3fscurl, void* param)
}
}else{
S3FS_PRN_WARN("param(multi_head_callback_param*) is nullptr, then can not call filler.");
return false;
}
return true;
@ -3163,20 +3164,20 @@ struct multi_head_notfound_callback_param
s3obj_list_t notfound_list;
};
static bool multi_head_notfound_callback(S3fsCurl* s3fscurl, void* param)
static bool multi_head_notfound_callback(S3fsCurl* s3fscurl, std::any param)
{
if(!s3fscurl){
return false;
}
S3FS_PRN_INFO("HEAD returned NotFound(404) for %s object, it maybe only the path exists and the object does not exist.", s3fscurl->GetPath().c_str());
if(!param){
if(param.type() != typeid(struct multi_head_notfound_callback_param*)){
S3FS_PRN_WARN("param(multi_head_notfound_callback_param*) is nullptr, then can not call filler.");
return false;
}
// set path to not found list
auto* pcbparam = reinterpret_cast<struct multi_head_notfound_callback_param*>(param);
auto* pcbparam = std::any_cast<struct multi_head_notfound_callback_param*>(param);
const std::lock_guard<std::mutex> lock(pcbparam->list_lock);
pcbparam->notfound_list.push_back(s3fscurl->GetBasePath());
@ -3236,13 +3237,13 @@ static int readdir_multi_head(const char* path, const S3ObjList& head, void* buf
// Success Callback function parameter(SyncFiller object)
SyncFiller syncfiller(buf, filler);
curlmulti.SetSuccessCallbackParam(reinterpret_cast<void*>(&syncfiller));
curlmulti.SetSuccessCallbackParam(&syncfiller);
// Not found Callback function parameter
struct multi_head_notfound_callback_param notfound_param;
if(support_compat_dir){
curlmulti.SetNotFoundCallback(multi_head_notfound_callback);
curlmulti.SetNotFoundCallbackParam(reinterpret_cast<void*>(&notfound_param));
curlmulti.SetNotFoundCallbackParam(&notfound_param);
}
// Make single head request(with max).

View File

@ -96,7 +96,7 @@ void ThreadPoolMan::Worker(ThreadPoolMan* psingleton, std::promise<int> promise)
}
}
void* retval = param.pfunc(param.args);
void* retval = param.pfunc(std::move(param.args));
if(nullptr != retval){
S3FS_PRN_WARN("The instruction function returned with something error code(%ld).", reinterpret_cast<long>(retval));
}

View File

@ -21,6 +21,7 @@
#ifndef S3FS_THREADPOOLMAN_H_
#define S3FS_THREADPOOLMAN_H_
#include <any>
#include <atomic>
#include <future>
#include <list>
@ -36,7 +37,7 @@
//
// Prototype function
//
typedef void* (*thpoolman_worker)(void*);
typedef void* (*thpoolman_worker)(std::any);
//
// Parameter structure
@ -48,7 +49,7 @@ typedef void* (*thpoolman_worker)(void*);
//
struct thpoolman_param
{
void* args = nullptr;
std::any args;
Semaphore* psem = nullptr;
thpoolman_worker pfunc = nullptr;
};