From patchwork Mon Aug 1 04:07:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timothy Gu X-Patchwork-Id: 46 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.67 with SMTP id o64csp2509877vsd; Sun, 31 Jul 2016 21:20:49 -0700 (PDT) X-Received: by 10.28.86.65 with SMTP id k62mr11944204wmb.53.1470025249174; Sun, 31 Jul 2016 21:20:49 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id mn4si29608170wjb.101.2016.07.31.21.20.48; Sun, 31 Jul 2016 21:20:49 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 60F0F689D44; Mon, 1 Aug 2016 07:20:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pa0-f67.google.com (mail-pa0-f67.google.com [209.85.220.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 934BB689D44 for ; Mon, 1 Aug 2016 07:20:29 +0300 (EEST) Received: by mail-pa0-f67.google.com with SMTP id q2so9135769pap.0 for ; Sun, 31 Jul 2016 21:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=3a+hFGguhd13t9MfM7IXIGPyVlMuvkmEECRPSoJtjiQ=; b=aKy/Ue/fMRi/CEtehuA8NVgMDDZHczPTrIJxEYi+U83XvI3fTz7opogXmsoNrqUb9G rUD+Eo9xvqA0a+uLmw//x3a+hRqdNZzCqONlSDlB+yy8GKT3ub4zJ85/vNbRuId+GvsV YNzSeBmro2ewCpOak5hYhcoFgS7Bn9XlVuJv86Utp5jRblSnaYcroJWz/p4ixfMt86Qe dmhw/YpIWN3JewegrSEP/CPmWI+CkGNYZoflf2IQ4WlpX287jJ9tUR+YAVlrXdgx9SR+ xmLXeKKA9x21Z/hMoAc7KJS0dKAkiuzdLkqmt0agG+HlA49Kmwx+ev43pQ/ueSK5WM1u w0TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=3a+hFGguhd13t9MfM7IXIGPyVlMuvkmEECRPSoJtjiQ=; b=K94xWkdUCJrkJaJbLNefHhMmn4PzIMIY6FgHuZuXtkhoGlSEYBLMpMsm/bIqQQizCe Vs36VVcHauA6On68Iu8gIaPKl8rV2aw3v/pOXBreDe/bkKH5aJsn5OFTtQ/bF/e29Pv1 fdbBnNTbpFxDsKikxLgCJJZiRHnlCmUgGHmfPAryXE3EUF71TrnEFqWPjADds5HksNq9 fMsU0WA6GIkjtXBDXwN/HL8wo7+KEspqDijtTaWPVZMgEtwrCKjLBgTkdgbmM1I5w2Ww o+jMtJZO04cxS9Nwa5/xtCzgkMXCwkHTxhaQ4PcyqhoZKhb1cTj2mzcPE/nqv+lE6t55 JiNA== X-Gm-Message-State: AEkoouuNgzcZPAcAaJ6qqqMfMLEI4S8aAzdDfVJRAEqUq72M/gyNiCeeIqWZyA4a6fxtBw== X-Received: by 10.66.25.171 with SMTP id d11mr91185367pag.3.1470024434557; Sun, 31 Jul 2016 21:07:14 -0700 (PDT) Received: from biggie.timothygu.me (ip68-5-186-115.oc.oc.cox.net. [68.5.186.115]) by smtp.gmail.com with ESMTPSA id f6sm41228562pfa.17.2016.07.31.21.07.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 31 Jul 2016 21:07:13 -0700 (PDT) From: Timothy Gu To: ffmpeg-devel@ffmpeg.org Date: Sun, 31 Jul 2016 21:07:03 -0700 Message-Id: <1470024424-23189-1-git-send-email-timothygu99@gmail.com> X-Mailer: git-send-email 2.1.4 Subject: [FFmpeg-devel] [PATCH 1/2] mem: Order function prototypes semantically X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Timothy Gu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavutil/mem.h | 198 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 99 insertions(+), 99 deletions(-) diff --git a/libavutil/mem.h b/libavutil/mem.h index 2f53b47..145ac91 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -83,6 +83,16 @@ void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1); /** + * Allocate a block of size bytes with alignment suitable for all + * memory accesses (including vectors if available on the CPU) and + * zero all the bytes of the block. + * @param size Size in bytes for the memory block to be allocated. + * @return Pointer to the allocated block, NULL if it cannot be allocated. + * @see av_malloc() + */ +void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1); + +/** * Allocate a block of size * nmemb bytes with av_malloc(). * @param nmemb Number of elements * @param size Size of the single element @@ -98,6 +108,34 @@ av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t siz } /** + * Allocate a block of size * nmemb bytes with av_mallocz(). + * @param nmemb Number of elements + * @param size Size of the single element + * @return Pointer to the allocated block, NULL if the block cannot + * be allocated. + * @see av_mallocz() + * @see av_malloc_array() + */ +av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size) +{ + if (!size || nmemb >= INT_MAX / size) + return NULL; + return av_mallocz(nmemb * size); +} + +/** + * Allocate a block of nmemb * size bytes with alignment suitable for all + * memory accesses (including vectors if available on the CPU) and + * zero all the bytes of the block. + * The allocation will fail if nmemb * size is greater than or equal + * to INT_MAX. + * @param nmemb + * @param size + * @return Pointer to the allocated block, NULL if it cannot be allocated. + */ +void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib; + +/** * Allocate or reallocate a block of memory. * If ptr is NULL and size > 0, allocate a new block. If * size is zero, free the memory block pointed to by ptr. @@ -119,16 +157,6 @@ void *av_realloc(void *ptr, size_t size) av_alloc_size(2); /** * Allocate or reallocate a block of memory. - * This function does the same thing as av_realloc, except: - * - It takes two arguments and checks the result of the multiplication for - * integer overflow. - * - It frees the input block in case of failure, thus avoiding the memory - * leak with the classic "buf = realloc(buf); if (!buf) return -1;". - */ -void *av_realloc_f(void *ptr, size_t nelem, size_t elsize); - -/** - * Allocate or reallocate a block of memory. * If *ptr is NULL and size > 0, allocate a new block. If * size is zero, free the memory block pointed to by ptr. * @param ptr Pointer to a pointer to a memory block already allocated @@ -148,6 +176,16 @@ av_warn_unused_result int av_reallocp(void *ptr, size_t size); /** + * Allocate or reallocate a block of memory. + * This function does the same thing as av_realloc, except: + * - It takes two arguments and checks the result of the multiplication for + * integer overflow. + * - It frees the input block in case of failure, thus avoiding the memory + * leak with the classic "buf = realloc(buf); if (!buf) return -1;". + */ +void *av_realloc_f(void *ptr, size_t nelem, size_t elsize); + +/** * Allocate or reallocate an array. * If ptr is NULL and nmemb > 0, allocate a new block. If * nmemb is zero, free the memory block pointed to by ptr. @@ -186,6 +224,42 @@ av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size) av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size); /** + * Reallocate the given block if it is not large enough, otherwise do nothing. + * + * @see av_realloc + */ +void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); + +/** + * Allocate a buffer, reusing the given one if large enough. + * + * Contrary to av_fast_realloc the current buffer contents might not be + * preserved and on error the old buffer is freed, thus no special + * handling to avoid memleaks is necessary. + * + * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer + * @param size size of the buffer *ptr points to + * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and + * *size 0 if an error occurred. + */ +void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size); + +/** + * Allocate a buffer, reusing the given one if large enough. + * + * All newly allocated space is initially cleared + * Contrary to av_fast_realloc the current buffer contents might not be + * preserved and on error the old buffer is freed, thus no special + * handling to avoid memleaks is necessary. + * + * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer + * @param size size of the buffer *ptr points to + * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and + * *size 0 if an error occurred. + */ +void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size); + +/** * Free a memory block which has been allocated with av_malloc(z)() or * av_realloc(). * @param ptr Pointer to the memory block which should be freed. @@ -196,42 +270,14 @@ av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size); void av_free(void *ptr); /** - * Allocate a block of size bytes with alignment suitable for all - * memory accesses (including vectors if available on the CPU) and - * zero all the bytes of the block. - * @param size Size in bytes for the memory block to be allocated. - * @return Pointer to the allocated block, NULL if it cannot be allocated. - * @see av_malloc() - */ -void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1); - -/** - * Allocate a block of nmemb * size bytes with alignment suitable for all - * memory accesses (including vectors if available on the CPU) and - * zero all the bytes of the block. - * The allocation will fail if nmemb * size is greater than or equal - * to INT_MAX. - * @param nmemb - * @param size - * @return Pointer to the allocated block, NULL if it cannot be allocated. - */ -void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib; - -/** - * Allocate a block of size * nmemb bytes with av_mallocz(). - * @param nmemb Number of elements - * @param size Size of the single element - * @return Pointer to the allocated block, NULL if the block cannot - * be allocated. - * @see av_mallocz() - * @see av_malloc_array() + * Free a memory block which has been allocated with av_malloc(z)() or + * av_realloc() and set the pointer pointing to it to NULL. + * @param ptr Pointer to the pointer to the memory block which should + * be freed. + * @note passing a pointer to a NULL pointer is safe and leads to no action. + * @see av_free() */ -av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size) -{ - if (!size || nmemb >= INT_MAX / size) - return NULL; - return av_mallocz(nmemb * size); -} +void av_freep(void *ptr); /** * Duplicate the string s. @@ -260,14 +306,15 @@ char *av_strndup(const char *s, size_t len) av_malloc_attrib; void *av_memdup(const void *p, size_t size); /** - * Free a memory block which has been allocated with av_malloc(z)() or - * av_realloc() and set the pointer pointing to it to NULL. - * @param ptr Pointer to the pointer to the memory block which should - * be freed. - * @note passing a pointer to a NULL pointer is safe and leads to no action. - * @see av_free() + * deliberately overlapping memcpy implementation + * @param dst destination buffer + * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0 + * @param cnt number of bytes to copy, must be >= 0 + * + * cnt > back is valid, this will copy the bytes we just copied, + * thus creating a repeating pattern with a period length of back. */ -void av_freep(void *ptr); +void av_memcpy_backptr(uint8_t *dst, int back, int cnt); /** * Add an element to a dynamic array. @@ -353,53 +400,6 @@ static inline int av_size_mult(size_t a, size_t b, size_t *r) void av_max_alloc(size_t max); /** - * deliberately overlapping memcpy implementation - * @param dst destination buffer - * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0 - * @param cnt number of bytes to copy, must be >= 0 - * - * cnt > back is valid, this will copy the bytes we just copied, - * thus creating a repeating pattern with a period length of back. - */ -void av_memcpy_backptr(uint8_t *dst, int back, int cnt); - -/** - * Reallocate the given block if it is not large enough, otherwise do nothing. - * - * @see av_realloc - */ -void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); - -/** - * Allocate a buffer, reusing the given one if large enough. - * - * Contrary to av_fast_realloc the current buffer contents might not be - * preserved and on error the old buffer is freed, thus no special - * handling to avoid memleaks is necessary. - * - * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer - * @param size size of the buffer *ptr points to - * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and - * *size 0 if an error occurred. - */ -void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size); - -/** - * Allocate a buffer, reusing the given one if large enough. - * - * All newly allocated space is initially cleared - * Contrary to av_fast_realloc the current buffer contents might not be - * preserved and on error the old buffer is freed, thus no special - * handling to avoid memleaks is necessary. - * - * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer - * @param size size of the buffer *ptr points to - * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and - * *size 0 if an error occurred. - */ -void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size); - -/** * @} */