Skip to content

Commit

Permalink
Move alloc() / free() of struct percent_esc * up a level in the
Browse files Browse the repository at this point in the history
function call tree -- reduce the number of alloc()/free() cycles.
  • Loading branch information
infracaninophile committed May 27, 2013
1 parent bd94d38 commit a48421c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 68 deletions.
113 changes: 63 additions & 50 deletions libpkg/pkg_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,41 @@ format_unknown(struct sbuf *sbuf, __unused const void *data,

/* -------------------------------------------------------------- */

struct percent_esc *
new_percent_esc(void)
{
struct percent_esc *p;

p = calloc(1, sizeof(struct percent_esc));
if (p != NULL) {
p->item_fmt = sbuf_new_auto();
p->sep_fmt = sbuf_new_auto();
}
if (p == NULL || p->item_fmt == NULL || p->sep_fmt == NULL) {
/* out of memory */
free_percent_esc(p);
return NULL;
}
return (p);
}

struct percent_esc *
clear_percent_esc(struct percent_esc *p)
{
p->flags = 0;
p->width = 0;
p->trailer_status = 0;
sbuf_clear(p->item_fmt);
sbuf_finish(p->item_fmt);

sbuf_clear(p->sep_fmt);
sbuf_finish(p->sep_fmt);

p->fmt_code = '\0';

return (p);
}

void
free_percent_esc(struct percent_esc *p)
{
Expand All @@ -1246,36 +1281,6 @@ free_percent_esc(struct percent_esc *p)
return;
}

struct percent_esc *
new_percent_esc(struct percent_esc *p)
{
/* reset or alloc new */
if (p == NULL) {
p = calloc(1, sizeof(struct percent_esc));
if (p != NULL) {
p->item_fmt = sbuf_new_auto();
p->sep_fmt = sbuf_new_auto();
}
if (p == NULL || p->item_fmt == NULL || p->sep_fmt == NULL) {
/* out of memory */
free_percent_esc(p);
return NULL;
}
} else {
p->flags = 0;
p->width = 0;
p->trailer_status = 0;
sbuf_clear(p->item_fmt);
sbuf_finish(p->item_fmt);

sbuf_clear(p->sep_fmt);
sbuf_finish(p->sep_fmt);

p->fmt_code = '\0';
}
return (p);
}

char *
gen_format(char *buf, size_t buflen, unsigned flags, const char *tail)
{
Expand Down Expand Up @@ -1598,16 +1603,23 @@ struct sbuf *
iterate_item(struct sbuf *sbuf, const struct pkg *pkg, const char *format,
const void *data, int count, unsigned context)
{
const char *f;
const char *f;
struct percent_esc *p;

/* Scan the format string and interpret any escapes */

f = format;
p = new_percent_esc();

if (p == NULL) {
sbuf_clear(sbuf);
return (sbuf); /* Out of memory */
}

while ( *f != '\0' ) {
switch(*f) {
case '%':
f = process_format_trailer(sbuf, f, pkg, data, count, context);
f = process_format_trailer(sbuf, p, f, pkg, data, count, context);
break;
case '\\':
f = process_escape(sbuf, f);
Expand All @@ -1622,6 +1634,8 @@ iterate_item(struct sbuf *sbuf, const struct pkg *pkg, const char *format,
break; /* Out of memory */
}
}

free_percent_esc(p);
return (sbuf);
}

Expand Down Expand Up @@ -2089,17 +2103,12 @@ process_escape(struct sbuf *sbuf, const char *f)
}

const char *
process_format_trailer(struct sbuf *sbuf, const char *f, const struct pkg *pkg,
process_format_trailer(struct sbuf *sbuf, struct percent_esc *p,
const char *f, const struct pkg *pkg,
const void *data, int count, unsigned context)
{
const char *fstart;
struct sbuf *s;
struct percent_esc *p;

p = new_percent_esc(NULL);

if (p == NULL)
return (NULL); /* Out of memory */

fstart = f;
f = parse_format(f, context, p);
Expand All @@ -2117,24 +2126,19 @@ process_format_trailer(struct sbuf *sbuf, const char *f, const struct pkg *pkg,
if (s == NULL)
f = fstart; /* Pass through unprocessed on error */

free_percent_esc(p);
clear_percent_esc(p);

return (f);
}

const char *
process_format_main(struct sbuf *sbuf, const char *f, va_list ap)
process_format_main(struct sbuf *sbuf, struct percent_esc *p, const char *f,
va_list ap)
{
const char *fstart;
struct sbuf *s;
struct percent_esc *p;
void *data;

p = new_percent_esc(NULL);

if (p == NULL)
return (NULL); /* Out of memory */

fstart = f;
f = parse_format(f, PP_PKG, p);

Expand All @@ -2148,7 +2152,7 @@ process_format_main(struct sbuf *sbuf, const char *f, va_list ap)
if (s == NULL)
f = fstart; /* Pass through unprocessed on error */

free_percent_esc(p);
clear_percent_esc(p);

return (f);
}
Expand Down Expand Up @@ -2341,17 +2345,24 @@ struct sbuf *
pkg_sbuf_vprintf(struct sbuf * restrict sbuf, const char * restrict format,
va_list ap)
{
const char *f;
const char *f;
struct percent_esc *p;

assert(sbuf != NULL);
assert(format != NULL);

f = format;
p = new_percent_esc();

if (p == NULL) {
sbuf_clear(sbuf);
return (sbuf); /* Out of memory */
}

while ( *f != '\0' ) {
switch(*f) {
case '%':
f = process_format_main(sbuf, f, ap);
f = process_format_main(sbuf, p, f, ap);
break;
case '\\':
f = process_escape(sbuf, f);
Expand All @@ -2366,6 +2377,8 @@ pkg_sbuf_vprintf(struct sbuf * restrict sbuf, const char * restrict format,
break; /* Error: out of memory */
}
}

free_percent_esc(p);
return (sbuf);
}
/*
Expand Down
11 changes: 7 additions & 4 deletions libpkg/private/pkg_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ _static struct sbuf *format_unknown(struct sbuf *, __unused const void *, __unus

/* Other static function prototypes */

_static struct percent_esc *new_percent_esc(void);
_static struct percent_esc *clear_percent_esc(struct percent_esc *);
_static void free_percent_esc(struct percent_esc *);
_static struct percent_esc *new_percent_esc(struct percent_esc *);

_static char *gen_format(char *, size_t, unsigned, const char *);

Expand Down Expand Up @@ -239,9 +240,11 @@ _static const char *maybe_read_hex_byte(struct sbuf *, const char *);
_static const char *read_oct_byte(struct sbuf *, const char *);
_static const char *process_escape(struct sbuf *, const char *);

_static const char *process_format_trailer(struct sbuf *, const char *, const struct pkg *,
const void *, int, unsigned);
_static const char *process_format_main(struct sbuf *, const char *, va_list);
_static const char *process_format_trailer(struct sbuf *, struct percent_esc *,
const char *, const struct pkg *,
const void *, int, unsigned);
_static const char *process_format_main(struct sbuf *, struct percent_esc *,
const char *, va_list);

#endif

Expand Down
28 changes: 14 additions & 14 deletions tests/lib/pkg_printf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ ATF_TC_BODY(human_number, tc)
};

sbuf = sbuf_new_auto();
p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(sbuf != NULL, true);
ATF_REQUIRE_EQ(p != NULL, true);
Expand Down Expand Up @@ -389,7 +389,7 @@ ATF_TC_BODY(string_val, tc)
};

sbuf = sbuf_new_auto();
p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(sbuf != NULL, true);
ATF_REQUIRE_EQ(p != NULL, true);
Expand Down Expand Up @@ -629,7 +629,7 @@ ATF_TC_BODY(int_val, tc)
};

sbuf = sbuf_new_auto();
p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(sbuf != NULL, true);
ATF_REQUIRE_EQ(p != NULL, true);
Expand Down Expand Up @@ -682,7 +682,7 @@ ATF_TC_BODY(bool_val, tc)
};

sbuf = sbuf_new_auto();
p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(sbuf != NULL, true);
ATF_REQUIRE_EQ(p != NULL, true);
Expand Down Expand Up @@ -797,7 +797,7 @@ ATF_TC_BODY(mode_val, tc)
};

