xref: /netbsd-src/usr.bin/dc/mem.c (revision fc8ee2f577a06855648d534cfaeafda777e59180)
1*fc8ee2f5Schristos /*	$NetBSD: mem.c,v 1.2 2017/04/10 16:37:48 christos Exp $	*/
27d0b3229Schristos /*	$OpenBSD: mem.c,v 1.7 2015/02/16 20:53:34 jca Exp $	*/
37d0b3229Schristos 
47d0b3229Schristos /*
57d0b3229Schristos  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
67d0b3229Schristos  *
77d0b3229Schristos  * Permission to use, copy, modify, and distribute this software for any
87d0b3229Schristos  * purpose with or without fee is hereby granted, provided that the above
97d0b3229Schristos  * copyright notice and this permission notice appear in all copies.
107d0b3229Schristos  *
117d0b3229Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
127d0b3229Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
137d0b3229Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
147d0b3229Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
157d0b3229Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
167d0b3229Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
177d0b3229Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
187d0b3229Schristos  */
19*fc8ee2f5Schristos #include <sys/cdefs.h>
20*fc8ee2f5Schristos __RCSID("$NetBSD: mem.c,v 1.2 2017/04/10 16:37:48 christos Exp $");
217d0b3229Schristos 
227d0b3229Schristos #include <openssl/err.h>
237d0b3229Schristos 
247d0b3229Schristos #include <err.h>
257d0b3229Schristos #include <stdlib.h>
267d0b3229Schristos #include <string.h>
277d0b3229Schristos 
287d0b3229Schristos #include "extern.h"
297d0b3229Schristos 
307d0b3229Schristos struct number *
new_number(void)317d0b3229Schristos new_number(void)
327d0b3229Schristos {
337d0b3229Schristos 	struct number *n;
347d0b3229Schristos 
357d0b3229Schristos 	n = bmalloc(sizeof(*n));
367d0b3229Schristos 	n->scale = 0;
377d0b3229Schristos 	n->number = BN_new();
387d0b3229Schristos 	if (n->number == NULL)
397d0b3229Schristos 		err(1, NULL);
407d0b3229Schristos 	return n;
417d0b3229Schristos }
427d0b3229Schristos 
437d0b3229Schristos void
free_number(struct number * n)447d0b3229Schristos free_number(struct number *n)
457d0b3229Schristos {
467d0b3229Schristos 	BN_free(n->number);
477d0b3229Schristos 	free(n);
487d0b3229Schristos }
497d0b3229Schristos 
507d0b3229Schristos struct number *
dup_number(const struct number * a)517d0b3229Schristos dup_number(const struct number *a)
527d0b3229Schristos {
537d0b3229Schristos 	struct number *n;
547d0b3229Schristos 
557d0b3229Schristos 	n = bmalloc(sizeof(*n));
567d0b3229Schristos 	n->scale = a->scale;
577d0b3229Schristos 	n->number = BN_dup(a->number);
587d0b3229Schristos 	bn_checkp(n->number);
597d0b3229Schristos 	return n;
607d0b3229Schristos }
617d0b3229Schristos 
627d0b3229Schristos void *
bmalloc(size_t sz)637d0b3229Schristos bmalloc(size_t sz)
647d0b3229Schristos {
657d0b3229Schristos 	void *p;
667d0b3229Schristos 
677d0b3229Schristos 	p = malloc(sz);
687d0b3229Schristos 	if (p == NULL)
697d0b3229Schristos 		err(1, NULL);
707d0b3229Schristos 	return p;
717d0b3229Schristos }
727d0b3229Schristos 
737d0b3229Schristos void *
breallocarray(void * p,size_t nmemb,size_t size)747d0b3229Schristos breallocarray(void *p, size_t nmemb, size_t size)
757d0b3229Schristos {
76*fc8ee2f5Schristos 	int ret = reallocarr(&p, nmemb, size);
77*fc8ee2f5Schristos 	if (ret)
78*fc8ee2f5Schristos 		errc(1, ret, NULL);
79*fc8ee2f5Schristos 	return p;
807d0b3229Schristos }
817d0b3229Schristos 
827d0b3229Schristos char *
bstrdup(const char * p)837d0b3229Schristos bstrdup(const char *p)
847d0b3229Schristos {
857d0b3229Schristos 	char *q;
867d0b3229Schristos 
877d0b3229Schristos 	q = strdup(p);
887d0b3229Schristos 	if (q == NULL)
897d0b3229Schristos 		err(1, NULL);
907d0b3229Schristos 	return q;
917d0b3229Schristos }
927d0b3229Schristos 
937d0b3229Schristos void
bn_check(int x)947d0b3229Schristos bn_check(int x)						\
957d0b3229Schristos {
967d0b3229Schristos 	if (x == 0)
977d0b3229Schristos 		err(1, "big number failure %lx", ERR_get_error());
987d0b3229Schristos }
997d0b3229Schristos 
1007d0b3229Schristos void
bn_checkp(const void * p)1017d0b3229Schristos bn_checkp(const void *p)						\
1027d0b3229Schristos {
1037d0b3229Schristos 	if (p == NULL)
1047d0b3229Schristos 		err(1, "allocation failure %lx", ERR_get_error());
1057d0b3229Schristos }
106