]> granicus.if.org Git - pgbouncer/commitdiff
remove extra argument for SBuf callback
authorMarko Kreen <markokr@gmail.com>
Tue, 8 Jul 2008 20:01:56 +0000 (20:01 +0000)
committerMarko Kreen <markokr@gmail.com>
Tue, 8 Jul 2008 20:01:56 +0000 (20:01 +0000)
conserves memory and conforms more with general coding style.

plus few cleanups

include/client.h
include/sbuf.h
include/server.h
src/client.c
src/objects.c
src/sbuf.c
src/server.c
src/takeover.c

index 5477a7fed4a2c5a3f71c813ec009583e7c897201..91b4bdfee78f111cc9063963501f1471e782ced2 100644 (file)
@@ -16,7 +16,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt, void *arg)  _MUSTCHECK;
+bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt)  _MUSTCHECK;
 bool set_pool(PgSocket *client, const char *dbname, const char *username) _MUSTCHECK;
 
 
index e3482d1baafba8d531229e2b82a0aa64139d491d..02565e03ce4273377b90bf3d6ecc2a79fc431a7b 100644 (file)
@@ -47,8 +47,7 @@ typedef struct SBuf SBuf;
    next event loop (eg. too few data available). */
 typedef bool (*sbuf_cb_t)(SBuf *sbuf,
                        SBufEvent evtype,
-                       MBuf *mbuf,
-                       void *arg);
+                       MBuf *mbuf);
 
 /* for some reason, libevent has no typedef for callback */
 typedef void (*sbuf_libevent_cb)(int, short, void *);
@@ -68,19 +67,18 @@ struct SBuf {
 
        int sock;               /* fd for this socket */
 
-       unsigned pkt_remain;            /* total packet length remaining */
+       unsigned pkt_remain;    /* total packet length remaining */
 
        sbuf_cb_t proto_cb;     /* protocol callback */
-       void *proto_cb_arg;     /* extra arg to callback */
 
        SBuf *dst;              /* target SBuf for current packet */
 
-       IOBuf *io;
+       IOBuf *io;              /* data buffer, lazily allocated */
 };
 
 #define sbuf_socket(sbuf) ((sbuf)->sock)
 
-void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn, void *arg);
+void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn);
 bool sbuf_accept(SBuf *sbuf, int read_sock, bool is_unix)  _MUSTCHECK;
 bool sbuf_connect(SBuf *sbuf, const PgAddr *addr, const char *unix_dir, int timeout_sec)  _MUSTCHECK;
 
index 8df244bc03542efc92afc89a2ad3dd06ac00d102..614e8411711924316d06ebc36f57193107d8f513 100644 (file)
@@ -16,5 +16,5 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt, void *arg)  _MUSTCHECK;
+bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt)  _MUSTCHECK;
 
index 2845852dabdfba81248f173e99c897d5c1d00624..4b8af738e3301e9a02c7cec8f1560e9525251ddd 100644 (file)
@@ -336,10 +336,10 @@ static bool handle_client_work(PgSocket *client, PktHdr *pkt)
 }
 
 /* callback from SBuf */
-bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data, void *arg)
+bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data)
 {
        bool res = false;
-       PgSocket *client = arg;
+       PgSocket *client = container_of(sbuf, PgSocket, sbuf);
        PktHdr pkt;
 
 
index f439571dc8330c7b73cb7ad55e077bcd0fc9ccd6..1b8c20152c768614d4ca1a830235ae257a96a83e 100644 (file)
@@ -69,7 +69,7 @@ static void construct_client(void *obj)
 
        memset(client, 0, sizeof(PgSocket));
        list_init(&client->head);
-       sbuf_init(&client->sbuf, client_proto, client);
+       sbuf_init(&client->sbuf, client_proto);
        client->state = CL_FREE;
 }
 
@@ -79,7 +79,7 @@ static void construct_server(void *obj)
 
        memset(server, 0, sizeof(PgSocket));
        list_init(&server->head);
-       sbuf_init(&server->sbuf, server_proto, server);
+       sbuf_init(&server->sbuf, server_proto);
        server->state = SV_FREE;
 }
 
index c76ecb845cfbfee8d631941c98ee4c1a0fab525c..02146ade6b98cd15ad664bb4d460e465ae9623c1 100644 (file)
@@ -66,10 +66,9 @@ static inline IOBuf *get_iobuf(SBuf *sbuf) { return sbuf->io; }
  *********************************/
 
 /* initialize SBuf with proto handler */
-void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn, void *arg)
+void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn)
 {
        memset(sbuf, 0, sizeof(SBuf));
-       sbuf->proto_cb_arg = arg;
        sbuf->proto_cb = proto_fn;
 }
 
@@ -227,7 +226,7 @@ bool sbuf_continue_with_callback(SBuf *sbuf, sbuf_libevent_cb user_cb)
        AssertActive(sbuf);
 
        event_set(&sbuf->ev, sbuf->sock, EV_READ | EV_PERSIST,
-                 user_cb, sbuf->proto_cb_arg);
+                 user_cb, sbuf);
 
        err = event_add(&sbuf->ev, NULL);
        if (err < 0) {
@@ -328,7 +327,7 @@ static bool sbuf_call_proto(SBuf *sbuf, int event)
        else
                memset(&mbuf, 0, sizeof(mbuf));
 
-       res = sbuf->proto_cb(sbuf, event, &mbuf, sbuf->proto_cb_arg);
+       res = sbuf->proto_cb(sbuf, event, &mbuf);
 
        AssertSanity(sbuf);
        Assert(event != SBUF_EV_READ || !res || sbuf->sock > 0);
index 314f2cd0c935306f16c7fa888f623c12479201c4..3efc0549138fc24d4aee74fb413e53e53927d620 100644 (file)
@@ -320,10 +320,10 @@ static bool handle_connect(PgSocket *server)
 }
 
 /* callback from SBuf */
-bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data, void *arg)
+bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data)
 {
        bool res = false;
-       PgSocket *server = arg;
+       PgSocket *server = container_of(sbuf, PgSocket, sbuf);
        PgPool *pool = server->pool;
        PktHdr pkt;
 
index fe74c649594f8576d6dbd58f380ffd67cf767449..91aad7258edc1eb741dd99058b9c0c0919daf70f 100644 (file)
@@ -265,7 +265,7 @@ static void takeover_parse_data(PgSocket *bouncer,
  */
 static void takeover_recv_cb(int sock, short flags, void *arg)
 {
-       PgSocket *bouncer = arg;
+       PgSocket *bouncer = container_of(arg, PgSocket, sbuf);
        uint8_t data_buf[2048];
        uint8_t cnt_buf[128];
        struct msghdr msg;