OCILIB (C Driver for Oracle) 3.9.2
D:/Perso/dev/ocilib/ocilib/src/memory.c
00001 /*
00002     +-----------------------------------------------------------------------------------------+
00003     |                                                                                         |
00004     |                               OCILIB - C Driver for Oracle                              |
00005     |                                                                                         |
00006     |                                (C Wrapper for Oracle OCI)                               |
00007     |                                                                                         |
00008     |                              Website : http://www.ocilib.net                            |
00009     |                                                                                         |
00010     |             Copyright (c) 2007-2011 Vincent ROGIER <vince.rogier@ocilib.net>            |
00011     |                                                                                         |
00012     +-----------------------------------------------------------------------------------------+
00013     |                                                                                         |
00014     |             This library is free software; you can redistribute it and/or               |
00015     |             modify it under the terms of the GNU Lesser General Public                  |
00016     |             License as published by the Free Software Foundation; either                |
00017     |             version 2 of the License, or (at your option) any later version.            |
00018     |                                                                                         |
00019     |             This library is distributed in the hope that it will be useful,             |
00020     |             but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00021     |             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           |
00022     |             Lesser General Public License for more details.                             |
00023     |                                                                                         |
00024     |             You should have received a copy of the GNU Lesser General Public            |
00025     |             License along with this library; if not, write to the Free                  |
00026     |             Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.          |
00027     |                                                                                         |
00028     +-----------------------------------------------------------------------------------------+
00029 */
00030 
00031 /* --------------------------------------------------------------------------------------------- *
00032  * $Id: memory.c, v 3.9.2 2011-07-13 00:00 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_MemAlloc
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 void * OCI_MemAlloc
00046 (
00047     int     ptr_type,
00048     size_t  block_size,
00049     size_t  block_count,
00050     boolean zero_fill
00051 )
00052 {
00053     void * ptr  = NULL;
00054     size_t size = (size_t) (block_size * block_count);
00055 
00056     ptr = (void *) malloc(size);
00057 
00058     if (ptr != NULL)
00059     {
00060         if (zero_fill == TRUE)
00061         {
00062             memset(ptr, 0, size);
00063         }
00064     }
00065     else
00066     {
00067         OCI_ExceptionMemory(ptr_type, size, NULL, NULL);
00068     }
00069 
00070     return ptr;
00071 }
00072 
00073 /* --------------------------------------------------------------------------------------------- *
00074  * OCI_MemRealloc
00075  * --------------------------------------------------------------------------------------------- */
00076 
00077 void * OCI_MemRealloc
00078 (
00079     void * ptr_mem,
00080     int    ptr_type,
00081     size_t block_size,
00082     size_t block_count
00083 )
00084 {
00085     void * ptr  = NULL;
00086     size_t size = (size_t) (block_size * block_count);
00087 
00088     ptr = (void *) realloc(ptr_mem, size);
00089 
00090     if (ptr == NULL)
00091     {
00092         OCI_MemFree(ptr_mem);
00093 
00094         OCI_ExceptionMemory(ptr_type, size, NULL, NULL);
00095     }
00096 
00097     return ptr;
00098 }
00099 
00100 /* --------------------------------------------------------------------------------------------- *
00101  * OCI_MemFree
00102  * --------------------------------------------------------------------------------------------- */
00103 
00104 void OCI_MemFree
00105 (
00106     void * ptr_mem
00107 )
00108 {
00109     if (ptr_mem != NULL)
00110     {
00111         free(ptr_mem);
00112     }
00113 }
00114 
00115 /* --------------------------------------------------------------------------------------------- *
00116  * OCI_HandleAlloc
00117  * --------------------------------------------------------------------------------------------- */
00118 
00119 sword OCI_HandleAlloc
00120 (
00121     CONST dvoid *parenth,
00122     dvoid      **hndlpp,
00123     CONST ub4    type,
00124     CONST size_t xtramem_sz,
00125     dvoid      **usrmempp
00126 )
00127 {
00128     sword ret = OCI_SUCCESS;
00129 
00130     ret = OCIHandleAlloc(parenth, hndlpp, type, xtramem_sz, usrmempp);
00131 
00132     if (ret == OCI_SUCCESS)
00133     {
00134         OCILib.nb_hndlp++;
00135     }
00136 
00137     return ret;
00138 }
00139 
00140 /* --------------------------------------------------------------------------------------------- *
00141  * OCI_HandleFree
00142  * --------------------------------------------------------------------------------------------- */
00143 
00144 sword OCI_HandleFree
00145 (
00146     dvoid    *hndlp,
00147     CONST ub4 type
00148 )
00149 {
00150     sword ret = OCI_SUCCESS;
00151 
00152     if (hndlp != NULL)
00153     {
00154         OCILib.nb_hndlp--;
00155 
00156         ret = OCIHandleFree(hndlp, type);
00157     }
00158 
00159     return ret;
00160 }
00161 
00162 /* --------------------------------------------------------------------------------------------- *
00163  * OCI_DescriptorAlloc
00164  * --------------------------------------------------------------------------------------------- */
00165 
00166 sword OCI_DescriptorAlloc
00167 (
00168     CONST dvoid *parenth,
00169     dvoid      **descpp,
00170     CONST ub4    type,
00171     CONST size_t xtramem_sz,
00172     dvoid      **usrmempp
00173 )
00174 {
00175     sword ret = OCI_SUCCESS;
00176 
00177     ret = OCIDescriptorAlloc(parenth, descpp, type, xtramem_sz, usrmempp);
00178 
00179     if (ret == OCI_SUCCESS)
00180     {
00181         OCILib.nb_descp++;
00182     }
00183 
00184     return ret;
00185 }
00186 
00187 /* --------------------------------------------------------------------------------------------- *
00188  * OCI_DescriptorArrayAlloc
00189  * --------------------------------------------------------------------------------------------- */
00190 
00191 sword OCI_DescriptorArrayAlloc
00192 (
00193     CONST dvoid *parenth,
00194     dvoid      **descpp,
00195     CONST ub4    type,
00196     ub4          nb_elem,
00197     CONST size_t xtramem_sz,
00198     dvoid      **usrmempp
00199 )
00200 {
00201     sword ret = OCI_SUCCESS;
00202 
00203 #if OCI_VERSION_COMPILE >= OCI_11_1
00204 
00205     if (OCILib.version_runtime >= OCI_11_1)
00206     {
00207         ret = OCIArrayDescriptorAlloc(parenth, descpp, type, nb_elem, xtramem_sz, usrmempp);
00208 
00209     }
00210     else
00211 
00212 #endif
00213 
00214     {
00215         ub4 i;
00216 
00217         for(i = 0; (i < nb_elem) && (ret == OCI_SUCCESS); i++)
00218         {
00219             ret = OCIDescriptorAlloc(parenth, &descpp[i], type, xtramem_sz, usrmempp);
00220         }
00221     }
00222 
00223     if (ret == OCI_SUCCESS)
00224     {
00225         OCILib.nb_descp += nb_elem;
00226     }
00227 
00228     return ret;
00229 }
00230 
00231 /* --------------------------------------------------------------------------------------------- *
00232  * OCI_DescriptorFree
00233  * --------------------------------------------------------------------------------------------- */
00234 
00235 sword OCI_DescriptorFree
00236 (
00237     dvoid    *descp,
00238     CONST ub4 type
00239 )
00240 {
00241     sword ret = OCI_SUCCESS;
00242 
00243     if (descp != NULL)
00244     {
00245         OCILib.nb_descp--;
00246 
00247         ret = OCIDescriptorFree(descp, type);
00248     }
00249 
00250     return ret;
00251 }
00252 
00253 /* --------------------------------------------------------------------------------------------- *
00254  * OCI_DescriptorFree
00255  * --------------------------------------------------------------------------------------------- */
00256 
00257 sword OCI_DescriptorArrayFree
00258 (
00259     dvoid   **descp,
00260     CONST ub4 type,
00261     ub4       nb_elem
00262 )
00263 {
00264     sword ret = OCI_SUCCESS;
00265 
00266     if (descp != NULL)
00267     {
00268 
00269     #if OCI_VERSION_COMPILE >= OCI_11_1
00270 
00271         if (OCILib.version_runtime >= OCI_11_1)
00272         {
00273             ret = OCIArrayDescriptorFree(descp, type);
00274 
00275         }
00276         else
00277 
00278     #endif
00279 
00280         {
00281             ub4 i;
00282 
00283             for(i = 0; (i < nb_elem) && (ret == OCI_SUCCESS); i++)
00284             {
00285                 ret = OCIDescriptorFree(descp[i], type);
00286             }
00287         }
00288 
00289         OCILib.nb_descp -= nb_elem;
00290     }
00291 
00292     return ret;
00293 }
00294 
00295 /* --------------------------------------------------------------------------------------------- *
00296  * OCI_ObjectNew
00297  * --------------------------------------------------------------------------------------------- */
00298 
00299 sword OCI_ObjectNew
00300 (
00301     OCIEnv          *env,
00302     OCIError        *err,
00303     CONST OCISvcCtx *svc,
00304     OCITypeCode      typecode,
00305     OCIType         *tdo,
00306     dvoid           *table,
00307     OCIDuration      duration,
00308     boolean          value,
00309     dvoid          **instance
00310 )
00311 {
00312     sword ret = OCI_SUCCESS;
00313 
00314     ret = OCIObjectNew(env, err, svc, typecode, tdo, table, duration, value, instance);
00315 
00316     if (ret == OCI_SUCCESS)
00317     {
00318         OCILib.nb_objinst++;
00319     }
00320 
00321     return ret;
00322 }
00323 
00324 /* --------------------------------------------------------------------------------------------- *
00325  * OCI_OCIObjectFree
00326  * --------------------------------------------------------------------------------------------- */
00327 
00328 sword OCI_OCIObjectFree
00329 (
00330     OCIEnv   *env,
00331     OCIError *err,
00332     dvoid    *instance,
00333     ub2       flags
00334 )
00335 {
00336     sword ret = OCI_SUCCESS;
00337 
00338     if (instance != NULL)
00339     {
00340         OCILib.nb_objinst--;
00341 
00342         ret = OCIObjectFree(env, err, instance, flags);
00343     }
00344 
00345     return ret;
00346 }
00347