]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/ibl.git/blob - src/interp/elf/ArrayList.c
Update copyright information
[keystone-rtos/ibl.git] / src / interp / elf / ArrayList.c
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  *  Redistribution and use in source and binary forms, with or without 
32  *  modification, are permitted provided that the following conditions 
33  *  are met:
34  *
35  *    Redistributions of source code must retain the above copyright 
36  *    notice, this list of conditions and the following disclaimer.
37  *
38  *    Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the 
40  *    documentation and/or other materials provided with the   
41  *    distribution.
42  *
43  *    Neither the name of Texas Instruments Incorporated nor the names of
44  *    its contributors may be used to endorse or promote products derived
45  *    from this software without specific prior written permission.
46  *
47  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
48  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
49  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
51  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
52  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
53  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
56  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
57  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59 */
61 #include <inttypes.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include "ArrayList.h"
65 #include "ewrap.h"
66 #include "dload_api.h"
68 /*****************************************************************************/
69 /* AL_INITIALIZE() - Initialize a newly created Array_List object.           */
70 /*****************************************************************************/
71 void AL_initialize(Array_List* obj, int32_t type_size, int32_t num_elem)
72 {
73    if (num_elem == 0) num_elem = 1;
74    obj->buf = DLIF_malloc(type_size * num_elem);
75    obj->type_size = type_size;
76    obj->size = 0;
77    obj->buffer_size = num_elem;
78 }
80 /*****************************************************************************/
81 /* AL_APPEND() - Append an element to the end of an Array_List.              */
82 /*****************************************************************************/
83 void AL_append(Array_List* obj, void* to_append)
84 {
85    /*------------------------------------------------------------------------*/
86    /* If there is already space in the specified buffer for the new data,    */
87    /* just append it to the end of the data that is already in the buffer.   */
88    /*------------------------------------------------------------------------*/
89    if (obj->size < obj->buffer_size)
90       memcpy(((uint8_t*)obj->buf) + obj->type_size * ((obj->size)++), to_append,
91                obj->type_size);
93    /*------------------------------------------------------------------------*/
94    /* Grow the buffer if we need more space to add the new data to it.       */
95    /*------------------------------------------------------------------------*/
96    else
97    {
98        void* old_buffer = obj->buf;
99        obj->buffer_size *= 2;
100        obj->buf = DLIF_malloc(obj->buffer_size*obj->type_size);
101        memcpy(obj->buf,old_buffer,obj->size*obj->type_size);
102        DLIF_free(old_buffer);
103        memcpy(((uint8_t*)obj->buf) + obj->type_size *((obj->size)++), to_append,
104                obj->type_size);
105    }
108 /*****************************************************************************/
109 /* AL_SIZE() - Get the number of elements in an Array_List.                  */
110 /*****************************************************************************/
111 int32_t AL_size(Array_List* obj)
113    return obj->size;
116 /*****************************************************************************/
117 /* AL_DESTROY() - Free up memory associated with an Array_List that is no    */
118 /*       longer in use.                                                         */
119 /*****************************************************************************/
120 void AL_destroy(Array_List* obj)
122    free(obj->buf);