Merge illumos-gate
[unleashed.git] / lib / libdemangle-sys / util.c
blob9c01c62874391a06d1d5a710b82e9729085cb2f6
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2017 Jason King
16 #include <sys/debug.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <demangle-sys.h>
20 #include "demangle_int.h"
22 void *
23 zalloc(sysdem_ops_t *ops, size_t len)
25 void *p = ops->alloc(len);
27 if (p != NULL)
28 (void) memset(p, 0, len);
30 #ifdef DEBUG
32 * In normal operation, we should never exhaust memory. Either
33 * something's wrong, or the system is so hosed that aborting
34 * shouldn't hurt anything, and it gives us a more useful stack
35 * trace.
37 if (p == NULL)
38 abort();
39 #endif
41 return (p);
44 void
45 xfree(sysdem_ops_t *ops, void *p, size_t len)
47 if (p == NULL || len == 0)
48 return;
50 ops->free(p, len);
53 void *
54 xrealloc(sysdem_ops_t *ops, void *p, size_t oldsz, size_t newsz)
56 if (newsz == oldsz)
57 return (p);
59 VERIFY3U(newsz, >, oldsz);
61 void *temp = zalloc(ops, newsz);
63 if (temp == NULL)
64 return (NULL);
66 if (oldsz > 0) {
67 (void) memcpy(temp, p, oldsz);
68 xfree(ops, p, oldsz);
71 return (temp);
74 /*ARGSUSED*/
75 static void
76 def_free(void *p, size_t len)
78 free(p);
81 static sysdem_ops_t i_sysdem_ops_default = {
82 .alloc = malloc,
83 .free = def_free
85 sysdem_ops_t *sysdem_ops_default = &i_sysdem_ops_default;