[x265] [PATCH 03 of 14] nits: do not check for NULL from new operations

Steve Borho steve at borho.org
Sat Sep 20 18:46:02 CEST 2014


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1411227644 -3600
#      Sat Sep 20 16:40:44 2014 +0100
# Node ID f2b4d2b34f8342d1194ed4e5d67aec2052f96299
# Parent  d5d19ae212568c35309f23646c8e2b53a38a3c6e
nits: do not check for NULL from new operations

By the C++ spec, new is incapable of returning NULL. If an allocation failure
actually occurs, an exception is issued (which we do not catch)
Long term, all of these new operations need to be replaced by malloc and
explicit initialization and destruction. In the short term, these return value
checks are redundant.

diff -r d5d19ae21256 -r f2b4d2b34f83 source/Lib/TLibCommon/TComPicSym.cpp
--- a/source/Lib/TLibCommon/TComPicSym.cpp	Sat Sep 20 16:31:00 2014 +0100
+++ b/source/Lib/TLibCommon/TComPicSym.cpp	Sat Sep 20 16:40:44 2014 +0100
@@ -66,8 +66,6 @@
 
     m_slice = new Slice;
     m_cuData = new TComDataCU[m_numCUsInFrame];
-    if (!m_slice || !m_cuData)
-        return false;
 
     bool tqBypass = param->bCULossless || param->bLossless;
     for (i = 0; i < m_numCUsInFrame; i++)
diff -r d5d19ae21256 -r f2b4d2b34f83 source/common/frame.cpp
--- a/source/common/frame.cpp	Sat Sep 20 16:31:00 2014 +0100
+++ b/source/common/frame.cpp	Sat Sep 20 16:40:44 2014 +0100
@@ -61,8 +61,6 @@
     m_defaultDisplayWindow = display;
 
     m_origPicYuv = new TComPicYuv;
-    if (!m_origPicYuv)
-        return false;
 
     bool ok = true;
     ok &= m_origPicYuv->create(param->sourceWidth, param->sourceHeight, param->internalCsp, g_maxCUSize, g_maxFullDepth);
@@ -103,23 +101,18 @@
 {
     m_picSym = new TComPicSym;
     m_reconPicYuv = new TComPicYuv;
-    if (m_picSym && m_reconPicYuv)
+    m_picSym->m_reconPicYuv = m_reconPicYuv;
+    bool ok = m_picSym->create(param) &&
+            m_reconPicYuv->create(param->sourceWidth, param->sourceHeight, param->internalCsp, g_maxCUSize, g_maxFullDepth);
+    if (ok)
     {
-        m_picSym->m_reconPicYuv = m_reconPicYuv;
-        bool ok = m_picSym->create(param) &&
-             m_reconPicYuv->create(param->sourceWidth, param->sourceHeight, param->internalCsp, g_maxCUSize, g_maxFullDepth);
-        if (ok)
-        {
-            // initialize m_reconpicYuv as SAO may read beyond the end of the picture accessing uninitialized pixels
-            int maxHeight = m_reconPicYuv->m_numCuInHeight * g_maxCUSize;
-            memset(m_reconPicYuv->m_picOrg[0], 0, m_reconPicYuv->m_stride * maxHeight);
-            memset(m_reconPicYuv->m_picOrg[1], 0, m_reconPicYuv->m_strideC * (maxHeight >> m_reconPicYuv->m_vChromaShift));
-            memset(m_reconPicYuv->m_picOrg[2], 0, m_reconPicYuv->m_strideC * (maxHeight >> m_reconPicYuv->m_vChromaShift));
-        }
-        return ok;
+        // initialize m_reconpicYuv as SAO may read beyond the end of the picture accessing uninitialized pixels
+        int maxHeight = m_reconPicYuv->m_numCuInHeight * g_maxCUSize;
+        memset(m_reconPicYuv->m_picOrg[0], 0, m_reconPicYuv->m_stride * maxHeight);
+        memset(m_reconPicYuv->m_picOrg[1], 0, m_reconPicYuv->m_strideC * (maxHeight >> m_reconPicYuv->m_vChromaShift));
+        memset(m_reconPicYuv->m_picOrg[2], 0, m_reconPicYuv->m_strideC * (maxHeight >> m_reconPicYuv->m_vChromaShift));
     }
-    else
-        return false;
+    return ok;
 }
 
 void Frame::reinit(x265_param *param)
