Skip to content

Commit

Permalink
Natively allocate rpmps and rpmprob structs, use vector for prob storage
Browse files Browse the repository at this point in the history
  • Loading branch information
pmatilai committed Apr 30, 2024
1 parent 22a4c32 commit c20f030
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lib/rpmprob.c
Expand Up @@ -29,7 +29,7 @@ rpmProblem rpmProblemCreate(rpmProblemType type,
const char * altNEVR,
const char * str, uint64_t number)
{
rpmProblem p = (rpmProblem)xcalloc(1, sizeof(*p));
rpmProblem p = new rpmProblem_s {};

p->type = type;
p->key = key;
Expand All @@ -52,7 +52,7 @@ rpmProblem rpmProblemFree(rpmProblem prob)
prob->pkgNEVR = _free(prob->pkgNEVR);
prob->altNEVR = _free(prob->altNEVR);
prob->str1 = _free(prob->str1);
free(prob);
delete prob;
return NULL;
}

Expand Down
46 changes: 16 additions & 30 deletions lib/rpmps.c
Expand Up @@ -4,6 +4,8 @@

#include "system.h"

#include <vector>

#include <inttypes.h>
#include <stdlib.h>

Expand All @@ -12,10 +14,10 @@

#include "debug.h"

using std::vector;

struct rpmps_s {
int numProblems; /*!< Current probs array size. */
int numProblemsAlloced; /*!< Allocated probs array size. */
rpmProblem *probs; /*!< Array of pointers to specific problems. */
vector<rpmProblem> probs; /*!< Array of pointers to specific problems. */
int nrefs; /*!< Reference count. */
};

Expand Down Expand Up @@ -44,16 +46,16 @@ rpmps rpmpsLink(rpmps ps)
int rpmpsNumProblems(rpmps ps)
{
int numProblems = 0;
if (ps && ps->probs)
numProblems = ps->numProblems;
if (ps)
numProblems = ps->probs.size();
return numProblems;
}

rpmpsi rpmpsInitIterator(rpmps ps)
{
rpmpsi psi = NULL;
if (ps != NULL && ps->numProblems > 0) {
psi = (rpmpsi)xcalloc(1, sizeof(*psi));
if (ps != NULL && ps->probs.empty() == false) {
psi = new rpmpsi_s {};
psi->ps = rpmpsLink(ps);
psi->ix = -1;
}
Expand All @@ -64,7 +66,7 @@ rpmpsi rpmpsFreeIterator(rpmpsi psi)
{
if (psi != NULL) {
rpmpsUnlink(psi->ps);
free(psi);
delete psi;
}
return NULL;
}
Expand All @@ -74,7 +76,7 @@ rpmProblem rpmpsiNext(rpmpsi psi)
rpmProblem p = NULL;
if (psi != NULL && psi->ps != NULL && ++psi->ix >= 0) {
rpmps ps = psi->ps;
if (psi->ix < ps->numProblems) {
if (psi->ix < ps->probs.size()) {
p = ps->probs[psi->ix];
} else {
psi->ix = -1;
Expand All @@ -99,7 +101,7 @@ rpmProblem rpmpsGetProblem(rpmpsi psi)

rpmps rpmpsCreate(void)
{
rpmps ps = (rpmps)xcalloc(1, sizeof(*ps));
rpmps ps = new rpmps_s {};
return rpmpsLink(ps);
}

Expand All @@ -110,33 +112,17 @@ rpmps rpmpsFree(rpmps ps)
return rpmpsUnlink(ps);
}

if (ps->probs) {
rpmpsi psi = rpmpsInitIterator(ps);
while (rpmpsNextIterator(psi) >= 0) {
rpmProblemFree(rpmpsGetProblem(psi));
}
rpmpsFreeIterator(psi);
ps->probs = _free(ps->probs);
}
ps = _free(ps);
for (auto & prob : ps->probs)
rpmProblemFree(prob);
delete ps;
return NULL;
}

void rpmpsAppendProblem(rpmps ps, rpmProblem prob)
{
if (ps == NULL || prob == NULL) return;

if (ps->numProblems == ps->numProblemsAlloced) {
if (ps->numProblemsAlloced)
ps->numProblemsAlloced *= 2;
else
ps->numProblemsAlloced = 2;
ps->probs = xrealloc(ps->probs,
ps->numProblemsAlloced * sizeof(*ps->probs));
}

ps->probs[ps->numProblems] = rpmProblemLink(prob);
ps->numProblems++;
ps->probs.push_back(rpmProblemLink(prob));
}

/*
Expand Down

0 comments on commit c20f030

Please sign in to comment.