/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/mc_list/list.h

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 207 $ : Last Committed Revision
00003  * $Date: 2008-07-11 17:55:19 -0700 (Fri, 11 Jul 2008) $ : Last Committed Date */
00004 /*
00005  * Copyright (c) 2006 Regents of the University of California.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms are permitted
00009  * provided that the above copyright notice and this paragraph are
00010  * duplicated in all such forms and that any documentation,
00011  * advertising materials, and other materials related to such
00012  * distribution and use acknowledge that the software was developed
00013  * by the Integration Engineering Laboratory of the University of 
00014  * California, Davis. The name of the University may not be used to 
00015  * endorse or promote products derived from this software without 
00016  * specific prior written permission. 
00017  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00019  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020 */
00021 
00022 /* Filename: list.h */
00023 
00024 #ifndef LIST_H
00025 #define LIST_H
00026 
00027 #include<stdio.h>
00028 #include<stdlib.h>
00029 
00030 #define DATA void*
00031 
00032 typedef struct listNode_s{
00033 
00034   DATA node_data;
00035   struct listNode_s *next; 
00036 
00037 }listNode_t;
00038 
00039 typedef listNode_t* listNode_p;
00040 
00041 typedef struct list_s{
00042 
00043   listNode_p listhead;
00044   int size;
00045 
00046 }list_t;
00047 
00048 /* create a pointer type for the LinkedList */
00049 typedef list_t* list_p;
00050 
00051 /* some basic functions that help  */
00052 list_p ListInitialize(void);
00053   /* note: this Terminate Function should only be called on empty lists */
00054 void ListTerminate(list_p list);
00055 
00056 /* functions that return intergers */
00057 int ListGetSize(list_p list);
00058 int ListAdd(list_p list, DATA data);
00059 int ListInsert(list_p list, DATA data, const int index);
00060 
00061 /* functions that will return pointers to the data */
00062 DATA ListGetHead(list_p list);
00063 DATA ListPop(list_p list);
00064 DATA ListSearch(list_p list, const int index);
00065 DATA ListDelete(list_p list, const int index);
00066 
00067 
00068 #define QUEUE_TEMPLATE( name, node_type, search_type, search_var_name ) \
00069 typedef struct name##_s name \
00070 { \
00071   int size; \
00072   list_p list; \
00073   MUTEX_T* lock; \
00074   COND_T* cond; \
00075 } name##_t; \
00076   \
00077 typedef name##_t* name##_p; \
00078   \
00079 name##_p name##Initialize( void ); \
00080 void name##Destroy( name##_p name ); \
00081 int name##Add( name##_p name, node_type ); \
00082 name##_p name##Pop( name##_p name ); \
00083 name##_p name##Search( name##_p name, search_type key ); \
00084 int name##Remove(name##_p name, search_type key ); \
00085 name##_p name##SearchIndex( name##_p name, int index ); \
00086 int name##RemoveIndex(name##_p name, int index); \
00087   \
00088 name##_p name##Initialize( void ) \
00089 { \
00090   name##_p temp; \
00091   temp = (name##_p)malloc(sizeof(name##_t)); \
00092   temp->size = 0; \
00093   temp->list = ListInitialize(); \
00094   \
00095   temp->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T)); \
00096   temp->cond = (COND_T*)malloc(sizeof(COND_T)); \
00097   return temp; \
00098 } \
00099   \
00100 int name##Destroy( name##_p name ) \
00101 { \
00102   ListTerminate(name->list); \
00103   MUTEX_DESTROY(name->lock); \
00104   COND_DESTROY(name->cond); \
00105   free(name->lock); \
00106   free(name->cond); \
00107   return 0; \
00108 } \
00109   \
00110 int name##Add( name##_p name, node_type* node ) \
00111 { \
00112   MUTEX_LOCK(name->lock); \
00113   ListAdd(name->list, node); \
00114   name->size++; \
00115   COND_SIGNAL(name->cond); \
00116   MUTEX_UNLOCK(name->lock); \
00117   return 0; \
00118 } \
00119   \
00120 node_type* name##Pop( name##_p name ) \
00121 { \
00122   node_type *ret; \
00123   MUTEX_LOCK(name->lock); \
00124   ret = ListPop(name->list); \
00125   name->size--; \
00126   COND_SIGNAL(name->cond); \
00127   MUTEX_UNLOCK(name->lock); \
00128   return ret; \
00129 } \
00130   \
00131 node_type* name##Search( name##_p name, search_type value ) \
00132 { \
00133   listNode_t* parsenode; \
00134   node_type* node; \
00135   node = NULL; \
00136   \
00137   MUTEX_LOCK(name->lock); \
00138   if (name->list->listhead == NULL) { \
00139     MUTEX_UNLOCK(name->lock); \
00140     return NULL; \
00141   } \
00142   for( \
00143       parsenode = (listNode_t*)name->list->listhead; \
00144       parsenode->next != NULL; \
00145       parsenode = (listNode_t*)parsenode->next \
00146      ) \
00147   { \
00148     node = (node_type*)parsenode->node_data; \
00149     if (node->search_var_name == value ) \
00150       break; \
00151   } \
00152   MUTEX_UNLOCK(name->lock); \
00153   return node; \
00154 } \
00155   \
00156 int name##Remove( name##_p name, search_type key ) \
00157 { \
00158   listNode_t* parsenode; \
00159   node_type* node; \
00160   node = NULL; \
00161   \
00162   MUTEX_LOCK(name->lock); \
00163   if (name->list->listhead == NULL) { \
00164     MUTEX_UNLOCK(name->lock); \
00165     return MC_ERR_NOT_FOUND; \
00166   } \
00167   for( \
00168       parsenode = (listNode_t*)name->list->listhead; \
00169       parsenode->next != NULL; \
00170       parsenode = (listNode_t*)parsenode->next \
00171      ) \
00172   { \
00173     node = (node_type*)parsenode->node_data; \
00174     if (node->search_var_name == key) \
00175       break; \
00176   } \
00177   MUTEX_UNLOCK(name->lock); \
00178 } \
00179   \
00180 name##_p name##SearchIndex( name##_p name, int index ) \
00181 { \
00182   node_type* node; \
00183   MUTEX_LOCK(name->lock); \
00184   node = (node_type*)ListSearch(name->list, index); \
00185   MUTEX_UNLOCK(name->lock); \
00186   return node; \
00187 } \
00188   \
00189 int name##RemoveIndex( name##_p name, int index ) \
00190 { \
00191   node_type* node; \
00192   MUTEX_LOCK(name->lock); \
00193   node = ListDelete(name->list, index); \
00194   node_type##Destroy(node); \
00195   MUTEX_UNLOCK(name->lock); \
00196   return 0; \
00197 } 
00198 
00199 #endif

Generated on Fri Jul 11 17:59:45 2008 for Mobile-C by  doxygen 1.5.4