• Main Page
  • Modules
  • Data Structures
  • Files
  • File List

D:/Perso/dev/ocilib/ocilib/src/list.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-2010 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: list.c, v 3.8.1 2010-12-13 00:00 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_ListCreate
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 OCI_List * OCI_ListCreate
00046 (
00047     int type
00048 )
00049 {
00050     OCI_List *list = NULL;
00051 
00052     /* allocate list */
00053 
00054     list = (OCI_List *) OCI_MemAlloc(OCI_IPC_LIST, sizeof(*list), (size_t) 1, TRUE);
00055 
00056     /* create a mutex on multithreaded environments */
00057 
00058     if (list != NULL)
00059     {
00060         list->type = type;
00061 
00062         if (OCI_LIB_THREADED)
00063         {
00064             list->mutex = OCI_MutexCreateInternal();
00065 
00066             if (list->mutex == NULL)
00067                 OCI_FREE(list);
00068         }
00069     }
00070 
00071     return list;
00072 }
00073 
00074 /* --------------------------------------------------------------------------------------------- *
00075  * OCI_ListFree
00076  * --------------------------------------------------------------------------------------------- */
00077 
00078 boolean OCI_ListFree
00079 (
00080     OCI_List *list
00081 )
00082 {
00083     boolean res = TRUE;
00084 
00085     OCI_CHECK(list == NULL, FALSE);
00086 
00087     OCI_ListClear(list);
00088 
00089     if (list->mutex != NULL)
00090         res = OCI_MutexFree(list->mutex);
00091 
00092     OCI_FREE(list);
00093 
00094     return res;
00095 }
00096 
00097 /* --------------------------------------------------------------------------------------------- *
00098  * OCI_ListCreateItem
00099  * --------------------------------------------------------------------------------------------- */
00100 
00101 OCI_Item * OCI_ListCreateItem
00102 (
00103     int type,
00104     int size
00105 )
00106 {
00107     OCI_Item *item = NULL;
00108 
00109     /* allocate list item entry */
00110 
00111     item = (OCI_Item *) OCI_MemAlloc(OCI_IPC_LIST_ITEM, sizeof(*item),
00112                                      (size_t) 1, TRUE);
00113 
00114     if (item != NULL)
00115     {
00116         /* allocate item data buffer */
00117 
00118         item->data = (void *) OCI_MemAlloc(type, (size_t) size, (size_t) 1, TRUE);
00119 
00120         if (item->data == NULL)
00121             OCI_FREE(item);
00122     }
00123 
00124     return item;
00125 }
00126 
00127 /* --------------------------------------------------------------------------------------------- *
00128  * OCI_ListAppend
00129  * --------------------------------------------------------------------------------------------- */
00130 
00131 OCI_Item * OCI_ListAppend
00132 (
00133     OCI_List *list,
00134     int       size
00135 )
00136 {
00137     OCI_Item *item = NULL;
00138     OCI_Item *temp = NULL;
00139 
00140     OCI_CHECK(list == NULL, NULL);
00141 
00142     item = OCI_ListCreateItem(list->type, size);
00143 
00144     OCI_CHECK(item == NULL, FALSE);
00145 
00146     if (list->mutex != NULL)
00147         OCI_MutexAcquire(list->mutex);
00148 
00149     temp = list->head;
00150 
00151     while (temp != NULL && temp->next)
00152     {
00153         temp = temp->next;
00154     }
00155 
00156     if (temp != NULL)
00157         temp->next = item;
00158     else
00159         list->head = item;
00160 
00161     list->count++;
00162 
00163     if (list->mutex != NULL)
00164         OCI_MutexRelease(list->mutex);
00165 
00166     return item;
00167 }
00168 
00169 /* --------------------------------------------------------------------------------------------- *
00170  * OCI_ListClear
00171  * --------------------------------------------------------------------------------------------- */
00172 
00173 boolean OCI_ListClear
00174 (
00175     OCI_List *list
00176 )
00177 {
00178     OCI_Item *item = NULL;
00179     OCI_Item *temp = NULL;
00180 
00181     OCI_CHECK(list == NULL, FALSE);
00182 
00183     if (list->mutex != NULL)
00184         OCI_MutexAcquire(list->mutex);
00185 
00186     /* walk along the list to free item's buffer */
00187 
00188     item = list->head;
00189 
00190     while (item != NULL)
00191     {
00192         temp = item;
00193         item = item->next;
00194 
00195         /* free data */
00196 
00197         OCI_FREE(temp->data);
00198         OCI_FREE(temp);
00199     }
00200 
00201     list->head  = NULL;
00202     list->count = 0;
00203 
00204     if (list->mutex != NULL)
00205         OCI_MutexRelease(list->mutex);
00206 
00207     return TRUE;
00208 }
00209 
00210 /* --------------------------------------------------------------------------------------------- *
00211  * OCI_ListForEach
00212  * --------------------------------------------------------------------------------------------- */
00213 
00214 boolean OCI_ListForEach
00215 (
00216     OCI_List          *list,
00217     POCI_LIST_FOR_EACH proc
00218 )
00219 {
00220     OCI_Item *item = NULL;
00221 
00222     OCI_CHECK(list == NULL, FALSE);
00223 
00224     if (list->mutex != NULL)
00225         OCI_MutexAcquire(list->mutex);
00226 
00227     item = list->head;
00228 
00229     /* for each item in the list, execute the given callback */
00230 
00231     while (item != NULL)
00232     {
00233         proc(item->data);
00234         item = item->next;
00235     }
00236 
00237     if (list->mutex != NULL)
00238         OCI_MutexRelease(list->mutex);
00239 
00240     return TRUE;
00241 }
00242 
00243 /* --------------------------------------------------------------------------------------------- *
00244  * OCI_ListRemove
00245  * --------------------------------------------------------------------------------------------- */
00246 
00247 boolean OCI_ListRemove
00248 (
00249     OCI_List *list,
00250     void     *data
00251 )
00252 {
00253     OCI_Item *item = NULL;
00254     OCI_Item *temp = NULL;
00255 
00256     OCI_CHECK(list == NULL, FALSE);
00257     OCI_CHECK(data == NULL, FALSE);
00258 
00259     if (list->mutex != NULL)
00260         OCI_MutexAcquire(list->mutex);
00261 
00262     item = list->head;
00263 
00264     while (item != NULL)
00265     {
00266         if (item->data == data)
00267         {
00268             if (temp) temp->next = item->next;
00269 
00270             /* if item was the first entry, readjust the first list
00271                entry to next element */
00272 
00273             if (item == list->head) list->head = item->next;
00274 
00275             OCI_FREE(item);
00276 
00277             break;
00278         }
00279 
00280         temp = item;
00281         item = item->next;
00282     }
00283 
00284     list->count--;
00285 
00286     if (list->mutex != NULL)
00287         OCI_MutexRelease(list->mutex);
00288 
00289     return TRUE;
00290 }

Generated on Mon Dec 13 2010 22:32:01 for OCILIB (C Driver for Oracle) by  doxygen 1.7.2