aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Schmidt <john.schmidt.h@gmail.com>2014-04-03 17:07:14 +0200
committerThiago de Arruda <tpadilha84@gmail.com>2014-04-04 16:33:12 -0300
commit6f53cb1cc311025f0c21d142af06fbfbda113fc4 (patch)
treed4381dd18e74f4bec084ed7602e90561fa3ad50c /src
parent3fe0580a8edae062c91fda83fcb2766188088f2e (diff)
downloadrneovim-6f53cb1cc311025f0c21d142af06fbfbda113fc4.tar.gz
rneovim-6f53cb1cc311025f0c21d142af06fbfbda113fc4.tar.bz2
rneovim-6f53cb1cc311025f0c21d142af06fbfbda113fc4.zip
Extract memory.c from misc2.c
Diffstat (limited to 'src')
-rw-r--r--src/buffer.c1
-rw-r--r--src/charset.c1
-rw-r--r--src/diff.c1
-rw-r--r--src/digraph.c1
-rw-r--r--src/edit.c1
-rw-r--r--src/eval.c1
-rw-r--r--src/ex_cmds.c1
-rw-r--r--src/ex_cmds2.c1
-rw-r--r--src/ex_docmd.c1
-rw-r--r--src/ex_eval.c1
-rw-r--r--src/ex_getln.c1
-rw-r--r--src/file_search.c1
-rw-r--r--src/fileio.c1
-rw-r--r--src/fold.c1
-rw-r--r--src/garray.c1
-rw-r--r--src/getchar.c1
-rw-r--r--src/hardcopy.c1
-rw-r--r--src/hashtab.c1
-rw-r--r--src/if_cscope.c1
-rw-r--r--src/indent.c1
-rw-r--r--src/indent_c.c1
-rw-r--r--src/main.c1
-rw-r--r--src/mark.c1
-rw-r--r--src/mbyte.c1
-rw-r--r--src/memfile.c1
-rw-r--r--src/memline.c1
-rw-r--r--src/memory.c327
-rw-r--r--src/memory.h38
-rw-r--r--src/menu.c1
-rw-r--r--src/message.c1
-rw-r--r--src/misc1.c1
-rw-r--r--src/misc2.c306
-rw-r--r--src/misc2.h12
-rw-r--r--src/normal.c1
-rw-r--r--src/ops.c1
-rw-r--r--src/option.c1
-rw-r--r--src/os/event.c1
-rw-r--r--src/os/fs.c1
-rw-r--r--src/os/shell.c1
-rw-r--r--src/os/signal.c1
-rw-r--r--src/os_unix.c1
-rw-r--r--src/path.c1
-rw-r--r--src/quickfix.c1
-rw-r--r--src/regexp.c1
-rw-r--r--src/screen.c1
-rw-r--r--src/search.c1
-rw-r--r--src/spell.c1
-rw-r--r--src/syntax.c1
-rw-r--r--src/tag.c1
-rw-r--r--src/term.c1
-rw-r--r--src/ui.c1
-rw-r--r--src/undo.c1
-rw-r--r--src/window.c1
53 files changed, 415 insertions, 317 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 9f842cb4ee..5c77f85506 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -48,6 +48,7 @@
#include "mark.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/charset.c b/src/charset.c
index b88682b88a..928886f53a 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -12,6 +12,7 @@
#include "main.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "misc1.h"
#include "misc2.h"
#include "garray.h"
diff --git a/src/diff.c b/src/diff.c
index 67d941c35a..0db4890cfe 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -17,6 +17,7 @@
#include "message.h"
#include "misc1.h"
#include "misc2.h"
+#include "memory.h"
#include "move.h"
#include "normal.h"
#include "option.h"
diff --git a/src/digraph.c b/src/digraph.c
index bfa2d648c9..e0fda19387 100644
--- a/src/digraph.c
+++ b/src/digraph.c
@@ -12,6 +12,7 @@
#include "mbyte.h"
#include "message.h"
#include "misc2.h"
+#include "memory.h"
#include "garray.h"
#include "normal.h"
#include "screen.h"
diff --git a/src/edit.c b/src/edit.c
index 873d5b5898..55f9328ce0 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -30,6 +30,7 @@
#include "main.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/eval.c b/src/eval.c
index 94eca72b4e..32497a5e8f 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -35,6 +35,7 @@
#include "mark.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/ex_cmds.c b/src/ex_cmds.c
index d8839d213c..45f8e79e25 100644
--- a/src/ex_cmds.c
+++ b/src/ex_cmds.c
@@ -40,6 +40,7 @@
#include "misc1.h"
#include "misc2.h"
#include "garray.h"
+#include "memory.h"
#include "move.h"
#include "normal.h"
#include "ops.h"
diff --git a/src/ex_cmds2.c b/src/ex_cmds2.c
index fefe6ed461..a74f7b7d62 100644
--- a/src/ex_cmds2.c
+++ b/src/ex_cmds2.c
@@ -31,6 +31,7 @@
#include "misc1.h"
#include "misc2.h"
#include "garray.h"
+#include "memory.h"
#include "move.h"
#include "normal.h"
#include "option.h"
diff --git a/src/ex_docmd.c b/src/ex_docmd.c
index 4d80c9462e..23d03e7b32 100644
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -35,6 +35,7 @@
#include "mark.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "menu.h"
#include "message.h"
#include "misc1.h"
diff --git a/src/ex_eval.c b/src/ex_eval.c
index 79fbd03bc6..639dbbfd43 100644
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -19,6 +19,7 @@
#include "ex_docmd.h"
#include "message.h"
#include "misc2.h"
+#include "memory.h"
#include "regexp.h"
diff --git a/src/ex_getln.c b/src/ex_getln.c
index 75d400daba..9e0e35dd09 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -38,6 +38,7 @@
#include "message.h"
#include "misc1.h"
#include "misc2.h"
+#include "memory.h"
#include "cursor_shape.h"
#include "keymap.h"
#include "garray.h"
diff --git a/src/file_search.c b/src/file_search.c
index 5808c600dc..d5cc280c05 100644
--- a/src/file_search.c
+++ b/src/file_search.c
@@ -50,6 +50,7 @@
#include "file_search.h"
#include "charset.h"
#include "fileio.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/fileio.c b/src/fileio.c
index 9ab8090f51..33e9bb96cb 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -30,6 +30,7 @@
#include "mbyte.h"
#include "memfile.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/fold.c b/src/fold.c
index c789871804..fecb2e376c 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -23,6 +23,7 @@
#include "indent.h"
#include "mark.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/garray.c b/src/garray.c
index 87fbcb3d91..1c79274fc8 100644
--- a/src/garray.c
+++ b/src/garray.c
@@ -7,6 +7,7 @@
#include "vim.h"
#include "ascii.h"
#include "misc2.h"
+#include "memory.h"
#include "path.h"
#include "garray.h"
diff --git a/src/getchar.c b/src/getchar.c
index 95ca63cc16..4d6f693912 100644
--- a/src/getchar.c
+++ b/src/getchar.c
@@ -29,6 +29,7 @@
#include "main.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/hardcopy.c b/src/hardcopy.c
index 036c59d8ca..f8a9a5b496 100644
--- a/src/hardcopy.c
+++ b/src/hardcopy.c
@@ -24,6 +24,7 @@
#include "fileio.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/hashtab.c b/src/hashtab.c
index 2d9dcfe6ff..5ce33219bb 100644
--- a/src/hashtab.c
+++ b/src/hashtab.c
@@ -23,6 +23,7 @@
#include "vim.h"
#include "hashtab.h"
#include "message.h"
+#include "memory.h"
#include "misc2.h"
// Magic value for algorithm that walks through the array.
diff --git a/src/if_cscope.c b/src/if_cscope.c
index d5a766e1b9..d1f709010c 100644
--- a/src/if_cscope.c
+++ b/src/if_cscope.c
@@ -17,6 +17,7 @@
#include "message.h"
#include "misc1.h"
#include "misc2.h"
+#include "memory.h"
#include "os/time.h"
#include "path.h"
#include "quickfix.h"
diff --git a/src/indent.c b/src/indent.c
index decd1ed132..29fe3fbdb4 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -2,6 +2,7 @@
#include "eval.h"
#include "charset.h"
#include "memline.h"
+#include "memory.h"
#include "misc1.h"
#include "misc2.h"
#include "option.h"
diff --git a/src/indent_c.c b/src/indent_c.c
index 9a9968ab16..10e463860a 100644
--- a/src/indent_c.c
+++ b/src/indent_c.c
@@ -5,6 +5,7 @@
#include "indent.h"
#include "indent_c.h"
#include "memline.h"
+#include "memory.h"
#include "misc2.h"
#include "option.h"
#include "search.h"
diff --git a/src/main.c b/src/main.c
index 9722d7fdae..7ef7b48d2f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -33,6 +33,7 @@
#include "misc2.h"
#include "crypt.h"
#include "garray.h"
+#include "memory.h"
#include "move.h"
#include "normal.h"
#include "ops.h"
diff --git a/src/mark.c b/src/mark.c
index 1f113dd957..f133c1b3fa 100644
--- a/src/mark.c
+++ b/src/mark.c
@@ -24,6 +24,7 @@
#include "fold.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/mbyte.c b/src/mbyte.c
index facea8e02e..8278deb6f8 100644
--- a/src/mbyte.c
+++ b/src/mbyte.c
@@ -88,6 +88,7 @@
#include "message.h"
#include "misc1.h"
#include "misc2.h"
+#include "memory.h"
#include "option.h"
#include "screen.h"
#include "spell.h"
diff --git a/src/memfile.c b/src/memfile.c
index b55d9cee4f..cdcdd23484 100644
--- a/src/memfile.c
+++ b/src/memfile.c
@@ -41,6 +41,7 @@
#include "message.h"
#include "misc1.h"
#include "misc2.h"
+#include "memory.h"
#include "os_unix.h"
#include "path.h"
#include "ui.h"
diff --git a/src/memline.c b/src/memline.c
index fb5da647d8..c1375a1a85 100644
--- a/src/memline.c
+++ b/src/memline.c
@@ -54,6 +54,7 @@
#include "mark.h"
#include "mbyte.h"
#include "memfile.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/memory.c b/src/memory.c
new file mode 100644
index 0000000000..66a4a5d3c6
--- /dev/null
+++ b/src/memory.c
@@ -0,0 +1,327 @@
+ // Various routines dealing with allocation and deallocation of memory.
+
+#include <string.h>
+
+#include "vim.h"
+#include "misc2.h"
+#include "file_search.h"
+#include "blowfish.h"
+#include "buffer.h"
+#include "charset.h"
+#include "diff.h"
+#include "edit.h"
+#include "eval.h"
+#include "ex_cmds.h"
+#include "ex_docmd.h"
+#include "ex_getln.h"
+#include "fileio.h"
+#include "fold.h"
+#include "getchar.h"
+#include "mark.h"
+#include "mbyte.h"
+#include "memfile.h"
+#include "memline.h"
+#include "memory.h"
+#include "message.h"
+#include "misc1.h"
+#include "move.h"
+#include "option.h"
+#include "ops.h"
+#include "os_unix.h"
+#include "path.h"
+#include "quickfix.h"
+#include "regexp.h"
+#include "screen.h"
+#include "search.h"
+#include "spell.h"
+#include "syntax.h"
+#include "tag.h"
+#include "term.h"
+#include "ui.h"
+#include "window.h"
+#include "os/os.h"
+
+static void try_to_free_memory();
+
+/*
+ * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
+ * Use lalloc for larger blocks.
+ */
+char_u *alloc(unsigned size)
+{
+ return lalloc((long_u)size, TRUE);
+}
+
+/*
+ * Allocate memory and set all bytes to zero.
+ */
+char_u *alloc_clear(unsigned size)
+{
+ char_u *p;
+
+ p = lalloc((long_u)size, TRUE);
+ if (p != NULL)
+ (void)memset(p, 0, (size_t)size);
+ return p;
+}
+
+/*
+ * alloc() with check for maximum line length
+ */
+char_u *alloc_check(unsigned size)
+{
+#if !defined(UNIX) && !defined(__EMX__)
+ if (sizeof(int) == 2 && size > 0x7fff) {
+ /* Don't hide this message */
+ emsg_silent = 0;
+ EMSG(_("E340: Line is becoming too long"));
+ return NULL;
+ }
+#endif
+ return lalloc((long_u)size, TRUE);
+}
+
+/*
+ * Allocate memory like lalloc() and set all bytes to zero.
+ */
+char_u *lalloc_clear(long_u size, int message)
+{
+ char_u *p = lalloc(size, message);
+ memset(p, 0, (size_t)size);
+ return p;
+}
+
+/// Try to free memory. Used when trying to recover from out of memory errors.
+/// @see {xmalloc}
+static void try_to_free_memory()
+{
+ static bool trying_to_free = false;
+ // avoid recursive calls
+ if (trying_to_free)
+ return;
+ trying_to_free = true;
+
+ // free any scrollback text
+ clear_sb_text();
+ // Try to save all buffers and release as many blocks as possible
+ mf_release_all();
+ // cleanup recursive lists/dicts
+ garbage_collect();
+
+ trying_to_free = false;
+}
+
+void *xmalloc(size_t size)
+{
+ void *ret = malloc(size);
+
+ if (!ret && !size)
+ ret = malloc(1);
+
+ if (!ret) {
+ try_to_free_memory();
+ ret = malloc(size);
+ if (!ret && !size)
+ ret = malloc(1);
+ if (!ret) {
+ OUT_STR("Vim: Error: Out of memory.\n");
+ preserve_exit();
+ }
+ }
+
+ return ret;
+}
+
+void *xrealloc(void *ptr, size_t size)
+{
+ void *ret = realloc(ptr, size);
+
+ if (!ret && !size)
+ ret = realloc(ptr, 1);
+
+ if (!ret) {
+ try_to_free_memory();
+ ret = realloc(ptr, size);
+ if (!ret && !size)
+ ret = realloc(ptr, 1);
+ if (!ret) {
+ OUT_STR("Vim: Error: Out of memory.\n");
+ preserve_exit();
+ }
+ }
+
+ return ret;
+}
+
+char_u *lalloc(long_u size, int message)
+{
+ return (char_u *)xmalloc((size_t)size);
+}
+
+/*
+ * Avoid repeating the error message many times (they take 1 second each).
+ * Did_outofmem_msg is reset when a character is read.
+ */
+void do_outofmem_msg(long_u size)
+{
+ if (!did_outofmem_msg) {
+ /* Don't hide this message */
+ emsg_silent = 0;
+
+ /* Must come first to avoid coming back here when printing the error
+ * message fails, e.g. when setting v:errmsg. */
+ did_outofmem_msg = TRUE;
+
+ EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
+ }
+}
+
+#if defined(EXITFREE) || defined(PROTO)
+
+/*
+ * Free everything that we allocated.
+ * Can be used to detect memory leaks, e.g., with ccmalloc.
+ * NOTE: This is tricky! Things are freed that functions depend on. Don't be
+ * surprised if Vim crashes...
+ * Some things can't be freed, esp. things local to a library function.
+ */
+void free_all_mem(void)
+{
+ buf_T *buf, *nextbuf;
+ static int entered = FALSE;
+
+ /* When we cause a crash here it is caught and Vim tries to exit cleanly.
+ * Don't try freeing everything again. */
+ if (entered)
+ return;
+ entered = TRUE;
+
+ block_autocmds(); /* don't want to trigger autocommands here */
+
+ /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
+ p_ea = FALSE;
+ if (first_tabpage->tp_next != NULL)
+ do_cmdline_cmd((char_u *)"tabonly!");
+ if (firstwin != lastwin)
+ do_cmdline_cmd((char_u *)"only!");
+
+ /* Free all spell info. */
+ spell_free_all();
+
+ /* Clear user commands (before deleting buffers). */
+ ex_comclear(NULL);
+
+ /* Clear menus. */
+ do_cmdline_cmd((char_u *)"aunmenu *");
+ do_cmdline_cmd((char_u *)"menutranslate clear");
+
+ /* Clear mappings, abbreviations, breakpoints. */
+ do_cmdline_cmd((char_u *)"lmapclear");
+ do_cmdline_cmd((char_u *)"xmapclear");
+ do_cmdline_cmd((char_u *)"mapclear");
+ do_cmdline_cmd((char_u *)"mapclear!");
+ do_cmdline_cmd((char_u *)"abclear");
+ do_cmdline_cmd((char_u *)"breakdel *");
+ do_cmdline_cmd((char_u *)"profdel *");
+ do_cmdline_cmd((char_u *)"set keymap=");
+
+ free_titles();
+ free_findfile();
+
+ /* Obviously named calls. */
+ free_all_autocmds();
+ clear_termcodes();
+ free_all_options();
+ free_all_marks();
+ alist_clear(&global_alist);
+ free_homedir();
+ free_users();
+ free_search_patterns();
+ free_old_sub();
+ free_last_insert();
+ free_prev_shellcmd();
+ free_regexp_stuff();
+ free_tag_stuff();
+ free_cd_dir();
+ free_signs();
+ set_expr_line(NULL);
+ diff_clear(curtab);
+ clear_sb_text(); /* free any scrollback text */
+
+ /* Free some global vars. */
+ vim_free(last_cmdline);
+ vim_free(new_last_cmdline);
+ set_keep_msg(NULL, 0);
+
+ /* Clear cmdline history. */
+ p_hi = 0;
+ init_history();
+
+ {
+ win_T *win;
+ tabpage_T *tab;
+
+ qf_free_all(NULL);
+ /* Free all location lists */
+ FOR_ALL_TAB_WINDOWS(tab, win)
+ qf_free_all(win);
+ }
+
+ /* Close all script inputs. */
+ close_all_scripts();
+
+ /* Destroy all windows. Must come before freeing buffers. */
+ win_free_all();
+
+ /* Free all buffers. Reset 'autochdir' to avoid accessing things that
+ * were freed already. */
+ p_acd = FALSE;
+ for (buf = firstbuf; buf != NULL; ) {
+ nextbuf = buf->b_next;
+ close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
+ if (buf_valid(buf))
+ buf = nextbuf; /* didn't work, try next one */
+ else
+ buf = firstbuf;
+ }
+
+ free_cmdline_buf();
+
+ /* Clear registers. */
+ clear_registers();
+ ResetRedobuff();
+ ResetRedobuff();
+
+
+ /* highlight info */
+ free_highlight();
+
+ reset_last_sourcing();
+
+ free_tabpage(first_tabpage);
+ first_tabpage = NULL;
+
+# ifdef UNIX
+ /* Machine-specific free. */
+ mch_free_mem();
+# endif
+
+ /* message history */
+ for (;; )
+ if (delete_first_msg() == FAIL)
+ break;
+
+ eval_clear();
+
+ free_termoptions();
+
+ /* screenlines (can't display anything now!) */
+ free_screenlines();
+
+ clear_hl_tables();
+
+ vim_free(IObuff);
+ vim_free(NameBuff);
+}
+
+#endif
diff --git a/src/memory.h b/src/memory.h
new file mode 100644
index 0000000000..49661d6117
--- /dev/null
+++ b/src/memory.h
@@ -0,0 +1,38 @@
+#ifndef NEOVIM_MEMORY_H
+#define NEOVIM_MEMORY_H
+
+#include "func_attr.h"
+
+char_u *alloc(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
+char_u *alloc_clear(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
+char_u *alloc_check(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
+char_u *lalloc_clear(long_u size, int message) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
+
+/// malloc() wrapper
+///
+/// xmalloc() succeeds or gracefully aborts when out of memory.
+/// Before aborting try to free some memory and call malloc again.
+///
+/// @see {try_to_free_memory}
+/// @param size
+/// @return pointer to allocated space. Never NULL
+void *xmalloc(size_t size)
+ FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1) FUNC_ATTR_NONNULL_RET;
+
+/// realloc() wrapper
+///
+/// @see {xmalloc}
+/// @param size
+/// @return pointer to reallocated space. Never NULL
+void *xrealloc(void *ptr, size_t size)
+ FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET;
+
+/// Old low level memory allocation function.
+///
+/// @deprecated use xmalloc() directly instead
+/// @param size
+/// @return pointer to allocated space. Never NULL
+char_u *lalloc(long_u size, int message) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
+void do_outofmem_msg(long_u size);
+void free_all_mem(void);
+#endif
diff --git a/src/menu.c b/src/menu.c
index e64a28e91a..5461bd186a 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -20,6 +20,7 @@
#include "eval.h"
#include "ex_docmd.h"
#include "getchar.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/message.c b/src/message.c
index af62f80275..9a130dd202 100644
--- a/src/message.c
+++ b/src/message.c
@@ -23,6 +23,7 @@
#include "fileio.h"
#include "getchar.h"
#include "mbyte.h"
+#include "memory.h"
#include "misc1.h"
#include "misc2.h"
#include "keymap.h"
diff --git a/src/misc1.c b/src/misc1.c
index db8b9dc58f..ea3763f929 100644
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -32,6 +32,7 @@
#include "mark.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc2.h"
#include "garray.h"
diff --git a/src/misc2.c b/src/misc2.c
index c5620e2103..e97d838477 100644
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -31,6 +31,7 @@
#include "mbyte.h"
#include "memfile.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "move.h"
@@ -595,311 +596,6 @@ int leftcol_changed(void)
return retval;
}
-/**********************************************************************
- * Various routines dealing with allocation and deallocation of memory.
- */
-
-/*
- * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
- * Use lalloc for larger blocks.
- */
-char_u *alloc(unsigned size)
-{
- return lalloc((long_u)size, TRUE);
-}
-
-/*
- * Allocate memory and set all bytes to zero.
- */
-char_u *alloc_clear(unsigned size)
-{
- char_u *p;
-
- p = lalloc((long_u)size, TRUE);
- if (p != NULL)
- (void)memset(p, 0, (size_t)size);
- return p;
-}
-
-/*
- * alloc() with check for maximum line length
- */
-char_u *alloc_check(unsigned size)
-{
-#if !defined(UNIX) && !defined(__EMX__)
- if (sizeof(int) == 2 && size > 0x7fff) {
- /* Don't hide this message */
- emsg_silent = 0;
- EMSG(_("E340: Line is becoming too long"));
- return NULL;
- }
-#endif
- return lalloc((long_u)size, TRUE);
-}
-
-/*
- * Allocate memory like lalloc() and set all bytes to zero.
- */
-char_u *lalloc_clear(long_u size, int message)
-{
- char_u *p = lalloc(size, message);
- memset(p, 0, (size_t)size);
- return p;
-}
-
-/// Try to free memory. Used when trying to recover from out of memory errors.
-/// @see {xmalloc}
-void try_to_free_memory()
-{
- static bool trying_to_free = false;
- // avoid recursive calls
- if (trying_to_free)
- return;
- trying_to_free = true;
-
- // free any scrollback text
- clear_sb_text();
- // Try to save all buffers and release as many blocks as possible
- mf_release_all();
- // cleanup recursive lists/dicts
- garbage_collect();
-
- trying_to_free = false;
-}
-
-/// malloc() wrapper
-///
-/// xmalloc() succeeds or gracefully aborts when out of memory.
-/// Before aborting try to free some memory and call malloc again.
-///
-/// @see {try_to_free_memory}
-/// @param size
-/// @return pointer to allocated space. Never NULL
-void *xmalloc(size_t size)
-{
- void *ret = malloc(size);
-
- if (!ret && !size)
- ret = malloc(1);
-
- if (!ret) {
- try_to_free_memory();
- ret = malloc(size);
- if (!ret && !size)
- ret = malloc(1);
- if (!ret) {
- OUT_STR("Vim: Error: Out of memory.\n");
- preserve_exit();
- }
- }
-
- return ret;
-}
-
-/// realloc() wrapper
-///
-/// @see {xmalloc}
-/// @param size
-/// @return pointer to reallocated space. Never NULL
-void *xrealloc(void *ptr, size_t size)
-{
- void *ret = realloc(ptr, size);
-
- if (!ret && !size)
- ret = realloc(ptr, 1);
-
- if (!ret) {
- try_to_free_memory();
- ret = realloc(ptr, size);
- if (!ret && !size)
- ret = realloc(ptr, 1);
- if (!ret) {
- OUT_STR("Vim: Error: Out of memory.\n");
- preserve_exit();
- }
- }
-
- return ret;
-}
-
-/// Old low level memory allocation function.
-///
-/// @deprecated use xmalloc() directly instead
-/// @param size
-/// @return pointer to allocated space. Never NULL
-char_u *lalloc(long_u size, int message)
-{
- return (char_u *)xmalloc((size_t)size);
-}
-
-/*
- * Avoid repeating the error message many times (they take 1 second each).
- * Did_outofmem_msg is reset when a character is read.
- */
-void do_outofmem_msg(long_u size)
-{
- if (!did_outofmem_msg) {
- /* Don't hide this message */
- emsg_silent = 0;
-
- /* Must come first to avoid coming back here when printing the error
- * message fails, e.g. when setting v:errmsg. */
- did_outofmem_msg = TRUE;
-
- EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
- }
-}
-
-#if defined(EXITFREE) || defined(PROTO)
-
-/*
- * Free everything that we allocated.
- * Can be used to detect memory leaks, e.g., with ccmalloc.
- * NOTE: This is tricky! Things are freed that functions depend on. Don't be
- * surprised if Vim crashes...
- * Some things can't be freed, esp. things local to a library function.
- */
-void free_all_mem(void)
-{
- buf_T *buf, *nextbuf;
- static int entered = FALSE;
-
- /* When we cause a crash here it is caught and Vim tries to exit cleanly.
- * Don't try freeing everything again. */
- if (entered)
- return;
- entered = TRUE;
-
- block_autocmds(); /* don't want to trigger autocommands here */
-
- /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
- p_ea = FALSE;
- if (first_tabpage->tp_next != NULL)
- do_cmdline_cmd((char_u *)"tabonly!");
- if (firstwin != lastwin)
- do_cmdline_cmd((char_u *)"only!");
-
- /* Free all spell info. */
- spell_free_all();
-
- /* Clear user commands (before deleting buffers). */
- ex_comclear(NULL);
-
- /* Clear menus. */
- do_cmdline_cmd((char_u *)"aunmenu *");
- do_cmdline_cmd((char_u *)"menutranslate clear");
-
- /* Clear mappings, abbreviations, breakpoints. */
- do_cmdline_cmd((char_u *)"lmapclear");
- do_cmdline_cmd((char_u *)"xmapclear");
- do_cmdline_cmd((char_u *)"mapclear");
- do_cmdline_cmd((char_u *)"mapclear!");
- do_cmdline_cmd((char_u *)"abclear");
- do_cmdline_cmd((char_u *)"breakdel *");
- do_cmdline_cmd((char_u *)"profdel *");
- do_cmdline_cmd((char_u *)"set keymap=");
-
- free_titles();
- free_findfile();
-
- /* Obviously named calls. */
- free_all_autocmds();
- clear_termcodes();
- free_all_options();
- free_all_marks();
- alist_clear(&global_alist);
- free_homedir();
- free_users();
- free_search_patterns();
- free_old_sub();
- free_last_insert();
- free_prev_shellcmd();
- free_regexp_stuff();
- free_tag_stuff();
- free_cd_dir();
- free_signs();
- set_expr_line(NULL);
- diff_clear(curtab);
- clear_sb_text(); /* free any scrollback text */
-
- /* Free some global vars. */
- vim_free(last_cmdline);
- vim_free(new_last_cmdline);
- set_keep_msg(NULL, 0);
-
- /* Clear cmdline history. */
- p_hi = 0;
- init_history();
-
- {
- win_T *win;
- tabpage_T *tab;
-
- qf_free_all(NULL);
- /* Free all location lists */
- FOR_ALL_TAB_WINDOWS(tab, win)
- qf_free_all(win);
- }
-
- /* Close all script inputs. */
- close_all_scripts();
-
- /* Destroy all windows. Must come before freeing buffers. */
- win_free_all();
-
- /* Free all buffers. Reset 'autochdir' to avoid accessing things that
- * were freed already. */
- p_acd = FALSE;
- for (buf = firstbuf; buf != NULL; ) {
- nextbuf = buf->b_next;
- close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
- if (buf_valid(buf))
- buf = nextbuf; /* didn't work, try next one */
- else
- buf = firstbuf;
- }
-
- free_cmdline_buf();
-
- /* Clear registers. */
- clear_registers();
- ResetRedobuff();
- ResetRedobuff();
-
-
- /* highlight info */
- free_highlight();
-
- reset_last_sourcing();
-
- free_tabpage(first_tabpage);
- first_tabpage = NULL;
-
-# ifdef UNIX
- /* Machine-specific free. */
- mch_free_mem();
-# endif
-
- /* message history */
- for (;; )
- if (delete_first_msg() == FAIL)
- break;
-
- eval_clear();
-
- free_termoptions();
-
- /* screenlines (can't display anything now!) */
- free_screenlines();
-
- clear_hl_tables();
-
- vim_free(IObuff);
- vim_free(NameBuff);
-}
-
-#endif
-
/*
* Copy "string" into newly allocated memory.
*/
diff --git a/src/misc2.h b/src/misc2.h
index 41a87ab372..4013c6b99e 100644
--- a/src/misc2.h
+++ b/src/misc2.h
@@ -24,18 +24,6 @@ void check_cursor_col_win(win_T *win);
void check_cursor(void);
void adjust_cursor_col(void);
int leftcol_changed(void);
-char_u *alloc(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
-char_u *alloc_clear(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
-char_u *alloc_check(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
-char_u *lalloc_clear(long_u size, int message) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
-void try_to_free_memory();
-void *xmalloc(size_t size)
- FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1) FUNC_ATTR_NONNULL_RET;
-void *xrealloc(void *ptr, size_t size)
- FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET;
-char_u *lalloc(long_u size, int message) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
-void do_outofmem_msg(long_u size);
-void free_all_mem(void);
char_u *vim_strsave(char_u *string);
char_u *vim_strnsave(char_u *string, int len);
char_u *vim_strsave_escaped(char_u *string, char_u *esc_chars);
diff --git a/src/normal.c b/src/normal.c
index 6319e19203..358dfea075 100644
--- a/src/normal.c
+++ b/src/normal.c
@@ -36,6 +36,7 @@
#include "main.h"
#include "mark.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/ops.c b/src/ops.c
index 38517f9ad0..4946bf2af2 100644
--- a/src/ops.c
+++ b/src/ops.c
@@ -29,6 +29,7 @@
#include "mark.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/option.c b/src/option.c
index afcab215ac..fbc577293f 100644
--- a/src/option.c
+++ b/src/option.c
@@ -54,6 +54,7 @@
#include "mbyte.h"
#include "memfile.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/os/event.c b/src/os/event.c
index 943af8c24e..fe06ff4581 100644
--- a/src/os/event.c
+++ b/src/os/event.c
@@ -8,6 +8,7 @@
#include "os/input.h"
#include "os/signal.h"
#include "vim.h"
+#include "memory.h"
#include "misc2.h"
typedef struct EventNode {
diff --git a/src/os/fs.c b/src/os/fs.c
index c97a38f1c7..44156f0dde 100644
--- a/src/os/fs.c
+++ b/src/os/fs.c
@@ -1,6 +1,7 @@
// fs.c -- filesystem access
#include "os/os.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/os/shell.c b/src/os/shell.c
index 47a9b61bba..4c341a799e 100644
--- a/src/os/shell.c
+++ b/src/os/shell.c
@@ -5,6 +5,7 @@
#include "types.h"
#include "vim.h"
#include "ascii.h"
+#include "memory.h"
#include "misc2.h"
#include "option_defs.h"
#include "charset.h"
diff --git a/src/os/signal.c b/src/os/signal.c
index 9c75377062..4b14090c56 100644
--- a/src/os/signal.c
+++ b/src/os/signal.c
@@ -8,6 +8,7 @@
#include "memline.h"
#include "eval.h"
#include "term.h"
+#include "memory.h"
#include "misc1.h"
#include "misc2.h"
#include "os/event.h"
diff --git a/src/os_unix.c b/src/os_unix.c
index 7663bf6997..3afd4b9843 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -40,6 +40,7 @@
#include "main.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/path.c b/src/path.c
index a07c897735..a2d69100fd 100644
--- a/src/path.c
+++ b/src/path.c
@@ -11,6 +11,7 @@
#include "garray.h"
#include "memfile.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/quickfix.c b/src/quickfix.c
index aa33a2073f..05ea75b16f 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -32,6 +32,7 @@
#include "message.h"
#include "misc1.h"
#include "misc2.h"
+#include "memory.h"
#include "move.h"
#include "normal.h"
#include "option.h"
diff --git a/src/regexp.c b/src/regexp.c
index bfd7cda9fe..8ce9ffef42 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -53,6 +53,7 @@
#include "ex_cmds2.h"
#include "mark.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/screen.c b/src/screen.c
index 7cbe287538..bd895cf1d5 100644
--- a/src/screen.c
+++ b/src/screen.c
@@ -106,6 +106,7 @@
#include "main.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "menu.h"
#include "message.h"
#include "misc1.h"
diff --git a/src/search.c b/src/search.c
index 091178f693..eed9457a1c 100644
--- a/src/search.c
+++ b/src/search.c
@@ -29,6 +29,7 @@
#include "mark.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/spell.c b/src/spell.c
index 5ed788816a..d349b9fe80 100644
--- a/src/spell.c
+++ b/src/spell.c
@@ -314,6 +314,7 @@
#include "hashtab.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/syntax.c b/src/syntax.c
index 73db29bc49..453ee49e4b 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -26,6 +26,7 @@
#include "indent_c.h"
#include "mbyte.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
diff --git a/src/tag.c b/src/tag.c
index 74fe04af19..836136ddef 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -33,6 +33,7 @@
#include "misc2.h"
#include "file_search.h"
#include "garray.h"
+#include "memory.h"
#include "move.h"
#include "option.h"
#include "os_unix.h"
diff --git a/src/term.c b/src/term.c
index f1f0ff2b0a..0553d98a81 100644
--- a/src/term.c
+++ b/src/term.c
@@ -38,6 +38,7 @@
#include "misc2.h"
#include "garray.h"
#include "keymap.h"
+#include "memory.h"
#include "move.h"
#include "normal.h"
#include "option.h"
diff --git a/src/ui.c b/src/ui.c
index 43a99adef4..0c9a86d618 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -28,6 +28,7 @@
#include "misc1.h"
#include "misc2.h"
#include "garray.h"
+#include "memory.h"
#include "move.h"
#include "normal.h"
#include "option.h"
diff --git a/src/undo.c b/src/undo.c
index 7cfefa2479..150c31f600 100644
--- a/src/undo.c
+++ b/src/undo.c
@@ -94,6 +94,7 @@
#include "message.h"
#include "misc1.h"
#include "misc2.h"
+#include "memory.h"
#include "crypt.h"
#include "garray.h"
#include "option.h"
diff --git a/src/window.c b/src/window.c
index 924c64d75a..2ea8a29084 100644
--- a/src/window.c
+++ b/src/window.c
@@ -27,6 +27,7 @@
#include "main.h"
#include "mark.h"
#include "memline.h"
+#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"