[icinga-checkins] icinga.org: icinga2/master: Avoid unnecessary new/deletes

git at icinga.org git at icinga.org
Mon Feb 1 09:38:35 CET 2016


Module: icinga2
Branch: master
Commit: c03e458e134e8ed9468bcef555151007ec47c358
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=c03e458e134e8ed9468bcef555151007ec47c358

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Mon Feb  1 09:33:33 2016 +0100

Avoid unnecessary new/deletes

refs #11014

---

 lib/base/socketevents.cpp |   26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/lib/base/socketevents.cpp b/lib/base/socketevents.cpp
index 5a99a5e..fb73901 100644
--- a/lib/base/socketevents.cpp
+++ b/lib/base/socketevents.cpp
@@ -80,18 +80,16 @@ void SocketEvents::ThreadProc(void)
 {
 	Utility::SetThreadName("SocketIO");
 
-	pollfd *pfds = NULL;
-	SocketEventDescriptor *descriptors = NULL;
-	int pfdcount;
+	std::vector<pollfd> pfds;
+	std::vector<SocketEventDescriptor> descriptors;
 
 	for (;;) {
 		{
 			boost::mutex::scoped_lock lock(l_SocketIOMutex);
 
-			if (pfds == NULL) {
-				pfdcount = l_SocketIOSockets.size();
-				pfds  = new pollfd[pfdcount];
-				descriptors = new SocketEventDescriptor[pfdcount];
+			if (l_SocketIOFDChanged) {
+				pfds.resize(l_SocketIOSockets.size());
+				descriptors.resize(l_SocketIOSockets.size());
 
 				int i = 0;
 
@@ -111,10 +109,12 @@ void SocketEvents::ThreadProc(void)
 			}
 		}
 
+		ASSERT(!pfds.empty());
+
 #ifdef _WIN32
-		(void) WSAPoll(pfds, pfdcount, -1);
+		(void) WSAPoll(&pfds[0], pfds.size(), -1);
 #else /* _WIN32 */
-		(void) poll(pfds, pfdcount, -1);
+		(void) poll(&pfds[0], pfds.size(), -1);
 #endif /* _WIN32 */
 
 		std::vector<EventDescription> events;
@@ -122,14 +122,10 @@ void SocketEvents::ThreadProc(void)
 		{
 			boost::mutex::scoped_lock lock(l_SocketIOMutex);
 
-			if (l_SocketIOFDChanged) {
-				delete [] pfds;
-				delete [] descriptors;
-				pfds = NULL;
+			if (l_SocketIOFDChanged)
 				continue;
-			}
 
-			for (int i = 0; i < pfdcount; i++) {
+			for (int i = 0; i < pfds.size(); i++) {
 				if ((pfds[i].revents & (POLLIN | POLLOUT | POLLHUP | POLLERR)) == 0)
 					continue;
 



More information about the icinga-checkins mailing list