GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/bc/../dc/mem.c Lines: 26 32 81.3 %
Date: 2017-11-13 Branches: 6 12 50.0 %

Line Branch Exec Source
1
/*	$OpenBSD: mem.c,v 1.7 2015/02/16 20:53:34 jca Exp $	*/
2
3
/*
4
 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <openssl/err.h>
20
21
#include <err.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "extern.h"
26
27
struct number *
28
new_number(void)
29
{
30
	struct number *n;
31
32
692592
	n = bmalloc(sizeof(*n));
33
346296
	n->scale = 0;
34
346296
	n->number = BN_new();
35
346296
	if (n->number == NULL)
36
		err(1, NULL);
37
346296
	return n;
38
}
39
40
void
41
free_number(struct number *n)
42
{
43
1310680
	BN_free(n->number);
44
655340
	free(n);
45
655340
}
46
47
struct number *
48
dup_number(const struct number *a)
49
{
50
	struct number *n;
51
52
620896
	n = bmalloc(sizeof(*n));
53
310448
	n->scale = a->scale;
54
310448
	n->number = BN_dup(a->number);
55
310448
	bn_checkp(n->number);
56
310448
	return n;
57
}
58
59
void *
60
bmalloc(size_t sz)
61
{
62
	void *p;
63
64
1529536
	p = malloc(sz);
65
764768
	if (p == NULL)
66
		err(1, NULL);
67
764768
	return p;
68
}
69
70
void *
71
breallocarray(void *p, size_t nmemb, size_t size)
72
{
73
	void *q;
74
75
558392
	q = reallocarray(p, nmemb, size);
76
279196
	if (q == NULL)
77
		err(1, NULL);
78
279196
	return q;
79
}
80
81
char *
82
bstrdup(const char *p)
83
{
84
	char *q;
85
86
54360
	q = strdup(p);
87
27180
	if (q == NULL)
88
		err(1, NULL);
89
27180
	return q;
90
}
91
92
void
93
bn_check(int x)						\
94
{
95
2952024
	if (x == 0)
96
		err(1, "big number failure %lx", ERR_get_error());
97
1476012
}
98
99
void
100
bn_checkp(const void *p)						\
101
{
102
1664440
	if (p == NULL)
103
		err(1, "allocation failure %lx", ERR_get_error());
104
832220
}