[ruby-oci8-commit] [559] trunk/ruby-oci8: refactor oci8_lob_t.

nobody at rubyforge.org nobody at rubyforge.org
Sun Mar 3 05:45:37 UTC 2013


Revision: 559
Author:   kubo
Date:     2013-03-03 05:45:36 +0000 (Sun, 03 Mar 2013)
Log Message:
-----------
refactor oci8_lob_t.

Modified Paths:
--------------
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/ext/oci8/lob.c
    trunk/ruby-oci8/ext/oci8/oci8.c
    trunk/ruby-oci8/ext/oci8/oci8.h

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2013-03-03 05:34:24 UTC (rev 558)
+++ trunk/ruby-oci8/ChangeLog	2013-03-03 05:45:36 UTC (rev 559)
@@ -1,4 +1,8 @@
 2013-03-03  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/lob.c, ext/oci8/oci8.c, ext/oci8/oci8.h:
+	    refactor oci8_lob_t.
+
+2013-03-03  KUBO Takehiro  <kubo at jiubao.org>
 	* ext/oci8/oci8.c: fix segmentation fault when the process
 	    exits on rubinius.
 

Modified: trunk/ruby-oci8/ext/oci8/lob.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/lob.c	2013-03-03 05:34:24 UTC (rev 558)
+++ trunk/ruby-oci8/ext/oci8/lob.c	2013-03-03 05:45:36 UTC (rev 559)
@@ -1,4 +1,9 @@
 /* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
+/*
+ * lob.c - part of ruby-oci8
+ *
+ * Copyright (C) 2002-2013 Kubo Takehiro <kubo at jiubao.org>
+ */
 #include "oci8.h"
 
 static ID id_plus;
@@ -22,8 +27,7 @@
 };
 typedef struct {
     oci8_base_t base;
-    VALUE svc;
-    OCISvcCtx *svchp;
+    oci8_svcctx_t *svcctx;
     ub4 pos;
     int char_width;
     ub1 csfrm;
@@ -31,6 +35,15 @@
     enum state state;
 } oci8_lob_t;
 
+static oci8_svcctx_t *check_svcctx(oci8_lob_t *lob)
+{
+    oci8_svcctx_t *svcctx = lob->svcctx;
+    if (svcctx == NULL || svcctx->base.type != OCI_HTYPE_SVCCTX) {
+        rb_raise(rb_eRuntimeError, "Invalid Svcctx");
+    }
+    return svcctx;
+}
+
 static VALUE oci8_lob_write(VALUE self, VALUE data);
 
 static VALUE oci8_make_lob(VALUE klass, oci8_svcctx_t *svcctx, OCILobLocator *s)
