File: utils.c

package info (click to toggle)
simulavr 0.1.2.2-7
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,760 kB
  • ctags: 3,179
  • sloc: ansic: 19,986; sh: 3,623; python: 3,528; makefile: 406; asm: 308; yacc: 145; lex: 48
file content (376 lines) | stat: -rw-r--r-- 9,083 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * $Id: utils.c,v 1.19 2003/12/01 09:10:17 troth Exp $
 *
 ****************************************************************************
 *
 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
 * Copyright (C) 2001, 2002, 2003  Theodore A. Roth
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ****************************************************************************
 */

/**
 * \file utils.c
 * \brief Utility functions.
 *
 * This module provides general purpose utilities.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>

#include "avrerror.h"
#include "avrmalloc.h"
#include "avrclass.h"
#include "utils.h"

/** \brief Utility function to convert a string to a FileFormatType code. */

int
str2ffmt (char *str)
{
    if (strncmp (str, "bin", 3) == 0)
        return FFMT_BIN;
    if (strncmp (str, "ihex", 4) == 0)
        return FFMT_IHEX;
    if (strncmp (str, "elf", 3) == 0)
        return FFMT_ELF;

    return -1;
}

/** \brief Set a bit in src to 1 if val != 0, clears bit if val == 0. */

extern inline uint8_t set_bit_in_byte (uint8_t src, int bit, int val);

/** \brief Set a bit in src to 1 if val != 0, clears bit if val == 0. */

extern inline uint16_t set_bit_in_word (uint16_t src, int bit, int val);

/** \brief Return the number of milliseconds of elapsed program time.

    \return an unsigned 64 bit number. Time zero is not well
    defined, so only time differences should be used. */

uint64_t
get_program_time (void)
{
    uint64_t result;
    struct timeval tv;

    if (gettimeofday (&tv, NULL) < 0)
        avr_error ("Failed to get program time.");

    result = ((uint64_t) tv.tv_sec * 1000) + ((uint64_t) tv.tv_usec / 1000);

    return result;
}

/***************************************************************************\
 *
 * DList(AvrClass) Methods : A doubly linked list.
 *
\***************************************************************************/

static DList *dlist_new_node (AvrClass *data);
static void dlist_construct_node (DList *node, AvrClass *data);
static void dlist_destroy_node (void *node);

#ifndef DOXYGEN                 /* Don't expose to doxygen, structure is
                                   opaque. */

struct _DList
{
    AvrClass parent;
    struct _DList *prev;
    struct _DList *next;
    AvrClass *data;
};

#endif

static DList *
dlist_new_node (AvrClass *data)
{
    DList *node;

    node = avr_new (DList, 1);
    dlist_construct_node (node, data);
    class_overload_destroy ((AvrClass *)node, dlist_destroy_node);

    return node;
}

static void
dlist_construct_node (DList *node, AvrClass *data)
{
    if (node == NULL)
        avr_error ("passed null ptr");

    class_construct ((AvrClass *)node);

    node->prev = NULL;
    node->next = NULL;

    node->data = data;
}

static void
dlist_destroy_node (void *node)
{
    DList *_node = (DList *)node;

    if (_node == NULL)
        return;

    class_unref (_node->data);

    class_destroy (node);
}

/** \brief Add a new node to the end of the list.

   If cmp argument is not NULL, use cmp() to see if node already exists and
   don't add node if it exists.

   It is the responsibility of this function to unref data if not added. */

DList *
dlist_add (DList *head, AvrClass *data, DListFP_Cmp cmp)
{
    DList *node = head;

    if (head == NULL)
        /* The list is empty, make new node the head. */
        return dlist_new_node (data);

    /* Walk the list to find the end */

    while (node)
    {
        if (cmp && ((*cmp) (node->data, data) == 0))
        {
            /* node already exists and we were asked to keep nodes unique */
            class_unref (data);
            break;
        }

        if (node->next == NULL)
        {
            /* at the tail */
            node->next = dlist_new_node (data);
            node->next->prev = node;
            break;
        }

        /* move on to next node */
        node = node->next;
    }

    return head;
}

