Thrift: Add "#include <cstdlib>" in a few places and s/malloc/std::malloc/
authorDavid Reiss <dreiss@apache.org>
Tue, 19 Feb 2008 22:47:29 +0000 (22:47 +0000)
committerDavid Reiss <dreiss@apache.org>
Tue, 19 Feb 2008 22:47:29 +0000 (22:47 +0000)
Summary:
There were a few places where we were calling malloc/reallaoc/free without
including cstdlib (or stdlib.h).  This is broken, but it worked because
other headers that we were including included stdlib.h.  However, on a
platform where this wasn't true, it broke the Thrift build.  This change
adds the proper includes.  It also changes malloc to std::malloc (same
with realloc and free) in a few places, because that is the correct way
of doing it when you include cstdlib.

Reviewed By: mcslee

Test Plan: Compiled Thrift.

Revert Plan: ok

Other Notes:
This bug was noticed by a Thrudb user, and the patch was sent in by
Ross McFarland.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665487 13f79535-47bb-0310-9956-ffa450edef68

configure.ac
lib/cpp/src/protocol/TBinaryProtocol.cpp
lib/cpp/src/protocol/TBinaryProtocol.h
lib/cpp/src/server/TNonblockingServer.cpp
lib/cpp/src/server/TNonblockingServer.h
lib/cpp/src/transport/TFileTransport.cpp
lib/cpp/src/transport/THttpClient.cpp
lib/cpp/src/transport/TTransportUtils.cpp
lib/cpp/src/transport/TTransportUtils.h

index fd29a0f..a1b059f 100644 (file)
@@ -56,6 +56,7 @@ AC_CHECK_HEADERS([netdb.h])
 AC_CHECK_HEADERS([netinet/in.h])
 AC_CHECK_HEADERS([pthread.h])
 AC_CHECK_HEADERS([stddef.h])
+AC_CHECK_HEADERS([stdlib.h])
 AC_CHECK_HEADERS([sys/socket.h])
 AC_CHECK_HEADERS([sys/time.h])
 AC_CHECK_HEADERS([unistd.h])
