History

mem: introduce av_malloc_array and av_mallocz_array

Both function ease allocating large arrays implementing the overflow check inside it.

Luca Barbato committed 14 years ago

Commit f3e5e6f1 parent 2d497c13 files · +39 −3Browse files

Unified · Split

3 files
  1. doc/APIchanges +3 −0
  2. libavutil/mem.h +35 −2
  3. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2011-04-18 · 12 unmodified lines
13 13
14API changes, most recent first:14API changes, most recent first:
15 15
162012-07-10 - xxxxxxx - lavu 51.37.0
17 Add av_malloc_array() and av_mallocz_array()
18
162012-06-22 - xxxxxxx - lavu 51.34.0192012-06-22 - xxxxxxx - lavu 51.34.0
17 Add av_usleep()20 Add av_usleep()
18 21
libavutil/mem.h+35−2
@@ -63,9 +63,9 @@ · 62 unmodified lines
63#endif63#endif
64 64
65#if AV_GCC_VERSION_AT_LEAST(4,3)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#else67#else
68 #define av_alloc_size(n)68 #define av_alloc_size(...)
69#endif69#endif
70 70
71/**71/**
@@ -78,6 +78,22 @@ · 6 unmodified lines
78 */78 */
79void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);79void *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 */
90av_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 * Allocate or reallocate a block of memory.98 * Allocate or reallocate a block of memory.
83 * If ptr is NULL and size > 0, allocate a new block. If99 * 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 */
113void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);129void *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 */
141av_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 * Duplicate the string s.149 * Duplicate the string s.
117 * @param s string to be duplicated150 * @param s string to be duplicated
libavutil/version.h+1−1
@@ -37,7 +37,7 @@ · 36 unmodified lines
37 */37 */
38 38
39#define LIBAVUTIL_VERSION_MAJOR 5139#define LIBAVUTIL_VERSION_MAJOR 51
40#define LIBAVUTIL_VERSION_MINOR 3640#define LIBAVUTIL_VERSION_MINOR 37
41#define LIBAVUTIL_VERSION_MICRO 041#define LIBAVUTIL_VERSION_MICRO 0
42 42
43#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \43#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t