xref: /illumos-gate/usr/src/tools/smatch/src/ptrmap.c (revision c85f09cc)
1*c85f09ccSJohn Levon // SPDX-License-Identifier: MIT
2*c85f09ccSJohn Levon /*
3*c85f09ccSJohn Levon  * Stupid implementation of pointer -> pointer map.
4*c85f09ccSJohn Levon  *
5*c85f09ccSJohn Levon  * Copyright (c) 2017 Luc Van Oostenryck.
6*c85f09ccSJohn Levon  *
7*c85f09ccSJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
8*c85f09ccSJohn Levon  * of this software and associated documentation files (the "Software"), to deal
9*c85f09ccSJohn Levon  * in the Software without restriction, including without limitation the rights
10*c85f09ccSJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11*c85f09ccSJohn Levon  * copies of the Software, and to permit persons to whom the Software is
12*c85f09ccSJohn Levon  * furnished to do so, subject to the following conditions:
13*c85f09ccSJohn Levon  *
14*c85f09ccSJohn Levon  * The above copyright notice and this permission notice shall be included in
15*c85f09ccSJohn Levon  * all copies or substantial portions of the Software.
16*c85f09ccSJohn Levon  *
17*c85f09ccSJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*c85f09ccSJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*c85f09ccSJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20*c85f09ccSJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*c85f09ccSJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*c85f09ccSJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23*c85f09ccSJohn Levon  * THE SOFTWARE.
24*c85f09ccSJohn Levon  */
25*c85f09ccSJohn Levon 
26*c85f09ccSJohn Levon #include "ptrmap.h"
27*c85f09ccSJohn Levon #include "allocate.h"
28*c85f09ccSJohn Levon #include <stddef.h>
29*c85f09ccSJohn Levon 
30*c85f09ccSJohn Levon #define	MAP_NR	7
31*c85f09ccSJohn Levon 
32*c85f09ccSJohn Levon struct ptrpair {
33*c85f09ccSJohn Levon 	void *key;
34*c85f09ccSJohn Levon 	void *val;
35*c85f09ccSJohn Levon };
36*c85f09ccSJohn Levon struct ptrmap {
37*c85f09ccSJohn Levon 	struct ptrmap *next;
38*c85f09ccSJohn Levon 	int nr;
39*c85f09ccSJohn Levon 	struct ptrpair pairs[MAP_NR];
40*c85f09ccSJohn Levon };
41*c85f09ccSJohn Levon 
42*c85f09ccSJohn Levon DECLARE_ALLOCATOR(ptrmap);
43*c85f09ccSJohn Levon ALLOCATOR(ptrmap, "ptrmap");
44*c85f09ccSJohn Levon 
__ptrmap_add(struct ptrmap ** mapp,void * key,void * val)45*c85f09ccSJohn Levon void __ptrmap_add(struct ptrmap **mapp, void *key, void *val)
46*c85f09ccSJohn Levon {
47*c85f09ccSJohn Levon 	struct ptrmap *head = *mapp;
48*c85f09ccSJohn Levon 	struct ptrmap *newmap;
49*c85f09ccSJohn Levon 	struct ptrmap *map;
50*c85f09ccSJohn Levon 	struct ptrpair *pair;
51*c85f09ccSJohn Levon 	int nr;
52*c85f09ccSJohn Levon 
53*c85f09ccSJohn Levon 	if ((map = head)) {
54*c85f09ccSJohn Levon 		struct ptrmap *next = map->next;
55*c85f09ccSJohn Levon 		if (next)		// head is full
56*c85f09ccSJohn Levon 			map = next;
57*c85f09ccSJohn Levon 		if ((nr = map->nr) < MAP_NR)
58*c85f09ccSJohn Levon 			goto oldmap;
59*c85f09ccSJohn Levon 	}
60*c85f09ccSJohn Levon 
61*c85f09ccSJohn Levon 	// need a new block
62*c85f09ccSJohn Levon 	newmap = __alloc_ptrmap(0);
63*c85f09ccSJohn Levon 	if (!head) {
64*c85f09ccSJohn Levon 		*mapp = newmap;
65*c85f09ccSJohn Levon 	} else {
66*c85f09ccSJohn Levon 		newmap->next = head->next;
67*c85f09ccSJohn Levon 		head->next = newmap;
68*c85f09ccSJohn Levon 	}
69*c85f09ccSJohn Levon 	map = newmap;
70*c85f09ccSJohn Levon 	nr = 0;
71*c85f09ccSJohn Levon 
72*c85f09ccSJohn Levon oldmap:
73*c85f09ccSJohn Levon 	pair = &map->pairs[nr];
74*c85f09ccSJohn Levon 	pair->key = key;
75*c85f09ccSJohn Levon 	pair->val = val;
76*c85f09ccSJohn Levon 	map->nr = ++nr;
77*c85f09ccSJohn Levon }
78*c85f09ccSJohn Levon 
__ptrmap_lookup(struct ptrmap * map,void * key)79*c85f09ccSJohn Levon void *__ptrmap_lookup(struct ptrmap *map, void *key)
80*c85f09ccSJohn Levon {
81*c85f09ccSJohn Levon 	for (; map; map = map->next) {
82*c85f09ccSJohn Levon 		int i, n = map->nr;
83*c85f09ccSJohn Levon 		for (i = 0; i < n; i++) {
84*c85f09ccSJohn Levon 			struct ptrpair *pair = &map->pairs[i];
85*c85f09ccSJohn Levon 			if (pair->key == key)
86*c85f09ccSJohn Levon 				return pair->val;
87*c85f09ccSJohn Levon 		}
88*c85f09ccSJohn Levon 	}
89*c85f09ccSJohn Levon 	return NULL;
90*c85f09ccSJohn Levon }
91*c85f09ccSJohn Levon 
__ptrmap_update(struct ptrmap ** mapp,void * key,void * val)92*c85f09ccSJohn Levon void __ptrmap_update(struct ptrmap **mapp, void *key, void *val)
93*c85f09ccSJohn Levon {
94*c85f09ccSJohn Levon 	struct ptrmap *map = *mapp;
95*c85f09ccSJohn Levon 
96*c85f09ccSJohn Levon 	for (; map; map = map->next) {
97*c85f09ccSJohn Levon 		int i, n = map->nr;
98*c85f09ccSJohn Levon 		for (i = 0; i < n; i++) {
99*c85f09ccSJohn Levon 			struct ptrpair *pair = &map->pairs[i];
100*c85f09ccSJohn Levon 			if (pair->key == key) {
101*c85f09ccSJohn Levon 				if (pair->val != val)
102*c85f09ccSJohn Levon 					pair->val = val;
103*c85f09ccSJohn Levon 				return;
104*c85f09ccSJohn Levon 			}
105*c85f09ccSJohn Levon 		}
106*c85f09ccSJohn Levon 	}
107*c85f09ccSJohn Levon 
108*c85f09ccSJohn Levon 	__ptrmap_add(mapp, key, val);
109*c85f09ccSJohn Levon }
110