From 880957ad4e3fc0ff681025f5e29c5eccf797c564 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 10 May 2014 00:53:36 +0400 Subject: Move documentation from function declarations to definitions Uses a perl script to move it (scripts/movedocs.pl) --- src/nvim/memory.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) (limited to 'src/nvim/memory.c') diff --git a/src/nvim/memory.c b/src/nvim/memory.c index 238a6791c0..17d4a1ba8c 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -63,6 +63,14 @@ static void try_to_free_memory() trying_to_free = false; } +/// malloc() wrapper +/// +/// try_malloc() is a malloc() wrapper that tries to free some memory before +/// trying again. +/// +/// @see {try_to_free_memory} +/// @param size +/// @return pointer to allocated space. NULL if out of memory void *try_malloc(size_t size) { void *ret = malloc(size); @@ -80,6 +88,12 @@ void *try_malloc(size_t size) return ret; } +/// try_malloc() wrapper that shows an out-of-memory error message to the user +/// before returning NULL +/// +/// @see {try_malloc} +/// @param size +/// @return pointer to allocated space. NULL if out of memory void *verbose_try_malloc(size_t size) { void *ret = try_malloc(size); @@ -89,6 +103,14 @@ void *verbose_try_malloc(size_t size) return ret; } +/// 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 {try_to_free_memory} +/// @param size +/// @return pointer to allocated space. Never NULL void *xmalloc(size_t size) { void *ret = try_malloc(size); @@ -100,6 +122,12 @@ void *xmalloc(size_t size) return ret; } +/// calloc() wrapper +/// +/// @see {xmalloc} +/// @param count +/// @param size +/// @return pointer to allocated space. Never NULL void *xcalloc(size_t count, size_t size) { void *ret = calloc(count, size); @@ -121,6 +149,11 @@ void *xcalloc(size_t count, size_t size) 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); @@ -142,6 +175,11 @@ void *xrealloc(void *ptr, size_t size) return 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) { size_t total_size = size + 1; @@ -158,17 +196,62 @@ void *xmallocz(size_t size) return 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) { return memcpy(xmallocz(len), data, len); } +/// The xstpcpy() function shall copy the string pointed to by src (including +/// the terminating NUL character) into the array pointed to by dst. +/// +/// The xstpcpy() function shall return a pointer to the terminating NUL +/// character 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. +/// +/// This is the Neovim version of stpcpy(3) as defined in POSIX 2008. We +/// don't require that supported platforms implement POSIX 2008, so we +/// implement our own version. +/// +/// @param dst +/// @param src char *xstpcpy(char *restrict dst, const char *restrict src) { const size_t len = strlen(src); return (char *)memcpy(dst, src, len + 1) + len; } +/// The xstpncpy() function shall copy 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, the xstpncpy() function +/// shall return 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. +/// +/// TODO(aktau): I don't see a good reason to have this last behaviour, and +/// it is potentially wasteful. Could we perhaps deviate from the standard +/// and not zero the rest of the buffer? +/// +/// @param dst +/// @param src +/// @param maxlen char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen) { const char *p = memchr(src, '\0', maxlen); @@ -183,6 +266,17 @@ char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen) } } +/// 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. +/// +/// @param dst Where to copy the string to +/// @param src Where to copy the string from +/// @param size Size of destination buffer +/// @return Length of the source string (i.e.: strlen(src)) size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size) { size_t ret = strlen(src); @@ -196,6 +290,11 @@ size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size) return ret; } +/// 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) { char *ret = strdup(str); @@ -212,12 +311,23 @@ char *xstrdup(const char *str) return 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) { char *p = memchr(str, '\0', len); return xmemdupz(str, p ? (size_t)(p - str) : len); } +/// Duplicates a chunk of memory using xmalloc +/// +/// @see {xmalloc} +/// @param data pointer to the chunk +/// @param len size of the chunk +/// @return a pointer void *xmemdup(const void *data, size_t len) { return memcpy(xmalloc(len), data, len); -- cgit From 70929f7e1616bab2783cc5735c6061981cda8a0f Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 10 May 2014 17:24:13 +0400 Subject: Add automatic generation of headers - The 'stripdecls.py' script replaces declarations in all headers by includes to generated headers. `ag '#\s*if(?!ndef NEOVIM_).*((?!#\s*endif).*\n)*#ifdef INCLUDE_GENERATED'` was used for this. - Add and integrate gendeclarations.lua into the build system to generate the required includes. - Add -Wno-unused-function - Made a bunch of old-style definitions ANSI This adds a requirement: all type and structure definitions must be present before INCLUDE_GENERATED_DECLARATIONS-protected include. Warning: mch_expandpath (path.h.generated.h) was moved manually. So far it is the only exception. --- src/nvim/memory.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/nvim/memory.c') diff --git a/src/nvim/memory.c b/src/nvim/memory.c index 17d4a1ba8c..fdec9a49f9 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -41,7 +41,9 @@ #include "nvim/window.h" #include "nvim/os/os.h" -static void try_to_free_memory(); +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "memory.c.generated.h" +#endif /// Try to free memory. Used when trying to recover from out of memory errors. /// @see {xmalloc} @@ -71,7 +73,7 @@ static void try_to_free_memory() /// @see {try_to_free_memory} /// @param size /// @return pointer to allocated space. NULL if out of memory -void *try_malloc(size_t size) +void *try_malloc(size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1) { void *ret = malloc(size); @@ -94,7 +96,7 @@ void *try_malloc(size_t size) /// @see {try_malloc} /// @param size /// @return pointer to allocated space. NULL if out of memory -void *verbose_try_malloc(size_t size) +void *verbose_try_malloc(size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1) { void *ret = try_malloc(size); if (!ret) { @@ -112,6 +114,7 @@ void *verbose_try_malloc(size_t size) /// @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 { void *ret = try_malloc(size); @@ -129,6 +132,7 @@ void *xmalloc(size_t size) /// @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 { void *ret = calloc(count, size); @@ -155,6 +159,7 @@ void *xcalloc(size_t count, size_t size) /// @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 { void *ret = realloc(ptr, size); @@ -296,6 +301,7 @@ size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size) /// @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 { char *ret = strdup(str); @@ -317,6 +323,7 @@ char *xstrdup(const char *str) /// @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 { char *p = memchr(str, '\0', len); return xmemdupz(str, p ? (size_t)(p - str) : len); -- cgit