Added util_malloc(), util_calloc().
Showing
2 changed files
with
32 additions
and
22 deletions
... | @@ -242,6 +242,8 @@ void util_save_outgoing __P((message_t msg, char *savefile)); | ... | @@ -242,6 +242,8 @@ void util_save_outgoing __P((message_t msg, char *savefile)); |
242 | void util_error __P((const char *format, ...)); | 242 | void util_error __P((const char *format, ...)); |
243 | int util_help __P((const struct mail_command_entry *table, char *word)); | 243 | int util_help __P((const struct mail_command_entry *table, char *word)); |
244 | int util_tempfile __P((char **namep)); | 244 | int util_tempfile __P((char **namep)); |
245 | void *util_malloc __P((size_t size)); | ||
246 | void *util_calloc __P((size_t nitems, size_t size)); | ||
245 | 247 | ||
246 | int ml_got_interrupt __P((void)); | 248 | int ml_got_interrupt __P((void)); |
247 | void ml_clear_interrupt __P((void)); | 249 | void ml_clear_interrupt __P((void)); | ... | ... |
... | @@ -41,7 +41,7 @@ static node *env_cursor = NULL; | ... | @@ -41,7 +41,7 @@ static node *env_cursor = NULL; |
41 | static node * | 41 | static node * |
42 | util_ll_add (node *c, int data) | 42 | util_ll_add (node *c, int data) |
43 | { | 43 | { |
44 | c->next = malloc (sizeof (node)); | 44 | c->next = util_malloc (sizeof (node)); |
45 | c->data = data; | 45 | c->data = data; |
46 | c->next->env_entry.var = NULL; | 46 | c->next->env_entry.var = NULL; |
47 | c->next->env_entry.set = 0; | 47 | c->next->env_entry.set = 0; |
... | @@ -80,7 +80,7 @@ util_expand_msglist (const int argc, char **argv, int **list) | ... | @@ -80,7 +80,7 @@ util_expand_msglist (const int argc, char **argv, int **list) |
80 | int undelete = 0; | 80 | int undelete = 0; |
81 | int *ret = NULL; | 81 | int *ret = NULL; |
82 | /* let's try a linked list */ | 82 | /* let's try a linked list */ |
83 | node *first = malloc (sizeof (node)); | 83 | node *first = util_malloc (sizeof (node)); |
84 | node *current = first; | 84 | node *current = first; |
85 | first->next = NULL; | 85 | first->next = NULL; |
86 | 86 | ||
... | @@ -150,7 +150,7 @@ util_expand_msglist (const int argc, char **argv, int **list) | ... | @@ -150,7 +150,7 @@ util_expand_msglist (const int argc, char **argv, int **list) |
150 | { | 150 | { |
151 | /* all messages */ | 151 | /* all messages */ |
152 | util_ll_free (first); | 152 | util_ll_free (first); |
153 | current = first = malloc (sizeof (node)); | 153 | current = first = util_malloc (sizeof (node)); |
154 | for (i = 1; i <= total; i++) | 154 | for (i = 1; i <= total; i++) |
155 | current = util_ll_add (current, i); | 155 | current = util_ll_add (current, i); |
156 | i = argc + 1; | 156 | i = argc + 1; |
... | @@ -237,23 +237,13 @@ util_expand_msglist (const int argc, char **argv, int **list) | ... | @@ -237,23 +237,13 @@ util_expand_msglist (const int argc, char **argv, int **list) |
237 | 237 | ||
238 | if (!lc) | 238 | if (!lc) |
239 | { | 239 | { |
240 | ret = calloc (1, sizeof (int)); | 240 | ret = util_calloc (1, sizeof (int)); |
241 | if (!ret) | ||
242 | { | ||
243 | util_error("not enough memory"); | ||
244 | exit (1); | ||
245 | } | ||
246 | ret [0] = cursor; | 241 | ret [0] = cursor; |
247 | lc = 1; | 242 | lc = 1; |
248 | } | 243 | } |
249 | else | 244 | else |
250 | { | 245 | { |
251 | ret = malloc (lc * sizeof (int)); | 246 | ret = util_malloc (lc * sizeof (int)); |
252 | if (!ret) | ||
253 | { | ||
254 | util_error("not enough memory"); | ||
255 | exit (1); | ||
256 | } | ||
257 | lc = 0; | 247 | lc = 0; |
258 | for (current = first; current->next != NULL; current = current->next) | 248 | for (current = first; current->next != NULL; current = current->next) |
259 | ret [lc++] = current->data; | 249 | ret [lc++] = current->data; |
... | @@ -482,7 +472,7 @@ util_find_env (const char *variable) | ... | @@ -482,7 +472,7 @@ util_find_env (const char *variable) |
482 | 472 | ||
483 | if (environment == NULL) | 473 | if (environment == NULL) |
484 | { | 474 | { |
485 | environment = malloc (sizeof (node)); | 475 | environment = util_malloc (sizeof (node)); |
486 | environment->env_entry.var = NULL; | 476 | environment->env_entry.var = NULL; |
487 | environment->env_entry.set = 0; | 477 | environment->env_entry.set = 0; |
488 | environment->env_entry.value = NULL; | 478 | environment->env_entry.value = NULL; |
... | @@ -790,12 +780,7 @@ util_escape_percent (char **str) | ... | @@ -790,12 +780,7 @@ util_escape_percent (char **str) |
790 | return; /* nothing to do */ | 780 | return; /* nothing to do */ |
791 | 781 | ||
792 | /* expand the string */ | 782 | /* expand the string */ |
793 | newstr = malloc (strlen (*str) + 1 + count); | 783 | newstr = util_malloc (strlen (*str) + 1 + count); |
794 | if (!newstr) | ||
795 | { | ||
796 | util_error("not enough memory"); | ||
797 | exit (1); /* be on the safe side */ | ||
798 | } | ||
799 | 784 | ||
800 | /* and escape percent signs */ | 785 | /* and escape percent signs */ |
801 | p = newstr; | 786 | p = newstr; |
... | @@ -1005,3 +990,26 @@ util_tempfile(char **namep) | ... | @@ -1005,3 +990,26 @@ util_tempfile(char **namep) |
1005 | 990 | ||
1006 | return fd; | 991 | return fd; |
1007 | } | 992 | } |
993 | |||
994 | void * | ||
995 | util_malloc (size_t size) | ||
996 | { | ||
997 | void *p = malloc (size); | ||
998 | if (!p) | ||
999 | { | ||
1000 | util_error ("not enough memory (allocating %d bytes)", size); | ||
1001 | abort (); | ||
1002 | } | ||
1003 | return p; | ||
1004 | } | ||
1005 | |||
1006 | void * | ||
1007 | util_calloc (size_t nitems, size_t size) | ||
1008 | { | ||
1009 | void *p; | ||
1010 | |||
1011 | size *= nitems; | ||
1012 | p = util_malloc (size); | ||
1013 | memset (p, 0, size); | ||
1014 | return p; | ||
1015 | } | ... | ... |
-
Please register or sign in to post a comment