/home/dko/projects/mobilec/trunk/src/mc_list/list.h

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

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4