X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Futil%2Fralloc.h;h=857ca5f797f06fec121857ef4722686cffa3ae98;hb=403eb507f586e62acd648778dc1e7d20b5e1fa2f;hp=7d90651966174e6242565dcea1cc8b39af5e953c;hpb=63d1ebca3a6e1ec51f7f31ab9b77af0fb7e7f857;p=mesa.git diff --git a/src/util/ralloc.h b/src/util/ralloc.h index 7d906519661..857ca5f797f 100644 --- a/src/util/ralloc.h +++ b/src/util/ralloc.h @@ -121,6 +121,23 @@ void *rzalloc_size(const void *ctx, size_t size) MALLOCLIKE; */ void *reralloc_size(const void *ctx, void *ptr, size_t size); +/** + * Resize a ralloc-managed array, preserving data and initializing any newly + * allocated data to zero. + * + * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the + * memory. Instead, it resizes it to a 0-byte ralloc context, just like + * calling ralloc_size(ctx, 0). This is different from talloc. + * + * \param ctx The context to use for new allocation. If \p ptr != NULL, + * it must be the same as ralloc_parent(\p ptr). + * \param ptr Pointer to the memory to be resized. May be NULL. + * \param old_size The amount of memory in the previous allocation, in bytes. + * \param new_size The amount of memory to allocate, in bytes. + */ +void *rerzalloc_size(const void *ctx, void *ptr, + size_t old_size, size_t new_size); + /// \defgroup array Array Allocators @{ /** @@ -177,6 +194,28 @@ void *reralloc_size(const void *ctx, void *ptr, size_t size); #define reralloc(ctx, ptr, type, count) \ ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count)) +/** + * \def rerzalloc(ctx, ptr, type, count) + * Resize a ralloc-managed array, preserving data and initializing any newly + * allocated data to zero. + * + * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the + * memory. Instead, it resizes it to a 0-byte ralloc context, just like + * calling ralloc_size(ctx, 0). This is different from talloc. + * + * More than a convenience function, this also checks for integer overflow when + * multiplying \c sizeof(type) and \p count. This is necessary for security. + * + * \param ctx The context to use for new allocation. If \p ptr != NULL, + * it must be the same as ralloc_parent(\p ptr). + * \param ptr Pointer to the array to be resized. May be NULL. + * \param type The element type. + * \param old_count The number of elements in the previous allocation. + * \param new_count The number of elements to allocate. + */ +#define rerzalloc(ctx, ptr, type, old_count, new_count) \ + ((type *) rerzalloc_array_size(ctx, ptr, sizeof(type), old_count, new_count)) + /** * Allocate memory for an array chained off the given context. * @@ -217,6 +256,29 @@ void *rzalloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIK */ void *reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count); + +/** + * Resize a ralloc-managed array, preserving data and initializing any newly + * allocated data to zero. + * + * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the + * memory. Instead, it resizes it to a 0-byte ralloc context, just like + * calling ralloc_size(ctx, 0). This is different from talloc. + * + * More than a convenience function, this also checks for integer overflow when + * multiplying \c sizeof(type) and \p count. This is necessary for security. + * + * \param ctx The context to use for new allocation. If \p ptr != NULL, + * it must be the same as ralloc_parent(\p ptr). + * \param ptr Pointer to the array to be resized. May be NULL. + * \param size The size of an individual element. + * \param old_count The number of elements in the previous allocation. + * \param new_count The number of elements to allocate. + * + * \return True unless allocation failed. + */ +void *rerzalloc_array_size(const void *ctx, void *ptr, size_t size, + unsigned old_count, unsigned new_count); /// @} /** @@ -292,6 +354,24 @@ bool ralloc_strcat(char **dest, const char *str); */ bool ralloc_strncat(char **dest, const char *str, size_t n); +/** + * Concatenate two strings, allocating the necessary space. + * + * This appends \p n bytes of \p str to \p *dest, using ralloc_resize + * to expand \p *dest to the appropriate size. \p dest will be updated to the + * new pointer unless allocation fails. + * + * The result will always be null-terminated. + * + * This function differs from ralloc_strcat() and ralloc_strncat() in that it + * does not do any strlen() calls which can become costly on large strings. + * + * \return True unless allocation failed. + */ +bool +ralloc_str_append(char **dest, const char *str, + size_t existing_length, size_t str_size); + /** * Print to a string. * @@ -412,7 +492,7 @@ bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args); private: \ static void _ralloc_destructor(void *p) \ { \ - reinterpret_cast(p)->~TYPE(); \ + reinterpret_cast(p)->TYPE::~TYPE(); \ } \ public: \ static void* operator new(size_t size, void *mem_ctx) \