Re: [libav-devel] [PATCH 1/3] avutil: Add functions for allocating opaque contexts for algorithms

2012-10-11 Thread Luca Barbato
On 10/11/2012 03:19 PM, Martin Storsjö wrote:
> The current API where the plain size is exposed is not of much
> use - in most cases it is allocated dynamically anyway.
> 

The set looks fine.

lu

___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel


[libav-devel] [PATCH 1/3] avutil: Add functions for allocating opaque contexts for algorithms

2012-10-11 Thread Martin Storsjö
The current API where the plain size is exposed is not of much
use - in most cases it is allocated dynamically anyway.

If allocated e.g. on the stack via an uint8_t array, there's no
guarantee that the struct's members are aligned properly (unless
the array is overallocated and the opaque pointer within it
manually aligned to some unspecified alignment).
---
 doc/APIchanges  |4 
 libavutil/aes.c |7 +++
 libavutil/aes.h |   12 +++-
 libavutil/md5.c |8 
 libavutil/md5.h |8 +++-
 libavutil/sha.c |8 
 libavutil/sha.h |   12 +++-
 libavutil/tree.c|7 +++
 libavutil/tree.h|   12 +++-
 libavutil/version.h |5 -
 10 files changed, 78 insertions(+), 5 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 3e93354..81a2266 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,10 @@ libavutil: 2011-04-18
 
 API changes, most recent first:
 
+2012-10-xx - xxx - lavu 51.43.0 - aes.h, md5.h, sha.h, tree.h
+  Add functions for allocating the opaque contexts for the algorithms,
+  deprecate the context size variables.
+
 2012-10-xx - xxx - lavf 54.18.0 - avio.h
   Add avio_closep to complement avio_close.
 
diff --git a/libavutil/aes.c b/libavutil/aes.c
index 6803c71..4656a48 100644
--- a/libavutil/aes.c
+++ b/libavutil/aes.c
@@ -39,7 +39,14 @@ typedef struct AVAES {
 int rounds;
 } AVAES;
 
+#if FF_API_CONTEXT_SIZE
 const int av_aes_size= sizeof(AVAES);
+#endif
+
+struct AVAES *av_aes_alloc(void)
+{
+return av_mallocz(sizeof(struct AVAES));
+}
 
 static const uint8_t rcon[10] = {
   0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
diff --git a/libavutil/aes.h b/libavutil/aes.h
index cf7b462..edff275 100644
--- a/libavutil/aes.h
+++ b/libavutil/aes.h
@@ -23,17 +23,27 @@
 
 #include 
 
+#include "attributes.h"
+#include "version.h"
+
 /**
  * @defgroup lavu_aes AES
  * @ingroup lavu_crypto
  * @{
  */
 
-extern const int av_aes_size;
+#if FF_API_CONTEXT_SIZE
+extern attribute_deprecated const int av_aes_size;
+#endif
 
 struct AVAES;
 
 /**
+ * Allocate an AVAES context.
+ */
+struct AVAES *av_aes_alloc(void);
+
+/**
  * Initialize an AVAES context.
  * @param key_bits 128, 192 or 256
  * @param decrypt 0 for encryption, 1 for decryption
diff --git a/libavutil/md5.c b/libavutil/md5.c
index ca0e598..93a91d7 100644
--- a/libavutil/md5.c
+++ b/libavutil/md5.c
@@ -34,6 +34,7 @@
 #include "bswap.h"
 #include "intreadwrite.h"
 #include "md5.h"
+#include "mem.h"
 
 typedef struct AVMD5{
 uint64_t len;
@@ -41,7 +42,14 @@ typedef struct AVMD5{
 uint32_t ABCD[4];
 } AVMD5;
 
+#if FF_API_CONTEXT_SIZE
 const int av_md5_size = sizeof(AVMD5);
+#endif
+
+struct AVMD5 *av_md5_alloc(void)
+{
+return av_mallocz(sizeof(struct AVMD5));
+}
 
 static const uint8_t S[4][4] = {
 { 7, 12, 17, 22 },  /* round 1 */
diff --git a/libavutil/md5.h b/libavutil/md5.h
index c5b858a..29e4e7c 100644
--- a/libavutil/md5.h
+++ b/libavutil/md5.h
@@ -23,16 +23,22 @@
 
 #include 
 
+#include "attributes.h"
+#include "version.h"
+
 /**
  * @defgroup lavu_md5 MD5
  * @ingroup lavu_crypto
  * @{
  */
 
-extern const int av_md5_size;
+#if FF_API_CONTEXT_SIZE
+extern attribute_deprecated const int av_md5_size;
+#endif
 
 struct AVMD5;
 
+struct AVMD5 *av_md5_alloc(void);
 void av_md5_init(struct AVMD5 *ctx);
 void av_md5_update(struct AVMD5 *ctx, const uint8_t *src, const int len);
 void av_md5_final(struct AVMD5 *ctx, uint8_t *dst);
diff --git a/libavutil/sha.c b/libavutil/sha.c
index cbe1608..d583191 100644
--- a/libavutil/sha.c
+++ b/libavutil/sha.c
@@ -26,6 +26,7 @@
 #include "bswap.h"
 #include "sha.h"
 #include "intreadwrite.h"
+#include "mem.h"
 
 /** hash context */
 typedef struct AVSHA {
@@ -37,7 +38,14 @@ typedef struct AVSHA {
 void (*transform)(uint32_t *state, const uint8_t buffer[64]);
 } AVSHA;
 
+#if FF_API_CONTEXT_SIZE
 const int av_sha_size = sizeof(AVSHA);
+#endif
+
+struct AVSHA *av_sha_alloc(void)
+{
+return av_mallocz(sizeof(struct AVSHA));
+}
 
 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits
 
diff --git a/libavutil/sha.h b/libavutil/sha.h
index 8350954..4c9a0c9 100644
--- a/libavutil/sha.h
+++ b/libavutil/sha.h
@@ -23,17 +23,27 @@
 
 #include 
 
+#include "attributes.h"
+#include "version.h"
+
 /**
  * @defgroup lavu_sha SHA
  * @ingroup lavu_crypto
  * @{
  */
 
-extern const int av_sha_size;
+#if FF_API_CONTEXT_SIZE
+extern attribute_deprecated const int av_sha_size;
+#endif
 
 struct AVSHA;
 
 /**
+ * Allocate an AVSHA context.
+ */
+struct AVSHA *av_sha_alloc(void);
+
+/**
  * Initialize SHA-1 or SHA-2 hashing.
  *
  * @param context pointer to the function context (of size av_sha_size)
diff --git a/libavutil/tree.c b/libavutil/tree.c
index 0e68bb7..55dcbc5 100644
--- a/libavutil/tree.c
+++ b/libavutil/tree.c
@@ -28,7 +28,14 @@ typedef struct AVTreeNode {
 int state;
 } AVTreeNode;
 
+#if FF_API_CONTEXT_SIZE
 co