Commit 46fb9c29 authored by Bruce Momjian's avatar Bruce Momjian

Here is the patch with memory leak checker. This checker allow detect

in-chunk leaks, overwrite-next-chunk leaks and overwrite block-freeptr leaks.

A in-chunk leak --- if something overwrite space after wanted (via palloc()
size, but it is still inside chunk. For example

        x = palloc(12);         /* create 16b chunk */
        memset(x, '#', 13);

this leak is in the current source total invisible, because chunk is 16b and
leak is in the "align space".

 For this feature I add data_size to StandardChunk, and all memory which go
from AllocSetAlloc() is marked as 0x7F.

 The MemoryContextCheck() is compiled '#ifdef USE_ASSERT_CHECKING'.

I add this checking to 'tcop/postgres.c' and is active after each backend
query, but it is probably not sufficient, because some MemoryContext exist
only during memory processing --- will good if someone who known where
it is needful (Tom:-) add it for others contexts;
 A problem in the current source is that we have still some malloc()
allocation that is not needful and this allocation is total invisible for
all context routines. For example Dllist in backend (pretty dirty it is in
catcache where values in Dllist are palloc-ed, but list is malloc-ed).
--- and BTW. this Dllist design stand in the way for query cache :-)

 Tom, if you agree I start replace some mallocs.

 BTW. --- Tom, have you idea for across transaction presistent allocation for
          SQL functions? (like regex - now it is via malloc)


 I almost forget. I add one if() to AllocSetAlloc(), for 'size' that are
greater than ALLOC_BIGCHUNK_LIMIT is not needful check AllocSetFreeIndex(),
because 'fidx' is always 'ALLOCSET_NUM_FREELISTS - 1'. It a little brisk up
allocation for very large chunks. Right?

                                                Karel
parent 0d32cdc3
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.167 2000/07/08 03:04:15 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.168 2000/07/11 14:30:27 momjian Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
......@@ -1415,7 +1415,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
if (!IsUnderPostmaster)
{
puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.167 $ $Date: 2000/07/08 03:04:15 $\n");
puts("$Revision: 1.168 $ $Date: 2000/07/11 14:30:27 $\n");
}
/*
......@@ -1653,6 +1653,13 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
if (IsUnderPostmaster)
NullCommand(Remote);
}
#ifdef MEMORY_CONTEXT_CHECKING
/*
* Check all memory after each backend loop
*/
MemoryContextCheck(TopMemoryContext);
#endif
} /* infinite for-loop */
proc_exit(0); /* shouldn't get here... */
......
This diff is collapsed.
......@@ -14,7 +14,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.22 2000/06/28 03:32:50 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.23 2000/07/11 14:30:28 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -238,6 +238,27 @@ MemoryContextStats(MemoryContext context)
}
}
/*
* MemoryContextCheck
* Check all chunks in the named context.
*
* This is just a debugging utility, so it's not fancy.
*/
#ifdef MEMORY_CONTEXT_CHECKING
void
MemoryContextCheck(MemoryContext context)
{
MemoryContext child;
(*context->methods->check) (context);
for (child = context->firstchild; child != NULL; child = child->nextchild)
{
MemoryContextCheck(child);
}
}
#endif
/*
* MemoryContextContains
* Detect whether an allocated chunk of memory belongs to a given
......
......@@ -8,7 +8,7 @@
* or in config.h afterwards. Of course, if you edit config.h, then your
* changes will be overwritten the next time you run configure.
*
* $Id: config.h.in,v 1.124 2000/07/09 13:14:13 petere Exp $
* $Id: config.h.in,v 1.125 2000/07/11 14:30:30 momjian Exp $
*/
#ifndef CONFIG_H
......@@ -229,6 +229,12 @@
#define CLOBBER_FREED_MEMORY
#endif
/* Define this to check memory leaks
*/
#ifdef USE_ASSERT_CHECKING
#define MEMORY_CONTEXT_CHECKING
#endif
/* Define this to force all parse and plan trees to be passed through
* copyObject(), to facilitate catching errors and omissions in copyObject().
* XXX For 7.1 development, define this automatically if --enable-cassert.
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: memnodes.h,v 1.17 2000/06/28 03:33:15 tgl Exp $
* $Id: memnodes.h,v 1.18 2000/07/11 14:30:34 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -42,6 +42,9 @@ typedef struct MemoryContextMethods
void (*init) (MemoryContext context);
void (*reset) (MemoryContext context);
void (*delete) (MemoryContext context);
#ifdef MEMORY_CONTEXT_CHECKING
void (*check) (MemoryContext context);
#endif
void (*stats) (MemoryContext context);
} MemoryContextMethods;
......
......@@ -10,7 +10,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: memutils.h,v 1.36 2000/06/28 03:33:33 tgl Exp $
* $Id: memutils.h,v 1.37 2000/07/11 14:30:37 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -48,6 +48,9 @@ typedef struct StandardChunkHeader
{
MemoryContext context; /* owning context */
Size size; /* size of data space allocated in chunk */
#ifdef MEMORY_CONTEXT_CHECKING
Size data_size; /* real data size (without align) */
#endif
} StandardChunkHeader;
#define STANDARDCHUNKHEADERSIZE MAXALIGN(sizeof(StandardChunkHeader))
......@@ -78,6 +81,7 @@ extern void MemoryContextResetChildren(MemoryContext context);
extern void MemoryContextDeleteChildren(MemoryContext context);
extern void MemoryContextResetAndDeleteChildren(MemoryContext context);
extern void MemoryContextStats(MemoryContext context);
extern void MemoryContextCheck(MemoryContext context);
extern bool MemoryContextContains(MemoryContext context, void *pointer);
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment