aboutsummaryrefslogtreecommitdiff
path: root/src/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/memory.c')
-rw-r--r--src/memory.c155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/memory.c b/src/memory.c
new file mode 100644
index 00000000..37ec1cba
--- /dev/null
+++ b/src/memory.c
@@ -0,0 +1,155 @@
1/* Copyrights 2002 Luis Figueiredo (stdio@netc.pt) All rights reserved.
2 *
3 * See the LICENSE file
4 *
5 * The origin of this software must not be misrepresented, either by
6 * explicit claim or by omission. Since few users ever read sources,
7 * credits must appear in the documentation.
8 *
9 * date: Sat Mar 30 14:25:25 GMT 2002
10 *
11 * -- memory functions
12 */
13#include "memory.h"
14
15IFDEBUG(int _t=0;)
16
17/*********************************************************************************************************/
18/*
19 * same as malloc with error reporting and libwebserver debug
20 */
21void * __ILWS_malloc(size_t s) {
22 void *ret;
23
24 ret=malloc(s);
25 if(ret==NULL) {
26 IFDEBUG(fprintf(stderr,"memory.c: malloc: %s (size %d)\n",strerror(errno),s);fflush(stderr));
27 return NULL;
28 };
29 IFDEBUG(_t++;);
30
31 IFDEBUG(fprintf(stderr,"memory.c (%d): Allocated %d bytes to %p\n",_t,s,ret);fflush(stderr));
32 return ret;
33}
34
35/*********************************************************************************************************/
36/*
37 * same as calloc with error reporting and libwebserver debug
38 */
39void * __ILWS_calloc(size_t nmemb,size_t s) {
40 void *ret;
41 ret=calloc(nmemb,s);
42 if(ret==NULL) {
43 IFDEBUG(fprintf(stderr,"memory.c: calloc %s\n",strerror(errno));fflush(stderr));
44 return NULL;
45 };
46 IFDEBUG(_t++;);
47 IFDEBUG(fprintf(stderr,"memory.c (%d): Allocated %d bytes to %p\n",_t,s*nmemb,ret);fflush(stderr));
48 return ret;
49}
50
51/*********************************************************************************************************/
52/*
53 * same as realloc with error reporting and libwebserver debug
54 */
55void * __ILWS_realloc(void *buf,size_t s) {
56 void *ret;
57 ret=realloc(buf,s);
58#ifdef DEBUG
59 if(buf==NULL) {
60 _t++;
61 IFDEBUG(fprintf(stderr,"memory.c (%d): Allocated %d bytes to %p\n",_t,s,ret);fflush(stderr));
62 };
63#endif
64 if(ret==NULL) {
65 IFDEBUG(fprintf(stderr,"memory.c: realloc: %s\n",strerror(errno));fflush(stderr));
66 return NULL;
67 };
68 IFDEBUG(fprintf(stderr,"memory.c: Realloc buffer %p to %d\n",buf,s);fflush(stderr));
69 return ret;
70}
71
72
73/*********************************************************************************************************/
74/*
75 * same as free with error report and libwebserver debug
76 */
77void __ILWS_free(void *ptr) {
78 if(ptr!=NULL) {
79 free(ptr);
80 IFDEBUG(fprintf(stderr,"memory.c (%d): Buffer %p freed\n",_t,ptr);fflush(stderr));
81 IFDEBUG(_t--;);
82 };
83}
84
85
86/*********************************************************************************************************/
87/*
88 * Add a buffer to memrequest list
89 */
90void *__ILWS_add_buffer(struct memrequest *list,unsigned int size) {
91 struct memrequest *tmem;
92 if(size==0) {
93 return NULL;
94 };
95 if(list!=NULL) {
96 tmem=list;
97 }else {
98 return NULL;
99 };
100 while(tmem->next!=NULL)tmem=tmem->next;
101 tmem->next=__ILWS_malloc(sizeof(struct memrequest));
102 if(tmem->next==NULL) return NULL; // ERROR
103 tmem->next->ptr=__ILWS_malloc(size);
104 tmem->next->next=NULL;
105 return tmem->next->ptr;
106}
107
108/*********************************************************************************************************/
109/*
110 * Initialize memrequest list of buffers
111 */
112struct memrequest *__ILWS_init_buffer_list() {
113 struct memrequest *newlist;
114 newlist=__ILWS_malloc(sizeof(struct memrequest));
115 if(newlist==NULL) return NULL;
116
117 newlist->next=NULL;
118 newlist->ptr=NULL;
119 return newlist;
120}
121
122/*********************************************************************************************************/
123/*
124 * Delete memrequest buffer node (free)
125 */
126void __ILWS_delete_buffer(struct memrequest *mem) {
127 __ILWS_free(mem->ptr);
128 __ILWS_free(mem);
129}
130
131/*********************************************************************************************************/
132/*
133 * Delete memrequest next buffer
134 */
135void __ILWS_delete_next_buffer(struct memrequest *mem) {
136 struct memrequest *tmem;
137 tmem=mem->next;
138 mem->next=mem->next->next;
139 __ILWS_delete_buffer(tmem);
140}
141
142/*********************************************************************************************************/
143/*
144 * Delete whole memrequest buffer list
145 */
146void __ILWS_delete_buffer_list(struct memrequest *list) {
147 struct memrequest *tmem=list;
148 if(tmem==NULL) return;
149
150 while(tmem->next!=NULL) {
151 __ILWS_delete_next_buffer(tmem);
152 };
153 __ILWS_delete_buffer(tmem);
154}
155