[freetds] new dstr changes

ZIGLIO, Frediano, VF-IT Frediano.Ziglio at vodafone.com
Wed Aug 30 09:55:06 EDT 2006


> 
> On Tue, Aug 29, 2006 at 05:26:36PM +0200, ZIGLIO, Frediano, 
> VF-IT wrote:
> > I also noted that in code there is some code to use a 
> static allocated
> > buffer if possible (bcp if I reminds correctly), perhaps 
> something like
> 
> I think we sometimes use  DSTR structures where static allocations or 
> ordinary char* would suffice.  I can't think of any reason to use a 
> "statically allocated" DSTR.  
> 
> I see the advantage of distinguishing between byte-count and 
> char-count.  
> 

It should be encoding dependent so the "length" is a byte-count not a
human-char-count.

I think we agree to add a length field. I think there are 3 way to add a
length:

1-
struct DSTR_STRUCT {
	char *dstr_s;
	size_t dstr_len;
};
typedef struct DSTR_STRUCT DSTR;


2-
struct DSTR_STRUCT {
	size_t dstr_len;
	/* keep last */
	char dstr_s[1];
};
typedef struct DSTR_STRUCT *DSTR;


3-
struct DSTR_STRUCT {
	char *dstr_s;
	size_t dstr_len;
};
typedef struct DSTR_STRUCT *DSTR;


1 initialization is a bit more complicated but there are less
indirections
2 more complicate to init with a char * (ie asprintf) and reallocate
3 initialization easy but more indirections

IMHO I would exclude 3.


About static perhaps the better definition is preallocated that is
allocated by an external entity (consider parameters from client). It
would useful for instance in query.c where we don't know if we should
convert string or not. Consider something like

int tds_submit_query(TDSSOCKET *tds, const char *query)
{
	DSTR q;

	/* or tds_dstr_init_preallocated(&q, query, strlen(query)); */
	tds_dstr_init(&q);
	tds_dstr_set_preallocated(query, strlen(query));
	
	if (!tds_convert_string(tds, &q))
		return TDS_FAIL;

	...
	tds_dstr_free(&q);
}

Note that actually tds_convert_string has 5 arguments and there is no
need for a tds_convert_string_free (with two parameters) and a
converted_query_len.

freddy77




More information about the FreeTDS mailing list