2007-04-08 10:36:36 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* File: zmalloc.c Part of tbaMUD *
|
|
|
|
* Usage: A simple memory allocation monitor. *
|
|
|
|
* *
|
2007-09-26 19:40:22 +00:00
|
|
|
* Version 2. Copyright 1996, 1998, 1999, 2000 Eric Murray ericm@lne.com *
|
2007-04-08 10:36:36 +00:00
|
|
|
**************************************************************************/
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-26 19:40:22 +00:00
|
|
|
/* Usage: To run tbaMUD in debug mode change the flags line in your Makefile
|
|
|
|
* as below, make clean, and reboot.
|
|
|
|
*
|
|
|
|
* Makefile: # Any special flags you want to pass to the compiler
|
|
|
|
* Makefile: MYFLAGS = -Wall -DMEMORY_DEBUG */
|
2006-12-19 22:56:18 +00:00
|
|
|
|
|
|
|
/* protect our calloc() and free() calls from recursive redefinition: */
|
|
|
|
#define ZMALLOC_H
|
|
|
|
|
2013-08-24 17:56:48 -04:00
|
|
|
#include "conf.h"
|
2006-12-19 22:56:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-09-13 15:00:59 +00:00
|
|
|
#include <ctype.h>
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-26 19:40:22 +00:00
|
|
|
#define NUM_ZBUCKETS 256
|
2008-05-07 23:34:40 +00:00
|
|
|
#define GET_ZBUCKET(addr) (((long)(addr) >> 3) & 0xFF)
|
2007-09-26 19:40:22 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
//#define NO_MEMORY_PADDING
|
2006-12-19 22:56:18 +00:00
|
|
|
|
|
|
|
#ifndef NO_MEMORY_PADDING
|
|
|
|
static unsigned char beginPad[4] = {
|
|
|
|
0xde, 0xad, 0xde, 0xad };
|
|
|
|
|
|
|
|
static unsigned char endPad[4] = {
|
|
|
|
0xde, 0xad, 0xde, 0xad };
|
|
|
|
#endif
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
FILE *zfd = NULL;
|
2006-12-19 22:56:18 +00:00
|
|
|
|
|
|
|
typedef struct meminfo {
|
|
|
|
struct meminfo *next;
|
|
|
|
int size; /* number of bytes malloced */
|
2007-09-13 15:00:59 +00:00
|
|
|
unsigned char *addr; /* address of memory returned */
|
2006-12-19 22:56:18 +00:00
|
|
|
int frees; /* number of times that 'free' was called on this memory */
|
|
|
|
char *file; /* file where malloc was called */
|
|
|
|
int line; /* line in the code where malloc was called */
|
|
|
|
} meminfo;
|
|
|
|
|
2007-09-26 19:40:22 +00:00
|
|
|
static meminfo *memlist[NUM_ZBUCKETS];
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 0 = only end summary
|
|
|
|
* 1 = show errors
|
|
|
|
* 2 = errors with dumps
|
|
|
|
* 3 = all of the above plus all mallocs/frees
|
|
|
|
*/
|
|
|
|
int zmalloclogging = 2;
|
2006-12-19 22:56:18 +00:00
|
|
|
|
|
|
|
/* functions: */
|
2007-09-13 15:00:59 +00:00
|
|
|
unsigned char *zmalloc(int len, char *file, int line);
|
|
|
|
unsigned char *zrealloc(unsigned char *what, int len, char *file, int line);
|
|
|
|
void zdump(meminfo *m);
|
|
|
|
void zfree(unsigned char *what, char *file, int line);
|
2006-12-19 22:56:18 +00:00
|
|
|
char *zstrdup(const char *src, char *file, int line);
|
2007-09-13 15:00:59 +00:00
|
|
|
void zmalloc_init(void);
|
|
|
|
void zmalloc_check(void);
|
2006-12-19 22:56:18 +00:00
|
|
|
void pad_check(meminfo *m);
|
|
|
|
void zmalloc_free_list(meminfo *m);
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
|
2007-09-26 19:40:22 +00:00
|
|
|
void zmalloc_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ZBUCKETS; i++)
|
|
|
|
memlist[i] = NULL;
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
zfd = fopen("zmalloc.log","w+");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void zdump(meminfo *m)
|
2006-12-19 22:56:18 +00:00
|
|
|
{
|
2007-09-13 15:00:59 +00:00
|
|
|
#define MAX_ZDUMP_SIZE 32
|
|
|
|
const unsigned char *hextab = (unsigned char *)"0123456789ABCDEF";
|
|
|
|
unsigned char hexline[37], ascline[17], *hexp, *ascp, *inp;
|
|
|
|
int len, c = 1;
|
|
|
|
|
|
|
|
if (m->addr == NULL || m->size <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hexp = hexline;
|
|
|
|
ascp = ascline;
|
|
|
|
inp = m->addr;
|
|
|
|
len = (m->size > MAX_ZDUMP_SIZE ? MAX_ZDUMP_SIZE : m->size);
|
|
|
|
|
|
|
|
for ( ; len > 0; len--, inp++, c++) {
|
|
|
|
*(hexp++) = hextab[(int) (*inp & 0xF0) >> 4]; /* high 4 bit */
|
|
|
|
*(hexp++) = hextab[(int) (*inp & 0x0F)]; /* low 4 bit */
|
|
|
|
if (c % 4 == 0) *(hexp++) = ' ';
|
|
|
|
*(ascp++) = isprint(*inp) ? *inp : '.';
|
|
|
|
if (c % 16 == 0 || len <= 1) {
|
|
|
|
*hexp = '\0';
|
|
|
|
*ascp = '\0';
|
|
|
|
fprintf(zfd, " %-40.40s%s\n", hexline, ascline);
|
|
|
|
hexp = hexline;
|
|
|
|
ascp = ascline;
|
|
|
|
}
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
2007-09-13 15:00:59 +00:00
|
|
|
fprintf(zfd, "\n");
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
unsigned char *zmalloc(int len, char *file, int line)
|
2006-12-19 22:56:18 +00:00
|
|
|
{
|
2007-09-13 15:00:59 +00:00
|
|
|
unsigned char *ret;
|
2006-12-19 22:56:18 +00:00
|
|
|
meminfo *m;
|
|
|
|
|
|
|
|
#ifndef NO_MEMORY_PADDING
|
2007-09-13 15:00:59 +00:00
|
|
|
ret = (unsigned char *) calloc(1, len + sizeof(beginPad) + sizeof(endPad));
|
2006-12-19 22:56:18 +00:00
|
|
|
#else
|
2007-09-13 15:00:59 +00:00
|
|
|
ret = (unsigned char *) calloc(1, len);
|
2006-12-19 22:56:18 +00:00
|
|
|
#endif
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
if (!ret) {
|
2006-12-19 22:56:18 +00:00
|
|
|
fprintf(zfd,"zmalloc: malloc FAILED");
|
2007-09-13 15:00:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-12-19 22:56:18 +00:00
|
|
|
#ifndef NO_MEMORY_PADDING
|
|
|
|
/* insert begin and end padding to detect buffer under/overruns: */
|
2007-09-13 15:00:59 +00:00
|
|
|
memcpy(ret, beginPad, sizeof(beginPad));
|
2006-12-19 22:56:18 +00:00
|
|
|
ret += sizeof(beginPad); /* make ret skip begin pad */
|
2007-09-13 15:00:59 +00:00
|
|
|
memcpy(ret + len, endPad, sizeof(endPad));
|
2006-12-19 22:56:18 +00:00
|
|
|
#endif
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
if (zmalloclogging > 2)
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zmalloc: 0x%p %d bytes %s:%d\n", ret, len, file, line);
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
m = (meminfo *) calloc(1, sizeof(meminfo));
|
2006-12-19 22:56:18 +00:00
|
|
|
if (!m) {
|
|
|
|
fprintf(zfd,"zmalloc: FAILED mem alloc for zmalloc struct... bailing!\n");
|
2007-09-13 15:00:59 +00:00
|
|
|
return NULL;
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
2007-09-13 15:00:59 +00:00
|
|
|
m->addr = ret;
|
|
|
|
m->size = len;
|
2006-12-19 22:56:18 +00:00
|
|
|
m->frees = 0;
|
2007-09-13 15:00:59 +00:00
|
|
|
m->file = strdup(file);
|
2006-12-19 22:56:18 +00:00
|
|
|
if (!m->file) {
|
|
|
|
fprintf(zfd,"zmalloc: FAILED mem alloc for zmalloc struct... bailing!\n");
|
2007-09-13 15:00:59 +00:00
|
|
|
free(m);
|
|
|
|
return NULL;
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
|
|
|
m->line = line;
|
2007-09-26 19:40:22 +00:00
|
|
|
m->next = memlist[GET_ZBUCKET(ret)];
|
|
|
|
memlist[GET_ZBUCKET(ret)] = m;
|
2007-09-13 15:00:59 +00:00
|
|
|
return (ret);
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
unsigned char *zrealloc(unsigned char *what, int len, char *file, int line)
|
2006-12-19 22:56:18 +00:00
|
|
|
{
|
2007-09-13 15:00:59 +00:00
|
|
|
unsigned char *ret;
|
2007-09-26 19:40:22 +00:00
|
|
|
meminfo *m, *prev_m;
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
if (what) {
|
2007-09-26 19:40:22 +00:00
|
|
|
for (prev_m = NULL, m = memlist[GET_ZBUCKET(what)]; m; prev_m = m, m = m->next) {
|
2007-09-13 15:00:59 +00:00
|
|
|
if (m->addr == what) {
|
|
|
|
#ifndef NO_MEMORY_PADDING
|
|
|
|
ret = (unsigned char *) realloc(what - sizeof(beginPad), len + sizeof(beginPad) + sizeof(endPad));
|
|
|
|
#else
|
|
|
|
ret = (unsigned char *) realloc(what, len);
|
|
|
|
#endif
|
|
|
|
if (!ret) {
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zrealloc: FAILED for 0x%p %d bytes mallocd at %s:%d,\n"
|
2007-09-13 15:00:59 +00:00
|
|
|
" %d bytes reallocd at %s:%d.\n",
|
2008-05-07 23:34:40 +00:00
|
|
|
m->addr, m->size, m->file, m->line, len, file, line);
|
2007-09-13 15:00:59 +00:00
|
|
|
if (zmalloclogging > 1) zdump(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#ifndef NO_MEMORY_PADDING
|
|
|
|
/* insert begin and end padding to detect buffer under/overruns: */
|
|
|
|
memcpy(ret, beginPad, sizeof(beginPad));
|
|
|
|
ret += sizeof(beginPad); /* make ret skip begin pad */
|
|
|
|
memcpy(ret + len, endPad, sizeof(endPad));
|
|
|
|
#endif
|
|
|
|
if (zmalloclogging > 2)
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zrealloc: 0x%p %d bytes mallocd at %s:%d, %d bytes reallocd at %s:%d.\n",
|
|
|
|
m->addr, m->size, m->file, m->line, len, file, line);
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
m->addr = ret;
|
|
|
|
m->size = len;
|
|
|
|
if (m->file) free(m->file);
|
|
|
|
m->file = strdup(file);
|
|
|
|
m->line = line;
|
2007-09-26 19:40:22 +00:00
|
|
|
|
|
|
|
/* detach node */
|
|
|
|
if (prev_m)
|
|
|
|
prev_m->next = m->next;
|
|
|
|
else
|
|
|
|
memlist[GET_ZBUCKET(what)] = m->next;
|
|
|
|
|
|
|
|
/* readd to proper bucket */
|
|
|
|
m->next = memlist[GET_ZBUCKET(ret)];
|
|
|
|
memlist[GET_ZBUCKET(ret)] = m;
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
/* could continue the loop to check for multiply-allocd memory */
|
|
|
|
/* but that's highly improbable so lets just return instead. */
|
|
|
|
return (ret);
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
/* NULL or invalid pointer given */
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zrealloc: invalid pointer 0x%p, %d bytes to realloc at %s:%d.\n",
|
|
|
|
what, len, file, line);
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
return (zmalloc(len, file, line));
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
/* doesn't actually free memory */
|
|
|
|
void zfree(unsigned char *what, char *file, int line)
|
2006-12-19 22:56:18 +00:00
|
|
|
{
|
2007-09-13 15:00:59 +00:00
|
|
|
meminfo *m;
|
2006-12-19 22:56:18 +00:00
|
|
|
int gotit = 0;
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
if (!what) {
|
|
|
|
fprintf(zfd,"zfree: ERR: Null pointer free'd: %s:%d.\n", file, line);
|
2006-12-19 22:56:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
/* look up allocated mem in list: */
|
2007-09-26 19:40:22 +00:00
|
|
|
for (m = memlist[GET_ZBUCKET(what)]; m; m = m->next) {
|
2007-09-13 15:00:59 +00:00
|
|
|
if (m->addr == what) {
|
|
|
|
/* got it. Print it if verbose: */
|
|
|
|
if (zmalloclogging > 2) {
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zfree: Freed 0x%p %d bytes mallocd at %s:%d, freed at %s:%d\n",
|
|
|
|
m->addr, m->size, m->file, m->line, file, line);
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
2007-09-13 15:00:59 +00:00
|
|
|
/* check the padding: */
|
|
|
|
pad_check(m);
|
|
|
|
|
|
|
|
/* note that we freed the memory */
|
|
|
|
m->frees++;
|
|
|
|
|
|
|
|
/* check to see if it was freed > once */
|
|
|
|
if (m->frees > 1) {
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zfree: ERR: multiple frees! 0x%p %d bytes\n"
|
2007-09-13 15:00:59 +00:00
|
|
|
" mallocd at %s:%d, freed at %s:%d.\n",
|
2008-05-07 23:34:40 +00:00
|
|
|
m->addr, m->size, m->file, m->line, file, line);
|
2007-09-13 15:00:59 +00:00
|
|
|
if (zmalloclogging > 1) zdump(m);
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
2007-09-13 15:00:59 +00:00
|
|
|
gotit++;
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
2007-09-13 15:00:59 +00:00
|
|
|
} /* for.. */
|
|
|
|
|
|
|
|
if (!gotit) {
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zfree: ERR: attempt to free unallocated memory 0x%p at %s:%d.\n",
|
|
|
|
what, file, line);
|
2007-09-13 15:00:59 +00:00
|
|
|
}
|
|
|
|
if (gotit > 1) {
|
|
|
|
/* this shouldn't happen, eh? */
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zfree: ERR: Multiply-allocd memory 0x%p.\n", what);
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
|
2006-12-19 22:56:18 +00:00
|
|
|
char *zstrdup(const char *src, char *file, int line)
|
|
|
|
{
|
2007-09-13 15:00:59 +00:00
|
|
|
char *result;
|
|
|
|
#ifndef NO_MEMORY_STRDUP
|
|
|
|
result = (char*)zmalloc(strlen(src) + 1, file, line);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
strcpy(result, src);
|
|
|
|
return result;
|
2006-12-19 22:56:18 +00:00
|
|
|
#else
|
2007-09-13 15:00:59 +00:00
|
|
|
result = (char*)malloc(strlen(src) + 1);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
strcpy(result, src); /* strcpy ok, size checked above */
|
|
|
|
return result;
|
2006-12-19 22:56:18 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
|
2006-12-19 22:56:18 +00:00
|
|
|
void zmalloc_check()
|
|
|
|
{
|
2007-09-26 19:40:22 +00:00
|
|
|
meminfo *m, *next_m;
|
2006-12-19 22:56:18 +00:00
|
|
|
char *admonishemnt;
|
2007-09-26 19:40:22 +00:00
|
|
|
int total_leak = 0, num_leaks = 0, i;
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
fprintf(zfd, "\n------------ Checking leaks ------------\n\n");
|
2007-09-26 19:40:22 +00:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_ZBUCKETS; i++) {
|
|
|
|
for (m = memlist[i]; m; m = next_m) {
|
|
|
|
next_m = m->next;
|
|
|
|
if (m->addr != 0 && m->frees <= 0) {
|
2008-05-07 23:34:40 +00:00
|
|
|
fprintf(zfd,"zmalloc: UNfreed memory 0x%p %d bytes mallocd at %s:%d\n",
|
|
|
|
m->addr, m->size, m->file, m->line);
|
2007-09-26 19:40:22 +00:00
|
|
|
if (zmalloclogging > 1) zdump(m);
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-26 19:40:22 +00:00
|
|
|
/* check padding on un-freed memory too: */
|
|
|
|
pad_check(m);
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-26 19:40:22 +00:00
|
|
|
total_leak += m->size;
|
|
|
|
num_leaks++;
|
|
|
|
}
|
|
|
|
#ifndef NO_MEMORY_PADDING
|
|
|
|
if (m->addr) free(m->addr - sizeof(beginPad));
|
|
|
|
#else
|
|
|
|
if (m->addr) free(m->addr);
|
|
|
|
#endif
|
|
|
|
if (m->file) free(m->file);
|
|
|
|
free(m);
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-26 19:40:22 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
if (total_leak) {
|
|
|
|
if (total_leak > 10000)
|
|
|
|
admonishemnt = "you must work for Microsoft.";
|
|
|
|
else if (total_leak > 5000)
|
|
|
|
admonishemnt = "you should be ashamed!";
|
|
|
|
else if (total_leak > 2000)
|
|
|
|
admonishemnt = "you call yourself a programmer?";
|
|
|
|
else if (total_leak > 1000)
|
|
|
|
admonishemnt = "the X consortium has a job for you...";
|
|
|
|
else
|
|
|
|
admonishemnt = "close, but not there yet.";
|
|
|
|
fprintf(zfd,"zmalloc: %d leaks totalling %d bytes... %s\n",
|
|
|
|
num_leaks, total_leak, admonishemnt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(zfd,"zmalloc: Congratulations: leak-free code!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zfd) {
|
|
|
|
fflush(zfd);
|
|
|
|
fclose(zfd);
|
|
|
|
}
|
2006-12-19 22:56:18 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
|
2006-12-19 22:56:18 +00:00
|
|
|
void pad_check(meminfo *m)
|
|
|
|
{
|
|
|
|
#ifndef NO_MEMORY_PADDING
|
2007-09-13 15:00:59 +00:00
|
|
|
if (memcmp(m->addr - sizeof(beginPad), beginPad, sizeof(beginPad)) != 0) {
|
|
|
|
fprintf(zfd,"pad_check: ERR: beginPad was modified! (mallocd@ %s:%d)\n", m->file, m->line);
|
|
|
|
if (zmalloclogging > 1) zdump(m);
|
|
|
|
}
|
|
|
|
if (memcmp(m->addr + m->size, endPad, sizeof(endPad)) != 0) {
|
|
|
|
fprintf(zfd,"pad_check: ERR: endPad was modified! (mallocd@ %s:%d)\n", m->file, m->line);
|
|
|
|
if (zmalloclogging > 1) zdump(m);
|
|
|
|
}
|
2006-12-19 22:56:18 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
|
2006-12-19 22:56:18 +00:00
|
|
|
#ifdef ZTEST
|
|
|
|
#undef ZMALLOC_H
|
|
|
|
|
|
|
|
#include "zmalloc.h"
|
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
int main()
|
2006-12-19 22:56:18 +00:00
|
|
|
{
|
2007-12-05 09:55:41 +00:00
|
|
|
unsigned char * tmp;
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
zmalloc_init();
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
/* You should see no error here. */
|
2007-12-05 09:55:41 +00:00
|
|
|
tmp = (unsigned char*)malloc(200);
|
|
|
|
free(tmp);
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
/* Multiple frees test */
|
2007-12-05 09:55:41 +00:00
|
|
|
tmp = (unsigned char*)malloc(200);
|
|
|
|
strcpy(tmp, "This should show up in the dump but truncated to MAX_ZDUMP_SIZE chars");
|
|
|
|
free(tmp);
|
|
|
|
free(tmp);
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
/* Free unallocated mem test */
|
2007-12-05 09:55:41 +00:00
|
|
|
tmp += 4;
|
|
|
|
free(tmp);
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
/* Unfreed mem test... You should see "UNfreed mem at line 370" (at end) because of this */
|
2007-12-05 09:55:41 +00:00
|
|
|
tmp = (unsigned char*)malloc(200);
|
|
|
|
strcpy(tmp, "This is unfreed memory!");
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
/* Buffer overrun test... You should see an ERR:endPad here */
|
2007-12-05 09:55:41 +00:00
|
|
|
tmp = (unsigned char*)malloc(200);
|
|
|
|
tmp[202] = 0xbb;
|
|
|
|
free(tmp);
|
2007-09-13 15:00:59 +00:00
|
|
|
|
|
|
|
/* Buffer underrun test... You should see an ERR:beginPad here */
|
2007-12-05 09:55:41 +00:00
|
|
|
tmp = (unsigned char*)malloc(200);
|
|
|
|
tmp[-3] = 0x0f;
|
|
|
|
free(tmp);
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
/* Free NULL pointer test... */
|
2007-12-05 09:55:41 +00:00
|
|
|
tmp = NULL;
|
|
|
|
free(tmp);
|
2006-12-19 22:56:18 +00:00
|
|
|
|
2007-09-13 15:00:59 +00:00
|
|
|
printf("Test completed. See zmalloc.log for the messages.\n");
|
2006-12-19 22:56:18 +00:00
|
|
|
|
|
|
|
zmalloc_check();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif /* ZTEST */
|