mman.cpp

来自「Shorthand是一个强大的脚本语言」· C++ 代码 · 共 104 行

CPP
104
字号
/////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/mman.cpp 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#include "mman.h"
#include "array.h"

#define Xmalloc ::malloc
#define Xrealloc ::realloc
#define Xfree ::free

memory::memory()
{
    m_total_memory = m_user_memory = 0;
    m_chunks = NULL;
}

disp_chunk_t* memory::new_chunk(void* ptr, int size, disposer_t disposer)
{
    disp_chunk_t* chunk = (disp_chunk_t*) Xmalloc(sizeof(disp_chunk_t));
    chunk->disposer = NULL;
    chunk->next = m_chunks;
    chunk->data = ptr;
    return chunk;
}


void* memory::malloc(int size)
{
    void* ptr = Xmalloc(size);
    new_chunk(ptr, size);
    return ptr;
}


char* memory::strdup(const char* s)
{
    if (s == NULL) s = "";
    int size = strlen(s)+1;
    char* ptr = (char*) malloc(size);
    new_chunk(ptr, size);
    memcpy(ptr, s, size);
    return ptr;
}

char* memory::printf(const char* format, ...)
{
    va_list args; va_start(args, format);
    int w = vawidth(format, &args) + 1;
    char* ptr = (char*) malloc(w);
    new_chunk(ptr, w);
    vsprintf(ptr, format, args);
    return ptr;
    
}

void memory::include(void* object, disposer_t disposer)
{
    new_chunk(object, 0, disposer);
}
    
void memory::purge()
{
    disp_chunk_t* chunk = m_chunks;
    while(chunk != NULL) {
        disp_chunk_t* tmp = chunk->next;
        if (chunk->disposer) chunk->disposer(chunk->data);
        else Xfree(chunk->data);
        Xfree(chunk);
        chunk = tmp;
    }
    m_total_memory = m_user_memory = 0;
}
 
memory::~memory()
{
    purge();
}

⌨️ 快捷键说明

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