No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / ipsec-tools / src / racoon / debugrm.c
blob6c5fb8cec9aee21c4521991b240d36f03de7af86
1 /* $NetBSD$ */
3 /* $KAME: debugrm.c,v 1.6 2001/12/13 16:07:46 sakane Exp $ */
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.
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.
34 #define NONEED_DRM
36 #include "config.h"
38 #include <sys/types.h>
39 #include <sys/param.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <time.h>
45 #include <err.h>
47 #include "debugrm.h"
49 #include "vmbuf.h" /* need to mask vmbuf.c functions. */
51 #define DRMLISTSIZE 1024
53 struct drm_list_t {
54 void *ptr;
55 char msg[100];
57 static struct drm_list_t drmlist[DRMLISTSIZE];
59 static int drm_unknown;
61 static void DRM_add __P((void *, char *));
62 static void DRM_del __P((void *));
63 static void DRM_setmsg __P((char *, int, void *, int, char *, int, char *));
65 void
66 DRM_init()
68 int i;
69 drm_unknown = 0;
70 for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++)
71 drmlist[i].ptr = 0;
74 void
75 DRM_dump()
77 FILE *fp;
78 int i;
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);
88 fclose(fp);
91 static void
92 DRM_add(p, msg)
93 void *p;
94 char *msg;
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;
106 static void
107 DRM_del(p)
108 void *p;
110 int i;
112 if (!p)
113 return;
115 for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) {
116 if (drmlist[i].ptr == p) {
117 drmlist[i].ptr = 0;
118 return;
121 drm_unknown++;
124 static void
125 DRM_setmsg(buf, buflen, ptr, size, file, line, func)
126 char *buf, *file, *func;
127 int buflen, size, line;
128 void *ptr;
130 time_t t;
131 struct tm *tm;
132 int len;
134 t = time(NULL);
135 tm = localtime(&t);
136 len = strftime(buf, buflen, "%Y/%m/%d:%T ", tm);
138 snprintf(buf + len, buflen - len, "%p %6d %s:%d:%s",
139 ptr, size, file , line, func);
142 void *
143 DRM_malloc(file, line, func, size)
144 char *file, *func;
145 int line;
146 size_t size;
148 void *p;
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);
157 return p;
160 void *
161 DRM_calloc(file, line, func, number, size)
162 char *file, *func;
163 int line;
164 size_t number, size;
166 void *p;
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);
174 return p;
177 void *
178 DRM_realloc(file, line, func, ptr, size)
179 char *file, *func;
180 int line;
181 void *ptr;
182 size_t size;
184 void *p;
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);
196 return p;
199 void
200 DRM_free(file, line, func, ptr)
201 char *file, *func;
202 int line;
203 void *ptr;
205 DRM_del(ptr);
206 free(ptr);
209 char *
210 DRM_strdup(file, line, func, str)
211 char *file, *func;
212 int line;
213 const char *str;
215 char *p;
217 p = strdup(str);
219 if (p) {
220 char buf[1024];
221 DRM_setmsg(buf, sizeof(buf), p, size, file, line, func);
222 DRM_add(p, buf);
225 return p;
229 * mask vmbuf.c functions.
231 void *
232 DRM_vmalloc(file, line, func, size)
233 char *file, *func;
234 int line;
235 size_t size;
237 void *p;
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);
246 return p;
249 void *
250 DRM_vrealloc(file, line, func, ptr, size)
251 char *file, *func;
252 int line;
253 void *ptr;
254 size_t size;
256 void *p;
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);
268 return p;
271 void
272 DRM_vfree(file, line, func, ptr)
273 char *file, *func;
274 int line;
275 void *ptr;
277 DRM_del(ptr);
278 vfree(ptr);
281 void *
282 DRM_vdup(file, line, func, ptr)
283 char *file, *func;
284 int line;
285 void *ptr;
287 void *p;
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);
296 return p;