Home | History | Annotate | Line # | Download | only in dist
utils.c revision 1.1.1.1
      1  1.1  dholland /*-
      2  1.1  dholland  * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc.
      3  1.1  dholland  * All rights reserved.
      4  1.1  dholland  *
      5  1.1  dholland  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1  dholland  * by David A. Holland.
      7  1.1  dholland  *
      8  1.1  dholland  * Redistribution and use in source and binary forms, with or without
      9  1.1  dholland  * modification, are permitted provided that the following conditions
     10  1.1  dholland  * are met:
     11  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     12  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     13  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     16  1.1  dholland  *
     17  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1  dholland  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1  dholland  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1  dholland  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1  dholland  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1  dholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1  dholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  dholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1  dholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1  dholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1  dholland  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1  dholland  */
     29  1.1  dholland 
     30  1.1  dholland #include <stdlib.h>
     31  1.1  dholland #include <string.h>
     32  1.1  dholland #include <assert.h>
     33  1.1  dholland 
     34  1.1  dholland #include "utils.h"
     35  1.1  dholland 
     36  1.1  dholland #define MALLOCDEBUG
     37  1.1  dholland 
     38  1.1  dholland const char ws[] =
     39  1.1  dholland 	" \t\f\v"
     40  1.1  dholland ;
     41  1.1  dholland const char alnum[] =
     42  1.1  dholland 	"0123456789"
     43  1.1  dholland 	"abcdefghijklmnopqrstuvwxyz"
     44  1.1  dholland 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     45  1.1  dholland 	"_"
     46  1.1  dholland ;
     47  1.1  dholland 
     48  1.1  dholland ////////////////////////////////////////////////////////////
     49  1.1  dholland // malloc
     50  1.1  dholland 
     51  1.1  dholland #define ROUNDUP(len, size) ((size) * (((len) + (size) - 1) / (size)))
     52  1.1  dholland 
     53  1.1  dholland #ifdef MALLOCDEBUG
     54  1.1  dholland 
     55  1.1  dholland struct mallocheader {
     56  1.1  dholland 	struct mallocheader *self;
     57  1.1  dholland 	size_t len;
     58  1.1  dholland };
     59  1.1  dholland 
     60  1.1  dholland static
     61  1.1  dholland size_t
     62  1.1  dholland adjustsize(size_t len)
     63  1.1  dholland {
     64  1.1  dholland 	const size_t sz = sizeof(struct mallocheader);
     65  1.1  dholland 	return ROUNDUP(len, sz) + 2*sz;
     66  1.1  dholland }
     67  1.1  dholland 
     68  1.1  dholland static
     69  1.1  dholland void *
     70  1.1  dholland placeheaders(void *block, size_t len)
     71  1.1  dholland {
     72  1.1  dholland 	struct mallocheader *bothdr, *tophdr;
     73  1.1  dholland 	size_t roundedlen;
     74  1.1  dholland 	void *ret;
     75  1.1  dholland 
     76  1.1  dholland 	roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
     77  1.1  dholland 	bothdr = block;
     78  1.1  dholland 	bothdr->len = len;
     79  1.1  dholland 	bothdr->self = block;
     80  1.1  dholland 	ret = bothdr + 1;
     81  1.1  dholland 	tophdr = (void *)(((unsigned char *)ret) + roundedlen);
     82  1.1  dholland 	tophdr->len = len;
     83  1.1  dholland 	tophdr->self = bothdr;
     84  1.1  dholland 	return ret;
     85  1.1  dholland }
     86  1.1  dholland 
     87  1.1  dholland static
     88  1.1  dholland void *
     89  1.1  dholland checkheaders(void *block, size_t len)
     90  1.1  dholland {
     91  1.1  dholland 	struct mallocheader *bothdr, *tophdr;
     92  1.1  dholland 	size_t roundedlen;
     93  1.1  dholland 
     94  1.1  dholland 	if (block == NULL) {
     95  1.1  dholland 		assert(len == 0);
     96  1.1  dholland 		return block;
     97  1.1  dholland 	}
     98  1.1  dholland 
     99  1.1  dholland 	roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
    100  1.1  dholland 	bothdr = block;
    101  1.1  dholland 	bothdr--;
    102  1.1  dholland 	assert(bothdr->self == bothdr);
    103  1.1  dholland 	assert(bothdr->len == len);
    104  1.1  dholland 	tophdr = (void *)(((unsigned char *)(bothdr + 1)) + roundedlen);
    105  1.1  dholland 	assert(tophdr->self == bothdr);
    106  1.1  dholland 	assert(tophdr->len == len);
    107  1.1  dholland 	return bothdr;
    108  1.1  dholland }
    109  1.1  dholland 
    110  1.1  dholland #else
    111  1.1  dholland 
    112  1.1  dholland #define adjustsize(len) (len)
    113  1.1  dholland #define placeheaders(block, len) ((void)(len), (block))
    114  1.1  dholland #define checkheaders(ptr, len) ((void)(len), (ptr))
    115  1.1  dholland 
    116  1.1  dholland #endif /* MALLOCDEBUG */
    117  1.1  dholland 
    118  1.1  dholland void *
    119  1.1  dholland domalloc(size_t len)
    120  1.1  dholland {
    121  1.1  dholland 	void *ret;
    122  1.1  dholland 	size_t blocklen;
    123  1.1  dholland 
    124  1.1  dholland 	blocklen = adjustsize(len);
    125  1.1  dholland 	ret = malloc(blocklen);
    126  1.1  dholland 	if (ret == NULL) {
    127  1.1  dholland 		complain(NULL, "Out of memory");
    128  1.1  dholland 		die();
    129  1.1  dholland 	}
    130  1.1  dholland 
    131  1.1  dholland 	return placeheaders(ret, len);
    132  1.1  dholland }
    133  1.1  dholland 
    134  1.1  dholland void *
    135  1.1  dholland dorealloc(void *ptr, size_t oldlen, size_t newlen)
    136  1.1  dholland {
    137  1.1  dholland 	void *ret;
    138  1.1  dholland 	void *blockptr;
    139  1.1  dholland 	size_t newblocklen;
    140  1.1  dholland 
    141  1.1  dholland 	blockptr = checkheaders(ptr, oldlen);
    142  1.1  dholland 	newblocklen = adjustsize(newlen);
    143  1.1  dholland 
    144  1.1  dholland 	ret = realloc(blockptr, newblocklen);
    145  1.1  dholland 	if (ret == NULL) {
    146  1.1  dholland 		complain(NULL, "Out of memory");
    147  1.1  dholland 		die();
    148  1.1  dholland 	}
    149  1.1  dholland 
    150  1.1  dholland 	return placeheaders(ret, newlen);
    151  1.1  dholland }
    152  1.1  dholland 
    153  1.1  dholland void
    154  1.1  dholland dofree(void *ptr, size_t len)
    155  1.1  dholland {
    156  1.1  dholland 	void *blockptr;
    157  1.1  dholland 
    158  1.1  dholland 	blockptr = checkheaders(ptr, len);
    159  1.1  dholland 	free(blockptr);
    160  1.1  dholland }
    161  1.1  dholland 
    162  1.1  dholland ////////////////////////////////////////////////////////////
    163  1.1  dholland // string allocators
    164  1.1  dholland 
    165  1.1  dholland char *
    166  1.1  dholland dostrdup(const char *s)
    167  1.1  dholland {
    168  1.1  dholland 	char *ret;
    169  1.1  dholland 	size_t len;
    170  1.1  dholland 
    171  1.1  dholland 	len = strlen(s);
    172  1.1  dholland 	ret = domalloc(len+1);
    173  1.1  dholland 	strcpy(ret, s);
    174  1.1  dholland 	return ret;
    175  1.1  dholland }
    176  1.1  dholland 
    177  1.1  dholland char *
    178  1.1  dholland dostrdup2(const char *s, const char *t)
    179  1.1  dholland {
    180  1.1  dholland 	char *ret;
    181  1.1  dholland 	size_t len;
    182  1.1  dholland 
    183  1.1  dholland 	len = strlen(s) + strlen(t);
    184  1.1  dholland 	ret = domalloc(len+1);
    185  1.1  dholland 	strcpy(ret, s);
    186  1.1  dholland 	strcat(ret, t);
    187  1.1  dholland 	return ret;
    188  1.1  dholland }
    189  1.1  dholland 
    190  1.1  dholland char *
    191  1.1  dholland dostrdup3(const char *s, const char *t, const char *u)
    192  1.1  dholland {
    193  1.1  dholland 	char *ret;
    194  1.1  dholland 	size_t len;
    195  1.1  dholland 
    196  1.1  dholland 	len = strlen(s) + strlen(t) + strlen(u);
    197  1.1  dholland 	ret = domalloc(len+1);
    198  1.1  dholland 	strcpy(ret, s);
    199  1.1  dholland 	strcat(ret, t);
    200  1.1  dholland 	strcat(ret, u);
    201  1.1  dholland 	return ret;
    202  1.1  dholland }
    203  1.1  dholland 
    204  1.1  dholland char *
    205  1.1  dholland dostrndup(const char *s, size_t len)
    206  1.1  dholland {
    207  1.1  dholland 	char *ret;
    208  1.1  dholland 
    209  1.1  dholland 	ret = domalloc(len+1);
    210  1.1  dholland 	memcpy(ret, s, len);
    211  1.1  dholland 	ret[len] = '\0';
    212  1.1  dholland 	return ret;
    213  1.1  dholland }
    214  1.1  dholland 
    215  1.1  dholland void
    216  1.1  dholland dostrfree(char *s)
    217  1.1  dholland {
    218  1.1  dholland 	dofree(s, strlen(s)+1);
    219  1.1  dholland }
    220  1.1  dholland 
    221  1.1  dholland ////////////////////////////////////////////////////////////
    222  1.1  dholland // other stuff
    223  1.1  dholland 
    224  1.1  dholland size_t
    225  1.1  dholland notrailingws(char *buf, size_t len)
    226  1.1  dholland {
    227  1.1  dholland 	while (len > 0 && strchr(ws, buf[len-1])) {
    228  1.1  dholland 		buf[--len] = '\0';
    229  1.1  dholland 	}
    230  1.1  dholland 	return len;
    231  1.1  dholland }
    232  1.1  dholland 
    233  1.1  dholland bool
    234  1.1  dholland is_identifier(const char *str)
    235  1.1  dholland {
    236  1.1  dholland 	size_t len;
    237  1.1  dholland 
    238  1.1  dholland 	len = strlen(str);
    239  1.1  dholland 	if (len != strspn(str, alnum)) {
    240  1.1  dholland 		return false;
    241  1.1  dholland 	}
    242  1.1  dholland 	if (str[0] >= '0' && str[0] <= '9') {
    243  1.1  dholland 		return false;
    244  1.1  dholland 	}
    245  1.1  dholland 	return true;
    246  1.1  dholland }
    247