ELinks 0.18.0
memdebug.c File Reference

Memory debugging (leaks, overflows & co) More...

#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "util/error.h"
#include "util/lists.h"
#include "util/memdebug.h"
#include "util/memory.h"
Include dependency graph for memdebug.c:

Data Structures

struct  alloc_header
 

Macros

#define LESS_MEMORY_SPEED
 Eat less memory, but sacrifice speed? Default is defined.
 
#define FILL_ON_ALLOC
 Fill memory on alloc() ? Default is defined.
 
#define FILL_ON_ALLOC_VALUE   'X'
 
#define FILL_ON_REALLOC
 Fill memory on realloc() ? Default is defined.
 
#define FILL_ON_REALLOC_VALUE   'Y'
 
#define FILL_ON_FREE_VALUE   'Z'
 Fill memory before free() ? Default is undef.
 
#define CHECK_AH_SANITY
 Check alloc_header block sanity ? Default is defined.
 
#define AH_SANITY_MAGIC   0xD3BA110C
 
#define CHECK_DOUBLE_FREE
 Check for useless reallocation ? If oldsize is equal to newsize, print a message to stderr.
 
#define AH_FREE_MAGIC   0xD3BF110C
 
#define CHECK_XFLOWS
 Check for overflows and underflows ? Default is defined.
 
#define XFLOW_MAGIC   (char) 0xFA
 
#define SIZE_AH_ALIGNED   ((sizeof(struct alloc_header) + 7) & ~7)
 
#define XFLOW_INC   1
 
#define PTR_AH2BASE(ah)   (void *) ((char *) (ah) + SIZE_AH_ALIGNED)
 
#define PTR_BASE2AH(ptr)
 
#define SIZE_BASE2AH(size)   ((size) + SIZE_AH_ALIGNED + XFLOW_INC)
 
#define SIZE_AH2BASE(size)   ((size) - SIZE_AH_ALIGNED - XFLOW_INC)
 
#define PTR_OVERFLOW_MAGIC(ah)   ((char *) PTR_AH2BASE(ah) + (ah)->size)
 
#define PTR_UNDERFLOW_MAGIC(ah)   ((char *) PTR_AH2BASE(ah) - 1)
 
#define SET_OVERFLOW_MAGIC(ah)   (*PTR_OVERFLOW_MAGIC(ah) = XFLOW_MAGIC)
 
#define SET_UNDERFLOW_MAGIC(ah)   (*PTR_UNDERFLOW_MAGIC(ah) = XFLOW_MAGIC)
 
#define SET_XFLOW_MAGIC(ah)   SET_OVERFLOW_MAGIC(ah), SET_UNDERFLOW_MAGIC(ah)
 
#define dump_short_info(a, b, c, d)
 

Functions

static void dump_info (struct alloc_header *ah, const char *info, const char *file, int line, const char *type)
 
static int bad_ah_sanity (struct alloc_header *ah, const char *info, const char *file, int line)
 
static int bad_xflow_magic (struct alloc_header *ah, const char *info, const char *file, int line)
 
void check_memory_leaks (void)
 
static int patience (const char *file, int line, const char *of)
 
void * debug_mem_alloc (const char *file, int line, size_t size)
 
void * debug_mem_calloc (const char *file, int line, size_t eltcount, size_t eltsize)
 
void debug_mem_free (const char *file, int line, void *ptr)
 
void * debug_mem_realloc (const char *file, int line, void *ptr, size_t size)
 
void set_mem_comment (void *ptr, const char *str, int len)
 

Variables

struct mem_stats mem_stats
 
struct alloc_header list memory_list = { D_LIST_HEAD_EL(memory_list) }
 
static int alloc_try = 0
 

Detailed Description

Memory debugging (leaks, overflows & co)

Wrappers for libc memory managment providing protection against common pointers manipulation mistakes - bad realloc()/free() pointers, double free() problem, using uninitialized/freed memory, underflow/overflow protection, leaks tracking...

Copyright (C) 1999 - 2002 Mikulas Patocka Copyright (C) 2001 - 2004 Petr Baudis Copyright (C) 2002 - 2003 Laurent Monin

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA This file is covered by the General Public Licence v2.

Macro Definition Documentation

◆ AH_FREE_MAGIC

#define AH_FREE_MAGIC   0xD3BF110C

◆ AH_SANITY_MAGIC

#define AH_SANITY_MAGIC   0xD3BA110C

◆ CHECK_AH_SANITY

#define CHECK_AH_SANITY

Check alloc_header block sanity ? Default is defined.

◆ CHECK_DOUBLE_FREE

#define CHECK_DOUBLE_FREE

