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

Zurück zum Archiv-Index

susumu.yata null+****@clear*****
Mon Dec 1 15:01:10 JST 2014


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

  New Revision: 1783774a37e36afc68fbb295b749f21ee58227ff
  https://github.com/groonga/grnxx/commit/1783774a37e36afc68fbb295b749f21ee58227ff

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

  Modified files:
    lib/grnxx/string.cpp

  Modified: lib/grnxx/string.cpp (+18 -12)
===================================================================
--- lib/grnxx/string.cpp    2014-12-01 14:55:29 +0900 (fc5e5a3)
+++ lib/grnxx/string.cpp    2014-12-01 15:01:10 +0900 (c7c9d4d)
@@ -77,20 +77,26 @@ String &String::instantiate() {
 }
 
 void String::resize_buffer(size_t new_size) {
-  size_t new_capacity = capacity_ * 2;
-  if (new_size > new_capacity) {
-    new_capacity = new_size;
-  }
-  char *new_buffer = static_cast<char *>(std::malloc(new_capacity));
-  if (!new_buffer) {
-    throw "Memory allocation failed";  // TODO
-  }
-  std::memcpy(new_buffer, data_, size_);
   if (capacity_ != 0) {
-    std::free(buffer_);
+    size_t new_capacity = capacity_ * 2;
+    if (new_size > new_capacity) {
+      new_capacity = new_size;
+    }
+    char *new_buffer =
+        static_cast<char *>(std::realloc(buffer_, new_capacity));
+    if (!new_buffer) {
+      throw "Memory allocation failed";  // TODO
+    }
+    buffer_ = new_buffer;
+    capacity_ = new_capacity;
+  } else {
+    char *new_buffer = static_cast<char *>(std::malloc(new_size));
+    if (!new_buffer) {
+      throw "Memory allocation failed";  // TODO
+    }
+    buffer_ = new_buffer;
+    capacity_ = new_size;
   }
-  buffer_ = new_buffer;
-  capacity_ = new_capacity;
 }
 
 void String::append_overlap(const char *data, size_t size) {
-------------- next part --------------
HTML����������������������������...
Download 



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