diff -r d5d19ae21256 -r f2b4d2b34f83 source/encoder/api.cpp
--- a/source/encoder/api.cpp	Sat Sep 20 16:31:00 2014 +0100
+++ b/source/encoder/api.cpp	Sat Sep 20 16:40:44 2014 +0100
@@ -56,27 +56,24 @@
         return NULL;
 
     Encoder *encoder = new Encoder;
-    if (encoder)
+    if (!param->rc.bEnableSlowFirstPass)
+        x265_param_apply_fastfirstpass(param);
+    // may change params for auto-detect, etc
+    encoder->configure(param);
+    
+    // may change rate control and CPB params
+    if (!enforceLevel(*param, encoder->m_vps))
     {
-        if (!param->rc.bEnableSlowFirstPass)
-            x265_param_apply_fastfirstpass(param);
-        // may change params for auto-detect, etc
-        encoder->configure(param);
-        
-        // may change rate control and CPB params
-        if (!enforceLevel(*param, encoder->m_vps))
-        {
-            delete encoder;
-            return NULL;
-        }
+        delete encoder;
+        return NULL;
+    }
 
-        // will detect and set profile/tier/level in VPS
-        determineLevel(*param, encoder->m_vps);
+    // will detect and set profile/tier/level in VPS
+    determineLevel(*param, encoder->m_vps);
 
-        x265_print_params(param);
-        encoder->create();
-        encoder->init();
-    }
+    x265_print_params(param);
+    encoder->create();
+    encoder->init();
 
     return encoder;
 }
diff -r d5d19ae21256 -r f2b4d2b34f83 source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Sat Sep 20 16:31:00 2014 +0100
+++ b/source/encoder/encoder.cpp	Sat Sep 20 16:40:44 2014 +0100
@@ -85,9 +85,8 @@
     }
 
     m_frameEncoder = new FrameEncoder[m_param->frameNumThreads];
-    if (m_frameEncoder)
-        for (int i = 0; i < m_param->frameNumThreads; i++)
-            m_frameEncoder[i].setThreadPool(m_threadPool);
+    for (int i = 0; i < m_param->frameNumThreads; i++)
+        m_frameEncoder[i].setThreadPool(m_threadPool);
 
     if (!m_scalingList.init())
     {
@@ -106,16 +105,11 @@
     ThreadPool *pool = ThreadPool::getThreadPool();
     const int poolThreadCount = pool ? pool->getThreadCount() : 1;
     m_threadLocalData = new ThreadLocalData[poolThreadCount];
-    if (m_threadLocalData)
+    for (int i = 0; i < poolThreadCount; i++)
     {
-        for (int i = 0; i < poolThreadCount; i++)
-        {
-            m_threadLocalData[i].cuCoder.initSearch(m_param, m_scalingList);
-            m_threadLocalData[i].cuCoder.create(g_maxCUDepth + 1, g_maxCUSize);
-        }
+        m_threadLocalData[i].cuCoder.initSearch(m_param, m_scalingList);
+        m_threadLocalData[i].cuCoder.create(g_maxCUDepth + 1, g_maxCUSize);
     }
-    else
-        m_aborted = true;
 
     m_lookahead = new Lookahead(m_param, m_threadPool, this);
     m_dpb = new DPB(m_param);
@@ -288,15 +282,12 @@
         if (m_dpb->m_freeList.empty())
         {
             pic = new Frame;
-            if (!pic || !pic->create(m_param, m_sps.vuiParameters.defaultDisplayWindow, m_conformanceWindow))
+            if (!pic->create(m_param, m_sps.vuiParameters.defaultDisplayWindow, m_conformanceWindow))
             {
                 m_aborted = true;
                 x265_log(m_param, X265_LOG_ERROR, "memory allocation failure, aborting encode\n");
-                if (pic)
-                {
-                    pic->destroy();
-                    delete pic;
-                }
+                pic->destroy();
+                delete pic;
                 return -1;
             }
         }


More information about the x265-devel mailing list