File: mem.c

package info (click to toggle)
multitail 4.2.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 660 kB
  • ctags: 812
  • sloc: ansic: 12,046; makefile: 91; sh: 19
file content (192 lines) | stat: -rw-r--r-- 3,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>

#include "error.h"

#ifndef _DEBUG
#define MEMLOG(x)
#else
void MEMLOG(char *s, ...)
{
        va_list ap;
        FILE *fh = fopen("log.log", "a+");
        if (!fh)
                error_exit("error logging\n");

        va_start(ap, s);
        vfprintf(fh, s, ap);
        va_end(ap);

        fclose(fh);
}

typedef struct
{
	void *p;
	char *descr;
	int size;
} memlist;
memlist *pm = NULL;
int n_pm = 0;

void dump_mem(int sig)
{
	int loop;

	signal(SIGHUP, dump_mem);

	if (sig != SIGHUP)
		error_exit("dump_mem: unexpected signal %d for dump_mem\n", sig);

	MEMLOG("%d elements of memory used\n", n_pm);
	for(loop=0; loop<n_pm; loop++)
	{
		MEMLOG("%06d] %p %d (%s)\n", loop, pm[loop].p, pm[loop].size, pm[loop].descr);
	}
	MEMLOG("--- finished memory dump\n");
}

int remove_mem_element(void *p)
{
	int old_size = 0;

	if (p)
	{
		int loop;

		for(loop=0; loop<n_pm; loop++)
		{
			if (pm[loop].p == p)
			{
				int n_to_move;

				old_size = pm[loop].size;

				n_to_move = (n_pm - loop) - 1;
				if (n_to_move > 0)
					memmove(&pm[loop], &pm[loop + 1], n_to_move * sizeof(memlist));
				else if (n_to_move < 0)
					error_exit("remove_mem_element: n_to_move < 0!\n");
				n_pm--;
				loop=-1;

				break;
			}
		}

		if (loop != -1)
		{
			MEMLOG("remove_mem_element: pointer %p not found\n", p);
		}

		if (n_pm)
		{
			pm = (memlist *)realloc(pm, sizeof(memlist) * n_pm);
			if (!pm) error_exit("remove_mem_element: failed to shrink memorylist to %d elements\n", n_pm);
		}
		else
		{
			free(pm);
			pm = NULL;
		}
	}

	return old_size;
}
void add_mem_element(void *p, int size, char *what)
{
	pm = (memlist *)realloc(pm, sizeof(memlist) * (n_pm + 1));
	if (!pm) error_exit("add_mem_element: failed to grow memorylist from %d elements\n", n_pm);
	pm[n_pm].p = p;
	pm[n_pm].size = size;
	pm[n_pm].descr = what;
	n_pm++;
}
#endif

void myfree(void *p)
{
#ifdef _DEBUG
	int old_size = remove_mem_element(p);
#endif
	free(p);
}

void * myrealloc(void *oldp, int new_size, char *what)
{
#ifdef _DEBUG
	int old_size;
	void *newp;
/*	static int rand_val;
	char rand_val_init = 0;
	int rand_size;

	if (!rand_val_init)
	{
		srand48((int)time(NULL) + (int)getpid());
		rand_val_init = 1;
	}
	do
	{
		rand_val = lrand48();
	}
	while(rand_val == 0);
*/

	newp = realloc(oldp, new_size);
	if (!newp)
		error_exit("myrealloc: failed to reallocate to %d bytes for %s\n", new_size, what);
	old_size = remove_mem_element(oldp);
	add_mem_element(newp, new_size, what);
	signal(SIGHUP, dump_mem);

/*	rand_size = new_size - old_size;
	if (rand_size > 0)
	{
		MEMLOG("randomizing(%s) %d bytes\n", what, rand_size);
		memset(&((char *)newp)[old_size], rand_val, rand_size);
	}
*/
#else
	/* ----------------------------------------------------
	 * add code for repeatingly retry? -> then configurable
	 * via configurationfile with number of retries and/or
	 * sleep
	 * ----------------------------------------------------
	 */
	void *newp = realloc(oldp, new_size);
	if (!newp)
		error_exit("myrealloc: failed to reallocate to %d bytes for %s\n", new_size, what);
#endif

	return newp;
}

void * mymalloc(int size, char *what)
{
	return myrealloc(NULL, size, what);
}

char * mystrdup(char *in, char *what)
{
#ifdef _DEBUG
	int len = strlen(in) + 1;
	char *newp = (char *)mymalloc(len, what);

	memcpy(newp, in, len);

	return newp;
#else
	char *newp = strdup(in);
	if (!newp)
		error_exit("mystrdup: out of memory for %s\n", what);

	return newp;
#endif
}