[x264-devel] [PATCH 08/29] common: Move memory functions to a separate file
Vittorio Giovara
vittorio.giovara at gmail.com
Fri Feb 10 22:18:43 CET 2017
While these function do not depend on bit depth size, most of their
callers do, so they need to be moved to a self-contained module.
---
Makefile | 2 +-
common/common.c | 104 ----------------------------------------------
common/common.h | 9 +---
common/mem.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
common/mem.h | 49 ++++++++++++++++++++++
5 files changed, 177 insertions(+), 113 deletions(-)
create mode 100644 common/mem.c
create mode 100644 common/mem.h
diff --git a/Makefile b/Makefile
index 0718c2c..82df145 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ SRCS = common/mc.c common/predict.c common/pixel.c common/macroblock.c \
common/common.c common/osdep.c common/rectangle.c \
common/set.c common/quant.c common/deblock.c common/vlc.c \
common/mvpred.c common/bitstream.c \
- common/log.c \
+ common/log.c common/mem.c \
encoder/analyse.c encoder/me.c encoder/ratecontrol.c \
encoder/set.c encoder/macroblock.c encoder/cabac.c \
encoder/cavlc.c encoder/encoder.c encoder/lookahead.c
diff --git a/common/common.c b/common/common.c
index 05a2923..f196cce 100644
--- a/common/common.c
+++ b/common/common.c
@@ -28,13 +28,6 @@
#include <ctype.h>
-#if HAVE_MALLOC_H
-#include <malloc.h>
-#endif
-#if HAVE_THP
-#include <sys/mman.h>
-#endif
-
const int x264_bit_depth = BIT_DEPTH;
const int x264_chroma_format = X264_CHROMA_FORMAT;
@@ -1161,61 +1154,6 @@ void x264_picture_clean( x264_picture_t *pic )
memset( pic, 0, sizeof( x264_picture_t ) );
}
-/****************************************************************************
- * x264_malloc:
- ****************************************************************************/
-void *x264_malloc( int i_size )
-{
- uint8_t *align_buf = NULL;
-#if HAVE_MALLOC_H
-#if HAVE_THP
-#define HUGE_PAGE_SIZE 2*1024*1024
-#define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
- /* Attempt to allocate huge pages to reduce TLB misses. */
- if( i_size >= HUGE_PAGE_THRESHOLD )
- {
- align_buf = memalign( HUGE_PAGE_SIZE, i_size );
- if( align_buf )
- {
- /* Round up to the next huge page boundary if we are close enough. */
- size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
- madvise( align_buf, madv_size, MADV_HUGEPAGE );
- }
- }
- else
-#undef HUGE_PAGE_SIZE
-#undef HUGE_PAGE_THRESHOLD
-#endif
- align_buf = memalign( NATIVE_ALIGN, i_size );
-#else
- uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
- if( buf )
- {
- align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
- align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
- *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
- }
-#endif
- if( !align_buf )
- x264_log_internal( X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
- return align_buf;
-}
-
-/****************************************************************************
- * x264_free:
- ****************************************************************************/
-void x264_free( void *p )
-{
- if( p )
- {
-#if HAVE_MALLOC_H
- free( p );
-#else
- free( *( ( ( void **) p ) - 1 ) );
-#endif
- }
-}
-
/****************************************************************************
* x264_reduce_fraction:
****************************************************************************/
@@ -1241,48 +1179,6 @@ void name( type *n, type *d )\
REDUCE_FRACTION( x264_reduce_fraction , uint32_t )
REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
-/****************************************************************************
- * x264_slurp_file:
- ****************************************************************************/
-char *x264_slurp_file( const char *filename )
-{
- int b_error = 0;
- int64_t i_size;
- char *buf;
- FILE *fh = x264_fopen( filename, "rb" );
- if( !fh )
- return NULL;
-
- b_error |= fseek( fh, 0, SEEK_END ) < 0;
- b_error |= ( i_size = ftell( fh ) ) <= 0;
- if( WORD_SIZE == 4 )
- b_error |= i_size > INT32_MAX;
- b_error |= fseek( fh, 0, SEEK_SET ) < 0;
- if( b_error )
- goto error;
-
- buf = x264_malloc( i_size+2 );
- if( !buf )
- goto error;
-
- b_error |= fread( buf, 1, i_size, fh ) != i_size;
- fclose( fh );
- if( b_error )
- {
- x264_free( buf );
- return NULL;
- }
-
- if( buf[i_size-1] != '\n' )
- buf[i_size++] = '\n';
- buf[i_size] = '\0';
-
- return buf;
-error:
- fclose( fh );
- return NULL;
-}
-
/****************************************************************************
* x264_param2string:
****************************************************************************/
diff --git a/common/common.h b/common/common.h
index 35a982c..81f0503 100644
--- a/common/common.h
+++ b/common/common.h
@@ -39,7 +39,6 @@
#define XCHG(type,a,b) do { type t = a; a = b; b = t; } while( 0 )
#define IS_DISPOSABLE(type) ( type == X264_TYPE_B )
#define FIX8(f) ((int)(f*(1<<8)+.5))
-#define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
#define ARRAY_ELEMS(a) ((sizeof(a))/(sizeof(a[0])))
#define CHECKED_MALLOC( var, size )\
@@ -116,6 +115,7 @@ do {\
****************************************************************************/
#include "osdep.h"
#include "log.h"
+#include "mem.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
@@ -250,13 +250,6 @@ static const uint8_t x264_scan8[16*3 + 3] =
/****************************************************************************
* General functions
****************************************************************************/
-/* x264_malloc : will do or emulate a memalign
- * you have to use x264_free for buffers allocated with x264_malloc */
-void *x264_malloc( int );
-void x264_free( void * );
-
-/* x264_slurp_file: malloc space for the whole file and read it */
-char *x264_slurp_file( const char *filename );
/* x264_param2string: return a (malloced) string containing most of
* the encoding options */
diff --git a/common/mem.c b/common/mem.c
new file mode 100644
index 0000000..82cc08c
--- /dev/null
+++ b/common/mem.c
@@ -0,0 +1,126 @@
+/*****************************************************************************
+ * mem.c: memory functions
+ *****************************************************************************
+ * Copyright (C) 2003-2017 x264 project
+ *
+ * Authors: Loren Merritt <lorenm at u.washington.edu>
+ * Laurent Aimar <fenrir at via.ecp.fr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at licensing at x264.com.
+ *****************************************************************************/
+
+#include "mem.h"
+#include "log.h"
+#include "x264.h"
+
+/****************************************************************************
+ * x264_malloc:
+ ****************************************************************************/
+void *x264_malloc( int i_size )
+{
+ uint8_t *align_buf = NULL;
+#if HAVE_MALLOC_H
+#if HAVE_THP
+#define HUGE_PAGE_SIZE 2*1024*1024
+#define HUGE_PAGE_THRESHOLD HUGE_PAGE_SIZE*7/8 /* FIXME: Is this optimal? */
+ /* Attempt to allocate huge pages to reduce TLB misses. */
+ if( i_size >= HUGE_PAGE_THRESHOLD )
+ {
+ align_buf = memalign( HUGE_PAGE_SIZE, i_size );
+ if( align_buf )
+ {
+ /* Round up to the next huge page boundary if we are close enough. */
+ size_t madv_size = (i_size + HUGE_PAGE_SIZE - HUGE_PAGE_THRESHOLD) & ~(HUGE_PAGE_SIZE-1);
+ madvise( align_buf, madv_size, MADV_HUGEPAGE );
+ }
+ }
+ else
+#undef HUGE_PAGE_SIZE
+#undef HUGE_PAGE_THRESHOLD
+#endif
+ align_buf = memalign( NATIVE_ALIGN, i_size );
+#else
+ uint8_t *buf = malloc( i_size + (NATIVE_ALIGN-1) + sizeof(void **) );
+ if( buf )
+ {
+ align_buf = buf + (NATIVE_ALIGN-1) + sizeof(void **);
+ align_buf -= (intptr_t) align_buf & (NATIVE_ALIGN-1);
+ *( (void **) ( align_buf - sizeof(void **) ) ) = buf;
+ }
+#endif
+ if( !align_buf )
+ x264_log_internal( X264_LOG_ERROR, "malloc of size %d failed\n", i_size );
+ return align_buf;
+}
+
+/****************************************************************************
+ * x264_free:
+ ****************************************************************************/
+void x264_free( void *p )
+{
+ if( p )
+ {
+#if HAVE_MALLOC_H
+ free( p );
+#else
+ free( *( ( ( void **) p ) - 1 ) );
+#endif
+ }
+}
+
+/****************************************************************************
+ * x264_slurp_file:
+ ****************************************************************************/
+char *x264_slurp_file( const char *filename )
+{
+ int b_error = 0;
+ int64_t i_size;
+ char *buf;
+ FILE *fh = x264_fopen( filename, "rb" );
+ if( !fh )
+ return NULL;
+
+ b_error |= fseek( fh, 0, SEEK_END ) < 0;
+ b_error |= ( i_size = ftell( fh ) ) <= 0;
+ if( WORD_SIZE == 4 )
+ b_error |= i_size > INT32_MAX;
+ b_error |= fseek( fh, 0, SEEK_SET ) < 0;
+ if( b_error )
+ goto error;
+
+ buf = x264_malloc( i_size+2 );
+ if( !buf )
+ goto error;
+
+ b_error |= fread( buf, 1, i_size, fh ) != i_size;
+ fclose( fh );
+ if( b_error )
+ {
+ x264_free( buf );
+ return NULL;
+ }
+
+ if( buf[i_size-1] != '\n' )
+ buf[i_size++] = '\n';
+ buf[i_size] = '\0';
+
+ return buf;
+error:
+ fclose( fh );
+ return NULL;
+}
diff --git a/common/mem.h b/common/mem.h
new file mode 100644
index 0000000..a89316e
--- /dev/null
+++ b/common/mem.h
@@ -0,0 +1,49 @@
+/*****************************************************************************
+ * mem.h: memory functions
+ *****************************************************************************
+ * Copyright (C) 2003-2017 x264 project
+ *
+ * Authors: Loren Merritt <lorenm at u.washington.edu>
+ * Laurent Aimar <fenrir at via.ecp.fr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at licensing at x264.com.
+ *****************************************************************************/
+
+#ifndef COMMON_MEM_H
+#define COMMON_MEM_H
+
+#include "osdep.h"
+
+#if HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+#if HAVE_THP
+#include <sys/mman.h>
+#endif
+
+#define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
+
+/* x264_malloc : will do or emulate a memalign
+ * you have to use x264_free for buffers allocated with x264_malloc */
+void *x264_malloc( int );
+void x264_free( void * );
+
+/* x264_slurp_file: malloc space for the whole file and read it */
+char *x264_slurp_file( const char *filename );
+
+#endif
--
2.10.0
More information about the x264-devel
mailing list