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
1313
1414 API changes, most recent first:
1515
16+2012-07-10 - xxxxxxx - lavu 51.37.0
17+ Add av_malloc_array() and av_mallocz_array()
18+
1619 2012-06-22 - xxxxxxx - lavu 51.34.0
1720 Add av_usleep()
1821
libavutil/mem.h+35−2
@@ -63,9 +63,9 @@ · 62 unmodified lines
6363 #endif
6464
6565 #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__)))
6767 #else
68− #define av_alloc_size(n)
68+ #define av_alloc_size(...)
6969 #endif
7070
7171 /**
@@ -78,6 +78,22 @@ · 6 unmodified lines
7878 */
7979 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
8080
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+
8197 /**
8298 * Allocate or reallocate a block of memory.
8399 * If ptr is NULL and size > 0, allocate a new block. If
@@ -112,6 +128,23 @@ void av_free(void *ptr); · 28 unmodified lines
112128 */
113129 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
114130
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+
115148 /**
116149 * Duplicate the string s.
117150 * @param s string to be duplicated
libavutil/version.h+1−1
@@ -37,7 +37,7 @@ · 36 unmodified lines
3737 */
3838
3939 #define LIBAVUTIL_VERSION_MAJOR 51
40−#define LIBAVUTIL_VERSION_MINOR 36
40+#define LIBAVUTIL_VERSION_MINOR 37
4141 #define LIBAVUTIL_VERSION_MICRO 0
4242
4343 #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