aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/memory.c')
-rw-r--r--src/nvim/memory.c90
1 files changed, 47 insertions, 43 deletions
diff --git a/src/nvim/memory.c b/src/nvim/memory.c
index 7a8fc4da75..cc9c047fa0 100644
--- a/src/nvim/memory.c
+++ b/src/nvim/memory.c
@@ -1,26 +1,26 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
- // Various routines dealing with allocation and deallocation of memory.
+// Various routines dealing with allocation and deallocation of memory.
#include <assert.h>
#include <inttypes.h>
-#include <string.h>
#include <stdbool.h>
+#include <string.h>
-#include "nvim/vim.h"
+#include "nvim/api/vim.h"
#include "nvim/context.h"
+#include "nvim/decoration.h"
#include "nvim/eval.h"
#include "nvim/highlight.h"
+#include "nvim/lua/executor.h"
#include "nvim/memfile.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/ui.h"
#include "nvim/sign.h"
-#include "nvim/api/vim.h"
-#include "nvim/lua/executor.h"
-#include "nvim/decoration.h"
+#include "nvim/ui.h"
+#include "nvim/vim.h"
#ifdef UNIT_TESTING
# define malloc(size) mem_malloc(size)
@@ -47,8 +47,9 @@ void try_to_free_memory(void)
{
static bool trying_to_free = false;
// avoid recursive calls
- if (trying_to_free)
+ if (trying_to_free) {
return;
+ }
trying_to_free = true;
// free any scrollback text
@@ -182,7 +183,7 @@ void *xmallocz(size_t size)
}
void *ret = xmalloc(total_size);
- ((char*)ret)[size] = 0;
+ ((char *)ret)[size] = 0;
return ret;
}
@@ -339,16 +340,16 @@ char *xstpcpy(char *restrict dst, const char *restrict src)
char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen)
FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{
- const char *p = memchr(src, '\0', maxlen);
- if (p) {
- size_t srclen = (size_t)(p - src);
- memcpy(dst, src, srclen);
- memset(dst + srclen, 0, maxlen - srclen);
- return dst + srclen;
- } else {
- memcpy(dst, src, maxlen);
- return dst + maxlen;
- }
+ const char *p = memchr(src, '\0', maxlen);
+ if (p) {
+ size_t srclen = (size_t)(p - src);
+ memcpy(dst, src, srclen);
+ memset(dst + srclen, 0, maxlen - srclen);
+ return dst + srclen;
+ } else {
+ memcpy(dst, src, maxlen);
+ return dst + maxlen;
+ }
}
/// xstrlcpy - Copy a NUL-terminated string into a sized buffer
@@ -447,7 +448,7 @@ void *xmemrchr(const void *src, uint8_t c, size_t len)
{
while (len--) {
if (((uint8_t *)src)[len] == c) {
- return (uint8_t *) src + len;
+ return (uint8_t *)src + len;
}
}
return NULL;
@@ -500,7 +501,7 @@ bool striequal(const char *a, const char *b)
void do_outofmem_msg(size_t size)
{
if (!did_outofmem_msg) {
- /* Don't hide this message */
+ // Don't hide this message
emsg_silent = 0;
/* Must come first to avoid coming back here when printing the error
@@ -523,14 +524,15 @@ void time_to_bytes(time_t time_, uint8_t buf[8])
#if defined(EXITFREE)
-#include "nvim/file_search.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
#include "nvim/diff.h"
#include "nvim/edit.h"
+#include "nvim/eval/typval.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_getln.h"
+#include "nvim/file_search.h"
#include "nvim/fileio.h"
#include "nvim/fold.h"
#include "nvim/getchar.h"
@@ -538,8 +540,9 @@ void time_to_bytes(time_t time_, uint8_t buf[8])
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/move.h"
-#include "nvim/option.h"
#include "nvim/ops.h"
+#include "nvim/option.h"
+#include "nvim/os/os.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
#include "nvim/quickfix.h"
@@ -550,8 +553,6 @@ void time_to_bytes(time_t time_, uint8_t buf[8])
#include "nvim/syntax.h"
#include "nvim/tag.h"
#include "nvim/window.h"
-#include "nvim/os/os.h"
-#include "nvim/eval/typval.h"
/*
* Free everything that we allocated.
@@ -562,7 +563,7 @@ void time_to_bytes(time_t time_, uint8_t buf[8])
*/
void free_all_mem(void)
{
- buf_T *buf, *nextbuf;
+ buf_T *buf, *nextbuf;
// When we cause a crash here it is caught and Vim tries to exit cleanly.
// Don't try freeing everything again.
@@ -574,10 +575,11 @@ void free_all_mem(void)
// Don't want to trigger autocommands from here on.
block_autocmds();
- /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
+ // Close all tabs and windows. Reset 'equalalways' to avoid redraws.
p_ea = false;
- if (first_tabpage->tp_next != NULL)
+ if (first_tabpage->tp_next != NULL) {
do_cmdline_cmd("tabonly!");
+ }
if (!ONE_WINDOW) {
// to keep things simple, don't perform this
@@ -586,17 +588,17 @@ void free_all_mem(void)
do_cmdline_cmd("only!");
}
- /* Free all spell info. */
+ // Free all spell info.
spell_free_all();
- /* Clear user commands (before deleting buffers). */
+ // Clear user commands (before deleting buffers).
ex_comclear(NULL);
- /* Clear menus. */
+ // Clear menus.
do_cmdline_cmd("aunmenu *");
do_cmdline_cmd("menutranslate clear");
- /* Clear mappings, abbreviations, breakpoints. */
+ // Clear mappings, abbreviations, breakpoints.
do_cmdline_cmd("lmapclear");
do_cmdline_cmd("xmapclear");
do_cmdline_cmd("mapclear");
@@ -609,7 +611,7 @@ void free_all_mem(void)
free_titles();
free_findfile();
- /* Obviously named calls. */
+ // Obviously named calls.
free_all_autocmds();
free_all_marks();
alist_clear(&global_alist);
@@ -627,25 +629,25 @@ void free_all_mem(void)
diff_clear(curtab);
clear_sb_text(true); // free any scrollback text
- /* Free some global vars. */
+ // Free some global vars.
xfree(last_cmdline);
xfree(new_last_cmdline);
set_keep_msg(NULL, 0);
- /* Clear cmdline history. */
+ // Clear cmdline history.
p_hi = 0;
init_history();
qf_free_all(NULL);
- /* Free all location lists */
+ // Free all location lists
FOR_ALL_TAB_WINDOWS(tab, win) {
qf_free_all(win);
}
- /* Close all script inputs. */
+ // Close all script inputs.
close_all_scripts();
- /* Destroy all windows. Must come before freeing buffers. */
+ // Destroy all windows. Must come before freeing buffers.
win_free_all();
// Free all option values. Must come after closing windows.
@@ -653,13 +655,13 @@ void free_all_mem(void)
free_arshape_buf();
- /* Clear registers. */
+ // Clear registers.
clear_registers();
ResetRedobuff();
ResetRedobuff();
- /* highlight info */
+ // highlight info
free_highlight();
reset_last_sourcing();
@@ -667,10 +669,12 @@ void free_all_mem(void)
free_tabpage(first_tabpage);
first_tabpage = NULL;
- /* message history */
- for (;; )
- if (delete_first_msg() == FAIL)
+ // message history
+ for (;; ) {
+ if (delete_first_msg() == FAIL) {
break;
+ }
+ }
eval_clear();
api_vim_free_all_mem();