YAZ  5.34.0
Macros | Functions | Variables
xmalloc.c File Reference

Implements malloc interface. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <yaz/log.h>
#include <yaz/xmalloc.h>

Go to the source code of this file.

Macros

#define TRACE_XMALLOC   1
 
#define MALLOC_SIZE_MAX   ((size_t)(-1) / 4)
 
#define xrealloc_d(o, x, f, l)   realloc(o, x)
 
#define xmalloc_d(x, f, l)   malloc(x)
 
#define xcalloc_d(x, y, f, l)   calloc(x,y)
 
#define xfree_d(x, f, l)   free(x)
 
#define xmalloc_trav_d(f, l)
 

Functions

void xmalloc_trav_f (const char *s, const char *file, int line)
 logs all xmalloc buffers More...
 
void xmalloc_fatal (size_t size)
 
void * xrealloc_f (void *o, size_t size, const char *file, int line)
 realloc More...
 
void * xmalloc_f (size_t size, const char *file, int line)
 malloc More...
 
void * xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
 calloc More...
 
char * xstrdup_f (const char *s, const char *file, int line)
 strdup More...
 
void xfree_f (void *p, const char *file, int line)
 free More...
 
char * xstrndup_f (const char *s, size_t n, const char *file, int line)
 strndup More...
 

Variables

static int log_level = 0
 
static int log_level_initialized = 0
 

Detailed Description

Implements malloc interface.

Definition in file xmalloc.c.

Macro Definition Documentation

◆ MALLOC_SIZE_MAX

#define MALLOC_SIZE_MAX   ((size_t)(-1) / 4)

Definition at line 27 of file xmalloc.c.

◆ TRACE_XMALLOC

#define TRACE_XMALLOC   1

Definition at line 23 of file xmalloc.c.

◆ xcalloc_d

#define xcalloc_d (   x,
  y,
  f,
 
)    calloc(x,y)

Definition at line 235 of file xmalloc.c.

◆ xfree_d

#define xfree_d (   x,
  f,
 
)    free(x)

Definition at line 236 of file xmalloc.c.

◆ xmalloc_d

#define xmalloc_d (   x,
  f,
 
)    malloc(x)

Definition at line 234 of file xmalloc.c.

◆ xmalloc_trav_d

#define xmalloc_trav_d (   f,
 
)

Definition at line 237 of file xmalloc.c.

◆ xrealloc_d

#define xrealloc_d (   o,
  x,
  f,
 
)    realloc(o, x)

Definition at line 233 of file xmalloc.c.

Function Documentation

◆ xcalloc_f()

void* xcalloc_f ( size_t  nmemb,
size_t  size,
const char *  file,
int  line 
)

calloc

Parameters
nmembnumber of members
sizesize of member
filefname location of use
lineline location of use
Returns
buffer

This function is invoked via macro xcalloc in which file and line are set automatically.

Definition at line 302 of file xmalloc.c.

References log_level, log_level_initialized, xcalloc_d, xmalloc_fatal(), yaz_log(), yaz_log_module_level(), and YLOG_FATAL.

◆ xfree_f()

void xfree_f ( void *  p,
const char *  file,
int  line 
)

free

Parameters
pstring to be freed (might be NULL)
filefname location of use
lineline location of use

This function is invoked via macro xfree in which file and line are set automatically.

Definition at line 341 of file xmalloc.c.

References log_level, xfree_d, and yaz_log().

◆ xmalloc_f()

void* xmalloc_f ( size_t  size,
const char *  file,
int  line 
)

malloc

Parameters
sizesize of buffer to be allocated
filefname location of use
lineline location of use
Returns
buffer

This function is invoked via macro xmalloc in which file and line are set automatically.

Definition at line 279 of file xmalloc.c.

References log_level, log_level_initialized, xmalloc_d, xmalloc_fatal(), yaz_log(), yaz_log_module_level(), and YLOG_FATAL.

Referenced by xstrndup_f().

◆ xmalloc_fatal()

void xmalloc_fatal ( size_t  size)

Definition at line 251 of file xmalloc.c.

References MALLOC_SIZE_MAX.

Referenced by xcalloc_f(), xmalloc_f(), and xrealloc_f().

◆ xmalloc_trav_f()

void xmalloc_trav_f ( const char *  s,
const char *  file,
int  line 
)

logs all xmalloc buffers

Parameters
snot used
filefname location of use
lineline location of use

This function is invoked via macro xmalloc_trav in which file and line are set automatically. Only if TRACE_XMALLOC > 1 this function does any real work!

Definition at line 240 of file xmalloc.c.

References log_level, log_level_initialized, xmalloc_trav_d, and yaz_log_module_level().

◆ xrealloc_f()

void* xrealloc_f ( void *  o,
size_t  size,
const char *  file,
int  line 
)

realloc

Parameters
obuffer to be reallocated
sizesize of buffer to be allocated
filefname location of use
lineline location of use
Returns
buffer

This function is invoked via macro xrealloc in which file and line are set automatically.

Definition at line 257 of file xmalloc.c.

References log_level, log_level_initialized, xmalloc_fatal(), xrealloc_d, yaz_log(), yaz_log_module_level(), and YLOG_FATAL.

◆ xstrdup_f()

char* xstrdup_f ( const char *  p,
const char *  file,
int  line 
)

strdup

Parameters
pstring to be cloned
filefname location of use
lineline location of use
Returns
resulting string

This function is invoked via macro xstrdup in which file and line are set automatically.

Definition at line 324 of file xmalloc.c.

References log_level, log_level_initialized, xmalloc_d, yaz_log(), and yaz_log_module_level().

Referenced by xstrndup_f().

◆ xstrndup_f()

char* xstrndup_f ( const char *  p,
size_t  n,
const char *  file,
int  line 
)

strndup

Parameters
pstring to be cloned
nmax size of resulting string (excluding 0)
filefname location of use
lineline location of use
Returns
resulting string

This function is invoked via macro xstrndup in which file and line are set automatically.

Definition at line 350 of file xmalloc.c.

References xmalloc_f(), and xstrdup_f().

Variable Documentation

◆ log_level

int log_level = 0
static

Definition at line 29 of file xmalloc.c.

Referenced by xcalloc_f(), xfree_f(), xmalloc_f(), xmalloc_trav_f(), xrealloc_f(), and xstrdup_f().

◆ log_level_initialized

int log_level_initialized = 0
static

Definition at line 30 of file xmalloc.c.

Referenced by xcalloc_f(), xmalloc_f(), xmalloc_trav_f(), xrealloc_f(), and xstrdup_f().