Skip to content
This repository
tree: 11efea7c70
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 66 lines (55 sloc) 1.609 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <libpq-fe.h>
#include <stdio.h>
#include <stdlib.h>
#include "hdbc-postgresql-helper.h"

/* Things can't finalize more than once.
We'd like to let people call them from the app.
Yet we'd also like to be able to have a ForeignPtr finalize them.

So, here's a little wrapper for things. */

void PQfinish_conditional_finalizer(finalizeonce *conn);

finalizeonce *wrapobjpg(void *obj, finalizeonce *parentobj) {
  finalizeonce *newobj;
  newobj = malloc(sizeof(finalizeonce));
  if (newobj == NULL) {
    fprintf(stderr, "HDBC: could not allocate wrapper!\n");
    return NULL;
  }
  newobj->isfinalized = 0;
  newobj->refcount = 1;
  newobj->encapobj = obj;
  newobj->parent = parentobj;
  if (parentobj != NULL)
    parentobj->refcount++;
  return newobj;
}
  
void PQfinish_app(finalizeonce *conn) {
  if (conn->isfinalized)
    return;
  PQfinish((PGconn *) (conn->encapobj));
  conn->encapobj = NULL;
  conn->isfinalized = 1;
}

void PQfinish_finalizer(finalizeonce *conn) {
  (conn->refcount)--;
  PQfinish_conditional_finalizer(conn);
}

void PQfinish_conditional_finalizer(finalizeonce *conn) {
  if (conn->refcount < 1) {
    PQfinish_app(conn);
    free(conn);
  }
}

void PQclear_app(finalizeonce *res) {
  if (res->isfinalized)
    return;
  PQclear((PGresult *) (res->encapobj));
  res->isfinalized = 1;
}

void PQclear_finalizer(finalizeonce *res) {
  PQclear_app(res);
  (res->refcount)--; /* Not really important since this is never a
parent */
  (res->parent->refcount)--;
  PQfinish_conditional_finalizer(res->parent);
  free(res);
}

Something went wrong with that request. Please try again.