sbuf = sbuf_new_auto();
p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(sbuf != NULL, true);
ATF_REQUIRE_EQ(p != NULL, true);
Expand Down Expand Up @@ -853,7 +853,7 @@ ATF_TC_BODY(liclog_val, tc)
};

sbuf = sbuf_new_auto();
p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(sbuf != NULL, true);
ATF_REQUIRE_EQ(p != NULL, true);
Expand Down Expand Up @@ -901,7 +901,7 @@ ATF_TC_BODY(list_count, tc)
};

sbuf = sbuf_new_auto();
p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(sbuf != NULL, true);
ATF_REQUIRE_EQ(p != NULL, true);
Expand Down Expand Up @@ -1240,7 +1240,7 @@ ATF_TC_BODY(field_modifier, tc)
{ NULL, 0, 0, '\0', },
};

p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(p != NULL, true);

Expand Down Expand Up @@ -1310,7 +1310,7 @@ ATF_TC_BODY(field_width, tc)
{ NULL, 0, 0, '\0', },
};

p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(p != NULL, true);

Expand Down Expand Up @@ -2038,7 +2038,7 @@ ATF_TC_BODY(format_code, tc)
{ NULL, 0, 0, 0, '\0', },
};

p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(p != NULL, true);

Expand Down Expand Up @@ -2087,7 +2087,7 @@ ATF_TC_BODY(format_trailer, tc)
{ NULL, NULL, NULL, 0, '\0', },
};

p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(p != NULL, true);

Expand Down Expand Up @@ -2156,13 +2156,11 @@ ATF_TC_BODY(parse_format, tc)
{ NULL, 0, 0, 0, 0, NULL, NULL, 0, '\0', },
};

p = new_percent_esc(NULL);
p = new_percent_esc();

ATF_REQUIRE_EQ(p != NULL, true);

for (i = 0; pf_test_vals[i].in != NULL; i++) {
p = new_percent_esc(p);

f = parse_format(pf_test_vals[i].in, pf_test_vals[i].context,
p);

Expand All @@ -2184,6 +2182,8 @@ ATF_TC_BODY(parse_format, tc)
"(test %d)", i);
ATF_CHECK_EQ_MSG(*f, pf_test_vals[i].fend_val,
"(test %d)", i);

p = clear_percent_esc(p);
}

free_percent_esc(p);
Expand Down

0 comments on commit a48421c

Please sign in to comment.