mman.h

来自「Shorthand是一个强大的脚本语言」· C头文件 代码 · 共 87 行

H
87
字号
#ifndef __mman_h
#define __mman_h
/////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/mman.h 3     8/28/02 6:27a Arm $
//---------------------------------------------------------------------------
// This file is part of "libAndrix" library - a collection of classes
// and functions developed by Andrei Remenchuk.
//---------------------------------------------------------------------------
// While you may own complete copyright on the project with which you have
// received this file, the author reserves the right to use code contained
// in this very file for any purposes, including publishing and usage in
// any free or commercial software.
//
// You may re-distribute this file or re-use it in your own free or
// commercial software provided that this text is included in the file.
// If you change this file you must include clear notice stating that
// you changed this file and the date of change.
//
// This statement doesn't apply to other files that are part of the same
// package unless otherwise noted.
//---------------------------------------------------------------------------
// (c) 1998-2002 Andrei Remenchuk <andrei@remenchuk.com>
//---------------------------------------------------------------------------
// mman.h - memory manager.
/////////////////////////////////////////////////////////////////////////////


#include "utils.h"
#include "array.h"

#undef strdup

typedef void (*disposer_t)(void* ptr);

struct disp_chunk_t {
  disposer_t disposer;
  disp_chunk_t* next;
  void* data;
#ifdef _DEBUG
  const char* file;
  int line;
#endif
};

/**
 * memory class. provides scratch memory storage that can be 
 * freed on demand or when the object is destroyed.
 */
class memory
{
private:

    unsigned long m_total_memory;
    unsigned long m_user_memory;

    disp_chunk_t* m_chunks;
    disp_chunk_t* new_chunk(void* ptr, int size, disposer_t disposer = NULL);

public:
    memory();  

    /** Allocates a chunk of memory */
    void* malloc(int size);

    /** Similar to ::strdup(), but allocates memory from object storage */
    char* strdup(const char* s);

    /** Similar to ::sprintf(), but allocates memory from object storage */
    char* printf(const char* format, ...);

    /** Incluses pre-allocated object with custom deallocator */
    void  include(void* object, disposer_t disposer);
    
    unsigned long get_total_memory() const { return m_total_memory; }
    unsigned long user_memory() const { return m_user_memory; }
  
    /** Releases all memory chunks */
    void purge();
    
    ~memory();
};



#endif // __mman_h

⌨️ 快捷键说明

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