#include "buffer.h"
#include "filter.h"
#include "lib.h"
+#include "lib/lib.h"
#include "myvar.h"
/**
#include <stdio.h>
#include "hash.h"
#include "lib.h"
+#include "lib/lib.h"
#define SOMEPRIME 149711
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;
#include <string.h>
#include <sys/types.h>
-#ifdef ENABLE_NLS
-#include <libintl.h>
-#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)
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);
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
* -# @subpage date
* -# @subpage exit
* -# @subpage md5
+ * -# @subpage memory
* -# @subpage sha1
*/
#include "lib_date.h"
#include "lib_exit.h"
#include "lib_md5.h"
+#include "lib_memory.h"
#include "lib_sha1.h"
#endif /* _LIB_LIB_H */
--- /dev/null
+/**
+ * @file
+ * Memory management wrappers
+ *
+ * @authors
+ * Copyright (C) 2017 Richard Russon <rich@flatcap.org>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * @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 <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#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;
+}
--- /dev/null
+/**
+ * @file
+ * Memory management wrappers
+ *
+ * @authors
+ * Copyright (C) 2017 Richard Russon <rich@flatcap.org>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _LIB_MEMORY_H
+#define _LIB_MEMORY_H
+
+#include <stdio.h>
+
+#ifdef ENABLE_NLS
+#include <libintl.h>
+#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 */
lib/lib_date.c
lib/lib_exit.c
lib/lib_md5.c
+lib/lib_memory.c
lib/lib_sha1.c
main.c
mbox.c