source: mainline/uspace/app/sbi/src/intmap.c@ 074444f

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

Update SBI to rev. 157.

  • Property mode set to 100644
File size: 3.4 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 *). Current implementation is trivial
32 * (linked list of key-value pairs).
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <assert.h>
38#include "list.h"
39#include "mytypes.h"
40
41#include "intmap.h"
42
43/** Initialize map.
44 *
45 * @param intmap Map to initialize.
46 */
47void intmap_init(intmap_t *intmap)
48{
49 list_init(&intmap->elem);
50}
51
52/** Set value corresponding to a key.
53 *
54 * If there already exists a mapping for @a key in the map, it is
55 * silently replaced. If @a value is @c NULL, the mapping for @a key
56 * is removed from the map.
57 *
58 * @param intmap Map.
59 * @param key Key (integer).
60 * @param value Value (must be a pointer) or @c NULL.
61 */
62void intmap_set(intmap_t *intmap, int key, void *value)
63{
64 list_node_t *node;
65 map_elem_t *elem;
66
67 node = list_first(&intmap->elem);
68 while (node != NULL) {
69 elem = list_node_data(node, map_elem_t *);
70 if (elem->key == key) {
71 if (value != NULL) {
72 /* Replace existing value. */
73 elem->value = value;
74 } else {
75 /* Remove map element. */
76 list_remove(&intmap->elem, node);
77 node->data = NULL;
78 free(node);
79 }
80 return;
81 }
82 node = list_next(&intmap->elem, node);
83 }
84
85 /* Allocate new map element and add it to the list. */
86
87 elem = calloc(1, sizeof(map_elem_t));
88 if (elem == NULL) {
89 printf("Memory allocation failed.\n");
90 exit(1);
91 }
92
93 elem->key = key;
94 elem->value = value;
95 list_append(&intmap->elem, elem);
96}
97
98/** Get value corresponding to a key.
99 *
100 * @param intmap Map.
101 * @param key Key for which to retrieve mapping.
102 *
103 * @return Value correspoding to @a key or @c NULL if no mapping
104 * exists.
105 */
106void *intmap_get(intmap_t *intmap, int key)
107{
108 list_node_t *node;
109 map_elem_t *elem;
110
111 node = list_first(&intmap->elem);
112 while (node != NULL) {
113 elem = list_node_data(node, map_elem_t *);
114 if (elem->key == key) {
115 return elem->value;
116 }
117 node = list_next(&intmap->elem, node);
118 }
119
120 /* Not found */
121 return NULL;
122}
Note: See TracBrowser for help on using the repository browser.