Beispiel #1
0
 // CLASS METHODS
 void my_Singleton::initSingleton(const char       *id,
                                  bslma::Allocator *basicAllocator)
 {
     static bsls::AlignedBuffer<sizeof(my_Singleton)> singleton;
     d_singleton_p = new (singleton.buffer()) my_Singleton(id,
                                                           basicAllocator);
 }
Beispiel #2
0
enum { VERBOSE_ARG_NUM = 2, VERY_VERBOSE_ARG_NUM, VERY_VERY_VERBOSE_ARG_NUM };

//=============================================================================
//                              USAGE EXAMPLES
//-----------------------------------------------------------------------------

// The 'allocateFromBuffer' function below uses an aligned buffer as a small
// heap from which objects can be allocated.  We choose 'int' alignment
// (4-byte alignment) for our buffer because the objects we are allocating
// are composed of 'char', 'short', and 'int' values only.  If no alignment
// were specified, the buffer would be maximally aligned, which could be
// wasteful on some platforms.
//..
    const int MY_ALIGNMENT = bsls::AlignmentFromType<int>::VALUE;
    bsls::AlignedBuffer<1000, MY_ALIGNMENT> my_AllocBuffer;
    const char* my_AllocEnd = my_AllocBuffer.buffer() + 1000;
    char *my_AllocPtr = my_AllocBuffer.buffer();
        // Invariant: my_AllocPtr is always aligned on a multiple of 4 bytes

    static void *allocateFromBuffer(int size)
    {
        if (size > my_AllocEnd - my_AllocPtr)
            return 0;       // Out of buffer space                    // RETURN

        void *result = my_AllocPtr;
        my_AllocPtr += size;
        if (size % MY_ALIGNMENT) {
            // re-establish invariant by re-aligning my_AllocPtr
            my_AllocPtr += MY_ALIGNMENT - size % MY_ALIGNMENT;
        }