1/*	$NetBSD: debugrm.c,v 1.3 2006/09/09 16:22:09 manu Exp $	*/
2
3/*	$KAME: debugrm.c,v 1.6 2001/12/13 16:07:46 sakane Exp $	*/
4
5/*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#define NONEED_DRM
35
36#include "config.h"
37
38#include <sys/types.h>
39#include <sys/param.h>
40
41#include <stdio.h>
42#include <string.h>
43#include <stdlib.h>
44#include <time.h>
45#include <err.h>
46
47#include "debugrm.h"
48
49#include "vmbuf.h"	/* need to mask vmbuf.c functions. */
50
51#define DRMLISTSIZE 1024
52
53struct drm_list_t {
54	void *ptr;
55	char msg[100];
56};
57static struct drm_list_t drmlist[DRMLISTSIZE];
58
59static int drm_unknown;
60
61static void DRM_add __P((void *, char *));
62static void DRM_del __P((void *));
63static void DRM_setmsg __P((char *, int, void *, int, char *, int, char *));
64
65void
66DRM_init()
67{
68	int i;
69	drm_unknown = 0;
70	for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++)
71		drmlist[i].ptr = 0;
72}
73
74void
75DRM_dump()
76{
77	FILE *fp;
78	int i;
79
80	fp = fopen(DRMDUMPFILE, "w");
81	if (fp == NULL)
82		err(1, "fopen");	/*XXX*/
83	fprintf(fp, "drm_unknown=%d\n", drm_unknown);
84	for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) {
85		if (drmlist[i].ptr)
86			fprintf(fp, "%s\n", drmlist[i].msg);
87	}
88	fclose(fp);
89}
90
91static void
92DRM_add(p, msg)
93	void *p;
94	char *msg;
95{
96	int i;
97	for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) {
98		if (!drmlist[i].ptr) {
99			drmlist[i].ptr = p;
100			strlcpy(drmlist[i].msg, msg, sizeof(drmlist[i].msg));
101			return;
102		}
103	}
104}
105
106static void
107DRM_del(p)
108	void *p;
109{
110	int i;
111
112	if (!p)
113		return;
114
115	for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) {
116		if (drmlist[i].ptr == p) {
117			drmlist[i].ptr = 0;
118			return;
119		}
120	}
121	drm_unknown++;
122}
123
124static void
125DRM_setmsg(buf, buflen, ptr, size, file, line, func)
126	char *buf, *file, *func;
127	int buflen, size, line;
128	void *ptr;
129{
130	time_t t;
131	struct tm *tm;
132	int len;
133
134	t = time(NULL);
135	tm = localtime(&t);
136	len = strftime(buf, buflen, "%Y/%m/%d:%T ", tm);
137
138	snprintf(buf + len, buflen - len, "%p %6d %s:%d:%s",
139		ptr, size, file , line, func);
140}
141
142void *
143DRM_malloc(file, line, func, size)
144	char *file, *func;
145	int line;
146	size_t size;
147{
148	void *p;
149
150	p = malloc(size);
151	if (p) {
152		char buf[1024];
153		DRM_setmsg(buf, sizeof(buf), p, size, file, line, func);
154		DRM_add(p, buf);
155	}
156
157	return p;
158}
159
160void *
161DRM_calloc(file, line, func, number, size)
162	char *file, *func;
163	int line;
164	size_t number, size;
165{
166	void *p;
167
168	p = calloc(number, size);
169	if (p) {
170		char buf[1024];
171		DRM_setmsg(buf, sizeof(buf), p, number * size, file, line, func);
172		DRM_add(p, buf);
173	}
174	return p;
175}
176
177void *
178DRM_realloc(file, line, func, ptr, size)
179	char *file, *func;
180	int line;
181	void *ptr;
182	size_t size;
183{
184	void *p;
185
186	p = realloc(ptr, size);
187	if (p) {
188		char buf[1024];
189		if (ptr && p != ptr) {
190			DRM_del(ptr);
191			DRM_setmsg(buf, sizeof(buf), p, size, file, line, func);
192			DRM_add(p, buf);
193		}
194	}
195
196	return p;
197}
198
199void
200DRM_free(file, line, func, ptr)
201	char *file, *func;
202	int line;
203	void *ptr;
204{
205	DRM_del(ptr);
206	free(ptr);
207}
208
209char *
210DRM_strdup(file, line, func, str)
211	char *file, *func;
212	int line;
213	const char *str;
214{
215	char *p;
216
217	p = strdup(str);
218
219	if (p) {
220		char buf[1024];
221		DRM_setmsg(buf, sizeof(buf), p, size, file, line, func);
222		DRM_add(p, buf);
223	}
224
225	return p;
226}
227
228/*
229 * mask vmbuf.c functions.
230 */
231void *
232DRM_vmalloc(file, line, func, size)
233	char *file, *func;
234	int line;
235	size_t size;
236{
237	void *p;
238
239	p = vmalloc(size);
240	if (p) {
241		char buf[1024];
242		DRM_setmsg(buf, sizeof(buf), p, size, file, line, func);
243		DRM_add(p, buf);
244	}
245
246	return p;
247}
248
249void *
250DRM_vrealloc(file, line, func, ptr, size)
251	char *file, *func;
252	int line;
253	void *ptr;
254	size_t size;
255{
256	void *p;
257
258	p = vrealloc(ptr, size);
259	if (p) {
260		char buf[1024];
261		if (ptr && p != ptr) {
262			DRM_del(ptr);
263			DRM_setmsg(buf, sizeof(buf), p, size, file, line, func);
264			DRM_add(p, buf);
265		}
266	}
267
268	return p;
269}
270
271void
272DRM_vfree(file, line, func, ptr)
273	char *file, *func;
274	int line;
275	void *ptr;
276{
277	DRM_del(ptr);
278	vfree(ptr);
279}
280
281void *
282DRM_vdup(file, line, func, ptr)
283	char *file, *func;
284	int line;
285	void *ptr;
286{
287	void *p;
288
289	p = vdup(ptr);
290	if (p) {
291		char buf[1024];
292		DRM_setmsg(buf, sizeof(buf), p, 0, file, line, func);
293		DRM_add(p, buf);
294	}
295
296	return p;
297}
298