diff options
Diffstat (limited to 'src/memory.c')
-rw-r--r-- | src/memory.c | 150 |
1 files changed, 40 insertions, 110 deletions
diff --git a/src/memory.c b/src/memory.c index 37ec1cba..ca7e6f51 100644 --- a/src/memory.c +++ b/src/memory.c | |||
@@ -12,144 +12,74 @@ | |||
12 | */ | 12 | */ |
13 | #include "memory.h" | 13 | #include "memory.h" |
14 | 14 | ||
15 | IFDEBUG(int _t=0;) | 15 | struct memrequest { |
16 | char *ptr; | ||
17 | struct memrequest *next; | ||
18 | }; | ||
16 | 19 | ||
17 | /*********************************************************************************************************/ | ||
18 | /* | ||
19 | * same as malloc with error reporting and libwebserver debug | ||
20 | */ | ||
21 | void * __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 | */ | ||
39 | void * __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 | */ | ||
55 | void * __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 | */ | ||
77 | void __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 | /* | 20 | /* |
88 | * Add a buffer to memrequest list | 21 | * Add a buffer to memrequest list |
89 | */ | 22 | */ |
90 | void *__ILWS_add_buffer(struct memrequest *list,unsigned int size) { | 23 | void *__ILWS_add_buffer(struct memrequest *list,unsigned int size) { |
91 | struct memrequest *tmem; | 24 | struct memrequest *tmem; |
92 | if(size==0) { | 25 | if(size==0) { |
93 | return NULL; | 26 | return NULL; |
94 | }; | 27 | }; |
95 | if(list!=NULL) { | 28 | if(list!=NULL) { |
96 | tmem=list; | 29 | tmem=list; |
97 | }else { | 30 | }else { |
98 | return NULL; | 31 | return NULL; |
99 | }; | 32 | }; |
100 | while(tmem->next!=NULL)tmem=tmem->next; | 33 | while(tmem->next!=NULL)tmem=tmem->next; |
101 | tmem->next=__ILWS_malloc(sizeof(struct memrequest)); | 34 | tmem->next=__ILWS_malloc(sizeof(struct memrequest)); |
102 | if(tmem->next==NULL) return NULL; // ERROR | 35 | if(tmem->next==NULL) return NULL; // ERROR |
103 | tmem->next->ptr=__ILWS_malloc(size); | 36 | tmem->next->ptr=__ILWS_malloc(size); |
104 | tmem->next->next=NULL; | 37 | tmem->next->next=NULL; |
105 | return tmem->next->ptr; | 38 | return tmem->next->ptr; |
106 | } | 39 | } |
107 | 40 | ||
108 | /*********************************************************************************************************/ | ||
109 | /* | 41 | /* |
110 | * Initialize memrequest list of buffers | 42 | * Initialize memrequest list of buffers |
111 | */ | 43 | */ |
112 | struct memrequest *__ILWS_init_buffer_list() { | 44 | struct memrequest *__ILWS_init_buffer_list() { |
113 | struct memrequest *newlist; | 45 | struct memrequest *newlist; |
114 | newlist=__ILWS_malloc(sizeof(struct memrequest)); | 46 | newlist=__ILWS_malloc(sizeof(struct memrequest)); |
115 | if(newlist==NULL) return NULL; | 47 | if(newlist==NULL) |
116 | 48 | return NULL; | |
117 | newlist->next=NULL; | 49 | newlist->next=NULL; |
118 | newlist->ptr=NULL; | 50 | newlist->ptr=NULL; |
119 | return newlist; | 51 | return newlist; |
120 | } | 52 | } |
121 | 53 | ||
122 | /*********************************************************************************************************/ | ||
123 | /* | 54 | /* |
124 | * Delete memrequest buffer node (free) | 55 | * Delete memrequest buffer node (free) |
125 | */ | 56 | */ |
126 | void __ILWS_delete_buffer(struct memrequest *mem) { | 57 | void __ILWS_delete_buffer(struct memrequest *mem) { |
127 | __ILWS_free(mem->ptr); | 58 | __ILWS_free(mem->ptr); |
128 | __ILWS_free(mem); | 59 | __ILWS_free(mem); |
129 | } | 60 | } |
130 | 61 | ||
131 | /*********************************************************************************************************/ | ||
132 | /* | 62 | /* |
133 | * Delete memrequest next buffer | 63 | * Delete memrequest next buffer |
134 | */ | 64 | */ |
135 | void __ILWS_delete_next_buffer(struct memrequest *mem) { | 65 | void __ILWS_delete_next_buffer(struct memrequest *mem) { |
136 | struct memrequest *tmem; | 66 | struct memrequest *tmem; |
137 | tmem=mem->next; | 67 | tmem=mem->next; |
138 | mem->next=mem->next->next; | 68 | mem->next=mem->next->next; |
139 | __ILWS_delete_buffer(tmem); | 69 | __ILWS_delete_buffer(tmem); |
140 | } | 70 | } |
141 | 71 | ||
142 | /*********************************************************************************************************/ | ||
143 | /* | 72 | /* |
144 | * Delete whole memrequest buffer list | 73 | * Delete whole memrequest buffer list |
145 | */ | 74 | */ |
146 | void __ILWS_delete_buffer_list(struct memrequest *list) { | 75 | void __ILWS_delete_buffer_list(struct memrequest *list) { |
147 | struct memrequest *tmem=list; | 76 | struct memrequest *tmem=list; |
148 | if(tmem==NULL) return; | 77 | if (tmem==NULL) |
149 | 78 | return; | |
150 | while(tmem->next!=NULL) { | 79 | |
151 | __ILWS_delete_next_buffer(tmem); | 80 | while(tmem->next!=NULL) { |
152 | }; | 81 | __ILWS_delete_next_buffer(tmem); |
153 | __ILWS_delete_buffer(tmem); | 82 | }; |
83 | __ILWS_delete_buffer(tmem); | ||
154 | } | 84 | } |
155 | 85 | ||