00001 #include "rcs_defs.hh"
00002
00003 #ifndef NO_STDIO
00004 #include <stdio.h>
00005 #endif
00006
00007 #include <stddef.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include "_table.h"
00011 #include "dbg_mem.h"
00012 #include "rcs_prnt.hh"
00013
00014 int
00015 table_new (_RCS_TABLE * table, size_t dsize)
00016 {
00017 int div, rem;
00018
00019 table->table = NULL;
00020 table->dsize = dsize;
00021
00022 div = dsize / sizeof (unsigned long int);
00023 rem = dsize % sizeof (unsigned long int);
00024 if (rem)
00025 {
00026 table->lsize = div + 1;
00027 }
00028 else
00029 {
00030 table->lsize = div;
00031 }
00032 table->size = 0;
00033 table->count = 0;
00034 return 0;
00035 }
00036
00037 int
00038 table_delete (_RCS_TABLE * table)
00039 {
00040 if (table == NULL)
00041 {
00042 return -1;
00043 }
00044
00045 if (table->table == NULL)
00046 {
00047 return -1;
00048 }
00049 else
00050 {
00051 #ifdef sparcworks_sun4
00052
00053
00054 DEBUG_FREE ((char *) table->table);
00055 #else
00056 DEBUG_FREE (table->table);
00057 #endif
00058 return 0;
00059 }
00060 }
00061
00062 static unsigned long int *
00063 table_nth (_RCS_TABLE * table, unsigned long int nth)
00064 {
00065 if (NULL == table)
00066 {
00067 return 0;
00068 }
00069 if (NULL == table->table)
00070 {
00071 return 0;
00072 }
00073 return &table->table[nth * (1 + table->lsize)];
00074 }
00075
00076 int
00077 table_add (_RCS_TABLE * table, unsigned long int key, const void *data)
00078 {
00079 int t;
00080 _RCS_TABLE old = *table;
00081 unsigned long int *ptr;
00082 unsigned long int *oldptr;
00083
00084
00085 if (0 == key)
00086 {
00087 return -1;
00088 }
00089
00090 if (table == NULL)
00091 {
00092 return -1;
00093 }
00094
00095
00096 if (table->table == NULL)
00097 {
00098 table->size = 1;
00099 table->table = (unsigned long *) DEBUG_CALLOC (table->size,
00100 (1 +
00101 table->lsize) *
00102 sizeof (unsigned long
00103 int));
00104 table->table[0] = 0;
00105 }
00106
00107
00108 for (t = 0; t < table->size; t++)
00109 {
00110 ptr = table_nth (table, t);
00111 if (ptr[0] == 0)
00112 {
00113 ptr[0] = key;
00114 memcpy (&ptr[1], data, table->dsize);
00115 return 0;
00116 }
00117 if (ptr[0] == key)
00118 {
00119 memcpy (&ptr[1], data, table->dsize);
00120 table->count = 1;
00121 return 0;
00122 }
00123 }
00124
00125
00126
00127 table->size *= 2;
00128 table->table = (unsigned long *) DEBUG_CALLOC (table->size,
00129 (1 +
00130 table->lsize) *
00131 sizeof (unsigned long int));
00132 for (t = 0; t < table->size / 2; t++)
00133 {
00134 ptr = table_nth (table, t);
00135 oldptr = table_nth (&old, t);
00136 ptr[0] = oldptr[0];
00137 memcpy (&ptr[1], &oldptr[1], table->dsize);
00138 }
00139 if (NULL != old.table)
00140 {
00141 DEBUG_FREE (old.table);
00142 }
00143
00144 ptr = table_nth (table, table->size / 2);
00145 ptr[0] = key;
00146 memcpy (&ptr[1], data, table->dsize);
00147
00148 for (t = table->size / 2 + 1; t < table->size; t++)
00149 {
00150 ptr = table_nth (table, t);
00151 ptr[0] = 0;
00152 }
00153 table->count++;
00154 return 0;
00155 }
00156
00157 int
00158 table_get (_RCS_TABLE * table, unsigned long int key, void *data)
00159 {
00160 int t;
00161 unsigned long int *ptr;
00162
00163 if (key == 0)
00164 return -1;
00165
00166 if (table == NULL)
00167 {
00168 return -1;
00169 }
00170
00171 for (t = 0; t < table->size; t++)
00172 {
00173 ptr = table_nth (table, t);
00174 if (ptr[0] == key)
00175 {
00176 memcpy (data, &ptr[1], table->dsize);
00177 return 0;
00178 }
00179 }
00180
00181 return -1;
00182 }
00183
00184 int
00185 table_clear (_RCS_TABLE * table, unsigned long int key)
00186 {
00187 int t;
00188 unsigned long int *ptr;
00189
00190 if (key == 0)
00191 return -1;
00192
00193 if (table == NULL)
00194 {
00195 return -1;
00196 }
00197
00198 for (t = 0; t < table->size; t++)
00199 {
00200 ptr = table_nth (table, t);
00201 if (ptr[0] == key)
00202 {
00203 ptr[0] = 0;
00204 table->count--;
00205 return 0;
00206 }
00207 }
00208
00209 return -1;
00210 }
00211
00212 int
00213 table_clearall (_RCS_TABLE * table)
00214 {
00215 int t;
00216 unsigned long int *ptr;
00217
00218
00219 if (table == NULL)
00220 {
00221 return -1;
00222 }
00223
00224 for (t = 0; t < table->size; t++)
00225 {
00226 ptr = table_nth (table, t);
00227 ptr[0] = 0;
00228 }
00229 table->size = 0;
00230 table->count = 0;
00231 table->dsize = 0;
00232 table->lsize = 0;
00233 if (NULL != table->table)
00234 {
00235 free (table->table);
00236 table->table = NULL;
00237 }
00238
00239 return 0;
00240 }
00241
00242
00243 void
00244 table_print (_RCS_TABLE * table)
00245 {
00246 int t;
00247 unsigned long int *ptr;
00248 unsigned char *cptr;
00249 int i;
00250
00251 rcs_print ("*** TABLE PRINT ***\n");
00252 if (table == NULL)
00253 {
00254 return;
00255 }
00256
00257 rcs_print ("size = %d /* # of slots in table */\n", table->size);
00258 rcs_print ("dsize = %d /* actal size of each data, in bytes */\n",
00259 table->dsize);
00260 rcs_print ("lsize = %d /* upped size of each data, in longs */\n",
00261 table->lsize);
00262 rcs_print ("count = %d /* # of full slots */\n", table->count);
00263 for (t = 0; t < table->size; t++)
00264 {
00265 ptr = table_nth (table, t);
00266 if (ptr[0] == 0)
00267 {
00268 continue;
00269 }
00270 rcs_print ("%lu : ", ptr[0]);
00271 cptr = (char *) ptr;
00272 for (i = 0; i < 32 && i < table->dsize + 4; i++)
00273 {
00274 if (0 == i % 4)
00275 {
00276 rcs_print (" ");
00277 }
00278 rcs_print ("%2.2X", cptr[i]);
00279 }
00280 rcs_print ("\n");
00281 }
00282 fflush (stdout);
00283 }