1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /*- |
---|
10 | * SPDX-License-Identifier: BSD-3-Clause |
---|
11 | * |
---|
12 | * Copyright (c) 1987, 1991, 1993 |
---|
13 | * The Regents of the University of California. |
---|
14 | * Copyright (c) 2005-2009 Robert N. M. Watson |
---|
15 | * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> (mallocarray) |
---|
16 | * Copyright (c) 2009, 2018 embedded brains GmbH |
---|
17 | * All rights reserved. |
---|
18 | * |
---|
19 | * Redistribution and use in source and binary forms, with or without |
---|
20 | * modification, are permitted provided that the following conditions |
---|
21 | * are met: |
---|
22 | * 1. Redistributions of source code must retain the above copyright |
---|
23 | * notice, this list of conditions and the following disclaimer. |
---|
24 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
25 | * notice, this list of conditions and the following disclaimer in the |
---|
26 | * documentation and/or other materials provided with the distribution. |
---|
27 | * 3. Neither the name of the University nor the names of its contributors |
---|
28 | * may be used to endorse or promote products derived from this software |
---|
29 | * without specific prior written permission. |
---|
30 | * |
---|
31 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
32 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
33 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
34 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
35 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
37 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
39 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
40 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
41 | * SUCH DAMAGE. |
---|
42 | * |
---|
43 | * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 |
---|
44 | */ |
---|
45 | |
---|
46 | #include <machine/rtems-bsd-kernel-space.h> |
---|
47 | #include <machine/rtems-bsd-support.h> |
---|
48 | |
---|
49 | #include <sys/param.h> |
---|
50 | #include <sys/types.h> |
---|
51 | #include <sys/systm.h> |
---|
52 | #include <sys/malloc.h> |
---|
53 | #include <sys/kernel.h> |
---|
54 | |
---|
55 | MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); |
---|
56 | |
---|
57 | MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); |
---|
58 | |
---|
59 | MALLOC_DEFINE(M_IOV, "iov", "large iov's"); |
---|
60 | |
---|
61 | void |
---|
62 | malloc_init(void *data) |
---|
63 | { |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | void |
---|
68 | malloc_uninit(void *data) |
---|
69 | { |
---|
70 | struct malloc_type *mtp = data; |
---|
71 | |
---|
72 | BSD_PRINTF( "desc = %s\n", mtp->ks_shortdesc); |
---|
73 | } |
---|
74 | |
---|
75 | #undef malloc |
---|
76 | |
---|
77 | void * |
---|
78 | _bsd_malloc(size_t size, struct malloc_type *mtp, int flags) |
---|
79 | { |
---|
80 | void *p = malloc(size > 0 ? size : 1); |
---|
81 | |
---|
82 | if ((flags & M_ZERO) != 0 && p != NULL) { |
---|
83 | memset(p, 0, size); |
---|
84 | } |
---|
85 | |
---|
86 | return (p); |
---|
87 | } |
---|
88 | |
---|
89 | void * |
---|
90 | mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) |
---|
91 | { |
---|
92 | |
---|
93 | if (WOULD_OVERFLOW(nmemb, size)) |
---|
94 | panic("mallocarray: %zu * %zu overflowed", nmemb, size); |
---|
95 | |
---|
96 | return (_bsd_malloc(size * nmemb, type, flags)); |
---|
97 | } |
---|
98 | |
---|
99 | #undef realloc |
---|
100 | |
---|
101 | void * |
---|
102 | _bsd_realloc( void *addr, size_t size, struct malloc_type *type, int flags) |
---|
103 | { |
---|
104 | void *p = realloc(addr, size > 0 ? size : 1); |
---|
105 | |
---|
106 | if ((flags & M_ZERO) != 0 && p != NULL) { |
---|
107 | memset(p, 0, size); |
---|
108 | } |
---|
109 | |
---|
110 | return p; |
---|
111 | } |
---|
112 | |
---|
113 | #undef reallocf |
---|
114 | |
---|
115 | void * |
---|
116 | _bsd_reallocf( void *addr, size_t size, struct malloc_type *type, int flags) |
---|
117 | { |
---|
118 | void *p = realloc(addr, size > 0 ? size : 1); |
---|
119 | |
---|
120 | if (p == NULL) { |
---|
121 | free(addr, NULL); |
---|
122 | } |
---|
123 | |
---|
124 | return (p); |
---|
125 | } |
---|
126 | |
---|
127 | #undef free |
---|
128 | |
---|
129 | void |
---|
130 | _bsd_free(void *addr, struct malloc_type *mtp) |
---|
131 | { |
---|
132 | |
---|
133 | free(addr); |
---|
134 | } |
---|
135 | |
---|
136 | #undef strdup |
---|
137 | |
---|
138 | char * |
---|
139 | _bsd_strdup(const char *__restrict s, struct malloc_type *type) |
---|
140 | { |
---|
141 | |
---|
142 | return (strdup(s)); |
---|
143 | } |
---|