[freetds] allocations and cursors
freddyz77 at tin.it
Thu May 25 08:29:17 EDT 2006
Well... I'm not that active recently however as someone should have
noted I started implementing cursors in ODBC. Base cursor support was
implemented by Bill Thompson for CTLib. Mainly structures and functions
didn't change so much. However there are some issues i like to discuss.
1- memory allocation
Usually I think that layer how allocate a structure should be
responsible to free it. This is not the case for cursors (try searching
tds_alloc_cursor and tds_free_cursor). Mainly libTDS allocate cursor
while we free it sometimes in CTLib/ODBC and sometimes in libTDS, IMHO
very bad design). This cause the double free problem you can find at
http://freetds.sourceforge.net/out/test2/ (look at "VG errors" column).
I think that cursor allocation/deallocation should be handled entirely
within libTDS. The reason is that libTDS know much better when to
deallocate cursor from server so it can postpone deallocation as needed.
State information for cursors are quite complicated. There is a
srv_status with store server status (TDS_CUR_ISTAT_* constants) and a
status structure witch store for every possible request (declaration,
open, fetch, close and so on) the state. This is quite complicated and
lead to strange situation. For instance what should we do if state is
deallocated and a fetch is requested? The main reason for having so much
states is that you can send multiple cursors operations in a single row
(like declare, set option, open). Perhaps libTDS client should just try
to send a request (like declaration) and libTDS should handle states by
itself updating state and so on leaving client to only read state.
Cursors have many similarity with dynamics. They get allocated in
server, used and then freed. You can even mix them opening a cursor for
a dynamic! However current implementation have small differences. For
instance allocation (see also point 1) are handled entirely by upper
layer while allocation from libTDS.
Any comment or hint on how to improve any of these issues is welcome.
More information about the FreeTDS