Skip to content

Commit

Permalink
Natively allocate rpmfs structs
Browse files Browse the repository at this point in the history
  • Loading branch information
pmatilai committed Apr 30, 2024
1 parent ee3f62d commit 553d8d8
Showing 1 changed file with 21 additions and 36 deletions.
57 changes: 21 additions & 36 deletions lib/rpmfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@
#include <string.h>

#include "system.h"

#include <vector>

#include "rpmfs.h"
#include "debug.h"

using std::vector;

struct rpmfs_s {
unsigned int fc;

rpm_fstate_t * states;
rpmFileAction * actions; /*!< File disposition(s). */

sharedFileInfo replaced; /*!< (TR_ADDED) to be replaced files in the rpmdb */
int numReplaced;
int allocatedReplaced;
vector<rpm_fstate_t> states;
vector<rpmFileAction> actions; /*!< File disposition(s). */
vector<sharedFileInfo_s> replaced; /*!< (TR_ADDED) to be replaced files in the rpmdb */
};

rpmfs rpmfsNew(rpm_count_t fc, int initState)
{
rpmfs fs = (rpmfs)xcalloc(1, sizeof(*fs));
rpmfs fs = new rpmfs_s {};
fs->fc = fc;
fs->actions = (rpmFileAction *)xcalloc(fs->fc, sizeof(*fs->actions));
fs->actions.resize(fs->fc, FA_UNKNOWN);
if (initState) {
fs->states = (rpm_fstate_t *)xmalloc(sizeof(*fs->states) * fs->fc);
memset(fs->states, RPMFILE_STATE_NORMAL, fs->fc);
fs->states.resize(fs->fc, RPMFILE_STATE_NORMAL);
}
return fs;
}

rpmfs rpmfsFree(rpmfs fs)
{
if (fs != NULL) {
free(fs->replaced);
free(fs->states);
free(fs->actions);
memset(fs, 0, sizeof(*fs)); /* trash and burn */
free(fs);
delete fs;
}
return NULL;
}
Expand All @@ -48,35 +45,23 @@ rpm_count_t rpmfsFC(rpmfs fs)
void rpmfsAddReplaced(rpmfs fs, int pkgFileNum, char rstate,
int otherPkg, int otherFileNum)
{
if (!fs->replaced) {
fs->replaced = (sharedFileInfo)xcalloc(3, sizeof(*fs->replaced));
fs->allocatedReplaced = 3;
}
if (fs->numReplaced>=fs->allocatedReplaced) {
fs->allocatedReplaced += (fs->allocatedReplaced>>1) + 2;
fs->replaced = xrealloc(fs->replaced, fs->allocatedReplaced*sizeof(*fs->replaced));
}
fs->replaced[fs->numReplaced].pkgFileNum = pkgFileNum;
fs->replaced[fs->numReplaced].rstate = rstate;
fs->replaced[fs->numReplaced].otherPkg = otherPkg;
fs->replaced[fs->numReplaced].otherFileNum = otherFileNum;

fs->numReplaced++;
fs->replaced.push_back({pkgFileNum, otherPkg, otherFileNum, rstate});
}

sharedFileInfo rpmfsGetReplaced(rpmfs fs)
{
if (fs && fs->numReplaced)
return fs->replaced;
if (fs && fs->replaced.empty() == false)
return fs->replaced.data();
else
return NULL;
}

/* Eek */
sharedFileInfo rpmfsNextReplaced(rpmfs fs , sharedFileInfo replaced)
{
if (fs && replaced) {
replaced++;
if (replaced - fs->replaced < fs->numReplaced)
if (replaced - fs->replaced.data() < fs->replaced.size())
return replaced;
}
return NULL;
Expand All @@ -90,13 +75,13 @@ void rpmfsSetState(rpmfs fs, unsigned int ix, rpmfileState state)

rpm_fstate_t * rpmfsGetStates(rpmfs fs)
{
return (fs != NULL) ? fs->states : NULL;
return (fs != NULL) ? fs->states.data() : NULL;
}

rpmFileAction rpmfsGetAction(rpmfs fs, unsigned int ix)
{
rpmFileAction action;
if (fs && fs->actions != NULL && ix < fs->fc) {
if (fs && ix < fs->fc) {
action = fs->actions[ix];
} else {
action = FA_UNKNOWN;
Expand All @@ -106,14 +91,14 @@ rpmFileAction rpmfsGetAction(rpmfs fs, unsigned int ix)

void rpmfsSetAction(rpmfs fs, unsigned int ix, rpmFileAction action)
{
if (fs->actions != NULL && ix < fs->fc) {
if (ix < fs->fc) {
fs->actions[ix] = action;
}
}

void rpmfsResetActions(rpmfs fs)
{
if (fs && fs->actions) {
if (fs) {
for (int i = 0; i < fs->fc; i++) {
/* --excludepaths is processed early, avoid undoing that */
if (fs->actions[i] != FA_SKIPNSTATE)
Expand Down

0 comments on commit 553d8d8

Please sign in to comment.