diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-06 10:42:34 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-06 11:32:47 -0300 |
commit | fdec1d126fa146b7158ba430b1beb5a9a9fe2d6f (patch) | |
tree | 4aad36c4299d589027593c0029373a0658142588 /src | |
parent | 461b939b012c2b9ae46fc610b666af6e6e264095 (diff) | |
download | rneovim-fdec1d126fa146b7158ba430b1beb5a9a9fe2d6f.tar.gz rneovim-fdec1d126fa146b7158ba430b1beb5a9a9fe2d6f.tar.bz2 rneovim-fdec1d126fa146b7158ba430b1beb5a9a9fe2d6f.zip |
Implement `xstrdup` and `xstrndup`
Utility functions for copying strings to newly-allocated chunks. They either
succeed or exit the program.
Diffstat (limited to 'src')
-rw-r--r-- | src/memory.c | 56 | ||||
-rw-r--r-- | src/memory.h | 16 |
2 files changed, 72 insertions, 0 deletions
diff --git a/src/memory.c b/src/memory.c index 66a4a5d3c6..d970740cd4 100644 --- a/src/memory.c +++ b/src/memory.c @@ -42,6 +42,17 @@ #include "os/os.h" static void try_to_free_memory(); +static void *xmallocz(size_t size); + +/// 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 +static void *xmemdupz(const void *data, size_t len); /* * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc(). @@ -153,6 +164,29 @@ void *xrealloc(void *ptr, size_t size) return ret; } +char * xstrdup(const char *str) +{ + char *ret = strdup(str); + + if (!ret) { + try_to_free_memory(); + ret = strdup(str); + if (!ret) { + OUT_STR("Vim: Error: Out of memory.\n"); + preserve_exit(); + } + } + + return ret; +} + +char *xstrndup(const char *str, size_t len) +{ + char *p = memchr(str, '\0', len); + return xmemdupz(str, p ? (size_t)(p - str) : len); +} + + char_u *lalloc(long_u size, int message) { return (char_u *)xmalloc((size_t)size); @@ -325,3 +359,25 @@ void free_all_mem(void) } #endif + +static void *xmallocz(size_t size) +{ + size_t total_size = size + 1; + void *ret; + + if (total_size < size) { + OUT_STR("Vim: Data too large to fit into virtual memory space\n"); + preserve_exit(); + } + + ret = xmalloc(total_size); + ((char*)ret)[size] = 0; + + return ret; +} + +static void *xmemdupz(const void *data, size_t len) +{ + return memcpy(xmallocz(len), data, len); +} + diff --git a/src/memory.h b/src/memory.h index 49661d6117..f5dbe6f453 100644 --- a/src/memory.h +++ b/src/memory.h @@ -27,6 +27,22 @@ void *xmalloc(size_t size) void *xrealloc(void *ptr, size_t size) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_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) + 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 |