summaryrefslogtreecommitdiffstats
path: root/src/Make_all.mak
AgeCommit message (Expand)Author
2020-04-24patch 8.2.0630: "make tags" does not cover Haiku GUI filev8.2.0630Bram Moolenaar
2019-01-11patch 8.1.0723: cannot easily run specific test when in src/testdirv8.1.0723Bram Moolenaar
2019-01-11patch 8.1.0721: conceal mode is not sufficiently testedv8.1.0721Bram Moolenaar
2018-12-19patch 8.1.0610: MS-Windows ctags file list differs from Unixv8.1.0610Bram Moolenaar
2018-12-13patch 8.1.0579: cannot attach properties to textv8.1.0579Bram Moolenaar
2018-11-30patch 8.1.0553: it is not easy to edit a script that was sourcedv8.1.0553Bram Moolenaar
2018-11-10patch 8.1.0516: :move command marks buffer modified when nothing changedv8.1.0516Bram Moolenaar
2018-09-22patch 8.1.0428: the :suspend command is not testedv8.1.0428Bram Moolenaar
2018-09-14patch 8.1.0389: :behave command is not testedv8.1.0389Bram Moolenaar
2018-09-10patch 8.1.0362: cannot get the script line number when executing a functionv8.1.0362Bram Moolenaar
2018-08-07patch 8.1.0251: using full path is not supported for 'backupdir'v8.1.0251Bram Moolenaar
2018-07-23patch 8.1.0205: invalid memory access with invalid modelinev8.1.0205Bram Moolenaar
2018-07-04patch 8.1.0152: cannot easily run individual tests on MS-Windowsv8.1.0152Bram Moolenaar
ef='#n141'>141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/* $Id: xmalloc-debug.c,v 1.2 2007-10-04 11:52:03 nicm Exp $ */

/*
 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef DEBUG

#include <sys/types.h>

#include <ctype.h>
#include <dlfcn.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "tmux.h"

/* Single xmalloc allocated block. */
struct xmalloc_blk {
	void	*caller;

	void	*ptr;
	size_t	 size;

	SPLAY_ENTRY(xmalloc_blk) entry;
};

/* Splay tree of allocated blocks. */
SPLAY_HEAD(xmalloc_tree, xmalloc_blk);
struct xmalloc_tree xmalloc_tree = SPLAY_INITIALIZER(&xmalloc_tree);

/* Various statistics. */
size_t	xmalloc_allocated;
size_t	xmalloc_freed;
size_t	xmalloc_peak;
u_int	xmalloc_frees;
u_int	xmalloc_mallocs;
u_int	xmalloc_reallocs;

/* Print function. */
#define XMALLOC_PRINT log_debug

/* Bytes of unallocated blocks and number of allocated blocks to show. */
#define XMALLOC_BYTES 8
#define XMALLOC_LINES 32

/* Macro to update peek usage variable. */
#define XMALLOC_UPDATE() do {						\
	if (xmalloc_allocated - xmalloc_freed > xmalloc_peak)		\
		xmalloc_peak = xmalloc_allocated - xmalloc_freed;	\
} while (0)

/* Tree functions. */
int xmalloc_cmp(struct xmalloc_blk *, struct xmalloc_blk *);
SPLAY_PROTOTYPE(xmalloc_tree, xmalloc_blk, entry, xmalloc_cmp);
SPLAY_GENERATE(xmalloc_tree, xmalloc_blk, entry, xmalloc_cmp);

/* Compare two blocks. */
int
xmalloc_cmp(struct xmalloc_blk *blk1, struct xmalloc_blk *blk2)
{
	uintptr_t	ptr1 = (uintptr_t) blk1->ptr;
	uintptr_t	ptr2 = (uintptr_t) blk2->ptr;

	if (ptr1 < ptr2)
		return (-1);
	if (ptr1 > ptr2)
		return (1);
	return (0);
}

/* Clear statistics and block list; used to start fresh after fork(2). */
void
xmalloc_clear(void)
{
 	struct xmalloc_blk	*blk;

	xmalloc_allocated = 0;
	xmalloc_freed = 0;
	xmalloc_peak = 0;
	xmalloc_frees = 0;
	xmalloc_mallocs = 0;
	xmalloc_reallocs = 0;

	while (!SPLAY_EMPTY(&xmalloc_tree)) {
		blk = SPLAY_ROOT(&xmalloc_tree);
		SPLAY_REMOVE(xmalloc_tree, &xmalloc_tree, blk);
		free(blk);
	}
}

