]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/tossim/tossim.c
Merge over into the trunk.
[tinyos-2.x.git] / tos / lib / tossim / tossim.c
index f8af15c733f2b7abca2b86477b83b0d61608159a..0e807d16a3c9a8420be93c08a45c778b9788e5f4 100644 (file)
@@ -39,6 +39,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <hashtable.h>
 
 #include <mac.c>
 #include <radio.c>
@@ -74,12 +75,33 @@ Variable::Variable(char* str, char* formatStr, int array, int which) {
     data = NULL;
     ptr = NULL;
   }
+  printf("Allocated variable %s\n", realName);
 }
 
 Variable::~Variable() {
+  printf("Freeing variable %s\n", realName);
   free(data);
+  free(realName);
 }
 
+/* This is the sdbm algorithm, taken from
+   http://www.cs.yorku.ca/~oz/hash.html -pal */
+static unsigned int tossim_hash(void* key) {
+  char* str = (char*)key;
+  unsigned int hashVal = 0;
+  int c;
+  
+  while ((c = *str++))
+    hashVal = c + (hashVal << 6) + (hashVal << 16) - hashVal;
+  
+  return hashVal;
+}
+
+static int tossim_hash_eq(void* key1, void* key2) {
+  return strcmp((char*)key1, (char*)key2) == 0;
+}
+
+
 variable_string_t Variable::getData() {
   if (data != NULL && ptr != NULL) {
     str.ptr = data;
@@ -100,7 +122,9 @@ variable_string_t Variable::getData() {
 
 Mote::Mote(nesc_app_t* n) {
   app = n;
+  varTable = create_hashtable(128, tossim_hash, tossim_hash_eq);
 }
+
 Mote::~Mote(){}
 
 unsigned long Mote::id() {
@@ -143,20 +167,27 @@ void Mote::setID(unsigned long val) {
 Variable* Mote::getVariable(char* name) {
   char* typeStr = "";
   int isArray;
-  // Could hash this for greater efficiency,
-  // but that would either require transformation
-  // in Tossim class or a more complex typemap.
-  if (app != NULL) {
-    for (int i = 0; i < app->numVariables; i++) {
-      if(strcmp(name, app->variableNames[i]) == 0) {
-       typeStr = app->variableTypes[i];
-       isArray = app->variableArray[i];
-       break;
+  Variable* var;
+  
+  var = (Variable*)hashtable_search(varTable, name);
+  if (var == NULL) {
+    // Could hash this for greater efficiency,
+    // but that would either require transformation
+    // in Tossim class or a more complex typemap.
+    if (app != NULL) {
+      for (int i = 0; i < app->numVariables; i++) {
+       if(strcmp(name, app->variableNames[i]) == 0) {
+         typeStr = app->variableTypes[i];
+         isArray = app->variableArray[i];
+         break;
+       }
       }
     }
+    //  printf("Getting variable %s of type %s %s\n", name, typeStr, isArray? "[]" : "");
+    var = new Variable(name, typeStr, isArray, nodeID);
+    hashtable_insert(varTable, name, var);
   }
-  //  printf("Getting variable %s of type %s %s\n", name, typeStr, isArray? "[]" : "");
-  return new Variable(name, typeStr, isArray, nodeID);
+  return var;
 }
 
 Tossim::Tossim(nesc_app_t* n) {
@@ -226,6 +257,10 @@ bool Tossim::removeChannel(char* channel, FILE* file) {
   return sim_remove_channel(channel, file);
 }
 
+void Tossim::randomSeed(int seed) {
+  return sim_random_seed(seed);
+}
+
 bool Tossim::runNextEvent() {
   return sim_run_next_event();
 }