static_new and static_delete Here's a trick for preallocating some memory for some type T. Do there exist similar tricks? Improvements?

#include &lt;cassert&gt;

#include &lt;cstdlib&gt;

#include &lt;atomic&gt;

#include &lt;limits&gt;

#include &lt;new&gt;

#include &lt;thread&gt;

#include &lt;utility&gt;

namespace
{
  template &lt;typename T, typename A = unsigned&gt;
  struct static_store
  {
    static constexpr auto const max_instances =
      ::std::numeric_limits&lt;unsigned char&gt;::digits * sizeof(A);

    static void cleanup() { delete [] store_; }

#ifdef __GNUC__
    template &lt;typename U&gt;
    static int ffz(U const v)
    {
      return __builtin_ctzll(~v);
    }
#elif _MSC_VER &amp;&amp; !__INTEL_COMPILER
    template &lt;typename U&gt;
    static int ffz(U const v)
    {
      return ::std::numeric_limits&lt;unsigned char&gt;::digits * sizeof(v) -
        __lzcnt64(v &amp; -v);
    }
#elif __INTEL_COMPILER
    template &lt;typename U&gt;
    static int ffz(U const v)
    {
      return _bit_scan_forward(~v);
    }
#else
    template &lt;typename U&gt;
    static int ffz(U v)
    {
      decltype(ffz()) b{};

      for (; (v &amp; 1); ++b)
      {
        v &gt;&gt;= 1;
      }

      return b;
    }
#endif

    static ::std::atomic_flag lock_;

    static A memory_map_;

    static typename ::std::aligned_storage&lt;sizeof(T),
      alignof(T)&gt;::type* store_;
  };

  template &lt;typename T, typename A&gt;
  ::std::atomic_flag static_store&lt;T, A&gt;::lock_;

  template &lt;typename T, typename A&gt;
  A static_store&lt;T, A&gt;::memory_map_;

  template &lt;typename T, typename A&gt;
  typename ::std::aligned_storage&lt;sizeof(T), alignof(T)&gt;::type*
    static_store&lt;T, A&gt;::store_{(::std::atexit(static_store&lt;T, A&gt;::cleanup),
      new typename ::std::aligned_storage&lt;sizeof(T),
        alignof(T)&gt;::type[static_store&lt;T, A&gt;::max_instances])};

  template &lt;typename T, typename ...A&gt;
  inline T* static_new(A&amp;&amp; ...args)
  {
    using static_store = static_store&lt;T&gt;;

    while (static_store::lock_.test_and_set(::std::memory_order_acquire))
    {
      ::std::this_thread::yield();
    }

    auto const i(static_store::ffz(static_store::memory_map_));

    auto p(new (&amp;static_store::store_[i]) T(::std::forward&lt;A&gt;(args)...));

    static_store::memory_map_ |= 1ull &lt;&lt; i;

    static_store::lock_.clear(::std::memory_order_release);

    return p;
  }

  template &lt;typename T&gt;
  inline void static_delete(T* const p)
  {
    using static_store = static_store&lt;T&gt;;

    auto const i(p - static_cast&lt;T*&gt;(static_cast&lt;void*&gt;(
      static_store::store_)));
    //assert(!as_const(static_store::memory_map_)[i]);

    while (static_store::lock_.test_and_set(::std::memory_order_acquire))
    {
      ::std::this_thread::yield();
    }

    static_store::memory_map_ &amp;= ~(1ull &lt;&lt; i);

    static_cast&lt;T*&gt;(static_cast&lt;void*&gt;(&amp;static_store::store_[i]))-&gt;~T();

    static_store::lock_.clear(::std::memory_order_release);
  }
}


Usage:

auto const p(static_new&lt;int&gt;());
*p = 10;
static_delete(p);


The code uses an integral type (A), as a bit mask, where each cleared bit means, that a corresponding slot in the store_ is not occupied, and a set bit means it is occupied. This is done to minimize the storage requirements for the allocation mechanism. To find a free slot, the ffz function (find first zero) is used: it is compiled into a single instruction on many platforms. The same thing could have been done with an array of bools, but with greatly increased storage requirements.
I still have trouble understanding how the whole works, but there are still some small things that could be improved:


In your comments, you say that A is an integer type used as a bitmask. The safest bitmask types are unsigned types or std::bitset. You should make this condition clear in your code, write

  template &lt;typename T, typename BitmaskType=unsigned&gt;
  struct static_store { /* ... */ };


You could even make it an error to use a nonconforming type and create a is_bitmask trait, which would allow you to create a meaningful error message:

static_assert(is_bitmask&lt;BitmaskType&gt;::value,
              "the BitmaskType requirements are not satisfied");

I got confused with these lines:

template &lt;typename T, typename ...A&gt;
inline T* static_new(A&amp;&amp; ...args)


It is once again a naming problem: I had in mind the template parameter A from static_store. Since it represents arguments to be forwarding, you should just use the common convetion and name it Args instead. I don't know whether there are written guidelines to name variadic parameter packs, but Args is probably the name that I saw the most often.
You could probably slightly hide details and reduce the visual burden by using a typedef for the type of store_:

using store_type = typename ::std::aligned_storage&lt;sizeof(T), alignof(T)&gt;::type;