Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2017 The strace developers.
3 : : *
4 : : * Redistribution and use in source and binary forms, with or without
5 : : * modification, are permitted provided that the following conditions
6 : : * are met:
7 : : * 1. Redistributions of source code must retain the above copyright
8 : : * notice, this list of conditions and the following disclaimer.
9 : : * 2. Redistributions in binary form must reproduce the above copyright
10 : : * notice, this list of conditions and the following disclaimer in the
11 : : * documentation and/or other materials provided with the distribution.
12 : : * 3. The name of the author may not be used to endorse or promote products
13 : : * derived from this software without specific prior written permission.
14 : : *
15 : : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 : : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 : : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 : : * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 : : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 : : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 : : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 : : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 : : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 : : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 : : */
26 : :
27 : : #include "defs.h"
28 : :
29 : : struct dyxlat {
30 : : size_t used;
31 : : size_t allocated;
32 : : struct xlat *xlat;
33 : : };
34 : :
35 : : #define MARK_END(xlat) \
36 : : do { \
37 : : (xlat).val = 0; \
38 : : (xlat).str = 0; \
39 : : } while (0)
40 : :
41 : : struct dyxlat *
42 : 2 : dyxlat_alloc(const size_t nmemb)
43 : : {
44 : 2 : struct dyxlat *const dyxlat = xmalloc(sizeof(*dyxlat));
45 : :
46 : 2 : dyxlat->used = 1;
47 [ + - ]: 2 : dyxlat->allocated = nmemb ? nmemb : 16;
48 : 2 : dyxlat->xlat = xcalloc(dyxlat->allocated, sizeof(struct xlat));
49 : 2 : MARK_END(dyxlat->xlat[0]);
50 : :
51 : 2 : return dyxlat;
52 : : }
53 : :
54 : : void
55 : 0 : dyxlat_free(struct dyxlat *const dyxlat)
56 : : {
57 : : size_t i;
58 : :
59 [ # # ]: 0 : for (i = 0; i < dyxlat->used - 1; ++i) {
60 : 0 : free((void *) dyxlat->xlat[i].str);
61 : 0 : dyxlat->xlat[i].str = NULL;
62 : : }
63 : :
64 : 0 : free(dyxlat->xlat);
65 : 0 : dyxlat->xlat = NULL;
66 : 0 : free(dyxlat);
67 : 0 : }
68 : :
69 : : const struct xlat *
70 : 2 : dyxlat_get(const struct dyxlat *const dyxlat)
71 : : {
72 : 2 : return dyxlat->xlat;
73 : : }
74 : :
75 : : void
76 : 14 : dyxlat_add_pair(struct dyxlat *const dyxlat, const uint64_t val,
77 : : const char *const str, const size_t len)
78 : : {
79 : : size_t i;
80 : :
81 [ + + ]: 56 : for (i = 0; i < dyxlat->used - 1; ++i) {
82 [ - + ]: 42 : if (dyxlat->xlat[i].val == val) {
83 [ # # ]: 0 : if (strncmp(dyxlat->xlat[i].str, str, len) == 0
84 [ # # ]: 0 : && dyxlat->xlat[i].str[len] == '\0')
85 : : return;
86 : :
87 : 0 : free((void *) dyxlat->xlat[i].str);
88 : 0 : dyxlat->xlat[i].str = xstrndup(str, len);
89 : 0 : return;
90 : : }
91 : : }
92 : :
93 [ - + ]: 14 : if (dyxlat->used >= dyxlat->allocated) {
94 : 0 : dyxlat->allocated *= 2;
95 : 0 : dyxlat->xlat = xreallocarray(dyxlat->xlat, dyxlat->allocated,
96 : : sizeof(struct xlat));
97 : : }
98 : :
99 : 14 : dyxlat->xlat[dyxlat->used - 1].val = val;
100 : 14 : dyxlat->xlat[dyxlat->used - 1].str = xstrndup(str, len);
101 : 14 : MARK_END(dyxlat->xlat[dyxlat->used]);
102 : 14 : dyxlat->used++;
103 : : }
|