/* Print report of statistics and unfreed blocks. */
void
xmalloc_report(pid_t pid, const char *hdr)
{
 	struct xmalloc_blk	*blk;
	u_char			*iptr;
 	char	 		 buf[4 * XMALLOC_BYTES + 1], *optr;
 	size_t		 	 len;
  	u_int	 		 n;
	Dl_info			 info;

 	XMALLOC_PRINT("%s: %ld: allocated=%zu, freed=%zu, difference=%zd, "
	    "peak=%zu", hdr, (long) pid, xmalloc_allocated, xmalloc_freed,
	    xmalloc_allocated - xmalloc_freed, xmalloc_peak);
 	XMALLOC_PRINT("%s: %ld: mallocs=%u, reallocs=%u, frees=%u", hdr,
	    (long) pid, xmalloc_mallocs, xmalloc_reallocs, xmalloc_frees);

	n = 0;
	SPLAY_FOREACH(blk, xmalloc_tree, &xmalloc_tree) {
		n++;
		if (n >= XMALLOC_LINES)
			continue;

		len = blk->size;
		if (len > XMALLOC_BYTES)
			len = XMALLOC_BYTES;

		memset(&info, 0, sizeof info);
		if (dladdr(blk->caller, &info) == 0)
			info.dli_sname = info.dli_saddr = NULL;

		optr = buf;
		iptr = blk->ptr;
		for (; len > 0; len--) {
			if (isascii(*iptr) && !iscntrl(*iptr)) {
				*optr++ = *iptr++;
				continue;
			}
			*optr++ = '\\';
			*optr++ = '0' + ((*iptr >> 6) & 07);
			*optr++ = '0' + ((*iptr >> 3) & 07);
			*optr++ = '0' + (*iptr & 07);
			iptr++;
		}
		*optr = '\0';

		XMALLOC_PRINT("%s: %ld: %u, %s+0x%02tx: [%p %zu: %s]", hdr,
		    (long) pid, n, info.dli_sname, ((u_char *) blk->caller) -
		    ((u_char *) info.dli_saddr), blk->ptr, blk->size, buf);
	}
	XMALLOC_PRINT("%s: %ld: %u unfreed blocks", hdr, (long) pid, n);
}

/* Record a newly created block. */
void
xmalloc_new(void *caller, void *ptr, size_t size)
{
	struct xmalloc_blk	*blk;

	xmalloc_allocated += size;
	XMALLOC_UPDATE();

	if ((blk = malloc(sizeof *blk)) == NULL)
		abort();

	blk->ptr = ptr;
	blk->size = size;

	blk->caller = caller;

	SPLAY_INSERT(xmalloc_tree, &xmalloc_tree, blk);

	xmalloc_mallocs++;
	XMALLOC_UPDATE();
}

/* Record changes to a block. */
void
xmalloc_change(void *caller, void *oldptr, void *newptr, size_t newsize)
{
	struct xmalloc_blk	*blk, key;
	ssize_t			 change;

	if (oldptr == NULL) {
		xmalloc_new(caller, newptr, newsize);
		return;
	}

	key.ptr = oldptr;
	blk = SPLAY_FIND(xmalloc_tree, &xmalloc_tree, &key);
	if (blk == NULL)
		return;

	change = newsize - blk->size;
	if (change > 0)
		xmalloc_allocated += change;
	else
		xmalloc_freed -= change;
	XMALLOC_UPDATE();

	SPLAY_REMOVE(xmalloc_tree, &xmalloc_tree, blk);

 	blk->ptr = newptr;
	blk->size = newsize;

	blk->caller = caller;

	SPLAY_INSERT(xmalloc_tree, &xmalloc_tree, blk);

	xmalloc_reallocs++;
	XMALLOC_UPDATE();
}

/* Record a block free. */
void
xmalloc_free(void *ptr)
{
	struct xmalloc_blk	*blk, key;

	key.ptr = ptr;
	blk = SPLAY_FIND(xmalloc_tree, &xmalloc_tree, &key);
	if (blk == NULL)
		return;

	xmalloc_freed += blk->size;

	SPLAY_REMOVE(xmalloc_tree, &xmalloc_tree, blk);
	free(blk);

	xmalloc_frees++;
	XMALLOC_UPDATE();
}

#endif /* DEBUG */