[asterisk-commits] kpfleming: branch kpfleming/aligner2 r127828 - in /team/kpfleming/aligner2: c...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jul 3 13:35:40 CDT 2008


Author: kpfleming
Date: Thu Jul  3 13:35:40 2008
New Revision: 127828

URL: http://svn.digium.com/view/asterisk?view=rev&rev=127828
Log:
after some experimentation, i've learned that the compiler can lay out a structure type *at run time*, computing all the padding required to achieve proper alignment for the elements of the structure. using this method, we can then just ask the compiler to tell us how much space we need to allocate for the structure, and the location of each element within the structure. this greatly simplifies the code to create a 'runtime structure', and also ensures that any platform-specific alignment rules will be followed. now for some testing!


Modified:
    team/kpfleming/aligner2/codecs/codec_a_mu.c
    team/kpfleming/aligner2/codecs/codec_adpcm.c
    team/kpfleming/aligner2/codecs/codec_alaw.c
    team/kpfleming/aligner2/codecs/codec_g726.c
    team/kpfleming/aligner2/codecs/codec_gsm.c
    team/kpfleming/aligner2/codecs/codec_ilbc.c
    team/kpfleming/aligner2/codecs/codec_lpc10.c
    team/kpfleming/aligner2/codecs/codec_speex.c
    team/kpfleming/aligner2/codecs/codec_ulaw.c
    team/kpfleming/aligner2/include/asterisk/compiler.h
    team/kpfleming/aligner2/include/asterisk/translate.h
    team/kpfleming/aligner2/main/translate.c

