ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / VP_Os / vp_os_malloc.c
1 /**
2  * @file malloc.c
3  * @author aurelien.morelle@parrot.fr
4  * @date 2006/12/19
5  */
6
7 #include "VP_Os/vp_os_malloc.h"
8
9
10 #undef calloc
11 #undef malloc
12 #undef memset
13 #undef free
14 #undef realloc
15
16
17 void *
18 vp_os_calloc(size_t nmemb, size_t size)
19 {
20 #ifdef DEBUG_MODE
21   void *res = calloc(nmemb, size);
22   assert(res);
23   return (res);
24 #else // ! DEBUG_MODE
25   return calloc(nmemb, size);
26 #endif // <- DEBUG_MODE
27 }
28
29 void *
30 vp_os_malloc(size_t size)
31 {
32 #ifdef DEBUG_MODE
33   void *res = malloc(size);
34   assert(res);
35   return (res);
36 #else // ! DEBUG_MODE
37   return malloc(size);
38 #endif // <- DEBUG_MODE
39 }
40
41 void *
42 vp_os_malloc_no_assert(size_t size)
43 {
44   return malloc(size);
45 }
46
47 void
48 vp_os_free(void *ptr)
49 {
50 #ifdef DEBUG_MODE
51   assert(ptr);
52   free(ptr);
53 #else // ! DEBUG_MODE
54   free(ptr);
55 #endif // <- DEBUG_MODE
56 }
57
58 void
59 vp_os_sfree(void **ptr)
60 {
61 #ifdef DEBUG_MODE
62   assert(*ptr);
63   free(*ptr);
64 #else // ! DEBUG_MODE
65   free(*ptr);
66 #endif // <- DEBUG_MODE
67   *ptr=NULL;
68 }
69
70 // align_size has to be a power of two !!!
71 //
72 // The basic working of this algorithm is to allocate a bigger chunk of data than requested.
73 // This chunk of data must be big enough to contain an address aligned on requested boundary
74 // We also alloc 2 more words to keep base ptr (bptr) & requested size (size) of allocation
75 // bptr is the base pointer of this allocation
76 // _____ ______ ______ __________
77 //  ... | bptr | size |   ....   |
78 // _____|______|______|__________|
79 // 
80 void* vp_os_aligned_malloc(size_t size, size_t align_size)
81 {
82   char *ptr, *aligned_ptr;
83   int* ptr2;
84   int allocation_size;
85   size_t align_mask = align_size - 1;
86
87   // Check if align_size is a power of two
88   // If the result of this test is non zero then align_size is not a power of two
89   if( align_size & align_mask )
90     return NULL;
91
92   // Allocation size is :
93   //    - Requested user size
94   //    - a size (align_size) to make sure we can align on the requested boundary
95   //    - 8 more bytes to register base adress & allocation size 
96   allocation_size = size + align_size + 2*sizeof(int);
97
98   ptr = (char*) vp_os_malloc(allocation_size);
99   if(ptr == NULL)
100     return NULL;
101
102   ptr2 = (int*)(ptr + 2*sizeof(int));
103   aligned_ptr = ptr + 2*sizeof(int) + (align_size - ((size_t) ptr2 & align_mask));
104
105   ptr2    = (int*)(aligned_ptr - 2*sizeof(int));
106   *ptr2++ = (int) (aligned_ptr - ptr);
107   *ptr2   = size;
108
109   return aligned_ptr;
110 }
111
112 void vp_os_aligned_free(void *ptr)
113 {
114   int* ptr2 = (int*)ptr - 2;
115
116   vp_os_free( ((char*)ptr - *ptr2) );
117 }
118
119 void*
120 vp_os_aligned_realloc(void* ptr, size_t size, size_t align_size)
121 {
122   void* ptr_ret;
123   void* aligned_ptr;
124
125   if( size == 0 )
126   {
127     ptr_ret = NULL;
128     if( ptr != NULL )
129       vp_os_aligned_free(ptr);
130   }
131   else
132   {
133     if( ptr != NULL )
134     {
135       int* ptr2 = (int*)ptr - 1;
136       size_t old_size;
137
138       aligned_ptr = ptr;
139
140       old_size = *ptr2--;
141
142       ptr_ret = vp_os_aligned_malloc(size, align_size);
143
144       // Compute smallest size
145       if( size > old_size )
146       {
147         size = old_size;
148       }
149
150       // Copy old data
151       vp_os_memcpy( ptr_ret, aligned_ptr, size );
152
153       vp_os_free( ((char*)ptr - *ptr2) );
154     }
155     else
156     {
157       ptr_ret = vp_os_aligned_malloc(size, align_size);
158     }
159   }
160
161   return ptr_ret;
162 }
163
164 void*
165 vp_os_realloc(void *ptr, size_t size)
166 {
167 #ifdef DEBUG_MODE
168   void *res = realloc(ptr, size);
169   if (res==NULL) { perror(__FUNCTION__); }
170    assert(res!=NULL);
171   return (res);
172 #else // ! DEBUG_MODE
173   return realloc(ptr, size);
174 #endif // <- DEBUG_MODE
175 }
176