1 /*
2   Copyright 2020 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <platform.h>
26 #include <array_map_priv.h>
27 #include <alloc.h>
28 
29 /* FIXME: make configurable and move to map.c */
30 #define TINY_LIMIT 14
31 
ArrayMapNew(MapKeyEqualFn equal_fn,MapDestroyDataFn destroy_key_fn,MapDestroyDataFn destroy_value_fn)32 ArrayMap *ArrayMapNew(MapKeyEqualFn equal_fn,
33                       MapDestroyDataFn destroy_key_fn,
34                       MapDestroyDataFn destroy_value_fn)
35 {
36     ArrayMap *map = xcalloc(1, sizeof(ArrayMap));
37     map->equal_fn = equal_fn;
38     map->destroy_key_fn = destroy_key_fn;
39     map->destroy_value_fn = destroy_value_fn;
40     map->values = xcalloc(1, sizeof(MapKeyValue) * TINY_LIMIT);
41     return map;
42 }
43 
ArrayMapInsert(ArrayMap * map,void * key,void * value)44 int ArrayMapInsert(ArrayMap *map, void *key, void *value)
45 {
46     if (map->size == TINY_LIMIT)
47     {
48         return 0;
49     }
50 
51     for (int i = 0; i < map->size; ++i)
52     {
53         if (map->equal_fn(map->values[i].key, key))
54         {
55             /* Replace the key with the new one despite those two being the
56              * same, since the new key might be referenced somewhere inside
57              * the new value. */
58             map->destroy_key_fn(map->values[i].key);
59             map->destroy_value_fn(map->values[i].value);
60             map->values[i].key   = key;
61             map->values[i].value = value;
62             return 1;
63         }
64     }
65 
66     map->values[map->size++] = (MapKeyValue) { key, value };
67     return 2;
68 }
69 
ArrayMapRemove(ArrayMap * map,const void * key)70 bool ArrayMapRemove(ArrayMap *map, const void *key)
71 {
72     for (int i = 0; i < map->size; ++i)
73     {
74         if (map->equal_fn(map->values[i].key, key))
75         {
76             map->destroy_key_fn(map->values[i].key);
77             map->destroy_value_fn(map->values[i].value);
78 
79             memmove(map->values + i, map->values + i + 1,
80                     sizeof(MapKeyValue) * (map->size - i - 1));
81 
82             map->size--;
83             return true;
84         }
85     }
86     return false;
87 }
88 
ArrayMapGet(const ArrayMap * map,const void * key)89 MapKeyValue *ArrayMapGet(const ArrayMap *map, const void *key)
90 {
91     for (int i = 0; i < map->size; ++i)
92     {
93         if (map->equal_fn(map->values[i].key, key))
94         {
95             return map->values + i;
96         }
97     }
98     return NULL;
99 }
100 
ArrayMapClear(ArrayMap * map)101 void ArrayMapClear(ArrayMap *map)
102 {
103     for (int i = 0; i < map->size; ++i)
104     {
105         map->destroy_key_fn(map->values[i].key);
106         map->destroy_value_fn(map->values[i].value);
107     }
108     map->size = 0;
109 }
110 
ArrayMapSoftDestroy(ArrayMap * map)111 void ArrayMapSoftDestroy(ArrayMap *map)
112 {
113     if (map)
114     {
115         for (int i = 0; i < map->size; ++i)
116         {
117             map->destroy_key_fn(map->values[i].key);
118         }
119         map->size = 0;
120 
121         free(map->values);
122         free(map);
123     }
124 }
125 
ArrayMapDestroy(ArrayMap * map)126 void ArrayMapDestroy(ArrayMap *map)
127 {
128     if (map)
129     {
130         ArrayMapClear(map);
131         free(map->values);
132         free(map);
133     }
134 }
135 
136 /******************************************************************************/
137 
ArrayMapIteratorInit(ArrayMap * map)138 ArrayMapIterator ArrayMapIteratorInit(ArrayMap *map)
139 {
140     return (ArrayMapIterator) { map, 0 };
141 }
142 
ArrayMapIteratorNext(ArrayMapIterator * i)143 MapKeyValue *ArrayMapIteratorNext(ArrayMapIterator *i)
144 {
145     if (i->pos >= i->map->size)
146     {
147         return NULL;
148     }
149     else
150     {
151         return &i->map->values[i->pos++];
152     }
153 }
154