Commits

Anonymous committed d48151c

be less stupid about the std lib

  • Participants
  • Parent commits 018a039

Comments (0)

Files changed (3)

File include/js2erl.h

 // -*- mode: c++ -*-
+#include <list>
 #include <v8.h>
 #include "erl_driver.h"
 
 using namespace std;
 using namespace v8;
 
-class PointerList {
-public:
-  PointerList();
-  void **pointers;
-  int allocated;
-  int length;
-  void append(void *);
-  ~PointerList();
-};
-
 extern int terms_length(Handle<Value> val);
-extern int value_to_terms(Handle<Value> val, ErlDrvTermData *terms, PointerList *pl);
+extern int value_to_terms(Handle<Value> val, ErlDrvTermData *terms, list<void *> *);

File src/driver.cc

   terms[0] = ERL_DRV_ATOM;
   terms[1] = driver_mk_atom(const_cast<char *>(type));
 
-  PointerList pl;
+  list<void *> pl;
   value_to_terms(result, terms + 2, &pl);
 
   terms[tlen - 2] = ERL_DRV_TUPLE;
   terms[tlen - 1] = 2;
 
   driver_output_term(port, terms, tlen);
+
+  for (list<void *>::iterator iter = pl.begin() ; iter != pl.end(); ++iter) {
+    driver_free(*iter);
+  }
   driver_free(terms);
 };
 

File src/js2erl.cc

 #include "js2erl.h"
 #include <string.h>
 
-PointerList::PointerList() {
-  pointers = NULL;
-  allocated = 0;
-  length = 0;
-};
-
-void PointerList::append(void *pointer) {
-  if ((length + 1) > allocated) {
-    if (allocated == 0) {
-      allocated = 2;
-      pointers = reinterpret_cast<void **>(driver_alloc(sizeof(void *) * allocated));
-    } else {
-      allocated *= 2;
-      driver_realloc(pointers, sizeof(void *) * allocated);
-    }
-  }
-  pointers[length] = pointer;
-  length++;
-};
-
-PointerList::~PointerList() {
-  for (int i = 0; i < length; ++i) {
-    printf("[v2t] freeing pointer list length = %d\n", length);
-    driver_free(pointers[i]);
-  }
-};
-
 // internal
-static int date_to_terms(Handle<Date> val, ErlDrvTermData *terms, PointerList *pl);
-static int object_to_terms(Handle<Object> obj, ErlDrvTermData *terms, PointerList *pl);
-static int array_to_terms(Handle<Array> arr, ErlDrvTermData *terms, PointerList *pl);
-static int number_to_terms(Handle<Number> num, ErlDrvTermData *terms, PointerList *pl);
-static int string_to_terms(Handle<String> str, ErlDrvTermData *terms, PointerList *pl);
+static int date_to_terms(Handle<Date> val, ErlDrvTermData *terms, list<void *> *pl);
+static int object_to_terms(Handle<Object> obj, ErlDrvTermData *terms, list<void *> *pl);
+static int array_to_terms(Handle<Array> arr, ErlDrvTermData *terms, list<void *> *pl);
+static int number_to_terms(Handle<Number> num, ErlDrvTermData *terms, list<void *> *pl);
+static int string_to_terms(Handle<String> str, ErlDrvTermData *terms, list<void *> *pl);
 
 static int atom(const char *a, ErlDrvTermData *terms) {
   if (terms != NULL) {
   return value_to_terms(val, NULL, NULL);
 }
 
-int value_to_terms(Handle<Value> val, ErlDrvTermData *terms, PointerList *pl) {
+int value_to_terms(Handle<Value> val, ErlDrvTermData *terms, list<void *> *pl) {
   if (val->IsNull()) {
     //printf("[v2t] is null\n");
     return atom("null", terms);
 
 // private
 
-int array_to_terms(Handle<Array> arr, ErlDrvTermData *terms, PointerList *pl) {
+int array_to_terms(Handle<Array> arr, ErlDrvTermData *terms, list<void *> *pl) {
   int tlen = 0;
 
   int alen = arr->Length();
   return tlen;
 }
 
-
-int object_to_terms(Handle<Object> obj, ErlDrvTermData *terms, PointerList *pl) {
+int object_to_terms(Handle<Object> obj, ErlDrvTermData *terms, list<void *> *pl) {
   printf("[v2t] object_to_terms\n");
   int tlen = 0;
 
   return tlen;
 }
 
-int number_to_terms(Handle<Number> num, ErlDrvTermData *terms, PointerList *pl) {
+int number_to_terms(Handle<Number> num, ErlDrvTermData *terms, list<void *> *pl) {
   if (terms != NULL) {
     if (num->IsInt32()) {
       terms[0] = ERL_DRV_INT;
       terms[1] = num->Int32Value();
     } else {
-      double *f = reinterpret_cast<double *>(driver_alloc(sizeof(double))); // XXX memory leak
+      double *f = reinterpret_cast<double *>(driver_alloc(sizeof(double)));
       *f = num->Value();
-      pl->append(f);
+      pl->push_back(f);
       terms[0] = ERL_DRV_FLOAT;
       terms[1] = reinterpret_cast<ErlDrvTermData>(f);
     }
   return 2;
 }
 
-int string_to_terms(Handle<String> str, ErlDrvTermData *terms, PointerList *pl) {
+int string_to_terms(Handle<String> str, ErlDrvTermData *terms, list<void *> *pl) {
   if (terms != NULL) {
     String::Utf8Value utf8(str);
-    unsigned int len = utf8.length();
-    char *drv_str = reinterpret_cast<char *>(driver_alloc(len)); // XXX memory leak
+    size_t len = utf8.length();
+    char *drv_str = reinterpret_cast<char *>(driver_alloc(len));
     memcpy(drv_str, *utf8, len);
-    pl->append(drv_str);
+    pl->push_back(drv_str);
     terms[0] = ERL_DRV_BUF2BINARY;
     terms[1] = reinterpret_cast<ErlDrvTermData>(drv_str);
     terms[2] = len;
   return 3;
 }
 
-int date_to_terms(Handle<Date> date, ErlDrvTermData *terms, PointerList *pl) {
+int date_to_terms(Handle<Date> date, ErlDrvTermData *terms, list<void *> *pl) {
   if (terms != NULL) {
-    double *f = reinterpret_cast<double *>(driver_alloc(sizeof(double))); // XXX memory leak
+    double *f = reinterpret_cast<double *>(driver_alloc(sizeof(double)));
     *f = date->NumberValue();
-    pl->append(f);
+    pl->push_back(f);
     terms[0] = ERL_DRV_FLOAT;
     terms[1] = reinterpret_cast<ErlDrvTermData>(f);
   }