[Groonga-commit] groonga/grnxx at fe5bc80 [master] Update String::append() to use realloc(). (#123)

Zurück zum Archiv-Index

susumu.yata null+****@clear*****
Mon Dec 1 15:33:00 JST 2014


susumu.yata	2014-12-01 15:33:00 +0900 (Mon, 01 Dec 2014)

  New Revision: fe5bc807d7a6c4ba9ff0aea48e1a52a8f8361866
  https://github.com/groonga/grnxx/commit/fe5bc807d7a6c4ba9ff0aea48e1a52a8f8361866

  Message:
    Update String::append() to use realloc(). (#123)

  Modified files:
    include/grnxx/string.hpp
    lib/grnxx/string.cpp

  Modified: include/grnxx/string.hpp (+4 -3)
===================================================================
--- include/grnxx/string.hpp    2014-12-01 15:01:10 +0900 (e311988)
+++ include/grnxx/string.hpp    2014-12-01 15:33:00 +0900 (1c437b3)
@@ -251,9 +251,10 @@ class String {
   // On failure, throws an exception.
   String &append(const char *data, size_t size) {
     if ((size_ + size) > capacity_) {
-      // NOTE: If the given string is a part of "this", it is destoyed in
-      //       resize_buffer(), so append_overlap() is required.
-      if ((data >= buffer_) && (data < (buffer_ + size_))) {
+      // NOTE: If the given string is a part of this instance, it will be
+      //       moved to the new address in resize_buffer().
+      if ((capacity_ != 0) &&
+          (data >= buffer_) && (data < (buffer_ + size_))) {
         append_overlap(data, size);
         return *this;
       } else {

  Modified: lib/grnxx/string.cpp (+3 -5)
===================================================================
--- lib/grnxx/string.cpp    2014-12-01 15:01:10 +0900 (c7c9d4d)
+++ lib/grnxx/string.cpp    2014-12-01 15:33:00 +0900 (2458a78)
@@ -105,15 +105,13 @@ void String::append_overlap(const char *data, size_t size) {
   if (new_size > new_capacity) {
     new_capacity = new_size;
   }
-  char *new_buffer = static_cast<char *>(std::malloc(new_capacity));
+  char *new_buffer =
+      static_cast<char *>(std::realloc(buffer_, new_capacity));
   if (!new_buffer) {
     throw "Memory allocation failed";  // TODO
   }
-  std::memcpy(new_buffer, buffer_, size_);
+  data = new_buffer + (data - buffer_);
   std::memcpy(new_buffer + size_, data, size);
-  if (capacity_ != 0) {
-    std::free(buffer_);
-  }
   buffer_ = new_buffer;
   size_ = new_size;
   capacity_ = new_capacity;
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index