[Lldb-commits] [PATCH] Reduce number of threads in lldb-mi.

Zachary Turner zturner at google.com
Thu Feb 19 11:12:06 PST 2015


================
Comment at: tools/lldb-mi/MICmnStreamStdin.h:125-126
@@ -124,2 +124,4 @@
     IOSStdinHandler *m_pStdinReadHandler;
+    static const int m_constBufferSize = 2048;
+    char m_cmdBuffer[m_constBufferSize];
 };
----------------
ki.stfu wrote:
> abidh wrote:
> > ki.stfu wrote:
> > > Let's do it the same way as in MICmnStreamStdinLinux.cpp:
> > > 
> > > * Specify m_constBufferSize and m_pCmdBuffer in header file:
> > > ```
> > >  67     // Attributes:
> > >  68   private:
> > >  69     const MIuint m_constBufferSize;
> > >  70     FILE *m_pStdin;
> > >  71     MIchar *m_pCmdBuffer;
> > > ```
> > > 
> > > * Initialize in ctor:
> > > ```
> > >  42 CMICmnStreamStdinLinux::CMICmnStreamStdinLinux(void)
> > >  43     : m_constBufferSize(1024)
> > >  44     , m_pStdin(nullptr)
> > >  45     , m_pCmdBuffer(nullptr)
> > >  46     , m_waitForInput(true)
> > > ```
> > > 
> > > * Create buffer in CMICmnStreamStdinLinux::Initialize:
> > > ```
> > >  83     // Other resources required
> > >  84     if (bOk)
> > >  85     {
> > >  86         m_pCmdBuffer = new MIchar[m_constBufferSize];
> > >  87         m_pStdin = stdin;
> > >  88     }
> > > ```
> > > 
> > > * Use it in fgets:
> > > ```
> > > 199     // Read user input
> > > 200     const MIchar *pText = ::fgets(&m_pCmdBuffer[0], m_constBufferSize, stdin);
> > > ```
> > > 
> > > * Don't forget to free it in CMICmnStreamStdinLinux::Shutdown:
> > > ```
> > > 126     // Tidy up
> > > 127     if (m_pCmdBuffer != nullptr)
> > > 128     {
> > > 129         delete[] m_pCmdBuffer;
> > > 130         m_pCmdBuffer = nullptr;
> > > 131     }
> > > ```
> > What benefits that gives us to use dynamic buffer?
> Very large buffers on stack may cause an segfault. But if dynamic memory fails it will throw bad_alloc exception (what is better :))
Use dynamic buffer IMO.  If your program is out of heap space good luck doing anything else anyway.  Maybe this call doesn't fail by using stack, but the next call will.  We just had this discussion with Greg the other day on lldb-dev, and we agreed that we should reduce the usage of stack buffers in LLDB as a whole.  Performance of 1 heap allocation is not a concern, but using stack buffer reduces the amount of stack LLDB uses, and we don't know if it decides to do a deep recursion or something.

(BTW, if dynamic memory fails it won't throw bad_alloc exception, it will just terminate the program, because we compile with exceptions disabled.  So whether it's here, or 5 lines of code later, doesn't really make a difference :)

http://reviews.llvm.org/D7746

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the lldb-commits mailing list