Golem  v0.1.1
Generic Operating system Libraries for Embedded Multitasking
 All Data Structures Files Functions Variables Pages
glm_vector.c
1 
11 /* INCLUDES *******************************************************************/
12 #include "glm_vector.h"
13 
14 
15 /* PUBLIC FUNCTIONS ***********************************************************/
16 
17 vector_t *
18 vector_create( heap_id_t a_heap_id, uint_base_t a_length,
19  size_t a_objecsize )
20 {
21  vector_t *l_vector = NULL;
22  void **l_objects = NULL;
23 
24  // Allocate memory for the vector
25  l_vector = (vector_t*) heap_malloc( a_heap_id, sizeof(vector_t) );
26 
27  // Initialize it upon a successful creation
28  if ( l_vector != NULL )
29  {
30  // Initialize vector
31  l_vector->objects = NULL;
32  l_vector->length = 0;
33  l_vector->objectsize = a_objecsize;
34 
35  if( a_length > 0 )
36  {
37  // Allocate memory for the amount of objects requested
38  l_objects = heap_calloc(a_heap_id, a_length, a_objecsize);
39 
40  if( l_objects != NULL )
41  {
42  l_vector->objects = l_objects;
43  l_vector->length = a_length;
44  }
45  else
46  {
47  // We could not create the requested vector length, so destroy
48  // vector again and return 0
49  heap_free(l_vector);
50  }
51  }
52  }
53 
54  return l_vector;
55 } // vector_create()
56 
57 
58 void
59 vector_destroy( heap_id_t a_heap_id, vector_t * a_vector )
60 {
61  if( a_vector != NULL )
62  {
63  vector_clear( a_heap_id, a_vector );
64  heap_free(a_vector);
65  }
66 } // vector_destroy()
67 
68 
69 void
70 vector_clear( heap_id_t a_heap_id, vector_t * a_vector )
71 {
72  uint_base_t i = 0;
73 
74  if( a_vector != NULL )
75  {
76  // Free all objects in the vector
77  for( i=0 ; i<a_vector->length ; i++ )
78  {
79  // Free the actual object
80  heap_free(a_vector->objects[i]);
81  }
82 
83  // Then free the object array from the vector
84  heap_free(a_vector->objects);
85  a_vector->length = 0;
86  }
87 } // vector_clear()
88 
89 
90 uint_base_t
91 vector_add( heap_id_t a_heap_id, vector_t * a_vector, void * a_object )
92 {
93  uint_base_t i = 0;
94  void * *l_objects = NULL;
95  size_t obj_size = 0;
96 
97  if( a_vector != NULL )
98  {
99  // Store current length
100  i = a_vector->length;
101 
102  // Make room for a new object
103  obj_size = heap_sizeof(a_object);
104  l_objects = heap_realloc(a_vector->objects, a_vector->length + obj_size );
105 
106  // Proceed if reallocation succeeded
107  if( l_objects != NULL )
108  {
109  // Use reallocated array and store new object
110  a_vector->objects = l_objects;
111  a_vector->objects[i] = a_object;
112  a_vector->length++;
113  }
114  }
115 
116  return i;
117 } // vector_add()
118 
119 
120 
121 uint_base_t vector_get_length( vector_t * a_vector )
122 {
123  uint_base_t l_size = 0;
124 
125  if( a_vector != NULL )
126  {
127  l_size = a_vector->length;
128  }
129 
130  return l_size;
131 }
132 
133 
134 void * vector_get_object( vector_t * a_vector, uint_base_t a_index )
135 {
136  void * l_object = NULL;
137 
138  if( (a_vector != NULL) && ( a_index < a_vector->length) )
139  {
140  l_object = a_vector->objects[ a_index ];
141  }
142 
143  return l_object;
144 }