Home | History | Annotate | Download | only in strace
      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 dyxlat_alloc(const size_t nmemb)
     43 {
     44 	struct dyxlat *const dyxlat = xmalloc(sizeof(*dyxlat));
     45 
     46 	dyxlat->used = 1;
     47 	dyxlat->allocated = nmemb;
     48 	dyxlat->xlat = xgrowarray(NULL, &dyxlat->allocated, sizeof(struct xlat));
     49 	MARK_END(dyxlat->xlat[0]);
     50 
     51 	return dyxlat;
     52 }
     53 
     54 void
     55 dyxlat_free(struct dyxlat *const dyxlat)
     56 {
     57 	size_t i;
     58 
     59 	for (i = 0; i < dyxlat->used - 1; ++i) {
     60 		free((void *) dyxlat->xlat[i].str);
     61 		dyxlat->xlat[i].str = NULL;
     62 	}
     63 
     64 	free(dyxlat->xlat);
     65 	dyxlat->xlat = NULL;
     66 	free(dyxlat);
     67 }
     68 
     69 const struct xlat *
     70 dyxlat_get(const struct dyxlat *const dyxlat)
     71 {
     72 	return dyxlat->xlat;
     73 }
     74 
     75 void
     76 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 	for (i = 0; i < dyxlat->used - 1; ++i) {
     82 		if (dyxlat->xlat[i].val == val) {
     83 			if (strncmp(dyxlat->xlat[i].str, str, len) == 0
     84 			    && dyxlat->xlat[i].str[len] == '\0')
     85 				return;
     86 
     87 			free((void *) dyxlat->xlat[i].str);
     88 			dyxlat->xlat[i].str = xstrndup(str, len);
     89 			return;
     90 		}
     91 	}
     92 
     93 	if (dyxlat->used >= dyxlat->allocated)
     94 		dyxlat->xlat = xgrowarray(dyxlat->xlat, &dyxlat->allocated,
     95 					  sizeof(struct xlat));
     96 
     97 	dyxlat->xlat[dyxlat->used - 1].val = val;
     98 	dyxlat->xlat[dyxlat->used - 1].str = xstrndup(str, len);
     99 	MARK_END(dyxlat->xlat[dyxlat->used]);
    100 	dyxlat->used++;
    101 }
    102