From: Richard Russon Date: Wed, 2 Aug 2017 14:23:42 +0000 (+0100) Subject: move the memory functions to the library X-Git-Tag: neomutt-20170907~56^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3a873d339febc0345684ffc858cd1d1d164556b5;p=neomutt move the memory functions to the library --- diff --git a/buffer.c b/buffer.c index a23ec76f6..fb2603cf6 100644 --- a/buffer.c +++ b/buffer.c @@ -28,6 +28,7 @@ #include "buffer.h" #include "filter.h" #include "lib.h" +#include "lib/lib.h" #include "myvar.h" /** diff --git a/hash.c b/hash.c index 34b377984..b1fe55c21 100644 --- a/hash.c +++ b/hash.c @@ -25,6 +25,7 @@ #include #include "hash.h" #include "lib.h" +#include "lib/lib.h" #define SOMEPRIME 149711 diff --git a/lib.c b/lib.c index 6db7e4896..3bbaf5292 100644 --- a/lib.c +++ b/lib.c @@ -117,82 +117,6 @@ void mutt_nocurses_error(const char *fmt, ...) fputc('\n', stderr); } -void *safe_calloc(size_t nmemb, size_t size) -{ - void *p = NULL; - - if (!nmemb || !size) - return NULL; - - if (((size_t) -1) / nmemb <= size) - { - mutt_error(_("Integer overflow -- can't allocate memory!")); - sleep(1); - mutt_exit(1); - } - - if (!(p = calloc(nmemb, size))) - { - mutt_error(_("Out of memory!")); - sleep(1); - mutt_exit(1); - } - return p; -} - -void *safe_malloc(size_t siz) -{ - void *p = NULL; - - if (siz == 0) - return 0; - if ((p = malloc(siz)) == NULL) - { - mutt_error(_("Out of memory!")); - sleep(1); - mutt_exit(1); - } - return p; -} - -void safe_realloc(void *ptr, size_t siz) -{ - void *r = NULL; - void **p = (void **) ptr; - - if (siz == 0) - { - if (*p) - { - free(*p); - *p = NULL; - } - return; - } - - r = realloc(*p, siz); - if (!r) - { - mutt_error(_("Out of memory!")); - sleep(1); - mutt_exit(1); - } - - *p = r; -} - -void safe_free(void *ptr) -{ - if (!ptr) - return; - void **p = (void **) ptr; - if (*p) - { - free(*p); - *p = 0; - } -} - int safe_fclose(FILE **f) { int r = 0; diff --git a/lib.h b/lib.h index 3698d3eab..29cc2d835 100644 --- a/lib.h +++ b/lib.h @@ -32,25 +32,11 @@ #include #include -#ifdef ENABLE_NLS -#include -#define _(a) gettext(a) -#ifdef gettext_noop -#define N_(a) gettext_noop(a) -#else -#define N_(a) (a) -#endif -#else -#define _(a) (a) -#define N_(a) a -#endif - #define HUGE_STRING 8192 #define LONG_STRING 1024 #define STRING 256 #define SHORT_STRING 128 -#define FREE(x) safe_free(x) #define NONULL(x) x ? x : "" #define ISSPACE(c) isspace((unsigned char) c) @@ -179,16 +165,12 @@ int mutt_mkdir(const char *path, mode_t mode); size_t mutt_quote_filename(char *d, size_t l, const char *f); size_t mutt_strlen(const char *a); -void *safe_calloc(size_t nmemb, size_t size); -void *safe_malloc(size_t siz); void mutt_nocurses_error(const char *, ...); void mutt_remove_trailing_ws(char *s); void mutt_sanitize_filename(char *f, short slash); void mutt_str_replace(char **p, const char *s); void mutt_str_adjust(char **p); void mutt_unlink(const char *s); -void safe_free(void *ptr); -void safe_realloc(void *ptr, size_t siz); int mutt_inbox_cmp(const char *a, const char *b); const char *mutt_strsysexit(int e); diff --git a/lib/Makefile.am b/lib/Makefile.am index 55acb83e9..dc0665b99 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,12 +3,12 @@ include $(top_srcdir)/flymake.am AUTOMAKE_OPTIONS = 1.6 foreign -EXTRA_DIST = lib.h lib_ascii.h lib_base64.h lib_date.h lib_exit.h lib_md5.h lib_sha1.h +EXTRA_DIST = lib.h lib_ascii.h lib_base64.h lib_date.h lib_exit.h lib_md5.h lib_memory.h lib_sha1.h AM_CPPFLAGS = -I$(top_srcdir) noinst_LIBRARIES = liblib.a noinst_HEADERS = -liblib_a_SOURCES = lib_ascii.c lib_base64.c lib_date.c lib_exit.c lib_md5.c lib_sha1.c +liblib_a_SOURCES = lib_ascii.c lib_base64.c lib_date.c lib_exit.c lib_md5.c lib_memory.c lib_sha1.c diff --git a/lib/lib.h b/lib/lib.h index 04993dc04..687e94d40 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -33,6 +33,7 @@ * -# @subpage date * -# @subpage exit * -# @subpage md5 + * -# @subpage memory * -# @subpage sha1 */ @@ -44,6 +45,7 @@ #include "lib_date.h" #include "lib_exit.h" #include "lib_md5.h" +#include "lib_memory.h" #include "lib_sha1.h" #endif /* _LIB_LIB_H */ diff --git a/lib/lib_memory.c b/lib/lib_memory.c new file mode 100644 index 000000000..0b2ea4b8c --- /dev/null +++ b/lib/lib_memory.c @@ -0,0 +1,158 @@ +/** + * @file + * Memory management wrappers + * + * @authors + * Copyright (C) 2017 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +/** + * @page memory Memory management wrappers + * + * "Safe" memory management routines. + * + * @note If any of the allocators fail, the user is notified and the program is + * stopped immediately. + * + * | Function | Description + * | :------------- | :----------------------------------- + * | safe_calloc() | Allocate zeroed memory on the heap + * | safe_free() | Release memory allocated on the heap + * | safe_malloc() | Allocate memory on the heap + * | safe_realloc() | Resize a block of memory on the heap + */ + +#include +#include +#include +#include "lib_memory.h" +#include "lib_exit.h" +// #include "lib_message.h" +void mutt_error(const char *, ...); + +/** + * safe_calloc - Allocate zeroed memory on the heap + * @param nmemb Number of blocks + * @param size Size of blocks + * @retval ptr Memory on the heap + * + * @note This function will never return NULL. + * It will print and error and exit the program. + * + * The caller should call safe_free() to release the memory + */ +void *safe_calloc(size_t nmemb, size_t size) +{ + void *p = NULL; + + if (!nmemb || !size) + return NULL; + + if (((size_t) -1) / nmemb <= size) + { + mutt_error(_("Integer overflow -- can't allocate memory!")); + sleep(1); + mutt_exit(1); + } + + p = calloc(nmemb, size); + if (!p) + { + mutt_error(_("Out of memory!")); + sleep(1); + mutt_exit(1); + } + return p; +} + +/** + * safe_free - Release memory allocated on the heap + * @param ptr Memory to release + */ +void safe_free(void *ptr) +{ + if (!ptr) + return; + void **p = (void **) ptr; + if (*p) + { + free(*p); + *p = 0; + } +} + +/** + * safe_malloc - Allocate memory on the heap + * @param size Size of block to allocate + * @retval ptr Memory on the heap + * + * @note This function will never return NULL. + * It will print and error and exit the program. + * + * The caller should call safe_free() to release the memory + */ +void *safe_malloc(size_t size) +{ + void *p = NULL; + + if (size == 0) + return 0; + p = malloc(size); + if (p == NULL) + { + mutt_error(_("Out of memory!")); + sleep(1); + mutt_exit(1); + } + return p; +} + +/** + * safe_realloc - Resize a block of memory on the heap + * @param ptr Memory block to resize + * @param size New size + * + * @note This function will never return NULL. + * It will print and error and exit the program. + * + * If the new size is zero, the block will be freed. + */ +void safe_realloc(void *ptr, size_t size) +{ + void *r = NULL; + void **p = (void **) ptr; + + if (size == 0) + { + if (*p) + { + free(*p); + *p = NULL; + } + return; + } + + r = realloc(*p, size); + if (!r) + { + mutt_error(_("Out of memory!")); + sleep(1); + mutt_exit(1); + } + + *p = r; +} diff --git a/lib/lib_memory.h b/lib/lib_memory.h new file mode 100644 index 000000000..9299b4a60 --- /dev/null +++ b/lib/lib_memory.h @@ -0,0 +1,48 @@ +/** + * @file + * Memory management wrappers + * + * @authors + * Copyright (C) 2017 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _LIB_MEMORY_H +#define _LIB_MEMORY_H + +#include + +#ifdef ENABLE_NLS +#include +#define _(a) gettext(a) +#ifdef gettext_noop +#define N_(a) gettext_noop(a) +#else +#define N_(a) (a) +#endif +#else +#define _(a) (a) +#define N_(a) a +#endif + +void *safe_calloc(size_t nmemb, size_t size); +void safe_free(void *ptr); +void *safe_malloc(size_t size); +void safe_realloc(void *ptr, size_t size); + +#define FREE(x) safe_free(x) + +#endif /* _LIB_MEMORY_H */ diff --git a/po/POTFILES.in b/po/POTFILES.in index 13c3944c5..eb2b4fe25 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -64,6 +64,7 @@ lib/lib_base64.c lib/lib_date.c lib/lib_exit.c lib/lib_md5.c +lib/lib_memory.c lib/lib_sha1.c main.c mbox.c