@@ -101,20 +114,22 @@
 static void oci8_lob_mark(oci8_base_t *base)
 {
     oci8_lob_t *lob = (oci8_lob_t *)base;
-    rb_gc_mark(lob->svc);
+    if (lob->svcctx != NULL) {
+        rb_gc_mark(lob->svcctx->base.self);
+    }
 }
 
 static void oci8_lob_free(oci8_base_t *base)
 {
     oci8_lob_t *lob = (oci8_lob_t *)base;
     boolean is_temporary;
+    oci8_svcctx_t *svcctx = lob->svcctx;
 
-    if (lob->svchp != NULL
+    if (svcctx != NULL
         && OCILobIsTemporary(oci8_envhp, oci8_errhp, lob->base.hp.lob, &is_temporary) == OCI_SUCCESS
         && is_temporary) {
 
 #ifdef HAVE_RB_THREAD_BLOCKING_REGION
-        oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
         oci8_temp_lob_t *temp_lob = ALLOC(oci8_temp_lob_t);
 
         temp_lob->next = svcctx->temp_lobs;
@@ -124,11 +139,10 @@
         lob->base.hp.ptr = NULL;
 #else
         /* FIXME: This may stall the GC. */
-        OCILobFreeTemporary(lob->svchp, oci8_errhp, lob->base.hp.lob);
+        OCILobFreeTemporary(svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob);
 #endif
     }
-    lob->svc = Qnil;
-    lob->svchp = NULL;
+    lob->svcctx = NULL;
 }
 
 static oci8_base_vtable_t oci8_lob_vtable = {
@@ -139,7 +153,7 @@
 
 static ub4 oci8_lob_get_length(oci8_lob_t *lob)
 {
-    oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+    oci8_svcctx_t *svcctx = check_svcctx(lob);
     ub4 len;
 
     chker2(OCILobGetLength_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &len),
@@ -150,7 +164,7 @@
 static void lob_open(oci8_lob_t *lob)
 {
     if (lob->state == S_CLOSE) {
-        oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+        oci8_svcctx_t *svcctx = check_svcctx(lob);
 
         chker2(OCILobOpen_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, OCI_DEFAULT),
                &svcctx->base);
@@ -161,7 +175,7 @@
 static void lob_close(oci8_lob_t *lob)
 {
     if (lob->state == S_OPEN) {
-        oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+        oci8_svcctx_t *svcctx = check_svcctx(lob);
 
         chker2(OCILobClose_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob),
                &svcctx->base);
@@ -172,7 +186,7 @@
 static void bfile_close(oci8_lob_t *lob)
 {
     if (lob->state == S_BFILE_OPEN) {
-        oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+        oci8_svcctx_t *svcctx = check_svcctx(lob);
 
         chker2(OCILobFileClose_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob),
                &svcctx->base);
@@ -228,28 +242,26 @@
     oci8_lob_t *lob = DATA_PTR(self);
     VALUE svc;
     VALUE val;
+    oci8_svcctx_t *svcctx;
     sword rv;
 
     rb_scan_args(argc, argv, "11", &svc, &val);
-    TO_SVCCTX(svc); /* check argument type */
+    svcctx = oci8_get_svcctx(svc);
     rv = OCIDescriptorAlloc(oci8_envhp, &lob->base.hp.ptr, OCI_DTYPE_LOB, 0, NULL);
     if (rv != OCI_SUCCESS)
         oci8_env_raise(oci8_envhp, rv);
     lob->base.type = OCI_DTYPE_LOB;
-    lob->svc = svc;
-    lob->svchp = NULL;
     lob->pos = 0;
     lob->char_width = 1;
     lob->csfrm = csfrm;
     lob->lobtype = lobtype;
     lob->state = S_NO_OPEN_CLOSE;
     oci8_link_to_parent((oci8_base_t*)lob, (oci8_base_t*)DATA_PTR(svc));
+    lob->svcctx = svcctx;
     if (!NIL_P(val)) {
-        oci8_svcctx_t *svcctx = oci8_get_svcctx(svc);
         OCI8StringValue(val);
         chker2(OCILobCreateTemporary_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, 0, csfrm, lobtype, TRUE, OCI_DURATION_SESSION),
                &svcctx->base);
-        lob->svchp = oci8_get_oci_svcctx(svc);
         oci8_lob_write(self, val);
         lob->pos = 0; /* reset the position */
     }
@@ -461,7 +473,7 @@
 static VALUE oci8_lob_truncate(VALUE self, VALUE len)
 {
     oci8_lob_t *lob = DATA_PTR(self);
-    oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+    oci8_svcctx_t *svcctx = check_svcctx(lob);
 
     lob_open(lob);
     chker2(OCILobTrim_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, NUM2UINT(len)),
@@ -502,7 +514,7 @@
 static VALUE oci8_lob_read(int argc, VALUE *argv, VALUE self)
 {
     oci8_lob_t *lob = DATA_PTR(self);
-    oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+    oci8_svcctx_t *svcctx = check_svcctx(lob);
     ub4 length;
     ub4 nchar;
     ub4 amt;
@@ -626,7 +638,7 @@
 static VALUE oci8_lob_write(VALUE self, VALUE data)
 {
     oci8_lob_t *lob = DATA_PTR(self);
-    oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+    oci8_svcctx_t *svcctx = check_svcctx(lob);
     ub4 amt;
 
     lob_open(lob);
@@ -695,7 +707,7 @@
 static VALUE oci8_lob_get_chunk_size(VALUE self)
 {
     oci8_lob_t *lob = DATA_PTR(self);
-    oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+    oci8_svcctx_t *svcctx = check_svcctx(lob);
     ub4 len;
 
     chker2(OCILobGetChunkSize_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &len),
@@ -710,11 +722,11 @@
     VALUE newobj;
     boolean is_temporary;
 
-    newobj = rb_funcall(CLASS_OF(self), oci8_id_new, 1, lob->svc);
+    newobj = rb_funcall(CLASS_OF(self), oci8_id_new, 1, lob->svcctx ? lob->svcctx->base.self : Qnil);
     newlob = DATA_PTR(newobj);
     if (OCILobIsTemporary(oci8_envhp, oci8_errhp, lob->base.hp.lob, &is_temporary) == OCI_SUCCESS
         && is_temporary) {
-        oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+        oci8_svcctx_t *svcctx = check_svcctx(lob);
         chker2(OCILobLocatorAssign_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &newlob->base.hp.lob),
                &svcctx->base);
     } else {
@@ -796,16 +808,16 @@
     VALUE svc;
     VALUE dir_alias;
     VALUE filename;
+    oci8_svcctx_t *svcctx;
     int rv;
 
     rb_scan_args(argc, argv, "12", &svc, &dir_alias, &filename);
-    TO_SVCCTX(svc); /* check argument type */
+    svcctx = oci8_get_svcctx(svc);
     rv = OCIDescriptorAlloc(oci8_envhp, &lob->base.hp.ptr, OCI_DTYPE_LOB, 0, NULL);
     if (rv != OCI_SUCCESS) {
         oci8_env_raise(oci8_envhp, rv);
     }
     lob->base.type = OCI_DTYPE_LOB;
-    lob->svc = svc;
     lob->pos = 0;
     lob->char_width = 1;
     lob->csfrm = SQLCS_IMPLICIT;
@@ -817,6 +829,7 @@
         oci8_bfile_set_name(self, dir_alias, filename);
     }
     oci8_link_to_parent((oci8_base_t*)lob, (oci8_base_t*)DATA_PTR(svc));
+    lob->svcctx = svcctx;
     return Qnil;
 }
 
@@ -892,7 +905,7 @@
 static VALUE oci8_bfile_exists_p(VALUE self)
 {
     oci8_lob_t *lob = DATA_PTR(self);
-    oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
+    oci8_svcctx_t *svcctx = check_svcctx(lob);
     boolean flag;
 
     chker2(OCILobFileExists_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &flag),

Modified: trunk/ruby-oci8/ext/oci8/oci8.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/oci8.c	2013-03-03 05:34:24 UTC (rev 558)
+++ trunk/ruby-oci8/ext/oci8/oci8.c	2013-03-03 05:45:36 UTC (rev 559)
@@ -1185,12 +1185,6 @@
     return (oci8_svcctx_t *)oci8_get_handle(obj, cOCI8);
 }
 
-OCISvcCtx *oci8_get_oci_svcctx(VALUE obj)
-{
-    oci8_svcctx_t *svcctx = oci8_get_svcctx(obj);
-    return svcctx->base.hp.svc;
-}
-
 OCISession *oci8_get_oci_session(VALUE obj)
 {
     oci8_svcctx_t *svcctx = oci8_get_svcctx(obj);

Modified: trunk/ruby-oci8/ext/oci8/oci8.h
===================================================================
--- trunk/ruby-oci8/ext/oci8/oci8.h	2013-03-03 05:34:24 UTC (rev 558)
+++ trunk/ruby-oci8/ext/oci8/oci8.h	2013-03-03 05:45:36 UTC (rev 559)
@@ -2,7 +2,7 @@
 /*
  * oci8.h - part of ruby-oci8
  *
- * Copyright (C) 2002-2012 KUBO Takehiro <kubo at jiubao.org>
+ * Copyright (C) 2002-2013 Kubo Takehiro <kubo at jiubao.org>
  */
 #ifndef _RUBY_OCI_H_
 #define _RUBY_OCI_H_ 1
@@ -494,10 +494,8 @@
 void Init_oci8(VALUE *out);
 void oci8_do_parse_connect_string(VALUE conn_str, VALUE *user, VALUE *pass, VALUE *dbname, VALUE *mode);
 oci8_svcctx_t *oci8_get_svcctx(VALUE obj);
-OCISvcCtx *oci8_get_oci_svcctx(VALUE obj);
 OCISession *oci8_get_oci_session(VALUE obj);
 void oci8_check_pid_consistency(oci8_svcctx_t *svcctx);
-#define TO_SVCCTX oci8_get_oci_svcctx
 #define TO_SESSION oci8_get_oci_session
 
 /* connection_pool.c */



More information about the ruby-oci8-commit mailing list