Modified: team/kpfleming/aligner2/codecs/codec_a_mu.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_a_mu.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_a_mu.c (original)
+++ team/kpfleming/aligner2/codecs/codec_a_mu.c Thu Jul  3 13:35:40 2008
@@ -57,7 +57,7 @@
 {
 	int x = f->samples;
 	unsigned char *src = f->data;
-	unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
+	unsigned char *dst = (unsigned char *) pvt->outbuf + pvt->samples;
 
 	pvt->samples += x;
 	pvt->datalen += x;
@@ -73,7 +73,7 @@
 {
 	int x = f->samples;
 	unsigned char *src = f->data;
-	unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
+	unsigned char *dst = (unsigned char *) pvt->outbuf + pvt->samples;
 
 	pvt->samples += x;
 	pvt->datalen += x;

Modified: team/kpfleming/aligner2/codecs/codec_adpcm.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_adpcm.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_adpcm.c (original)
+++ team/kpfleming/aligner2/codecs/codec_adpcm.c Thu Jul  3 13:35:40 2008
@@ -240,7 +240,7 @@
 	struct adpcm_decoder_pvt *tmp = pvt->pvt;
 	int x = f->datalen;
 	unsigned char *src = f->data;
-	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf + pvt->samples;
 
 	while (x--) {
 		*dst++ = decode((*src >> 4) & 0xf, &tmp->state);
@@ -268,6 +268,7 @@
 	struct ast_frame *f;
 	int i;
 	int samples = pvt->samples;	/* save original number */
+	char *dst = (char *) pvt->outbuf;
   
 	if (samples < 2)
 		return NULL;
@@ -275,9 +276,7 @@
 	pvt->samples &= ~1; /* atomic size is 2 samples */
 
 	for (i = 0; i < pvt->samples; i += 2) {
-		pvt->outbuf[i/2] =
-			(adpcm(tmp->inbuf[i  ], &tmp->state) << 4) |
-			(adpcm(tmp->inbuf[i+1], &tmp->state)     );
+		dst[i/2] = (adpcm(tmp->inbuf[i  ], &tmp->state) << 4) | (adpcm(tmp->inbuf[i+1], &tmp->state));
 	};
 
 	f = ast_trans_frameout(pvt, pvt->samples/2, 0);

Modified: team/kpfleming/aligner2/codecs/codec_alaw.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_alaw.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_alaw.c (original)
+++ team/kpfleming/aligner2/codecs/codec_alaw.c Thu Jul  3 13:35:40 2008
@@ -56,7 +56,7 @@
 {
 	int i = f->samples;
 	unsigned char *src = f->data;
-	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf + pvt->samples;
 
 	pvt->samples += i;
 	pvt->datalen += i * 2;	/* 2 bytes/sample */
@@ -71,7 +71,7 @@
 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int i = f->samples;
-	char *dst = pvt->outbuf + pvt->samples;
+	char *dst = (char *) pvt->outbuf + pvt->samples;
 	int16_t *src = f->data;
 
 	pvt->samples += i;

Modified: team/kpfleming/aligner2/codecs/codec_g726.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_g726.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_g726.c (original)
+++ team/kpfleming/aligner2/codecs/codec_g726.c Thu Jul  3 13:35:40 2008
@@ -703,7 +703,7 @@
 {
 	struct g726_coder_pvt *tmp = pvt->pvt;
 	unsigned char *src = f->data;
-	int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf + pvt->samples;
 	unsigned int i;
 
 	for (i = 0; i < f->datalen; i++) {
@@ -723,12 +723,13 @@
 	struct g726_coder_pvt *tmp = pvt->pvt;
 	int16_t *src = f->data;
 	unsigned int i;
+	char *dst = (char *) pvt->outbuf;
 
 	for (i = 0; i < f->samples; i++) {
 		unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
 
 		if (tmp->next_flag & 0x80) {	/* merge with leftover sample */
-			pvt->outbuf[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
+			dst[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
 			pvt->samples += 2;	/* 2 samples per byte */
 			tmp->next_flag = 0;
 		} else {
@@ -744,7 +745,7 @@
 {
 	struct g726_coder_pvt *tmp = pvt->pvt;
 	unsigned char *src = f->data;
-	int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf + pvt->samples;
 	unsigned int i;
 
 	for (i = 0; i < f->datalen; i++) {
@@ -764,12 +765,13 @@
 	struct g726_coder_pvt *tmp = pvt->pvt;
 	int16_t *src = f->data;
 	unsigned int i;
+	char *dst = (char *) pvt->outbuf;
 
 	for (i = 0; i < f->samples; i++) {
 		unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
 
 		if (tmp->next_flag & 0x80) {	/* merge with leftover sample */
-			pvt->outbuf[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
+			dst[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
 			pvt->samples += 2;	/* 2 samples per byte */
 			tmp->next_flag = 0;
 		} else {

Modified: team/kpfleming/aligner2/codecs/codec_gsm.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_gsm.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_gsm.c (original)
+++ team/kpfleming/aligner2/codecs/codec_gsm.c Thu Jul  3 13:35:40 2008
@@ -114,7 +114,7 @@
 {
 	struct gsm_translator_pvt *tmp = pvt->pvt;
 	int x;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf;
 	/* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
 	int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
 		MSGSM_FRAME_LEN : GSM_FRAME_LEN;

Modified: team/kpfleming/aligner2/codecs/codec_ilbc.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_ilbc.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_ilbc.c (original)
+++ team/kpfleming/aligner2/codecs/codec_ilbc.c Thu Jul  3 13:35:40 2008
@@ -124,7 +124,7 @@
 	/* Assuming there's space left, decode into the current buffer at
 	   the tail location.  Read in as many frames as there are */
 	int x,i;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf;
 	float tmpf[ILBC_SAMPLES];
 
 	if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */

Modified: team/kpfleming/aligner2/codecs/codec_lpc10.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_lpc10.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_lpc10.c (original)
+++ team/kpfleming/aligner2/codecs/codec_lpc10.c Thu Jul  3 13:35:40 2008
@@ -151,7 +151,7 @@
 static int lpc10tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	struct lpc10_coder_pvt *tmp = pvt->pvt;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf;
 	int len = 0;
 
 	while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {

Modified: team/kpfleming/aligner2/codecs/codec_speex.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_speex.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_speex.c (original)
+++ team/kpfleming/aligner2/codecs/codec_speex.c Thu Jul  3 13:35:40 2008
@@ -200,7 +200,7 @@
 	   the tail location.  Read in as many frames as there are */
 	int x;
 	int res;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf;
 	/* XXX fout is a temporary buffer, may have different types */
 #ifdef _SPEEX_TYPES_H
 	spx_int16_t fout[1024];
@@ -323,7 +323,7 @@
 
 	/* Terminate bit stream */
 	speex_bits_pack(&tmp->bits, 15, 5);
-	datalen = speex_bits_write(&tmp->bits, pvt->outbuf, pvt->t->buf_size);
+	datalen = speex_bits_write(&tmp->bits, (char *) pvt->outbuf, pvt->t->buf_size);
 	return ast_trans_frameout(pvt, datalen, samples);
 }
 

Modified: team/kpfleming/aligner2/codecs/codec_ulaw.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/codecs/codec_ulaw.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/codecs/codec_ulaw.c (original)
+++ team/kpfleming/aligner2/codecs/codec_ulaw.c Thu Jul  3 13:35:40 2008
@@ -56,7 +56,7 @@
 {
 	int i = f->samples;
 	unsigned char *src = f->data;
-	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf + pvt->samples;
 
 	pvt->samples += i;
 	pvt->datalen += i * 2;	/* 2 bytes/sample */
@@ -72,7 +72,7 @@
 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int i = f->samples;
-	char *dst = pvt->outbuf + pvt->samples;
+	char *dst = (char *) pvt->outbuf + pvt->samples;
 	int16_t *src = f->data;
 
 	pvt->samples += i;

Modified: team/kpfleming/aligner2/include/asterisk/compiler.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/include/asterisk/compiler.h?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/include/asterisk/compiler.h (original)
+++ team/kpfleming/aligner2/include/asterisk/compiler.h Thu Jul  3 13:35:40 2008
@@ -59,4 +59,6 @@
 #define attribute_deprecated
 #endif
 
+#define AST_ALIGN_TO(type) __attribute__((aligned(__alignof__(type))))
+
 #endif /* _ASTERISK_COMPILER_H */

Modified: team/kpfleming/aligner2/include/asterisk/translate.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/include/asterisk/translate.h?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/include/asterisk/translate.h (original)
+++ team/kpfleming/aligner2/include/asterisk/translate.h Thu Jul  3 13:35:40 2008
@@ -133,19 +133,19 @@
  */
 struct ast_trans_pvt {
 	struct ast_translator *t;
-	struct ast_frame f;	/*!< used in frameout */
-	int samples;		/*!< samples available in outbuf */
+	struct ast_frame f;		/*!< used in frameout */
+	int samples;			/*!< samples available in outbuf */
 	/*! 
 	 * \brief actual space used in outbuf
 	 *
-	 * Also, for the sake of ABI compatability, a magic value of -1 in this
+	 * \note Also, for the sake of ABI compatability, a magic value of -1 in this
 	 * field means that the pvt has been requested to be destroyed, but is
 	 * pending destruction until ast_translate_frame_freed() gets called. 
 	 */
 	int datalen;
-	void *pvt;		/*!< more private data, if any */
-	char *outbuf;		/*!< the useful portion of the buffer */
-	plc_state_t *plc;	/*!< optional plc pointer */
+	void *pvt;			/*!< more private data, if any */
+	int16_t *outbuf;		/*!< the useful portion of the buffer */
+	plc_state_t *plc;		/*!< optional plc pointer */
 	struct ast_trans_pvt *next;	/*!< next in translator chain */
 	struct timeval nextin;
 	struct timeval nextout;

Modified: team/kpfleming/aligner2/main/translate.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/aligner2/main/translate.c?view=diff&rev=127828&r1=127827&r2=127828
==============================================================================
--- team/kpfleming/aligner2/main/translate.c (original)
+++ team/kpfleming/aligner2/main/translate.c Thu Jul  3 13:35:40 2008
@@ -99,40 +99,40 @@
 static void *newpvt(struct ast_translator *t)
 {
 	struct ast_trans_pvt *pvt;
-	int len;
-	int useplc = t->plc_samples > 0 && t->useplc;	/* cache, because it can change on the fly */
-	char *ofs;
-
-	/*
-	 * compute the required size adding private descriptor,
-	 * plc, buffer, AST_FRIENDLY_OFFSET.
-	 */
-	len = sizeof(*pvt) + t->desc_size;
-	if (useplc)
-		len += sizeof(plc_state_t);
-	if (t->buf_size)
-		len += AST_FRIENDLY_OFFSET + t->buf_size;
-	pvt = ast_calloc(1, len);
-	if (!pvt)
+	char *base;
+	unsigned char useplc = t->plc_samples > 0 && t->useplc;
+	struct template {
+		struct ast_trans_pvt pvt;
+		char desc[t->desc_size] AST_ALIGN_TO(uint32_t);
+		plc_state_t plc[useplc ? 1 : 0];
+		char friendly[AST_FRIENDLY_OFFSET] AST_ALIGN_TO(uint32_t);
+		char outbuf[t->buf_size] AST_ALIGN_TO(int16_t);
+	};
+
+	if (!(base = ast_calloc(sizeof(struct template), 1))) {
 		return NULL;
+	}
+
+	pvt = (struct ast_trans_pvt *) base;
 	pvt->t = t;
-	ofs = (char *)(pvt + 1);	/* pointer to data space */
-	if (t->desc_size) {		/* first comes the descriptor */
-		pvt->pvt = ofs;
-		ofs += t->desc_size;
-	}
-	if (useplc) {			/* then plc state */
-		pvt->plc = (plc_state_t *)ofs;
-		ofs += sizeof(plc_state_t);
-	}
-	if (t->buf_size)		/* finally buffer and header */
-		pvt->outbuf = ofs + AST_FRIENDLY_OFFSET;
+	if (t->desc_size) {
+		pvt->pvt = base + offsetof(struct template, desc);
+	}
+	if (useplc) {
+		pvt->plc = (plc_state_t *) (base + offsetof(struct template, plc));
+	}
+	if (t->buf_size) {
+		pvt->outbuf = (int16_t *) (base + offsetof(struct template, outbuf));
+	}
+
 	/* call local init routine, if present */
 	if (t->newpvt && t->newpvt(pvt)) {
-		free(pvt);
+		free(base);
 		return NULL;
 	}
+
 	ast_module_ref(t->module);
+
 	return pvt;
 }
 




More information about the asterisk-commits mailing list