Functions | Variables
memory.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <stdbool.h>
#include "nvim/vim.h"
#include "nvim/eval.h"
#include "nvim/highlight.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"

Functions

void try_to_free_memory (void)
 
void * try_malloc (size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1)
 
void * verbose_try_malloc (size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1)
 
void * xmalloc (size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1) FUNC_ATTR_NONNULL_RET
 
void xfree (void *ptr)
 
void * xcalloc (size_t count, size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE_PROD(1
 
 if (!ret)
 
void * xrealloc (void *ptr, size_t size) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET
 
void * xmallocz (size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT
 
void * xmemdupz (const void *data, size_t len) FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
 
char * xstrchrnul (const char *str, char c) FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
 
void * xmemscan (const void *addr, char c, size_t size) FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
 
void strchrsub (char *str, char c, char x) FUNC_ATTR_NONNULL_ALL
 
void memchrsub (void *data, char c, char x, size_t len) FUNC_ATTR_NONNULL_ALL
 
size_t strcnt (const char *str, char c) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
 
size_t memcnt (const void *data, char c, size_t len) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
 
char * xstpcpy (char *restrict dst, const char *restrict src) FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
 
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
 
size_t xstrlcpy (char *restrict dst, const char *restrict src, size_t dsize) FUNC_ATTR_NONNULL_ALL
 
size_t xstrlcat (char *const dst, const char *const src, const size_t dsize) FUNC_ATTR_NONNULL_ALL
 
char * xstrdup (const char *str) FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL
 
char * xstrdupnul (const char *const str) FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET
 
void * xmemrchr (const void *src, uint8_t c, size_t len) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
 
char * xstrndup (const char *str, size_t len) FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL
 
void * xmemdup (const void *data, size_t len) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
 
bool strequal (const char *a, const char *b) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
 Returns true if strings a and b are equal. Arguments may be NULL. More...
 
bool striequal (const char *a, const char *b) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
 Case-insensitive strequal. More...
 
void do_outofmem_msg (size_t size)
 
void time_to_bytes (time_t time_, uint8_t buf[8])
 Writes time_t to "buf[8]". More...
 

Variables

void FUNC_ATTR_NONNULL_RET
 
size_t allocated_size = count && size ? size : 1
 
void * ret = calloc(allocated_count, allocated_size)
 

Function Documentation

void do_outofmem_msg ( size_t  size)
if ( ret)
void memchrsub ( void *  data,
char  c,
char  x,
size_t  len 
)

Replaces every instance of c with x.

Parameters
dataAn object in memory. May contain NULs.
cThe unwanted byte.
xThe replacement.
lenThe length of data.
size_t memcnt ( const void *  data,
char  c,
size_t  len 
)

Counts the number of occurrences of byte c in data[len].

Parameters
dataPointer to the data to search.
cThe byte to search for.
lenThe length of data.
Returns
the number of occurrences of c in data[len].
void strchrsub ( char *  str,
char  c,
char  x 
)

Replaces every instance of c with x.

Warning
Will read past str + strlen(str) if c == NUL.
Parameters
strA NUL-terminated string.
cThe unwanted byte.
xThe replacement.
size_t strcnt ( const char *  str,
char  c 
)

Counts the number of occurrences of c in str.

Warning
Unsafe if c == NUL.
Parameters
strPointer to the string to search.
cThe byte to search for.
Returns
the number of occurrences of c in str.
bool strequal ( const char *  a,
const char *  b 
)

Returns true if strings a and b are equal. Arguments may be NULL.

bool striequal ( const char *  a,
const char *  b 
)

Case-insensitive strequal.

void time_to_bytes ( time_t  time_,
uint8_t  buf[8] 
)

Writes time_t to "buf[8]".

void* try_malloc ( size_t  size)

malloc() wrapper

try_malloc() is a malloc() wrapper that tries to free some memory before trying again.

See also
{try_to_free_memory}
Parameters
size
Returns
pointer to allocated space. NULL if out of memory
void try_to_free_memory ( void  )

Try to free memory. Used when trying to recover from out of memory errors.

See also
{xmalloc}
void* verbose_try_malloc ( size_t  size)

try_malloc() wrapper that shows an out-of-memory error message to the user before returning NULL

See also
{try_malloc}
Parameters
size
Returns
pointer to allocated space. NULL if out of memory
void* xcalloc ( size_t  count,
size_t  size 
)

calloc() wrapper

See also
{xmalloc}
Parameters
count
size
Returns
pointer to allocated space. Never NULL
void xfree ( void *  ptr)

free() wrapper that delegates to the backing memory manager

Note
Use XFREE_CLEAR() instead, if possible.
void* xmalloc ( size_t  size)

malloc() wrapper that never returns NULL

xmalloc() succeeds or gracefully aborts when out of memory. Before aborting try to free some memory and call malloc again.

See also
{try_to_free_memory}
Parameters
size
Returns
pointer to allocated space. Never NULL
void* xmallocz ( size_t  size)

xmalloc() wrapper that allocates size + 1 bytes and zeroes the last byte

See also
{xmalloc}
Parameters
size
Returns
pointer to allocated space. Never NULL
void* xmemdup ( const void *  data,
size_t  len 
)

Duplicates a chunk of memory using xmalloc

See also
{xmalloc}
Parameters
datapointer to the chunk
lensize of the chunk
Returns
a pointer
void* xmemdupz ( const void *  data,
size_t  len 
)

Allocates (len + 1) bytes of memory, duplicates len bytes of data to the allocated memory, zero terminates the allocated memory, and returns a pointer to the allocated memory. If the allocation fails, the program dies.

See also
{xmalloc}
Parameters
dataPointer to the data that will be copied
lennumber of bytes that will be copied
void* xmemrchr ( const void *  src,
uint8_t  c,
size_t  len 
)

A version of memchr that starts the search at src + len.

Based on glibc's memrchr.

Parameters
srcThe source memory object.
cThe byte to search for.
lenThe length of the memory object.
Returns
a pointer to the found byte in src[len], or NULL.
void* xmemscan ( const void *  addr,
char  c,
size_t  size 
)

A version of memchr() that returns a pointer one past the end if it doesn't find c.

Parameters
addrThe address of the memory object.
cThe char to look for.
sizeThe size of the memory object.
Returns
a pointer to the first instance of c, or one past the end if not found.
void* xrealloc ( void *  ptr,
size_t  size 
)

realloc() wrapper

See also
{xmalloc}
Parameters
size
Returns
pointer to reallocated space. Never NULL
char* xstpcpy ( char *restrict  dst,
const char *restrict  src 
)

Copies the string pointed to by src (including the terminating NUL character) into the array pointed to by dst.

Returns
pointer to the terminating NUL char copied into the dst buffer. This is the only difference with strcpy(), which returns dst.

WARNING: If copying takes place between objects that overlap, the behavior is undefined.

Nvim version of POSIX 2008 stpcpy(3). We do not require POSIX 2008, so implement our own version.

Parameters
dst
src
char* xstpncpy ( char *restrict  dst,
const char *restrict  src,
size_t  maxlen 
)

Copies not more than n bytes (bytes that follow a NUL character are not copied) from the array pointed to by src to the array pointed to by dst.

If a NUL character is written to the destination, xstpncpy() returns the address of the first such NUL character. Otherwise, it shall return &dst[maxlen].

WARNING: If copying takes place between objects that overlap, the behavior is undefined.

WARNING: xstpncpy will ALWAYS write maxlen bytes. If src is shorter than maxlen, zeroes will be written to the remaining bytes.

Parameters
dst
src
maxlen
char* xstrchrnul ( const char *  str,
char  c 
)

A version of strchr() that returns a pointer to the terminating NUL if it doesn't find c.

Parameters
strThe string to search.
cThe char to look for.
Returns
a pointer to the first instance of c, or to the NUL terminator if not found.
char* xstrdup ( const char *  str)

strdup() wrapper

See also
{xmalloc}
Parameters
str0-terminated string that will be copied
Returns
pointer to a copy of the string
char* xstrdupnul ( const char *const  str)

strdup() wrapper

Unlike xstrdup() allocates a new empty string if it receives NULL.

size_t xstrlcat ( char *const  dst,
const char *const  src,
const size_t  dsize 
)

Appends src to string dst of size dsize (unlike strncat, dsize is the full size of dst, not space left). At most dsize-1 characters will be copied. Always NUL terminates. src and dst may overlap.

See also
vim_strcat from Vim.
strlcat from OpenBSD.
Parameters
[in,out]dstBuffer to be appended-to. Must have a NUL byte.
[in]srcString to put at the end of dst.
[in]dsizeSize of dst including NUL byte. Must be greater than 0.
Returns
Length of the resulting string as if destination size was #SIZE_MAX. May be greater than dsize - 1, which would mean that string was truncated.
size_t xstrlcpy ( char *restrict  dst,
const char *restrict  src,
size_t  dsize 
)

xstrlcpy - Copy a NUL-terminated string into a sized buffer

Compatible with *BSD strlcpy: the result is always a valid NUL-terminated string that fits in the buffer (unless, of course, the buffer size is zero). It does not pad out the result like strncpy() does.

Parameters
[out]dstBuffer to store the result.
[in]srcString to be copied.
[in]dsizeSize of dst.
Returns
Length of src. May be greater than dsize - 1, which would mean that string was truncated.
char* xstrndup ( const char *  str,
size_t  len 
)

strndup() wrapper

See also
{xmalloc}
Parameters
str0-terminated string that will be copied
Returns
pointer to a copy of the string

Variable Documentation

size_t allocated_size = count && size ? size : 1
void FUNC_ATTR_NONNULL_RET
Initial value:
{
size_t allocated_count = count && size ? count : 1
return ret = calloc(allocated_count, allocated_size)