source: mainline/uspace/app/sbi/src/intmap.c@ 09ababb7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 09ababb7 was 09ababb7, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Add Sysel Bootstrap Interpreter (SBI) from Sysel repository rev. 53.

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file Integer map.
30 *
31 * Maps integers to pointers (void *).
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <assert.h>
37#include "list.h"
38#include "mytypes.h"
39
40#include "intmap.h"
41
42/** Initialize map. */
43void intmap_init(intmap_t *intmap)
44{
45 list_init(&intmap->elem);
46}
47
48/** Set value corresponding to a key. */
49void intmap_set(intmap_t *intmap, int key, void *value)
50{
51 list_node_t *node;
52 map_elem_t *elem;
53
54 node = list_first(&intmap->elem);
55 while (node != NULL) {
56 elem = list_node_data(node, map_elem_t *);
57 if (elem->key == key) {
58 if (value != NULL) {
59 /* Replace existing value. */
60 elem->value = value;
61 } else {
62 /* Remove map element. */
63 list_remove(&intmap->elem, node);
64 node->data = NULL;
65 free(node);
66 }
67 return;
68 }
69 node = list_next(&intmap->elem, node);
70 }
71
72 /* Allocate new map element and add it to the list. */
73
74 elem = calloc(1, sizeof(map_elem_t));
75 if (elem == NULL) {
76 printf("Memory allocation failed.\n");
77 exit(1);
78 }
79
80 elem->key = key;
81 elem->value = value;
82 list_append(&intmap->elem, elem);
83}
84
85/** Get value corresponding to a key. */
86void *intmap_get(intmap_t *intmap, int key)
87{
88 list_node_t *node;
89 map_elem_t *elem;
90
91 node = list_first(&intmap->elem);
92 while (node != NULL) {
93 elem = list_node_data(node, map_elem_t *);
94 if (elem->key == key) {
95 return elem->value;
96 }
97 node = list_next(&intmap->elem, node);
98 }
99
100 /* Not found */
101 return NULL;
102}
Note: See TracBrowser for help on using the repository browser.