From dc65c01ee09b29731b1623a6c54734847c07d5af Mon Sep 17 00:00:00 2001 From: Thierry Vignaud Date: Thu, 14 Jun 2012 18:23:50 +0000 Subject: (_run_cb_while_traversing) introduce this helper --- URPM.xs | 63 +++++++++++++++++++++------------------------------------------ 1 file changed, 21 insertions(+), 42 deletions(-) (limited to 'URPM.xs') diff --git a/URPM.xs b/URPM.xs index 0e70ae3..d792539 100644 --- a/URPM.xs +++ b/URPM.xs @@ -113,6 +113,24 @@ static int rpmError_callback() { return RPMLOG_DEFAULT; } +static inline int _run_cb_while_traversing(SV *callback, Header header) { + dSP; + URPM__Package pkg = calloc(1, sizeof(struct s_Package)); + + pkg->flag = FLAG_ID_INVALID | FLAG_NO_HEADER_FREE; + pkg->h = header; + + PUSHMARK(SP); + mXPUSHs(sv_setref_pv(newSVpvs(""), "URPM::Package", pkg)); + PUTBACK; + + int count = call_sv(callback, G_SCALAR); + + SPAGAIN; + pkg->h = NULL; /* avoid using it anymore, in case it has been copied inside callback */ + return count; +} + static inline void _header_free(URPM__Package pkg) { if (pkg->h && !(pkg->flag & FLAG_NO_HEADER_FREE)) pkg->h = headerFree(pkg->h); @@ -2530,20 +2548,7 @@ Db_traverse(db,callback) mi = rpmtsInitIterator(db->ts, RPMDBI_PACKAGES, NULL, 0); while ((header = rpmdbNextIterator(mi))) { if (SvROK(callback)) { - dSP; - URPM__Package pkg = calloc(1, sizeof(struct s_Package)); - - pkg->flag = FLAG_ID_INVALID | FLAG_NO_HEADER_FREE; - pkg->h = header; - - PUSHMARK(SP); - mXPUSHs(sv_setref_pv(newSVpvs(""), "URPM::Package", pkg)); - PUTBACK; - - call_sv(callback, G_DISCARD | G_SCALAR); - - SPAGAIN; - pkg->h = NULL; /* avoid using it anymore, in case it has been copied inside callback */ + _run_cb_while_traversing(callback, header); } ++count; } @@ -2583,20 +2588,7 @@ Db_traverse_tag(db,tag,names,callback) mi = rpmtsInitIterator(db->ts, rpmtag, name, str_len); while ((header = rpmdbNextIterator(mi))) { if (SvROK(callback)) { - dSP; - URPM__Package pkg = calloc(1, sizeof(struct s_Package)); - - pkg->flag = FLAG_ID_INVALID | FLAG_NO_HEADER_FREE; - pkg->h = header; - - PUSHMARK(SP); - mXPUSHs(sv_setref_pv(newSVpvs(""), "URPM::Package", pkg)); - PUTBACK; - - call_sv(callback, G_DISCARD | G_SCALAR); - - SPAGAIN; - pkg->h = NULL; /* avoid using it anymore, in case it has been copied inside callback */ + _run_cb_while_traversing(callback, header); } ++count; } @@ -2626,20 +2618,7 @@ Db_traverse_tag_find(db,tag,name,callback) ovsflags = ts_nosignature(db->ts); mi = rpmtsInitIterator(db->ts, rpmtag, name, 0); while ((header = rpmdbNextIterator(mi))) { - dSP; - URPM__Package pkg = calloc(1, sizeof(struct s_Package)); - - pkg->flag = FLAG_ID_INVALID | FLAG_NO_HEADER_FREE; - pkg->h = header; - - PUSHMARK(SP); - mXPUSHs(sv_setref_pv(newSVpvs(""), "URPM::Package", pkg)); - PUTBACK; - - int count = call_sv(callback, G_SCALAR); - - SPAGAIN; - pkg->h = NULL; /* avoid using it anymore, in case it has been copied inside callback */ + int count = _run_cb_while_traversing(callback, header); if (count == 1 && POPi) { found = 1; -- cgit v1.2.1