1 /*
2 * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org>
3 * Copyright (c) 2006-2014 Sippy Software, Inc., http://www.sippysoft.com
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29 #include <stdint.h>
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "rtpp_mallocs.h"
36 #include "rtpp_types.h"
37 #include "rtpp_refcnt.h"
38
39 void *
40 #if !defined(RTPP_CHECK_LEAKS)
rtpp_zmalloc(size_t msize)41 rtpp_zmalloc(size_t msize)
42 #else
43 rtpp_zmalloc_memdeb(size_t msize, void *memdeb_p, const char *fname,
44 int linen, const char *funcn)
45 #endif
46 {
47 void *rval;
48
49 #if !defined(RTPP_CHECK_LEAKS)
50 rval = malloc(msize);
51 #else
52 rval = rtpp_memdeb_malloc(msize, memdeb_p, fname, linen, funcn);
53 #endif
54 if (rval != NULL) {
55 memset(rval, '\0', msize);
56 }
57 return (rval);
58 }
59
60 struct alig_help {
61 char a[1];
62 intmax_t b;
63 };
64
65 void *
66 #if !defined(RTPP_CHECK_LEAKS)
rtpp_rzmalloc(size_t msize,struct rtpp_refcnt ** rcntp)67 rtpp_rzmalloc(size_t msize, struct rtpp_refcnt **rcntp)
68 #else
69 rtpp_rzmalloc_memdeb(const char *fname, int linen, const char *funcn,
70 size_t msize, struct rtpp_refcnt **rcntp)
71 #endif
72 {
73 void *rval;
74 struct rtpp_refcnt *rcnt;
75 size_t pad_size, asize;
76 void *rco;
77
78 if (offsetof(struct alig_help, b) > 1) {
79 pad_size = msize % offsetof(struct alig_help, b);
80 if (pad_size != 0) {
81 pad_size = offsetof(struct alig_help, b) - pad_size;
82 }
83 } else {
84 pad_size = 0;
85 }
86 asize = msize + pad_size + rtpp_refcnt_osize();
87 #if !defined(RTPP_CHECK_LEAKS)
88 rval = malloc(asize);
89 #else
90 rval = rtpp_memdeb_malloc(asize, _rtpproxy_memdeb, fname, linen, funcn);
91 #endif
92 if (rval == NULL) {
93 return (NULL);
94 }
95 memset(rval, '\0', asize);
96 rco = (char *)rval + msize + pad_size;
97 rcnt = rtpp_refcnt_ctor_pa(rco);
98 if (rcnt == NULL) {
99 goto e1;
100 }
101 *rcntp = rcnt;
102
103 return (rval);
104 e1:
105 free(rval);
106 return (NULL);
107 }
108