File: memory.c

package info (click to toggle)
stringlist 0.2.1-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 456 kB
  • ctags: 96
  • sloc: sh: 4,710; ansic: 1,280; makefile: 89
file content (182 lines) | stat: -rw-r--r-- 3,329 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
#include "StringList.h"

#ifdef DBUG_MEM
#define POINTERS_SIZE 10240
static unsigned int num_pointers = 0;
static void *pointers_ptr[POINTERS_SIZE];
static unsigned int pointers_size[POINTERS_SIZE];

#endif

void EDisplayMemUse()
{
#ifdef DBUG_MEM
	int i, min, max, sum;

	max = 0;
	min = 0x7ffffff;
	sum = 0;
	for (i = 0; i < num_pointers; i++) {
		sum += pointers_size[i];
		if (pointers_size[i] < min)
			min = pointers_size[i];
		if (pointers_size[i] > max)
			max = pointers_size[i];
	}
	if (num_pointers > 0) {
		fprintf(stderr, "Num:%6i Sum:%8i Av:%8i Min:%8i Max%6i\n",
				num_pointers, sum, sum / num_pointers, min, max);
	}
#endif
}

void *
 Emalloc(int size)
{
	void *p;

	EDBUG(9, "Emalloc");
	p = NULL;
	if (size <= 0) {
		Alert("Warning! Attempt to malloc 0 bytes\n");
		EDBUG_RETURN(NULL);
	} else {
		p = malloc(size);
		if (!p)
			Alert("Warning! malloc for %i bytes failed \n ", size);
#ifdef DBUG_MEM
		if (p) {
			num_pointers++;
			pointers_ptr[num_pointers - 1] = p;
			pointers_size[num_pointers - 1] = size;
		}
#endif
		EDBUG_RETURN(p);
	}
	EDBUG_RETURN(NULL);
}

void *
 Erealloc(void *ptr, int size)
{
	void *p;

#ifdef DBUG_MEM
	char bad = 0;

#endif

	EDBUG(9, "Erealloc");
	p = NULL;
	if (size <= 0) {
		Alert("Warning! Attempt to realloc 0 bytes\nFreeing instaed\n");
		if (ptr)
			Efree(ptr);
		EDBUG_RETURN(NULL);
	} else if (ptr == NULL) {
		EDBUG_RETURN(Emalloc(size));
	} else {
		p = realloc(ptr, size);
		if (!p)
			Alert("Warning! realloc for %i bytes failed\n", size);
#ifdef DBUG_MEM
		if (p) {
			int i;

			bad = 1;
			for (i = 0; i < num_pointers; i++) {
				if (pointers_ptr[i] == ptr) {
					pointers_size[i] = size;
					pointers_ptr[i] = p;
					bad = 0;
					i = num_pointers;
				}
			}
		}
		if (bad)
			Alert("WARNING!\n Attempt to free memory that hasn't been allocated.\n");
#endif
		EDBUG_RETURN(p);
	}
	EDBUG_RETURN(NULL);
}
void *
 Ecalloc(int num, int size)
{
	void *p;

	EDBUG(9, "Ecalloc");
	p = NULL;
	if (size <= 0) {
		Alert("Warning! Attempt to calloc 0 bytes\n");
		EDBUG_RETURN(NULL);
        } else if (num <= 0) {
                Alert("Warning! Attempt to calloc 0 elements\n");
                EDBUG_RETURN(NULL);
	} else {
		p = calloc(num, size);
		if (!p)
			Alert("Warning! calloc for %i elements of %i bytes"
                              "failed \n", num, size);
#ifdef DBUG_MEM
		if (p) {
			num_pointers++;
			pointers_ptr[num_pointers - 1] = p;
			pointers_size[num_pointers - 1] = num*size;
		}
#endif
		EDBUG_RETURN(p);
	}
	EDBUG_RETURN(NULL);
}

void Efree(void *ptr)
{
#ifdef DBUG_MEM
	char bad = 0;

#endif
	EDBUG(9, "Efree");
	if (!ptr)
		Alert("Warning! Attempting to free NULL pointer\n");
	else {
		free(ptr);
#ifdef DBUG_MEM
		{
			int i, j;

			bad = 1;
			for (i = 0; i < num_pointers; i++) {
				if (pointers_ptr[i] == ptr) {
					for (j = i; j < num_pointers - 1; j++) {
						pointers_ptr[j] = pointers_ptr[j + 1];
					}
					bad = 0;
					i = num_pointers;
					num_pointers--;
					break;
				}
			}
		}
#endif
	}
#ifdef DBUG_MEM
	if (bad)
		Alert("WARNING!\n Attempt to free memory that hasn't been allocated.\n");
#endif
	EDBUG_RETURN_;
}

char *duplicate(char *s)
{
	char *ss;
	int sz;

	EDBUG(9, "duplicate");
	if (!s)
		EDBUG_RETURN(NULL);
	sz = strlen(s);
	ss = Emalloc(sz + 1);
	strncpy(ss, s, sz + 1);
	EDBUG_RETURN(ss);
}