| @@ -63,9 +63,9 @@ · 62 unmodified lines |
| 63 | 63 | #endif |
| 64 | 64 | |
| 65 | 65 | #if AV_GCC_VERSION_AT_LEAST(4,3) |
| 66 | | − #define av_alloc_size(n) __attribute__((alloc_size(n))) |
| 66 | + #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) |
| 67 | 67 | #else |
| 68 | | − #define av_alloc_size(n) |
| 68 | + #define av_alloc_size(...) |
| 69 | 69 | #endif |
| 70 | 70 | |
| 71 | 71 | /** |
| @@ -78,6 +78,22 @@ · 6 unmodified lines |
| 78 | 78 | */ |
| 79 | 79 | void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1); |
| 80 | 80 | |
| 81 | +/** |
| 82 | + * Helper function to allocate a block of size * nmemb bytes with |
| 83 | + * using av_malloc() |
| 84 | + * @param nmemb Number of elements |
| 85 | + * @param size Size of the single element |
| 86 | + * @return Pointer to the allocated block, NULL if the block cannot |
| 87 | + * be allocated. |
| 88 | + * @see av_malloc() |
| 89 | + */ |
| 90 | +av_alloc_size(1,2) static inline void *av_malloc_array(size_t nmemb, size_t size) |
| 91 | +{ |
| 92 | + if (size <= 0 || nmemb >= INT_MAX / size) |
| 93 | + return NULL; |
| 94 | + return av_malloc(nmemb * size); |
| 95 | +} |
| 96 | + |
| 81 | 97 | /** |
| 82 | 98 | * Allocate or reallocate a block of memory. |
| 83 | 99 | * If ptr is NULL and size > 0, allocate a new block. If |
| @@ -112,6 +128,23 @@ void av_free(void *ptr); · 28 unmodified lines |
| 112 | 128 | */ |
| 113 | 129 | void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1); |
| 114 | 130 | |
| 131 | +/** |
| 132 | + * Helper function to allocate a block of size * nmemb bytes with |
| 133 | + * using av_mallocz() |
| 134 | + * @param nmemb Number of elements |
| 135 | + * @param size Size of the single element |
| 136 | + * @return Pointer to the allocated block, NULL if the block cannot |
| 137 | + * be allocated. |
| 138 | + * @see av_mallocz() |
| 139 | + * @see av_malloc_array() |
| 140 | + */ |
| 141 | +av_alloc_size(1,2) static inline void *av_mallocz_array(size_t nmemb, size_t size) |
| 142 | +{ |
| 143 | + if (size <= 0 || nmemb >= INT_MAX / size) |
| 144 | + return NULL; |
| 145 | + return av_mallocz(nmemb * size); |
| 146 | +} |
| 147 | + |
| 115 | 148 | /** |
| 116 | 149 | * Duplicate the string s. |
| 117 | 150 | * @param s string to be duplicated |