C++库研究笔记——用C语言函数指针包装内存分配、释放

本文介绍了一个用于大型数据处理的内存管理方案,通过SuiteSparse库提供的内存分配和释放接口实现自定义内存管理,增强了程序的灵活性和性能。

源自库:SuiteSparse_config


源码来自:SuiteSparse_config 

/* Copyright (c) 2012, Timothy A. Davis.  No licensing restrictions
 * apply to this file or to the SuiteSparse_config directory.
 * Author: Timothy A. Davis.
 */

对于操作大的数据,手工控制内存分配很重要的,写个wrapper 有时很必要。


/* SuiteSparse-wide parameters will be placed in this struct. */

typedef struct SuiteSparse_config_struct
{
    void *(*malloc_memory) (size_t) ;           /* pointer to malloc */
    void *(*realloc_memory) (void *, size_t) ;  /* pointer to realloc */
    void (*free_memory) (void *) ;              /* pointer to free */
    void *(*calloc_memory) (size_t, size_t) ;   /* pointer to calloc */

} SuiteSparse_config ;

void *SuiteSparse_malloc    /* pointer to allocated block of memory */
(
    size_t nitems,          /* number of items to malloc (>=1 is enforced) */
    size_t size_of_item,    /* sizeof each item */
    int *ok,                /* TRUE if successful, FALSE otherwise */
    SuiteSparse_config *config        /* SuiteSparse-wide configuration */
) ;

void *SuiteSparse_free      /* always returns NULL */
(
    void *p,                /* block to free */
    SuiteSparse_config *config        /* SuiteSparse-wide configuration */
) ;


#include "SuiteSparse_config.h"

/* -------------------------------------------------------------------------- */
/* SuiteSparse_malloc: malloc wrapper */
/* -------------------------------------------------------------------------- */

void *SuiteSparse_malloc    /* pointer to allocated block of memory */
(
    size_t nitems,          /* number of items to malloc (>=1 is enforced) */
    size_t size_of_item,    /* sizeof each item */
    int *ok,                /* TRUE if successful, FALSE otherwise */
    SuiteSparse_config *config      /* SuiteSparse-wide configuration */
)
{
    void *p ;
    if (nitems < 1) nitems = 1 ;
    if (nitems * size_of_item != ((double) nitems) * size_of_item)
    {
        /* Int overflow */
        *ok = 0 ;
        return (NULL) ;
    }
    if (!config || config->malloc_memory == NULL)
    {
        /* use malloc by default */
        p = (void *) malloc (nitems * size_of_item) ;
    }
    else
    {
        /* use the pointer to malloc in the config */
        p = (void *) (config->malloc_memory) (nitems * size_of_item) ;
    }
    *ok = (p != NULL) ;
    return (p) ;
}


/* -------------------------------------------------------------------------- */
/* SuiteSparse_free: free wrapper */
/* -------------------------------------------------------------------------- */

void *SuiteSparse_free      /* always returns NULL */
(
    void *p,                /* block to free */
    SuiteSparse_config *config        /* SuiteSparse-wide configuration */
)
{
    if (p)
    {
        if (!config || config->free_memory == NULL)
        {
            /* use free by default */
            free (p) ;
        }
        else
        {
            /* use the pointer to free in the config */
            (config->free_memory) (p) ;
        }
    }
    return (NULL) ;
}






评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值