17ec681f3Smrg/* 27ec681f3Smrg * Copyright (C) 2010-2011 Marcin Kościelnicki <koriakin@0x04.net> 37ec681f3Smrg * Copyright (C) 2010 Francisco Jerez <currojerez@riseup.net> 47ec681f3Smrg * All Rights Reserved. 57ec681f3Smrg * 67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 77ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 87ec681f3Smrg * to deal in the Software without restriction, including without limitation 97ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 107ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 117ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 127ec681f3Smrg * 137ec681f3Smrg * The above copyright notice and this permission notice (including the next 147ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 157ec681f3Smrg * Software. 167ec681f3Smrg * 177ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 187ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 197ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 207ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 217ec681f3Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 227ec681f3Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 237ec681f3Smrg * OTHER DEALINGS IN THE SOFTWARE. 247ec681f3Smrg */ 257ec681f3Smrg 267ec681f3Smrg#ifndef UTIL_H 277ec681f3Smrg#define UTIL_H 287ec681f3Smrg 297ec681f3Smrg#include <stdlib.h> 307ec681f3Smrg#include <stdio.h> 317ec681f3Smrg#include <inttypes.h> 327ec681f3Smrg 337ec681f3Smrg#define ADDARRAY(a, e) \ 347ec681f3Smrg do { \ 357ec681f3Smrg if ((a ## num) >= (a ## max)) { \ 367ec681f3Smrg if (!(a ## max)) \ 377ec681f3Smrg (a ## max) = 16; \ 387ec681f3Smrg else \ 397ec681f3Smrg (a ## max) *= 2; \ 407ec681f3Smrg (a) = realloc((a), (a ## max)*sizeof(*(a))); \ 417ec681f3Smrg } \ 427ec681f3Smrg (a)[(a ## num)++] = (e); \ 437ec681f3Smrg } while(0) 447ec681f3Smrg 457ec681f3Smrg#define FINDARRAY(a, tmp, pred) \ 467ec681f3Smrg ({ \ 477ec681f3Smrg int __i; \ 487ec681f3Smrg \ 497ec681f3Smrg for (__i = 0; __i < (a ## num); __i++) { \ 507ec681f3Smrg tmp = (a)[__i]; \ 517ec681f3Smrg if (pred) \ 527ec681f3Smrg break; \ 537ec681f3Smrg } \ 547ec681f3Smrg \ 557ec681f3Smrg tmp = ((pred) ? tmp : NULL); \ 567ec681f3Smrg }) 577ec681f3Smrg 587ec681f3Smrg/* ceil(log2(x)) */ 597ec681f3Smrgstatic inline int clog2(uint64_t x) { 607ec681f3Smrg if (!x) 617ec681f3Smrg return x; 627ec681f3Smrg int r = 0; 637ec681f3Smrg while (x - 1 > (1ull << r) - 1) 647ec681f3Smrg r++; 657ec681f3Smrg return r; 667ec681f3Smrg} 677ec681f3Smrg 687ec681f3Smrg#define ARRAY_SIZE(a) (sizeof (a) / sizeof *(a)) 697ec681f3Smrg 707ec681f3Smrg#define min(a,b) \ 717ec681f3Smrg ({ \ 727ec681f3Smrg typeof (a) _a = (a); \ 737ec681f3Smrg typeof (b) _b = (b); \ 747ec681f3Smrg _a < _b ? _a : _b; \ 757ec681f3Smrg }) 767ec681f3Smrg 777ec681f3Smrg#define max(a,b) \ 787ec681f3Smrg ({ \ 797ec681f3Smrg typeof (a) _a = (a); \ 807ec681f3Smrg typeof (b) _b = (b); \ 817ec681f3Smrg _a > _b ? _a : _b; \ 827ec681f3Smrg }) 837ec681f3Smrg 847ec681f3Smrg#define CEILDIV(a, b) (((a) + (b) - 1)/(b)) 857ec681f3Smrg 867ec681f3Smrg#define extr(a, b, c) ((uint64_t)(a) << (64 - (b) - (c)) >> (64 - (c))) 877ec681f3Smrg#define extrs(a, b, c) ((int64_t)(a) << (64 - (b) - (c)) >> (64 - (c))) 887ec681f3Smrg#define sext(a, b) extrs(a, 0, b+1) 897ec681f3Smrg#define bflmask(a) ((2ull << ((a)-1)) - 1) 907ec681f3Smrg#define insrt(a, b, c, d) ((a) = ((a) & ~(bflmask(c) << (b))) | ((d) & bflmask(c)) << (b)) 917ec681f3Smrg 927ec681f3Smrgstruct envy_loc { 937ec681f3Smrg int lstart; 947ec681f3Smrg int cstart; 957ec681f3Smrg int lend; 967ec681f3Smrg int cend; 977ec681f3Smrg const char *file; 987ec681f3Smrg}; 997ec681f3Smrg 1007ec681f3Smrg#define LOC_FORMAT(loc, str) "%s:%d.%d-%d.%d: " str, (loc).file, (loc).lstart, (loc).cstart, (loc).lend, (loc).cend 1017ec681f3Smrg 1027ec681f3Smrguint32_t elf_hash(const char *str); 1037ec681f3Smrg 1047ec681f3SmrgFILE *find_in_path(const char *name, const char *path, char **pfullname); 1057ec681f3Smrg 1067ec681f3Smrgstruct astr { 1077ec681f3Smrg char *str; 1087ec681f3Smrg size_t len; 1097ec681f3Smrg}; 1107ec681f3Smrg 1117ec681f3Smrgvoid print_escaped_astr(FILE *out, struct astr *astr); 1127ec681f3Smrg 1137ec681f3Smrgchar *aprintf(const char *format, ...); 1147ec681f3Smrg 1157ec681f3Smrg#endif 116