Author: olehougaard
Date: Wed Mar 18 05:27:37 2009
New Revision: 1532

Modified:
    branches/bleeding_edge/src/allocation.cc
    branches/bleeding_edge/src/allocation.h
    branches/bleeding_edge/src/d8-readline.cc

Log:
Introducing a new StrNDup function that uses new[] for when we dispose the  
result using delete[].
Review URL: http://codereview.chromium.org/48127

Modified: branches/bleeding_edge/src/allocation.cc
==============================================================================
--- branches/bleeding_edge/src/allocation.cc    (original)
+++ branches/bleeding_edge/src/allocation.cc    Wed Mar 18 05:27:37 2009
@@ -87,6 +87,16 @@
  }


+char* StrNDup(const char* str, size_t n) {
+  size_t length = strlen(str);
+  if (n < length) length = n;
+  char* result = NewArray<char>(length + 1);
+  memcpy(result, str, length * kCharSize);
+  result[length] = '\0';
+  return result;
+}
+
+
  int NativeAllocationChecker::allocation_disallowed_ = 0;



Modified: branches/bleeding_edge/src/allocation.h
==============================================================================
--- branches/bleeding_edge/src/allocation.h     (original)
+++ branches/bleeding_edge/src/allocation.h     Wed Mar 18 05:27:37 2009
@@ -119,10 +119,11 @@
  }


-// The normal strdup function uses malloc.  This version of StrDup
-// uses new and calls the FatalProcessOutOfMemory handler if
-// allocation fails.
+// The normal strdup functions use malloc.  These versions of StrDup
+// and StrNDup uses new and calls the FatalProcessOutOfMemory handler
+// if allocation fails.
  char* StrDup(const char* str);
+char* StrNDup(const char* str, size_t n);


  // Allocation policy for allocating in the C free store using malloc

Modified: branches/bleeding_edge/src/d8-readline.cc
==============================================================================
--- branches/bleeding_edge/src/d8-readline.cc   (original)
+++ branches/bleeding_edge/src/d8-readline.cc   Wed Mar 18 05:27:37 2009
@@ -103,7 +103,7 @@
    static unsigned current_index;
    static Persistent<Array> current_completions;
    if (state == 0) {
-    i::SmartPointer<char> full_text(i::OS::StrNDup(rl_line_buffer,  
rl_point));
+    i::SmartPointer<char> full_text(i::StrNDup(rl_line_buffer, rl_point));
      HandleScope scope;
      Handle<Array> completions =
        Shell::GetCompletions(String::New(text), String::New(*full_text));

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to