source: rtems-libbsd/freebsd/sys/sys/malloc.h @ 12b57ad

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 12b57ad was 12b57ad, checked in by Sebastian Huber <sebastian.huber@…>, on 10/24/13 at 08:44:22

Move malloc defines to <sys/malloc.h>

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*-
2 * Copyright (c) 1987, 1993
3 *      The Regents of the University of California.
4 * Copyright (c) 2005, 2009 Robert N. M. Watson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *      @(#)malloc.h    8.5 (Berkeley) 5/3/95
32 * $FreeBSD$
33 */
34
35#ifndef _SYS_MALLOC_H_
36#define _SYS_MALLOC_H_
37
38#include <rtems/bsd/sys/param.h>
39#include <sys/queue.h>
40#include <sys/_lock.h>
41#include <sys/_mutex.h>
42
43#define MINALLOCSIZE    UMA_SMALLEST_UNIT
44
45/*
46 * flags to malloc.
47 */
48#define M_NOWAIT        0x0001          /* do not block */
49#define M_WAITOK        0x0002          /* ok to block */
50#define M_ZERO          0x0100          /* bzero the allocation */
51#define M_NOVM          0x0200          /* don't ask VM for pages */
52#define M_USE_RESERVE   0x0400          /* can alloc out of reserve memory */
53
54#define M_MAGIC         877983977       /* time when first defined :-) */
55
56/*
57 * Two malloc type structures are present: malloc_type, which is used by a
58 * type owner to declare the type, and malloc_type_internal, which holds
59 * malloc-owned statistics and other ABI-sensitive fields, such as the set of
60 * malloc statistics indexed by the compile-time MAXCPU constant.
61 * Applications should avoid introducing dependence on the allocator private
62 * data layout and size.
63 *
64 * The malloc_type ks_next field is protected by malloc_mtx.  Other fields in
65 * malloc_type are static after initialization so unsynchronized.
66 *
67 * Statistics in malloc_type_stats are written only when holding a critical
68 * section and running on the CPU associated with the index into the stat
69 * array, but read lock-free resulting in possible (minor) races, which the
70 * monitoring app should take into account.
71 */
72struct malloc_type_stats {
73        uint64_t        mts_memalloced; /* Bytes allocated on CPU. */
74        uint64_t        mts_memfreed;   /* Bytes freed on CPU. */
75        uint64_t        mts_numallocs;  /* Number of allocates on CPU. */
76        uint64_t        mts_numfrees;   /* number of frees on CPU. */
77        uint64_t        mts_size;       /* Bitmask of sizes allocated on CPU. */
78        uint64_t        _mts_reserved1; /* Reserved field. */
79        uint64_t        _mts_reserved2; /* Reserved field. */
80        uint64_t        _mts_reserved3; /* Reserved field. */
81};
82
83/*
84 * Index definitions for the mti_probes[] array.
85 */
86#define DTMALLOC_PROBE_MALLOC           0
87#define DTMALLOC_PROBE_FREE             1
88#define DTMALLOC_PROBE_MAX              2
89
90struct malloc_type_internal {
91        uint32_t        mti_probes[DTMALLOC_PROBE_MAX];
92                                        /* DTrace probe ID array. */
93        struct malloc_type_stats        mti_stats[MAXCPU];
94};
95
96/*
97 * Public data structure describing a malloc type.  Private data is hung off
98 * of ks_handle to avoid encoding internal malloc(9) data structures in
99 * modules, which will statically allocate struct malloc_type.
100 */
101struct malloc_type {
102        struct malloc_type *ks_next;    /* Next in global chain. */
103        u_long           ks_magic;      /* Detect programmer error. */
104        const char      *ks_shortdesc;  /* Printable type name. */
105        void            *ks_handle;     /* Priv. data, was lo_class. */
106};
107
108/*
109 * Statistics structure headers for user space.  The kern.malloc sysctl
110 * exposes a structure stream consisting of a stream header, then a series of
111 * malloc type headers and statistics structures (quantity maxcpus).  For
112 * convenience, the kernel will provide the current value of maxcpus at the
113 * head of the stream.
114 */
115#define MALLOC_TYPE_STREAM_VERSION      0x00000001
116struct malloc_type_stream_header {
117        uint32_t        mtsh_version;   /* Stream format version. */
118        uint32_t        mtsh_maxcpus;   /* Value of MAXCPU for stream. */
119        uint32_t        mtsh_count;     /* Number of records. */
120        uint32_t        _mtsh_pad;      /* Pad/reserved field. */
121};
122
123#define MALLOC_MAX_NAME 32
124struct malloc_type_header {
125        char                            mth_name[MALLOC_MAX_NAME];
126};
127
128#ifdef _KERNEL
129#ifdef __rtems__
130#include <stdlib.h>
131#define malloc _bsd_malloc
132#define realloc _bsd_realloc
133#define reallocf _bsd_reallocf
134#define free _bsd_free
135#endif /* __rtems__ */
136#define MALLOC_DEFINE(type, shortdesc, longdesc)                        \
137        struct malloc_type type[1] = {                                  \
138                { NULL, M_MAGIC, shortdesc, NULL }                      \
139        };                                                              \
140        SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init, \
141            type);                                                      \
142        SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY,             \
143            malloc_uninit, type)
144
145#define MALLOC_DECLARE(type) \
146        extern struct malloc_type type[1]
147
148MALLOC_DECLARE(M_CACHE);
149MALLOC_DECLARE(M_DEVBUF);
150MALLOC_DECLARE(M_TEMP);
151
152MALLOC_DECLARE(M_IP6OPT); /* for INET6 */
153MALLOC_DECLARE(M_IP6NDP); /* for INET6 */
154
155/*
156 * Deprecated macro versions of not-quite-malloc() and free().
157 */
158#define MALLOC(space, cast, size, type, flags) \
159        ((space) = (cast)malloc((u_long)(size), (type), (flags)))
160#define FREE(addr, type) free((addr), (type))
161
162/*
163 * XXX this should be declared in <sys/uio.h>, but that tends to fail
164 * because <sys/uio.h> is included in a header before the source file
165 * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
166 */
167MALLOC_DECLARE(M_IOV);
168
169extern struct mtx malloc_mtx;
170
171/*
172 * Function type used when iterating over the list of malloc types.
173 */
174typedef void malloc_type_list_func_t(struct malloc_type *, void *);
175
176void    contigfree(void *addr, unsigned long size, struct malloc_type *type);
177void    *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
178            vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
179            unsigned long boundary) __malloc_like;
180void    free(void *addr, struct malloc_type *type);
181void    *malloc(unsigned long size, struct malloc_type *type, int flags) __malloc_like;
182void    malloc_init(void *);
183int     malloc_last_fail(void);
184void    malloc_type_allocated(struct malloc_type *type, unsigned long size);
185void    malloc_type_freed(struct malloc_type *type, unsigned long size);
186void    malloc_type_list(malloc_type_list_func_t *, void *);
187void    malloc_uninit(void *);
188void    *realloc(void *addr, unsigned long size, struct malloc_type *type,
189            int flags);
190void    *reallocf(void *addr, unsigned long size, struct malloc_type *type,
191            int flags);
192
193struct malloc_type *malloc_desc2type(const char *desc);
194#endif /* _KERNEL */
195
196#endif /* !_SYS_MALLOC_H_ */
Note: See TracBrowser for help on using the repository browser.