源自库: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) ;
}