index 929499b..b3e9fbc 100644 (file)
@@ -406,7 +406,7 @@ uint32_t TBinaryProtocol::readStringBody(string& str, int32_t size) {
 
   // Use the heap here to prevent stack overflow for v. large strings
   if (size > string_buf_size_ || string_buf_ == NULL) {
-    string_buf_ = (uint8_t*)realloc(string_buf_, (uint32_t)size);
+    string_buf_ = (uint8_t*)std::realloc(string_buf_, (uint32_t)size);
     if (string_buf_ == NULL) {
       string_buf_size_ = 0;
       throw TProtocolException(TProtocolException::UNKNOWN, "Out of memory in TBinaryProtocol::readString");
index 9203865..dc47b3a 100644 (file)
@@ -50,7 +50,7 @@ class TBinaryProtocol : public TProtocol {
 
   ~TBinaryProtocol() {
     if (string_buf_ != NULL) {
-      free(string_buf_);
+      std::free(string_buf_);
       string_buf_size_ = 0;
     }
   }
index de32db5..3263887 100644 (file)
@@ -108,7 +108,7 @@ void TConnection::workSocket() {
       while (readWant_ > readBufferSize_) {
         readBufferSize_ *= 2;
       }
-      readBuffer_ = (uint8_t*)realloc(readBuffer_, readBufferSize_);
+      readBuffer_ = (uint8_t*)std::realloc(readBuffer_, readBufferSize_);
       if (readBuffer_ == NULL) {
         GlobalOutput("TConnection::workSocket() realloc");
         close();
index e043b54..89c69a0 100644 (file)
@@ -12,6 +12,7 @@
 #include <transport/TTransportUtils.h>
 #include <concurrency/ThreadManager.h>
 #include <stack>
+#include <cstdlib>
 #include <event.h>
 
 namespace facebook { namespace thrift { namespace server {
@@ -288,7 +289,7 @@ class TConnection {
 
   // Constructor
   TConnection(int socket, short eventFlags, TNonblockingServer *s) {
-    readBuffer_ = (uint8_t*)malloc(1024);
+    readBuffer_ = (uint8_t*)std::malloc(1024);
     if (readBuffer_ == NULL) {
       throw new facebook::thrift::TException("Out of memory.");
     }
index ac739ec..a4b275f 100644 (file)
@@ -23,6 +23,7 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
+#include <cstdlib>
 #include <iostream>
 #include <sys/stat.h>
 
@@ -209,7 +210,7 @@ void TFileTransport::enqueueEvent(const uint8_t* buf, uint32_t eventLen, bool bl
   }
 
   eventInfo* toEnqueue = new eventInfo();
-  toEnqueue->eventBuff_ = (uint8_t *)malloc((sizeof(uint8_t) * eventLen) + 4);
+  toEnqueue->eventBuff_ = (uint8_t *)std::malloc((sizeof(uint8_t) * eventLen) + 4);
   // first 4 bytes is the event length
   memcpy(toEnqueue->eventBuff_, (void*)(&eventLen), 4);
   // actual event contents
index c213ef0..901a4e0 100644 (file)
@@ -4,6 +4,8 @@
 // See accompanying file LICENSE or visit the Thrift site at:
 // http://developers.facebook.com/thrift/
 
+#include <cstdlib>
+
 #include "THttpClient.h"
 #include "TSocket.h"
 
@@ -54,7 +56,7 @@ THttpClient::THttpClient(string host, int port, string path) :
 }
 
 void THttpClient::init() {
-  httpBuf_ = (char*)malloc(httpBufSize_+1);
+  httpBuf_ = (char*)std::malloc(httpBufSize_+1);
   if (httpBuf_ == NULL) {
     throw TTransportException("Out of memory.");
   }
@@ -63,7 +65,7 @@ void THttpClient::init() {
 
 THttpClient::~THttpClient() {
   if (httpBuf_ != NULL) {
-    free(httpBuf_);
+    std::free(httpBuf_);
   }
 }
 
@@ -202,7 +204,7 @@ void THttpClient::refill() {
   uint32_t avail = httpBufSize_ - httpBufLen_;
   if (avail <= (httpBufSize_ / 4)) {
     httpBufSize_ *= 2;
-    httpBuf_ = (char*)realloc(httpBuf_, httpBufSize_+1);
+    httpBuf_ = (char*)std::realloc(httpBuf_, httpBufSize_+1);
     if (httpBuf_ == NULL) {
       throw TTransportException("Out of memory.");
     }
index e1f37b8..cb7ab61 100644 (file)
@@ -310,7 +310,7 @@ void TMemoryBuffer::write(const uint8_t* buf, uint32_t len) {
       bufferSize_ *= 2;
       avail = bufferSize_ - wPos_;
     }
-    buffer_ = (uint8_t*)realloc(buffer_, bufferSize_);
+    buffer_ = (uint8_t*)std::realloc(buffer_, bufferSize_);
     if (buffer_ == NULL) {
       throw TTransportException("Out of memory.");
     }
@@ -354,7 +354,7 @@ uint32_t TPipedTransport::read(uint8_t* buf, uint32_t len) {
     // Double the size of the underlying buffer if it is full
     if (rLen_ == rBufSize_) {
       rBufSize_ *=2;
-      rBuf_ = (uint8_t *)realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
+      rBuf_ = (uint8_t *)std::realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
     }
 
     // try to fill up the buffer
@@ -387,7 +387,7 @@ void TPipedTransport::write(const uint8_t* buf, uint32_t len) {
     while ((len + wLen_) >= newBufSize) {
       newBufSize *= 2;
     }
-    wBuf_ = (uint8_t *)realloc(wBuf_, sizeof(uint8_t) * newBufSize);
+    wBuf_ = (uint8_t *)std::realloc(wBuf_, sizeof(uint8_t) * newBufSize);
     wBufSize_ = newBufSize;
   }
 
index 83abf8e..59d8fb8 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef _THRIFT_TRANSPORT_TTRANSPORTUTILS_H_
 #define _THRIFT_TRANSPORT_TTRANSPORTUTILS_H_ 1
 
+#include <cstdlib>
 #include <string>
 #include <algorithm>
 #include <transport/TTransport.h>
@@ -301,7 +302,7 @@ class TMemoryBuffer : public TTransport {
   void initCommon(uint8_t* buf, uint32_t size, bool owner, uint32_t wPos) {
     if (buf == NULL && size != 0) {
       assert(owner);
-      buf = (uint8_t*)malloc(size);
+      buf = (uint8_t*)std::malloc(size);
       if (buf == NULL) {
         throw TTransportException("Out of memory");
       }
@@ -394,7 +395,7 @@ class TMemoryBuffer : public TTransport {
 
   ~TMemoryBuffer() {
     if (owner_) {
-      free(buffer_);
+      std::free(buffer_);
       buffer_ = NULL;
     }
   }
@@ -537,8 +538,8 @@ class TPipedTransport : virtual public TTransport {
     pipeOnRead_ = true;
     pipeOnWrite_ = false;
 
-    rBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * rBufSize_);
-    wBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * wBufSize_);
+    rBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * rBufSize_);
+    wBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * wBufSize_);
   }
 
   TPipedTransport(boost::shared_ptr<TTransport> srcTrans,
@@ -549,13 +550,13 @@ class TPipedTransport : virtual public TTransport {
     rBufSize_(512), rPos_(0), rLen_(0),
     wBufSize_(sz), wLen_(0) {
 
-    rBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * rBufSize_);
-    wBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * wBufSize_);
+    rBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * rBufSize_);
+    wBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * wBufSize_);
   }
 
   ~TPipedTransport() {
-    free(rBuf_);
-    free(wBuf_);
+    std::free(rBuf_);
+    std::free(wBuf_);
   }
 
   bool isOpen() {
@@ -567,7 +568,7 @@ class TPipedTransport : virtual public TTransport {
       // Double the size of the underlying buffer if it is full
       if (rLen_ == rBufSize_) {
         rBufSize_ *=2;
-        rBuf_ = (uint8_t *)realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
+        rBuf_ = (uint8_t *)std::realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
       }
 
       // try to fill up the buffer