aboutsummaryrefslogtreecommitdiff
path: root/src/memory.c
blob: 37ec1cba5d7542832f59ddb17b58205e8f907880 (plain) (blame)
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
/* Copyrights 2002 Luis Figueiredo (stdio@netc.pt) All rights reserved. 
 *
 * See the LICENSE file
 *
 * The origin of this software must not be misrepresented, either by
 * explicit claim or by omission.  Since few users ever read sources,
 * credits must appear in the documentation.
 *
 * date: Sat Mar 30 14:25:25 GMT 2002 
 *
 * -- memory functions
 */
#include "memory.h"

IFDEBUG(int _t=0;)

/*********************************************************************************************************/
/*
 * same as malloc with error reporting and libwebserver debug
 */
void * __ILWS_malloc(size_t s) {
	void *ret;

	ret=malloc(s);
	if(ret==NULL) {
		IFDEBUG(fprintf(stderr,"memory.c: malloc: %s (size %d)\n",strerror(errno),s);fflush(stderr));
		return NULL;
	};
	IFDEBUG(_t++;);
	
	IFDEBUG(fprintf(stderr,"memory.c (%d): Allocated %d bytes to %p\n",_t,s,ret);fflush(stderr));
	return ret;
}

/*********************************************************************************************************/
/*
 * same as calloc with error reporting and libwebserver debug
 */
void * __ILWS_calloc(size_t nmemb,size_t s) {
	void *ret;
	ret=calloc(nmemb,s);
	if(ret==NULL) {
		IFDEBUG(fprintf(stderr,"memory.c: calloc %s\n",strerror(errno));fflush(stderr));
		return NULL;
	};
	IFDEBUG(_t++;);
	IFDEBUG(fprintf(stderr,"memory.c (%d): Allocated %d bytes to %p\n",_t,s*nmemb,ret);fflush(stderr));
	return ret;
}

/*********************************************************************************************************/
/* 
 * same as realloc with error reporting and libwebserver debug
 */
void * __ILWS_realloc(void *buf,size_t s) {
	void *ret;
	ret=realloc(buf,s);
#ifdef DEBUG
	if(buf==NULL) {
		_t++;
		IFDEBUG(fprintf(stderr,"memory.c (%d): Allocated %d bytes to %p\n",_t,s,ret);fflush(stderr));
	};
#endif
	if(ret==NULL) {
		IFDEBUG(fprintf(stderr,"memory.c: realloc: %s\n",strerror(errno));fflush(stderr));
		return NULL;
	};
	IFDEBUG(fprintf(stderr,"memory.c: Realloc buffer %p to %d\n",buf,s);fflush(stderr));
	return ret;
}


/*********************************************************************************************************/
/*
 * same as free with error report and libwebserver debug
 */ 
void __ILWS_free(void *ptr) {
	if(ptr!=NULL) {
		free(ptr);
		IFDEBUG(fprintf(stderr,"memory.c (%d): Buffer %p freed\n",_t,ptr);fflush(stderr));
		IFDEBUG(_t--;);
	};
}


/*********************************************************************************************************/
/*
 *  Add a buffer to memrequest list
 */
void *__ILWS_add_buffer(struct memrequest *list,unsigned int size) {
	struct memrequest *tmem;
	if(size==0) {
		return NULL;
	};
	if(list!=NULL) {
		tmem=list;
	}else {
		return NULL;
	};
	while(tmem->next!=NULL)tmem=tmem->next;
	tmem->next=__ILWS_malloc(sizeof(struct memrequest));
	if(tmem->next==NULL) return NULL;           // ERROR
	tmem->next->ptr=__ILWS_malloc(size);
	tmem->next->next=NULL;
	return tmem->next->ptr;
}

/*********************************************************************************************************/
/*
 * Initialize memrequest list of buffers
 */
struct memrequest *__ILWS_init_buffer_list() {
	struct memrequest *newlist;
	newlist=__ILWS_malloc(sizeof(struct memrequest));
	if(newlist==NULL) return NULL;
	
	newlist->next=NULL;
	newlist->ptr=NULL;
	return newlist;
}

/*********************************************************************************************************/
/*
 * Delete memrequest buffer node (free)
 */
void __ILWS_delete_buffer(struct memrequest *mem) {
	__ILWS_free(mem->ptr);
	__ILWS_free(mem);
}

/*********************************************************************************************************/
/*
 * Delete memrequest next buffer
 */
void __ILWS_delete_next_buffer(struct memrequest *mem) {
	struct memrequest *tmem;
	tmem=mem->next;
	mem->next=mem->next->next;
	__ILWS_delete_buffer(tmem);
}

/*********************************************************************************************************/
/*
 * Delete whole memrequest buffer list
 */
void __ILWS_delete_buffer_list(struct memrequest *list) {
	struct memrequest *tmem=list;
	if(tmem==NULL) return;

	while(tmem->next!=NULL) {
		__ILWS_delete_next_buffer(tmem);
	};
	__ILWS_delete_buffer(tmem);
}