Check for useless reallocation ? If oldsize is equal to newsize, print a message to stderr.

It may help to find inefficient code. Default is undefined. Check for validity of address passed to free() ? Note that this is VERY slow, as we iterate through whole memory_list each time. We can't check magics etc, as it would break double free() check. Default is undef. Check for double free ? Default is defined.

◆ CHECK_XFLOWS

#define CHECK_XFLOWS

Check for overflows and underflows ? Default is defined.

◆ dump_short_info

#define dump_short_info ( a,
b,
c,
d )

◆ FILL_ON_ALLOC

#define FILL_ON_ALLOC

Fill memory on alloc() ? Default is defined.

◆ FILL_ON_ALLOC_VALUE

#define FILL_ON_ALLOC_VALUE   'X'

◆ FILL_ON_FREE_VALUE

#define FILL_ON_FREE_VALUE   'Z'

Fill memory before free() ? Default is undef.

◆ FILL_ON_REALLOC

#define FILL_ON_REALLOC

Fill memory on realloc() ? Default is defined.

◆ FILL_ON_REALLOC_VALUE

#define FILL_ON_REALLOC_VALUE   'Y'

◆ LESS_MEMORY_SPEED

#define LESS_MEMORY_SPEED

Eat less memory, but sacrifice speed? Default is defined.

◆ PTR_AH2BASE

#define PTR_AH2BASE ( ah)    (void *) ((char *) (ah) + SIZE_AH_ALIGNED)

◆ PTR_BASE2AH

#define PTR_BASE2AH ( ptr)
Value:
(struct alloc_header *) \
((char *) (ptr) - SIZE_AH_ALIGNED)
#define SIZE_AH_ALIGNED
Definition memdebug.c:135
Definition memdebug.c:110

◆ PTR_OVERFLOW_MAGIC

#define PTR_OVERFLOW_MAGIC ( ah)    ((char *) PTR_AH2BASE(ah) + (ah)->size)

◆ PTR_UNDERFLOW_MAGIC

#define PTR_UNDERFLOW_MAGIC ( ah)    ((char *) PTR_AH2BASE(ah) - 1)

◆ SET_OVERFLOW_MAGIC

#define SET_OVERFLOW_MAGIC ( ah)    (*PTR_OVERFLOW_MAGIC(ah) = XFLOW_MAGIC)

◆ SET_UNDERFLOW_MAGIC

#define SET_UNDERFLOW_MAGIC ( ah)    (*PTR_UNDERFLOW_MAGIC(ah) = XFLOW_MAGIC)

◆ SET_XFLOW_MAGIC

#define SET_XFLOW_MAGIC ( ah)    SET_OVERFLOW_MAGIC(ah), SET_UNDERFLOW_MAGIC(ah)

◆ SIZE_AH2BASE

#define SIZE_AH2BASE ( size)    ((size) - SIZE_AH_ALIGNED - XFLOW_INC)

◆ SIZE_AH_ALIGNED

#define SIZE_AH_ALIGNED   ((sizeof(struct alloc_header) + 7) & ~7)

◆ SIZE_BASE2AH

#define SIZE_BASE2AH ( size)    ((size) + SIZE_AH_ALIGNED + XFLOW_INC)

◆ XFLOW_INC

#define XFLOW_INC   1

◆ XFLOW_MAGIC

#define XFLOW_MAGIC   (char) 0xFA

Function Documentation

◆ bad_ah_sanity()

static int bad_ah_sanity ( struct alloc_header * ah,
const char * info,
const char * file,
int line )
inlinestatic

◆ bad_xflow_magic()

static int bad_xflow_magic ( struct alloc_header * ah,
const char * info,
const char * file,
int line )
inlinestatic

◆ check_memory_leaks()

void check_memory_leaks ( void )

◆ debug_mem_alloc()

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

◆ debug_mem_calloc()

void * debug_mem_calloc ( const char * file,
int line,
size_t eltcount,
size_t eltsize )

◆ debug_mem_free()

void debug_mem_free ( const char * file,
int line,
void * ptr )

◆ debug_mem_realloc()

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

◆ dump_info()

static void dump_info ( struct alloc_header * ah,
const char * info,
const char * file,
int line,
const char * type )
static

◆ patience()

static int patience ( const char * file,
int line,
const char * of )
static

◆ set_mem_comment()

void set_mem_comment ( void * ptr,
const char * str,
int len )

Variable Documentation

◆ alloc_try

int alloc_try = 0
static

◆ mem_stats

struct mem_stats mem_stats

◆ memory_list

struct alloc_header list memory_list = { D_LIST_HEAD_EL(memory_list) }