Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id E9B572E003D for ; Thu, 15 May 2008 14:50:39 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 43091-05 for ; Thu, 15 May 2008 14:50:34 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.esilo.com (ns1.esilo.com [63.150.152.211]) by developer.postgresql.org (Postfix) with ESMTP id EDF332E002D for ; Thu, 15 May 2008 14:50:35 -0300 (ADT) Received: from [10.0.0.84] (fwext-esilo-jupiter.esilo.com [64.135.45.146]) (authenticated bits=0) by mail.esilo.com (8.13.1/8.13.1) with ESMTP id m4FHoUha026729; Thu, 15 May 2008 13:50:30 -0400 Message-ID: <482C77E5.1060500@esilo.com> Date: Thu, 15 May 2008 13:50:29 -0400 From: Andrew Chernow User-Agent: Thunderbird 2.0.0.14 (Windows/20080421) MIME-Version: 1.0 To: Tom Lane CC: Merlin Moncure , Andrew Dunstan , Alvaro Herrera , "Patches (PostgreSQL)" Subject: Re: libpq object hooks References: <482A58D8.2030205@dunslane.net> <20080514043313.GS6966@alvh.no-ip.org> <482AD896.4000504@dunslane.net> <19342.1210776271@sss.pgh.pa.us> <6044.1210867771@sss.pgh.pa.us> In-Reply-To: <6044.1210867771@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200805/280 X-Sequence-Number: 6306 Tom Lane wrote: > "Merlin Moncure" writes: >> The problem is the functions PQhookData(conn, hookname) and >> PQresultHookData(result, hookName). We need these to work in >> functions that are not callbacks. If we eliminate hookname >> completely, there is no way for libpq to know which private state we >> are asking for. > > Well, depending on a hook name for this is broken-by-design anyway, > because there is no way for two independently written libraries to > be sure they don't choose conflicting hook names. So the need for > a hook name has to go away. > > It might work to use the address of the hook callback function as > a key for retrieving the associated void * pointer. You'd need to > not register the same callback function more than once per object, > but from what I gather here you don't need to. > > regards, tom lane > > There can be cases to use the same callbacks, although unlikely. To completely avoid collisions, the below would work: Use the address of a static, maybe an 'int', as a hook hanlde. Provide the user with a macro that can make a hook handle. typedef void *PGhookHandle; // Declare an int and point "tokname" at it. The value doesn't // matter, its the pointer address we are interested in. #define PQ_MAKE_HOOK_HANDLE(tokname) \ static int hh__ ## tokname = 0; \ static const PGhookHandle tokname = &hh__ ## tokname As an example, here is what libpqtypes would do: // libpqtypes hooks.c PQ_MAKE_HOOK_HANDLE(pqthookhandle); Now the handle replaces the hookName. The "const char *hookName" member of the PQobjectHooks structure is changed to "const PGhookHanlde hookHandle". This allows for all the flexibility of a const char * w/o the collision issues. // these function prototypes change as well void *PQhookData(PGconn *, const PGhookHandle); void *PQresultHookData(PGresult *, const PGhookHandle); We will send in an updated patch. -- Andrew Chernow eSilo, LLC every bit counts http://www.esilo.com/