]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/libpreopen/lib/po_map.c
Remove more unsupported headers. (#123)
[wasi-libc.git] / libc-bottom-half / libpreopen / lib / po_map.c
1 /*-
2 * Copyright (c) 2016 Stanley Uche Godfrey
3 * Copyright (c) 2016, 2018 Jonathan Anderson
4 * All rights reserved.
5 *
6 * This software was developed at Memorial University under the
7 * NSERC Discovery program (RGPIN-2015-06048).
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /**
33 * @file po_map.c
34 * @brief Implementation of po_map management functions
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "internal.h"
42
43 #ifdef __wasilibc_unmodified_upstream
44 #else
45 static
46 #endif
47 struct po_map*
48 po_map_create(int capacity)
49 {
50 struct po_map *map;
51
52 map = malloc(sizeof(struct po_map));
53 if (map == NULL) {
54 return (NULL);
55 }
56
57 map->entries = calloc(sizeof(struct po_map_entry), capacity);
58 if (map->entries == NULL) {
59 free(map);
60 return (NULL);
61 }
62
63 map->refcount = 1;
64 map->capacity = capacity;
65 map->length = 0;
66
67 po_map_assertvalid(map);
68
69 return (map);
70 }
71
72 #ifdef __wasilibc_unmodified_upstream
73 #else
74 static
75 #endif
76 struct po_map*
77 po_map_enlarge(struct po_map *map)
78 {
79 struct po_map_entry *enlarged;
80 enlarged = calloc(sizeof(struct po_map_entry), 2 * map->capacity);
81 if (enlarged == NULL) {
82 return (NULL);
83 }
84 memcpy(enlarged, map->entries, map->length * sizeof(*enlarged));
85 free(map->entries);
86 map->entries = enlarged;
87 map->capacity = 2 * map->capacity;
88 return map;
89 }
90
91 #ifdef __wasilibc_unmodified_upstream
92 size_t
93 po_map_foreach(const struct po_map *map, po_map_iter_cb cb)
94 {
95 struct po_map_entry *entry;
96 size_t n;
97
98 po_map_assertvalid(map);
99
100 for (n = 0; n < map->length; n++) {
101 entry = map->entries + n;
102
103 if (!cb(entry->name, entry->fd, entry->rights)) {
104 break;
105 }
106 }
107
108 return (n);
109 }
110 #endif
111
112 #ifdef __wasilibc_unmodified_upstream
113 #else
114 static
115 #endif
116 void
117 po_map_release(struct po_map *map)
118 {
119 if (map == NULL) {
120 return;
121 }
122
123 po_map_assertvalid(map);
124
125 map->refcount -= 1;
126
127 if (map->refcount == 0) {
128 free(map->entries);
129 free(map);
130 }
131 }