Thrift: Add "#include <cstdlib>" in a few places and s/malloc/std::malloc/

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
diff --git a/lib/cpp/src/transport/TFileTransport.cpp b/lib/cpp/src/transport/TFileTransport.cpp
index ac739ec..a4b275f 100644
--- a/lib/cpp/src/transport/TFileTransport.cpp
+++ b/lib/cpp/src/transport/TFileTransport.cpp
@@ -23,6 +23,7 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
+#include <cstdlib>
 #include <iostream>
 #include <sys/stat.h>
 
@@ -209,7 +210,7 @@
   }
 
   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
diff --git a/lib/cpp/src/transport/THttpClient.cpp b/lib/cpp/src/transport/THttpClient.cpp
index c213ef0..901a4e0 100644
--- a/lib/cpp/src/transport/THttpClient.cpp
+++ b/lib/cpp/src/transport/THttpClient.cpp
@@ -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 @@
 }
 
 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 @@
 
 THttpClient::~THttpClient() {
   if (httpBuf_ != NULL) {
-    free(httpBuf_);
+    std::free(httpBuf_);
   }
 }
 
@@ -202,7 +204,7 @@
   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.");
     }
diff --git a/lib/cpp/src/transport/TTransportUtils.cpp b/lib/cpp/src/transport/TTransportUtils.cpp
index e1f37b8..cb7ab61 100644
--- a/lib/cpp/src/transport/TTransportUtils.cpp
+++ b/lib/cpp/src/transport/TTransportUtils.cpp
@@ -310,7 +310,7 @@
       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 @@
     // 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 @@
     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;
   }
 
diff --git a/lib/cpp/src/transport/TTransportUtils.h b/lib/cpp/src/transport/TTransportUtils.h
index 83abf8e..59d8fb8 100644
--- a/lib/cpp/src/transport/TTransportUtils.h
+++ b/lib/cpp/src/transport/TTransportUtils.h
@@ -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 @@
   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 @@
 
   ~TMemoryBuffer() {
     if (owner_) {
-      free(buffer_);
+      std::free(buffer_);
       buffer_ = NULL;
     }
   }
@@ -537,8 +538,8 @@
     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 @@
     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 @@
       // 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