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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (view) (download)

1 : edgomez 1.7 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 : edgomez 1.16 * - Aligned Memory Allocator -
5 : edgomez 1.7 *
6 : edgomez 1.16 * Copyright(C) 2002-2003 Edouard Gomez <ed.gomez@free.fr>
7 : edgomez 1.15 *
8 :     * 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
10 :     * the Free Software Foundation ; either version 2 of the License, or
11 : edgomez 1.7 * (at your option) any later version.
12 :     *
13 :     * This program is distributed in the hope that it will be useful,
14 : edgomez 1.15 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
15 : edgomez 1.7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     * GNU General Public License for more details.
17 :     *
18 :     * You should have received a copy of the GNU General Public License
19 : edgomez 1.15 * along with this program ; if not, write to the Free Software
20 : edgomez 1.7 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 :     *
22 : edgomez 1.16 * $Id$
23 : edgomez 1.7 *
24 :     ****************************************************************************/
25 :    
26 : Isibaar 1.2 #include <stdlib.h>
27 :     #include <stdio.h>
28 :     #include "mem_align.h"
29 :    
30 : edgomez 1.7 /*****************************************************************************
31 :     * xvid_malloc
32 :     *
33 :     * This function allocates 'size' bytes (usable by the user) on the heap and
34 :     * takes care of the requested 'alignment'.
35 :     * In order to align the allocated memory block, the xvid_malloc allocates
36 :     * 'size' bytes + 'alignment' bytes. So try to keep alignment very small
37 :     * when allocating small pieces of memory.
38 :     *
39 :     * NB : a block allocated by xvid_malloc _must_ be freed with xvid_free
40 :     * (the libc free will return an error)
41 :     *
42 :     * Returned value : - NULL on error
43 :     * - Pointer to the allocated aligned block
44 :     *
45 :     ****************************************************************************/
46 :    
47 : edgomez 1.6 void *
48 :     xvid_malloc(size_t size,
49 :     uint8_t alignment)
50 : Isibaar 1.2 {
51 : edgomez 1.3 uint8_t *mem_ptr;
52 : edgomez 1.6
53 :     if (!alignment) {
54 : edgomez 1.4
55 :     /* We have not to satisfy any alignment */
56 : edgomez 1.6 if ((mem_ptr = (uint8_t *) malloc(size + 1)) != NULL) {
57 : edgomez 1.4
58 :     /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-1) */
59 : edgomez 1.16 *mem_ptr = (uint8_t)1;
60 : edgomez 1.4
61 :     /* Return the mem_ptr pointer */
62 : edgomez 1.16 return ((void *)(mem_ptr+1));
63 : edgomez 1.3 }
64 : edgomez 1.6 } else {
65 : edgomez 1.3 uint8_t *tmp;
66 : edgomez 1.6
67 : edgomez 1.16 /* Allocate the required size memory + alignment so we
68 :     * can realign the data if necessary */
69 : edgomez 1.6 if ((tmp = (uint8_t *) malloc(size + alignment)) != NULL) {
70 : edgomez 1.4
71 :     /* Align the tmp pointer */
72 : edgomez 1.6 mem_ptr =
73 : Isibaar 1.8 (uint8_t *) ((ptr_t) (tmp + alignment - 1) &
74 :     (~(ptr_t) (alignment - 1)));
75 : edgomez 1.4
76 : edgomez 1.16 /* Special case where malloc have already satisfied the alignment
77 : edgomez 1.4 * We must add alignment to mem_ptr because we must store
78 :     * (mem_ptr - tmp) in *(mem_ptr-1)
79 :     * If we do not add alignment to mem_ptr then *(mem_ptr-1) points
80 : edgomez 1.16 * to a forbidden memory space */
81 : edgomez 1.6 if (mem_ptr == tmp)
82 :     mem_ptr += alignment;
83 : edgomez 1.4
84 : edgomez 1.16 /* (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve
85 :     * the real malloc block allocated and free it in xvid_free */
86 : edgomez 1.6 *(mem_ptr - 1) = (uint8_t) (mem_ptr - tmp);
87 : edgomez 1.4
88 :     /* Return the aligned pointer */
89 : edgomez 1.16 return ((void *)mem_ptr);
90 : edgomez 1.3 }
91 :     }
92 :    
93 : edgomez 1.16 return(NULL);
94 : Isibaar 1.2 }
95 : edgomez 1.7
96 :     /*****************************************************************************
97 :     * xvid_free
98 :     *
99 :     * Free a previously 'xvid_malloc' allocated block. Does not free NULL
100 :     * references.
101 :     *
102 :     * Returned value : None.
103 :     *
104 :     ****************************************************************************/
105 : Isibaar 1.2
106 : edgomez 1.6 void
107 :     xvid_free(void *mem_ptr)
108 : Isibaar 1.2 {
109 :    
110 : edgomez 1.16 uint8_t *ptr;
111 :    
112 :     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 : Isibaar 1.2
122 : edgomez 1.16 /* Free the memory */
123 :     free(ptr);
124 : Isibaar 1.2 }

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