Program Listing for File blockAllocator.h

Return to documentation for file (core/include/utils/blockAllocator/blockAllocator.h)

//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2022, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================

// See
// http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Performance-

#ifndef __ALLOCATOR_H
#define __ALLOCATOR_H

#include <stddef.h>
#include <cstdlib>

#include "utils/inttypes.h"

class Allocator {
public:
    Allocator(size_t size, usint objects = 0, char* memory = nullptr, const char* name = nullptr);  // NOLINT

    ~Allocator();

    void* Allocate(size_t size);

    void Deallocate(void* pBlock);

    const char* GetName() {
        return m_name;
    }

    size_t GetBlockSize() {
        return m_blockSize;
    }

    usint GetBlockCount() {
        return m_blockCnt;
    }

    usint GetBlocksInUse() {
        return m_blocksInUse;
    }

    usint GetAllocations() {
        return m_allocations;
    }

    usint GetDeallocations() {
        return m_deallocations;
    }

private:
    void Push(void* pMemory);

    void* Pop();

    struct Block {
        Block* pNext;
    };

    enum AllocatorMode { HEAP_BLOCKS, HEAP_POOL, STATIC_POOL };

    const size_t m_blockSize;
    const size_t m_objectSize;
    const usint m_maxObjects;
    AllocatorMode m_allocatorMode;
    Block* m_pHead;
    char* m_pPool;
    usint m_poolIndex;
    usint m_blockCnt;
    usint m_blocksInUse;
    usint m_allocations;
    usint m_deallocations;
    const char* m_name;
};

// Template class to create external memory pool
template <class T, usint Objects>
class AllocatorPool : public Allocator {
public:
    AllocatorPool() : Allocator(sizeof(T), Objects, m_memory) {}

private:
    char m_memory[sizeof(T) * Objects];
};

// macro to provide header file interface
#define DECLARE_ALLOCATOR                                   \
public:                                                     \
    void* operator new(size_t size) {                       \
        OPENFHE_DEBUG_FLAG(false);                          \
        OPENFHE_DEBUG("allocating   " << size << " bytes"); \
        return _allocator.Allocate(size);                   \
    }                                                       \
    void operator delete(void* pObject) {                   \
        OPENFHE_DEBUG_FLAG(false);                          \
        OPENFHE_DEBUG("deallocating  ");                    \
        _allocator.Deallocate(pObject);                     \
    }                                                       \
                                                            \
private:                                                    \
    static Allocator _allocator;

// macro to provide source file interface
#define IMPLEMENT_ALLOCATOR(class, objects, memory) \
    Allocator class ::_allocator(sizeof(class), objects, memory, #class);

#define IMPLEMENT_BALLOCATOR(class, blocksize, objects, memory) \
    Allocator class ::_allocator(blocksize, objects, memory, #class);

#endif