mirror of
https://github.com/neovim/neovim.git
synced 2025-09-27 13:38:34 +00:00

Unfortunately there's still a case where NULL can be returned from file_pat_to_reg_pat(). xmemdupz() and xmallocz() aren't static anymore. There are many use cases for these function.
81 lines
2.5 KiB
C
81 lines
2.5 KiB
C
#ifndef NEOVIM_MEMORY_H
|
|
#define NEOVIM_MEMORY_H
|
|
|
|
#include "func_attr.h"
|
|
#include "types.h"
|
|
#include "vim.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);
|
|
|
|
/// 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;
|
|
|
|
/// calloc() wrapper
|
|
///
|
|
/// @see {xmalloc}
|
|
/// @param count
|
|
/// @param size
|
|
/// @return pointer to allocated space. Never NULL
|
|
void *xcalloc(size_t count, size_t size)
|
|
FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE_PROD(1, 2) 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;
|
|
|
|
/// xmalloc() wrapper that allocates size + 1 bytes and zeroes the last byte
|
|
///
|
|
/// @see {xmalloc}
|
|
/// @param size
|
|
/// @return pointer to allocated space. Never NULL
|
|
void *xmallocz(size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET;
|
|
|
|
/// 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 {xmalloc}
|
|
/// @param data Pointer to the data that will be copied
|
|
/// @param len number of bytes that will be copied
|
|
void *xmemdupz(const void *data, size_t len);
|
|
|
|
/// strdup() wrapper
|
|
///
|
|
/// @see {xmalloc}
|
|
/// @param str 0-terminated string that will be copied
|
|
/// @return pointer to a copy of the string
|
|
char * xstrdup(const char *str)
|
|
FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET;
|
|
|
|
/// strndup() wrapper
|
|
///
|
|
/// @see {xmalloc}
|
|
/// @param str 0-terminated string that will be copied
|
|
/// @return pointer to a copy of the string
|
|
char * xstrndup(const char *str, size_t len)
|
|
FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT 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
|