]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/test/src/util_memory_helpers.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / test / src / util_memory_helpers.c
1 /***** includes *****/
2 #include "internal.h"
3
4
5
6
7
8 /****************************************************************************/
9 void *util_aligned_malloc( lfds700_pal_uint_t size, lfds700_pal_uint_t align_in_bytes )
10 {
11   lfds700_pal_uint_t
12     offset;
13
14   void
15     *memory,
16     *original_memory;
17
18   // TRD : size can be any value in its range
19   // TRD : align_in_bytes can be any value in its range
20
21   /* TRD : helper function to provide aligned allocations
22            no porting required
23   */
24
25   original_memory = memory = util_malloc_wrapper( size + sizeof(void *) + align_in_bytes );
26
27   if( memory != NULL )
28   {
29     memory = (void **) memory + 1;
30     offset = align_in_bytes - (lfds700_pal_uint_t) memory % align_in_bytes;
31     memory = (char unsigned *) memory + offset;
32     *( (void **) memory - 1 ) = original_memory;
33   }
34
35   return( memory );
36 }
37
38
39
40
41
42 /****************************************************************************/
43 void util_aligned_free( void *memory )
44 {
45   assert( memory != NULL );
46
47   // TRD : the "void *" stored above memory points to the root of the allocation
48   free( *( (void **) memory - 1 ) );
49
50   return;
51 }
52
53
54
55
56
57 /****************************************************************************/
58 void *util_malloc_wrapper( lfds700_pal_uint_t size )
59 {
60   void
61     *memory;
62
63   // TRD : size can be any value in its range
64
65   memory = malloc( size );
66
67   if( memory == NULL )
68   {
69     puts( "malloc() failed, exiting." );
70     exit( EXIT_FAILURE );
71   }
72
73   return( memory );
74 }
75