[asterisk-dev] too many string wrappers...

Luigi Rizzo rizzo at icir.org
Fri Dec 15 08:30:26 MST 2006


at the moment in asterisk we have 4 (four) different types of
wrappers to build strings (below). I would like to cleanup these
a bit, and possibly put them all in the same header files.

In detail:

1. ast_safe_string_alloc(...)

	this is basically vasprintf() with a different interface;
	considering it is very little used, i believe should go away
	and replaced by direct use of vasprintf()

2. ast_string_field_build(...)

	again a vasprintf() wrapper for string fields.
	This must be kept, together with its varargs format.

3. ast_build_string(char **buffer, size_t *space, const char *fmt, ...)

	widely used in the above form, never used in the vararg form.
	This is a reasonably good interface, except for the inconvenience
	of having to specify two arguments, and keeping track of the
	base address elsewhere.
	My suggestion is to change it slightly, using as modified
	struct ast_dynamic_str * as first argument; something like

		ast_str_add(struct ast_str *s, const char *fmt, ...)

4. ast_dynamic_str_thread_build_va(struct ast_dynamic_str **buf, ...)

	this is the backend for building thread-local strings.
	Here i would suggest the following:
	- move the declarations to include/asterisk/strings.h
	- change struct ast_dymamic_str {} to the following:

	struct ast_str {
		size_t len;	/*!< buffer size */
		size_t used;	/*!< used space */
		int flags;	/*!< see below */
		char str[0];	/*!< the actual buffer */
	}
	plus wrappers to declare, malloc(),  alloca()-te such structures.
	The flag field s to identify whether
	this is a static, automatic, malloc'ed, alloca()'ted
	buffer. Whether this is an extra field or we can use the
	high bits is an implementation detail - consider, we
	are unlikely to use 4GB strings so we should restrict to
	uint32_t or smaller len/used fields.

comments welcome as usual.

	cheers
	luigi


More information about the asterisk-dev mailing list