⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arena.h

📁 自适应网格划分通用程序包
💻 H
字号:
// This software is copyright (C) by the Lawrence Berkeley
// National Laboratory.  Permission is granted to reproduce
// this software for non-commercial purposes provided that
// this notice is left intact.
// 
// It is acknowledged that the U.S. Government has rights to
// this software under Contract DE-AC03-765F00098 between
// the U.S.  Department of Energy and the University of
// California.
//
// This software is provided as a professional and academic
// contribution for joint exchange. Thus it is experimental,
// is provided ``as is'', with no warranties of any kind
// whatsoever, no support, no promise of updates, or printed
// documentation. By using this software, you acknowledge
// that the Lawrence Berkeley National Laboratory and
// Regents of the University of California shall have no
// liability with respect to the infringement of other
// copyrights by any part of this software.
//

#ifndef CH_ARENA
#define CH_ARENA

//
// $Id: Arena_8H-source.html,v 1.2 2005/05/13 21:35:14 dbs Exp $
//

#include <cstddef>
#include <list>

class Arena;
typedef std::list<Arena*> ArenaList;


class Arena
{
public:

  Arena();


  virtual ~Arena();

    virtual void* alloc (size_t sz) = 0;

    virtual void free (void* pt) = 0;

    static size_t align (size_t sz);
    typedef void (*FP) ();

#ifdef ENABLE_MEMORY_TRACKING  


  long int bytes;
  long int peak;
  static ArenaList* arenaList_;
  char name_[120];
#endif
protected:
    //
    // Types used by align().
    //

#ifndef DOXYGEN

    union Word
    {
        void*  p;
        double d;
        long   l;
        FP     f;
    };
#endif
};

//
// Inlines.
//

inline
size_t
Arena::align (size_t s)
{
    size_t x = s + sizeof(Word) - 1;
    x -= x%sizeof(Word);
    return x;
}



class BArena
    :
    public Arena
{
public:


  BArena(const char* name = "unnamed");
    virtual void* alloc (size_t sz);
    //
    virtual void free (void* pt);
};

#include <cstddef>

#include <set>
#include <vector>
using std::set;




class CArena
    :
    public Arena
{
public:

    CArena (size_t hunk_size = 0);
    //
    //
    virtual ~CArena ();
    //
    //
    virtual void* alloc (size_t nbytes);

    virtual void free (void* ap);

    void* calloc (size_t nmemb, size_t size);

    void* realloc (void* ptr, size_t size);
    //
    enum { DefaultHunkSize = 1024*1024 };

protected:
    //
    // The nodes in our free list and block list.
    //
#ifndef DOXYGEN
    class Node
    {
    public:
        //
        // The default constructor.
        //
        Node ()
            :
            m_block(0), m_size(0) {}
        //
        // Another constructor.
        //
        Node (void* block, size_t size)
            :
            m_block(block), m_size(size) {}
        //
        // The copy constructor.
        //
        Node (const Node& rhs)
            :
            m_block(rhs.m_block), m_size(rhs.m_size) {}
        //
        // The copy assignment constructor.
        //
        Node& operator= (const Node& rhs)
        {
            m_block = rhs.m_block;
            m_size  = rhs.m_size;
            return *this;
        }
        //
        // The "less-than" operator.
        //
        bool operator< (const Node& rhs) const
        {
            return m_block < rhs.m_block;
        }
        //
        // The equality operator. 
        //
        bool operator== (const Node& rhs) const
        {
            return m_block == rhs.m_block;
        }
        //
        // The block address.
        //
        void* block () const { return m_block; }
        //
        // Set block address.
        //
        void block (void* blk) { m_block = blk; }
        //
        // The size of the memory block.
        //
        size_t size () const { return m_size; }
        //
        // Set size.
        //
        void size (size_t sz) { m_size = sz; }

    private:
        //
        // The block of memory we reference.
        //
        void* m_block;
        //
        // The size of the block we represent.
        //
        size_t m_size;
    };

    //
    // The type of our freelist and blocklist.
    // We use a set sorted from lo to hi memory addresses.
    //
    typedef set < Node > NL;
    //
    // The list of blocks allocated via ::operator new().
    //
    std::vector<void*> m_alloc;
    //
    // The free list of allocated but not currently used blocks.
    // Maintained in lo to hi memory sorted order.
    //
    NL m_freelist;
    //
    // The list of busy blocks.
    // A block is either on the freelist or on the blocklist, but not on both.
    //
    NL m_busylist;
    //
    // The minimal size of hunks to request via ::operator new().
    //
    size_t m_hunk;

#endif

private:
    //
    // Disallowed.
    //
    CArena (const CArena& rhs);
    CArena& operator= (const CArena& rhs);
};

//
// The Arena used by BaseFab code.
//
extern Arena* The_FAB_Arena;


#endif /*CH_ARENA*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -