Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Flexible C++ #2: Efficient Integer to String Conversions, part 3


Listing 1: Definition of Slot<> and Index<> classes

Listing 1: Definition of Slot<> and Index<> classes

// Listing 1: Definition of Slot<> and Index<> classes

template <typename C, size_t CCH>
struct Slot
{
  Slot(Slot *next)
    : next(next)
  {}
  ~Slot()
  {
    delete next;
  }

  // Use the process heap because:
  // 
  // 1. Don't want to worry about thread-specificity, since 
  //   deallocation will occur in a different thread to allocation
  // 2. Don't want to worry about linkage to any specific CRT or
    //      other library
  // 3. Doesn't matter how fast it is
  // 4. Want it to be *highly* unlikely that allocation will fail, 
  //    which is indeed pretty unheard of when using the Win32 
    //    process heap.
  // 5. Want a C++-exception free solution, so use the Win32-system
    //    out-of-memory exception, and not have to worry about any 
    //    linkage pains.
  void *operator new(size_t cb)
  {
    return ::HeapAlloc(::GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, cb);
  }
  void operator delete(void *pv)
  {
    ::HeapFree(::GetProcessHeap(), 0, pv);
  }

  C     buff[CCH];
  Slot  *next;
};

template <typename C, size_t CCH>
struct Index
{
  typedef Slot<C, CCH>  Slot;

  Index()
    : m_index(::TlsAlloc())
  {
    // Use Win32 exception because:
    //
    // 1. Process cannot recover from this error in any 
    // meaningful way
    // 2. Do not want to couple to C++ exception-handling
    // and there is no graceful way to allow this to be
    // parameterisable. (May allow a pp-discriminated
    // mechanism in next version.)
    if(TLS_OUT_OF_INDEXES == m_index)
    {
      ::RaiseException(STATUS_NO_MEMORY, EXCEPTION_NONCONTINUABLE, 0, 0);
    }
  }

  ~Index()
  {
    // Walk the slot list and free. This can be as slow as 
    // you like, since performance is not important here
    delete m_top;

    // Now release the index
    ::TlsFree(m_index);
  }

  Slot *GetSlot()
  {
    // NOTE: This does not need to be thread-safe
    return reinterpret_cast<Slot*>(::TlsGetValue(m_index));
  }

  Slot *AllocSlot()
  {
    Slot  *next;

    { // Protect linked-list manipulation
      lock_scope<thread_mutex>  lock(m_mx);

      m_top = next = new Slot(m_top);
    }

    ::TlsSetValue(m_index, next);

    return next;
  }

private:
  ws_dword_t const  m_index;
  Slot              *m_top;
  thread_mutex      m_mx;
};


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.