[xiph-commits] r17372 - experimental/derf/theora-ptalarbvorm/lib

tterribe at svn.xiph.org tterribe at svn.xiph.org
Sat Aug 28 11:32:13 PDT 2010


Author: tterribe
Date: 2010-08-28 11:32:12 -0700 (Sat, 28 Aug 2010)
New Revision: 17372

Modified:
   experimental/derf/theora-ptalarbvorm/lib/decode.c
   experimental/derf/theora-ptalarbvorm/lib/encode.c
   experimental/derf/theora-ptalarbvorm/lib/internal.c
   experimental/derf/theora-ptalarbvorm/lib/internal.h
Log:
glibc only guarantees 8-byte aligned mallocs on x86-32.
Add an aligned malloc so we can guarantee at least 16-byte alignment.


Modified: experimental/derf/theora-ptalarbvorm/lib/decode.c
===================================================================
--- experimental/derf/theora-ptalarbvorm/lib/decode.c	2010-08-26 15:12:36 UTC (rev 17371)
+++ experimental/derf/theora-ptalarbvorm/lib/decode.c	2010-08-28 18:32:12 UTC (rev 17372)
@@ -1965,9 +1965,9 @@
 th_dec_ctx *th_decode_alloc(const th_info *_info,const th_setup_info *_setup){
   oc_dec_ctx *dec;
   if(_info==NULL||_setup==NULL)return NULL;
-  dec=_ogg_malloc(sizeof(*dec));
+  dec=oc_aligned_malloc(sizeof(*dec),16);
   if(dec==NULL||oc_dec_init(dec,_info,_setup)<0){
-    _ogg_free(dec);
+    oc_aligned_free(dec);
     return NULL;
   }
   dec->state.curframe_num=0;
@@ -1977,7 +1977,7 @@
 void th_decode_free(th_dec_ctx *_dec){
   if(_dec!=NULL){
     oc_dec_clear(_dec);
-    _ogg_free(_dec);
+    oc_aligned_free(_dec);
   }
 }
 

Modified: experimental/derf/theora-ptalarbvorm/lib/encode.c
===================================================================
--- experimental/derf/theora-ptalarbvorm/lib/encode.c	2010-08-26 15:12:36 UTC (rev 17371)
+++ experimental/derf/theora-ptalarbvorm/lib/encode.c	2010-08-28 18:32:12 UTC (rev 17372)
@@ -1337,9 +1337,9 @@
 th_enc_ctx *th_encode_alloc(const th_info *_info){
   oc_enc_ctx *enc;
   if(_info==NULL)return NULL;
-  enc=_ogg_malloc(sizeof(*enc));
+  enc=oc_aligned_malloc(sizeof(*enc),16);
   if(enc==NULL||oc_enc_init(enc,_info)<0){
-    _ogg_free(enc);
+    oc_aligned_free(enc);
     return NULL;
   }
   return enc;
@@ -1348,7 +1348,7 @@
 void th_encode_free(th_enc_ctx *_enc){
   if(_enc!=NULL){
     oc_enc_clear(_enc);
-    _ogg_free(_enc);
+    oc_aligned_free(_enc);
   }
 }
 

Modified: experimental/derf/theora-ptalarbvorm/lib/internal.c
===================================================================
--- experimental/derf/theora-ptalarbvorm/lib/internal.c	2010-08-26 15:12:36 UTC (rev 17371)
+++ experimental/derf/theora-ptalarbvorm/lib/internal.c	2010-08-28 18:32:12 UTC (rev 17372)
@@ -171,6 +171,30 @@
 
 
 
+void *oc_aligned_malloc(size_t _sz,size_t _align){
+  unsigned char *p;
+  if(_align>UCHAR_MAX||(_align&_align-1)||_sz>~(size_t)0-_align)return NULL;
+  p=(unsigned char *)_ogg_malloc(_sz+_align);
+  if(p!=NULL){
+    int offs;
+    offs=((p-(unsigned char *)0)-1&_align-1);
+    p[offs]=offs;
+    p+=offs+1;
+  }
+  return p;
+}
+
+void oc_aligned_free(void *_ptr){
+  unsigned char *p;
+  p=(unsigned char *)_ptr;
+  if(p!=NULL){
+    int offs;
+    offs=*--p;
+    _ogg_free(p-offs);
+  }
+}
+
+
 void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz){
   size_t  rowsz;
   size_t  colsz;

Modified: experimental/derf/theora-ptalarbvorm/lib/internal.h
===================================================================
--- experimental/derf/theora-ptalarbvorm/lib/internal.h	2010-08-26 15:12:36 UTC (rev 17371)
+++ experimental/derf/theora-ptalarbvorm/lib/internal.h	2010-08-28 18:32:12 UTC (rev 17372)
@@ -421,6 +421,8 @@
 
 
 int oc_ilog(unsigned _v);
+void *oc_aligned_malloc(size_t _sz,size_t _align);
+void oc_aligned_free(void *_ptr);
 void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz);
 void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz);
 void oc_free_2d(void *_ptr);



More information about the commits mailing list