/** \brief Add a new node at the head of the list. */

DList *
dlist_add_head (DList *head, AvrClass *data)
{
    DList *node = dlist_new_node (data);;

    if (head)
    {
        head->prev = node;
        node->next = head;
    }

    return node;
}

/** \brief Conditionally delete a node from the list.

    Delete a node from the list if the node's data matches the specified
    data. Returns the head of the modified list. */

DList *
dlist_delete (DList *head, AvrClass *data, DListFP_Cmp cmp)
{
    DList *node = head;

    if (cmp == NULL)
        avr_error ("compare function not specified");

    while (node)
    {
        if ((*cmp) (node->data, data) == 0)
        {
            if ((node->prev == NULL) && (node->next == NULL))
            {
                /* deleting only node in list (node is head and tail) */
                head = NULL;
            }
            else if (node->prev == NULL)
            {
                /* node is head, but other nodes exist */
                node->next->prev = NULL;
                head = node->next;
            }
            else if (node->next == NULL)
            {
                /* node is tail, but other nodes exist */
                node->prev->next = NULL;
            }
            else
            {
                /* node is not head nor tail */
                node->prev->next = node->next;
                node->next->prev = node->prev;
            }

            /* this will also unref the node->data */
            class_unref ((AvrClass *)node);

            return head;
        }

        /* move on to next node */
        node = node->next;
    }

    /* if we get here, data wasn't found, just return original head */
    return head;
}

/** \brief Blow away the entire list. */

void
dlist_delete_all (DList *head)
{
    DList *node;

    while (head)
    {
        node = head;
        head = head->next;

        class_unref ((AvrClass *)node);
    }
}

/** \brief Lookup an item in the list.

    Walk the list pointed to by head and return a pointer to the data if
    found. If not found, return NULL. 

    \param head The head of the list to be iterated.
    \param data The data to be passed to the func when it is applied.
    \param cmp  A function to be used for comparing the items.

    \return     A pointer to the data found, or NULL if not found. */

AvrClass *
dlist_lookup (DList *head, AvrClass *data, DListFP_Cmp cmp)
{
    DList *node = head;

    if (cmp == NULL)
        avr_error ("compare function not specified");

    while (node)
    {
        if ((*cmp) (node->data, data) == 0)
            return node->data;

        node = node->next;
    }

    /* If we get here, no node was found, return NULL. */

    return NULL;
}

/** \brief Extract the data from the head of the list.

    Returns the data element for the head of the list. If the list is empty,
    return a NULL pointer.

    \param head The head of the list.

    \return     A pointer to the data found, or NULL if not found. */

AvrClass *
dlist_get_head_data (DList *head)
{

    if (head == NULL)
    {
        return NULL;
    }

    return head->data;
}

/* a simple node compare function for the iterator. */

static int
dlist_iterator_cmp (AvrClass *n1, AvrClass *n2)
{
    /* Since this is only used in the iterator, we are guaranteed that it is
       safe to compare data pointers because both n1 and n2 came from the
       list. */

    return (int)(n1 - n2);
}

/** \brief Iterate over all elements of the list.

    For each element, call the user supplied iterator function and pass it the
    node data and the user_data. If the iterator function return non-zero,
    remove the node from the list.

    \param head The head of the list to be iterated.
    \param func The function to be applied.
    \param user_data The data to be passed to the func when it is applied.

    \return A pointer to the head of the possibly modified list. */

DList *
dlist_iterator (DList *head, DListFP_Iter func, void *user_data)
{
    DList *node = head;

    if (func == NULL)
        avr_error ("no iteration func supplied");

    while (node)
    {
        if ((*func) (node->data, user_data))
        {
            /* remove node */
            head = dlist_delete (head, node->data, dlist_iterator_cmp);
        }

        node = node->next;
    }

    return head;
}