refactor: reduce use of tr_malloc / tr_free (#3652)

* refactor: avoid tr_new in macos Torrent.trashDataFile

* refactor: avoid tr_new in maco PiecesView

* refactor: avoid tr_new in qt createVariant()
This commit is contained in:
Charles Kerr 2022-08-16 12:03:09 -05:00 committed by GitHub
parent 293f4f6759
commit 6278ea7017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 26 deletions

View File

@ -2,6 +2,8 @@
// It may be used under the MIT (SPDX: MIT) license.
// License text can be found in the licenses/ folder.
#include <vector>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
@ -26,7 +28,7 @@ enum
@interface PiecesView ()
@property(nonatomic) int8_t* fPieces;
@property(nonatomic) std::vector<int8_t> fPieces;
@property(nonatomic) NSInteger fNumPieces;
@property(nonatomic) NSInteger fAcross;
@ -57,7 +59,6 @@ enum
- (void)dealloc
{
tr_free(_fPieces);
}
- (void)setTorrent:(Torrent*)torrent
@ -84,8 +85,7 @@ enum
- (void)clearView
{
tr_free(self.fPieces);
self.fPieces = NULL;
self.fPieces.clear();
}
- (void)updateView
@ -96,25 +96,25 @@ enum
}
//determine if first time
BOOL const first = self.fPieces == NULL;
BOOL const first = std::empty(self.fPieces);
if (first)
{
self.fPieces = (int8_t*)tr_malloc(self.fNumPieces * sizeof(int8_t));
self.fPieces.resize(self.fNumPieces);
}
int8_t* pieces = NULL;
float* piecesPercent = NULL;
auto pieces = std::vector<int8_t>{};
auto piecesPercent = std::vector<float>{};
BOOL const showAvailability = [NSUserDefaults.standardUserDefaults boolForKey:@"PiecesViewShowAvailability"];
if (showAvailability)
{
pieces = (int8_t*)tr_malloc(self.fNumPieces * sizeof(int8_t));
[self.torrent getAvailability:pieces size:self.fNumPieces];
pieces.resize(self.fNumPieces);
[self.torrent getAvailability:std::data(pieces) size:std::size(pieces)];
}
else
{
piecesPercent = (float*)tr_malloc(self.fNumPieces * sizeof(float));
[self.torrent getAmountFinished:piecesPercent size:self.fNumPieces];
piecesPercent.resize(self.fNumPieces);
[self.torrent getAmountFinished:std::data(piecesPercent) size:std::size(piecesPercent)];
}
NSImage* image = self.image;
@ -193,9 +193,6 @@ enum
[image unlockFocus];
[self setNeedsDisplay];
}
tr_free(pieces);
tr_free(piecesPercent);
}
- (BOOL)acceptsFirstMouse:(NSEvent*)event

View File

@ -3,6 +3,7 @@
// License text can be found in the licenses/ folder.
#include <optional>
#include <vector>
#include <fmt/format.h>
@ -1535,14 +1536,14 @@ bool trashDataFile(char const* filename, tr_error** error)
- (void)setFilePriority:(tr_priority_t)priority forIndexes:(NSIndexSet*)indexSet
{
NSUInteger const count = indexSet.count;
tr_file_index_t* files = static_cast<tr_file_index_t*>(tr_malloc(count * sizeof(tr_file_index_t)));
auto files = std::vector<tr_file_index_t>{};
files.resize(count);
for (NSUInteger index = indexSet.firstIndex, i = 0; index != NSNotFound; index = [indexSet indexGreaterThanIndex:index], i++)
{
files[i] = index;
}
tr_torrentSetFilePriorities(self.fHandle, files, count, priority);
tr_free(files);
tr_torrentSetFilePriorities(self.fHandle, std::data(files), std::size(files), priority);
}
- (BOOL)hasFilePriority:(tr_priority_t)priority forIndexes:(NSIndexSet*)indexSet

View File

@ -16,7 +16,6 @@
#include <libtransmission/rpcimpl.h>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h> // tr_free
#include <libtransmission/version.h> // LONG_VERSION_STRING
#include "VariantHelpers.h"
@ -31,15 +30,14 @@ namespace
char const constexpr* const RequestDataPropertyKey{ "requestData" };
char const constexpr* const RequestFutureinterfacePropertyKey{ "requestReplyFutureInterface" };
void destroyVariant(tr_variant* json)
{
tr_variantFree(json);
tr_free(json);
}
TrVariantPtr createVariant()
{
return { tr_new0(tr_variant, 1), &destroyVariant };
return { new tr_variant{},
[](tr_variant* var)
{
tr_variantFree(var);
delete var;
} };
}
} // namespace