Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Move alloc() / free() of struct percent_esc * up a level in the function call tree -- reduce the number of alloc()/free() cycles.
Matthew Seaman committed 12 years ago
commit a48421ca3eb9fe10fdfed52249580c72451f00c2
parent bd94d38
3 files changed +84 -68
modified libpkg/pkg_printf.c
@@ -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)
{
@@ -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)
{
@@ -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);
@@ -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);
}

@@ -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);
@@ -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);

@@ -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);
}
@@ -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);
@@ -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);
}
/*
modified libpkg/private/pkg_printf.h
@@ -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 *);

@@ -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

modified tests/lib/pkg_printf_test.c
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);

@@ -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);

@@ -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);

@@ -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);

@@ -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);

@@ -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);