Browse code

Add av_calloc() helper.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Laurent Aimar authored on 2011/09/25 01:39:13
Showing 2 changed files
... ...
@@ -168,6 +168,13 @@ void *av_mallocz(size_t size)
168 168
     return ptr;
169 169
 }
170 170
 
171
+void *av_calloc(size_t nmemb, size_t size)
172
+{
173
+    if (size <= 0 || nmemb >= INT_MAX / size)
174
+        return NULL;
175
+    return av_mallocz(nmemb * size);
176
+}
177
+
171 178
 char *av_strdup(const char *s)
172 179
 {
173 180
     char *ptr= NULL;
... ...
@@ -107,6 +107,18 @@ void av_free(void *ptr);
107 107
 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
108 108
 
109 109
 /**
110
+ * Allocate a block of nmemb * size bytes with alignment suitable for all
111
+ * memory accesses (including vectors if available on the CPU) and
112
+ * zero all the bytes of the block.
113
+ * The allocation will fail if nmemb * size is greater than or equal
114
+ * to INT_MAX.
115
+ * @param nmemb
116
+ * @param size
117
+ * @return Pointer to the allocated block, NULL if it cannot be allocated.
118
+ */
119
+void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
120
+
121
+/**
110 122
  * Duplicate the string s.
111 123
  * @param s string to be duplicated
112 124
  * @return Pointer to a newly allocated string containing a