1 /*
2 * ArrayList.c
3 *
4 * Array_List is a C implementation of a C++ vector class.
5 *
6 * This class emulates a resizable array along the lines of a C++
7 * vector or Java ArrayList class in C, and uses the convention
8 * of passing a pointer to the current "object" as the first
9 * argument.
10 *
11 * Usage is defined as follows:
12 *
13 * Array_List obj;
14 * AL_initialize(&obj, sizeof(type_name));
15 *
16 * ...
17 *
18 * type_name *ptr = (type_name*)(obj.buf);
19 * for(i = 0; i < AL_size(&obj); i++)
20 * do_something_to(ptr[i]);
21 * type_name to_append = ...;
22 * AL_append(&obj, &to_append);
23 *
24 * ...
25 *
26 * AL_destroy(&obj);
27 *
28 *
29 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
30 *
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 *
36 * Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 *
39 * Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the
42 * distribution.
43 *
44 * Neither the name of Texas Instruments Incorporated nor the names of
45 * its contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 *
60 */
62 #include <inttypes.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include "ArrayList.h"
66 #include "ewrap.h"
67 #include "dload_api.h"
69 /*****************************************************************************/
70 /* AL_INITIALIZE() - Initialize a newly created Array_List object. */
71 /*****************************************************************************/
72 void AL_initialize(Array_List* obj, int32_t type_size, int32_t num_elem)
73 {
74 if (num_elem == 0) num_elem = 1;
75 obj->buf = DLIF_malloc(type_size * num_elem);
76 obj->type_size = type_size;
77 obj->size = 0;
78 obj->buffer_size = num_elem;
79 }
81 /*****************************************************************************/
82 /* AL_APPEND() - Append an element to the end of an Array_List. */
83 /*****************************************************************************/
84 void AL_append(Array_List* obj, void* to_append)
85 {
86 /*------------------------------------------------------------------------*/
87 /* If there is already space in the specified buffer for the new data, */
88 /* just append it to the end of the data that is already in the buffer. */
89 /*------------------------------------------------------------------------*/
90 if (obj->size < obj->buffer_size)
91 memcpy(((uint8_t*)obj->buf) + obj->type_size * ((obj->size)++), to_append,
92 obj->type_size);
94 /*------------------------------------------------------------------------*/
95 /* Grow the buffer if we need more space to add the new data to it. */
96 /*------------------------------------------------------------------------*/
97 else
98 {
99 void* old_buffer = obj->buf;
100 obj->buffer_size *= 2;
101 obj->buf = DLIF_malloc(obj->buffer_size*obj->type_size);
102 memcpy(obj->buf,old_buffer,obj->size*obj->type_size);
103 DLIF_free(old_buffer);
104 memcpy(((uint8_t*)obj->buf) + obj->type_size *((obj->size)++), to_append,
105 obj->type_size);
106 }
107 }
109 /*****************************************************************************/
110 /* AL_SIZE() - Get the number of elements in an Array_List. */
111 /*****************************************************************************/
112 int32_t AL_size(Array_List* obj)
113 {
114 return obj->size;
115 }
117 /*****************************************************************************/
118 /* AL_DESTROY() - Free up memory associated with an Array_List that is no */
119 /* longer in use. */
120 /*****************************************************************************/
121 void AL_destroy(Array_List* obj)
122 {
123 free(obj->buf);
124 }