[cvs] / xvidcore / src / utils / mem_align.c Repository:
ViewVC logotype

Diff of /xvidcore/src/utils/mem_align.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.12, Wed Nov 27 21:20:33 2002 UTC revision 1.15.2.3, Mon Sep 29 00:30:31 2003 UTC
# Line 1  Line 1 
1  /*****************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Aligned memory allocator -   *  - Aligned Memory Allocator -
5   *   *
6   *  Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>   *  Copyright(C) 2002-2003 Edouard Gomez <ed.gomez@free.fr>
  *               2002 Edouard Gomez  
7   *   *
8   *  This file is part of XviD, a free MPEG-4 video encoder/decoder   *  This program is free software ; you can redistribute it and/or modify
9   *   *  it under the terms of the GNU General Public License as published by
  *  XviD is free software; you can redistribute it and/or modify it  
  *  under the terms of the GNU General Public License as published by  
10   *  the Free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
11   *  (at your option) any later version.   *  (at your option) any later version.
12   *   *
# Line 22  Line 19 
19   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
  *  Under section 8 of the GNU General Public License, the copyright  
  *  holders of XVID explicitly forbid distribution in the following  
  *  countries:  
  *  
  *    - Japan  
  *    - United States of America  
  *  
  *  Linking XviD statically or dynamically with other modules is making a  
  *  combined work based on XviD.  Thus, the terms and conditions of the  
  *  GNU General Public License cover the whole combination.  
  *  
  *  As a special exception, the copyright holders of XviD give you  
  *  permission to link XviD with independent modules that communicate with  
  *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the  
  *  license terms of these independent modules, and to copy and distribute  
  *  the resulting combined work under terms of your choice, provided that  
  *  every copy of the combined work is accompanied by a complete copy of  
  *  the source code of XviD (the version of XviD used to produce the  
  *  combined work), being distributed under the terms of the GNU General  
  *  Public License plus this exception.  An independent module is a module  
  *  which is not derived from or based on XviD.  
  *  
  *  Note that people who make modified versions of XviD are not obligated  
  *  to grant this special exception for their modified versions; it is  
  *  their choice whether to do so.  The GNU General Public License gives  
  *  permission to release a modified version without this exception; this  
  *  exception also makes it possible to release a modified version which  
  *  carries forward this exception.  
  *  
22   * $Id$   * $Id$
23   *   *
24   ****************************************************************************/   ****************************************************************************/
# Line 88  Line 56 
56                  if ((mem_ptr = (uint8_t *) malloc(size + 1)) != NULL) {                  if ((mem_ptr = (uint8_t *) malloc(size + 1)) != NULL) {
57    
58                          /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-1) */                          /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-1) */
59                          *mem_ptr = 0;                          *mem_ptr = (uint8_t)1;
60    
61                          /* Return the mem_ptr pointer */                          /* Return the mem_ptr pointer */
62                          return (void *)(mem_ptr+1);                          return ((void *)(mem_ptr+1));
   
63                  }                  }
   
64          } else {          } else {
65                  uint8_t *tmp;                  uint8_t *tmp;
66    
67                  /*                  /* Allocate the required size memory + alignment so we
68                   * Allocate the required size memory + alignment so we                   * can realign the data if necessary */
                  * can realign the data if necessary  
                  */  
   
69                  if ((tmp = (uint8_t *) malloc(size + alignment)) != NULL) {                  if ((tmp = (uint8_t *) malloc(size + alignment)) != NULL) {
70    
71                          /* Align the tmp pointer */                          /* Align the tmp pointer */
# Line 110  Line 73 
73                                  (uint8_t *) ((ptr_t) (tmp + alignment - 1) &                                  (uint8_t *) ((ptr_t) (tmp + alignment - 1) &
74                                                           (~(ptr_t) (alignment - 1)));                                                           (~(ptr_t) (alignment - 1)));
75    
76                          /*                          /* Special case where malloc have already satisfied the alignment
                          * Special case where malloc have already satisfied the alignment  
77                           * We must add alignment to mem_ptr because we must store                           * We must add alignment to mem_ptr because we must store
78                           * (mem_ptr - tmp) in *(mem_ptr-1)                           * (mem_ptr - tmp) in *(mem_ptr-1)
79                           * If we do not add alignment to mem_ptr then *(mem_ptr-1) points                           * If we do not add alignment to mem_ptr then *(mem_ptr-1) points
80                           * to a forbidden memory space                           * to a forbidden memory space */
                          */  
81                          if (mem_ptr == tmp)                          if (mem_ptr == tmp)
82                                  mem_ptr += alignment;                                  mem_ptr += alignment;
83    
84                          /*                          /* (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve
85                           * (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve                           * the real malloc block allocated and free it in xvid_free */
                          * the real malloc block allocated and free it in xvid_free  
                          */  
86                          *(mem_ptr - 1) = (uint8_t) (mem_ptr - tmp);                          *(mem_ptr - 1) = (uint8_t) (mem_ptr - tmp);
87    
88                          /* Return the aligned pointer */                          /* Return the aligned pointer */
89                          return (void *) mem_ptr;                          return ((void *)mem_ptr);
   
90                  }                  }
91          }          }
92    
93          return NULL;          return(NULL);
   
94  }  }
95    
96  /*****************************************************************************  /*****************************************************************************
# Line 150  Line 107 
107  xvid_free(void *mem_ptr)  xvid_free(void *mem_ptr)
108  {  {
109    
110          /* *(mem_ptr - 1) give us the offset to the real malloc block */          uint8_t *ptr;
111          if (mem_ptr)  
112                  free((uint8_t *) mem_ptr - *((uint8_t *) mem_ptr - 1));          if (mem_ptr == NULL)
113                    return;
114    
115            /* Aligned pointer */
116            ptr = mem_ptr;
117    
118            /* *(ptr - 1) holds the offset to the real allocated block
119             * we sub that offset os we free the real pointer */
120            ptr -= *(ptr - 1);
121    
122            /* Free the memory */
123            free(ptr);
124  }  }

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.15.2.3

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4