[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.3, Thu Mar 21 00:28:33 2002 UTC revision 1.6, Wed Jun 12 20:38:41 2002 UTC
# Line 2  Line 2 
2  #include <stdio.h>  #include <stdio.h>
3  #include "mem_align.h"  #include "mem_align.h"
4    
5  void *xvid_malloc(size_t size, uint8_t alignment)  void *
6    xvid_malloc(size_t size,
7                            uint8_t alignment)
8  {  {
9          uint8_t *mem_ptr;          uint8_t *mem_ptr;
10    
11          if(alignment == 0)          if (!alignment) {
12          {  
13                    /* We have not to satisfy any alignment */
14                  if((mem_ptr = (uint8_t *) malloc(size + 1)) != NULL) {                  if((mem_ptr = (uint8_t *) malloc(size + 1)) != NULL) {
15    
16                            /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-1) */
17                          *mem_ptr = 0;                          *mem_ptr = 0;
18    
19                            /* Return the mem_ptr pointer */
20                          return (void *) mem_ptr++;                          return (void *) mem_ptr++;
21    
22                  }                  }
23          }  
24          else          } else {
         {  
25                  uint8_t *tmp;                  uint8_t *tmp;
26    
27                    /*
28                     * Allocate the required size memory + alignment so we
29                     * can realign the data if necessary
30                     */
31    
32                  if((tmp = (uint8_t *) malloc(size + alignment)) != NULL) {                  if((tmp = (uint8_t *) malloc(size + alignment)) != NULL) {
33                          mem_ptr = (uint8_t *)((uint32_t)(tmp + alignment - 1)&(~(uint32_t)(alignment - 1)));  
34                            /* Align the tmp pointer */
35                            mem_ptr =
36                                    (uint8_t *) ((uint32_t) (tmp + alignment - 1) &
37                                                             (~(uint32_t) (alignment - 1)));
38    
39                            /*
40                             * Special case where malloc have already satisfied the alignment
41                             * We must add alignment to mem_ptr because we must store
42                             * (mem_ptr - tmp) in *(mem_ptr-1)
43                             * If we do not add alignment to mem_ptr then *(mem_ptr-1) points
44                             * to a forbidden memory space
45                             */
46                            if (mem_ptr == tmp)
47                                    mem_ptr += alignment;
48    
49                            /*
50                             * (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve
51                             * the real malloc block allocated and free it in xvid_free
52                             */
53                          *(mem_ptr - 1) = (uint8_t)(mem_ptr - tmp);                          *(mem_ptr - 1) = (uint8_t)(mem_ptr - tmp);
54    
55                            /* Return the aligned pointer */
56                          return (void *) mem_ptr;                          return (void *) mem_ptr;
57    
58                  }                  }
59          }          }
60    
# Line 28  Line 62 
62    
63  }  }
64    
65  void xvid_free(void *mem_ptr)  void
66    xvid_free(void *mem_ptr)
67  {  {
68    
69          uint8_t *real_ptr;          /* *(mem_ptr - 1) give us the offset to the real malloc block */
70            if (mem_ptr)
71          real_ptr = (uint8_t*)mem_ptr;                  free((uint8_t *) mem_ptr - *((uint8_t *) mem_ptr - 1));
   
         free(real_ptr - *(real_ptr -1));  
72    
73  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.6

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