/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* alloc.c: functions for memory management
*/
#include "vim.h"
/**********************************************************************
* Various routines dealing with allocation and deallocation of memory.
*/
#if defined(MEM_PROFILE) || defined(PROTO)
# define MEM_SIZES 8200
static long_u mem_allocs[MEM_SIZES];
static long_u mem_frees[MEM_SIZES];
static long_u mem_allocated;
static long_u mem_freed;
static long_u mem_peak;
static long_u num_alloc;
static long_u num_freed;
static void
mem_pre_alloc_s(size_t *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_pre_alloc_l(size_t *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_post_alloc(
void **pp,
size_t size)
{
if (*pp == NULL)
return;
size -= sizeof(size_t);
*(long_u *)*pp = size;
if (size <= MEM_SIZES-1)
mem_allocs[size-1]++;
else
mem_allocs[MEM_SIZES-1]++;
mem_allocated += size;
if (mem_allocated - mem_freed > mem_peak)
mem_peak = mem_allocated - mem_freed;
num_alloc++;
*pp = (void *)((char *)*pp + sizeof(size_t));
}
static void
mem_pre_free(void **pp)
{
long_u size;
*pp = (void *)((char *)*pp - sizeof(size_t));
size = *(size_t *)*pp;
if (size <= MEM_SIZES-1)
mem_frees[size-1]++;
else
mem_frees[MEM_SIZES-1]++;
mem_freed += size;
num_freed++;
}
/*
* called on exit via atexit()
*/
void
vim_mem_profile_dump(void)
{
int i, j;
printf("\r\n");
j = 0;
for (i = 0; i < MEM_SIZES - 1; i++)
{
if (mem_allocs[i] || mem_frees[i])
{
if (mem_frees[i] > mem_allocs[i])
printf("\r\n%s", _("ERROR: "));
printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
j++;
if (j > 3)
{
j = 0;
printf("\r\n");
}
}
}
i = MEM_SIZES - 1;
if (mem_allocs[i])
{
printf("\r\n");
if (mem_frees[i] > mem_allocs[i])
puts(_("ERROR: "));
printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
}
printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
num_alloc, num_freed);
}
#endif // MEM_PROFILE
#ifdef FEAT_EVAL
int
alloc_does_fail(size_t size)
{
if (alloc_fail_countdown == 0)
{
if (--alloc_fail_repeat <= 0)
alloc_fail_id = 0;
do_outofmem_msg(size);
return TRUE;
}
--alloc_fail_countdown;
return FALSE;
}
#endif
/*
* Some memory is reserved for error messages and for being able to
* call mf_release_all(), which needs some memory for mf_trans_add().
*/
#define KEEP_ROOM (2 * 8192L)
#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
/*
* The normal way to allocate memory. This handles an out-of-memory situation
* as well as possible, still returns NULL when we're completely out.
*/
void *
alloc(size_t size)
{
return lalloc(size, TRUE);
}
/*
* alloc() with an ID for alloc_fail().
*/
void *
alloc_id(size_t size, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail(size))
return NULL;
#endif
return lalloc(size, TRUE);
}
/*
* Allocate memory and set all bytes to zero.
*/
void *
alloc_clear(size_t size)
{
void *p;
p = lalloc(size, TRUE);
if (p != NULL)
(void)vim_memset(p, 0, size);
return p;
}
/*
* Same as alloc_clear() but with allocation id for testing
*/
void *
alloc_clear_id(size_t size, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail(size))
return NULL;
#endif
return alloc_clear(size);
}
/*
* Allocate memory like lalloc() and set all bytes to zero.
*/
void *
lalloc_clear(size_t size, int message)
{
void *p;
p = lalloc(size, message);
if (p != NULL)
(void)vim_memset(p, 0, size);
return p;
}
/*
* Low level memory allocation function.
* This is used often, KEEP IT FAST!
*/
void *
lalloc(size_t size, int message)
{
void *p; // pointer to new storage space
static int releasing = FALSE; // don't do mf_release_all() recursive
int<