Home | History | Annotate | Download | only in usrsctplib
      1 /*-
      2  * Copyright (c) 1987, 1993
      3  *	The Regents of the University of California.
      4  * Copyright (c) 2005 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  */
     32 
     33 /* This file has been renamed user_malloc.h for Userspace */
     34 #ifndef _USER_MALLOC_H_
     35 #define	_USER_MALLOC_H_
     36 
     37 /*__Userspace__*/
     38 #include <stdlib.h>
     39 #include <sys/types.h>
     40 #if !defined (__Userspace_os_Windows)
     41 #include <strings.h>
     42 #include <stdint.h>
     43 #else
     44 #include "netinet/sctp_os_userspace.h"
     45 #endif
     46 
     47 #define	MINALLOCSIZE	UMA_SMALLEST_UNIT
     48 
     49 /*
     50  * flags to malloc.
     51  */
     52 #define	M_NOWAIT	0x0001		/* do not block */
     53 #define	M_WAITOK	0x0002		/* ok to block */
     54 #define	M_ZERO		0x0100		/* bzero the allocation */
     55 #define	M_NOVM		0x0200		/* don't ask VM for pages */
     56 #define	M_USE_RESERVE	0x0400		/* can alloc out of reserve memory */
     57 
     58 #define	M_MAGIC		877983977	/* time when first defined :-) */
     59 
     60 /*
     61  * Two malloc type structures are present: malloc_type, which is used by a
     62  * type owner to declare the type, and malloc_type_internal, which holds
     63  * malloc-owned statistics and other ABI-sensitive fields, such as the set of
     64  * malloc statistics indexed by the compile-time MAXCPU constant.
     65  * Applications should avoid introducing dependence on the allocator private
     66  * data layout and size.
     67  *
     68  * The malloc_type ks_next field is protected by malloc_mtx.  Other fields in
     69  * malloc_type are static after initialization so unsynchronized.
     70  *
     71  * Statistics in malloc_type_stats are written only when holding a critical
     72  * section and running on the CPU associated with the index into the stat
     73  * array, but read lock-free resulting in possible (minor) races, which the
     74  * monitoring app should take into account.
     75  */
     76 struct malloc_type_stats {
     77 	uint64_t	mts_memalloced;	/* Bytes allocated on CPU. */
     78 	uint64_t	mts_memfreed;	/* Bytes freed on CPU. */
     79 	uint64_t	mts_numallocs;	/* Number of allocates on CPU. */
     80 	uint64_t	mts_numfrees;	/* number of frees on CPU. */
     81 	uint64_t	mts_size;	/* Bitmask of sizes allocated on CPU. */
     82 	uint64_t	_mts_reserved1;	/* Reserved field. */
     83 	uint64_t	_mts_reserved2;	/* Reserved field. */
     84 	uint64_t	_mts_reserved3;	/* Reserved field. */
     85 };
     86 
     87 #ifndef MAXCPU /* necessary on Linux */
     88 #define MAXCPU 4 /* arbitrary? */
     89 #endif
     90 
     91 struct malloc_type_internal {
     92 	struct malloc_type_stats	mti_stats[MAXCPU];
     93 };
     94 
     95 /*
     96  * ABI-compatible version of the old 'struct malloc_type', only all stats are
     97  * now malloc-managed in malloc-owned memory rather than in caller memory, so
     98  * as to avoid ABI issues.  The ks_next pointer is reused as a pointer to the
     99  * internal data handle.
    100  */
    101 struct malloc_type {
    102 	struct malloc_type *ks_next;	/* Next in global chain. */
    103 	u_long		 _ks_memuse;	/* No longer used. */
    104 	u_long		 _ks_size;	/* No longer used. */
    105 	u_long		 _ks_inuse;	/* No longer used. */
    106 	uint64_t	 _ks_calls;	/* No longer used. */
    107 	u_long		 _ks_maxused;	/* No longer used. */
    108 	u_long		 ks_magic;	/* Detect programmer error. */
    109 	const char	*ks_shortdesc;	/* Printable type name. */
    110 
    111 	/*
    112 	 * struct malloc_type was terminated with a struct mtx, which is no
    113 	 * longer required.  For ABI reasons, continue to flesh out the full
    114 	 * size of the old structure, but reuse the _lo_class field for our
    115 	 * internal data handle.
    116 	 */
    117 	void		*ks_handle;	/* Priv. data, was lo_class. */
    118 	const char	*_lo_name;
    119 	const char	*_lo_type;
    120 	u_int		 _lo_flags;
    121 	void		*_lo_list_next;
    122 	struct witness	*_lo_witness;
    123 	uintptr_t	 _mtx_lock;
    124 	u_int		 _mtx_recurse;
    125 };
    126 
    127 /*
    128  * Statistics structure headers for user space.  The kern.malloc sysctl
    129  * exposes a structure stream consisting of a stream header, then a series of
    130  * malloc type headers and statistics structures (quantity maxcpus).  For
    131  * convenience, the kernel will provide the current value of maxcpus at the
    132  * head of the stream.
    133  */
    134 #define	MALLOC_TYPE_STREAM_VERSION	0x00000001
    135 struct malloc_type_stream_header {
    136 	uint32_t	mtsh_version;	/* Stream format version. */
    137 	uint32_t	mtsh_maxcpus;	/* Value of MAXCPU for stream. */
    138 	uint32_t	mtsh_count;	/* Number of records. */
    139 	uint32_t	_mtsh_pad;	/* Pad/reserved field. */
    140 };
    141 
    142 #define	MALLOC_MAX_NAME	32
    143 struct malloc_type_header {
    144 	char				mth_name[MALLOC_MAX_NAME];
    145 };
    146 
    147 /* __Userspace__
    148 Notice that at places it uses ifdef _KERNEL. That line cannot be
    149 removed because it causes conflicts with malloc definition in
    150 /usr/include/malloc.h, which essentially says that malloc.h has
    151 been overridden by stdlib.h. We will need to use names like
    152 user_malloc.h for isolating kernel interface headers. using
    153 original names like malloc.h in a user_include header can be
    154 confusing, All userspace header files are being placed in ./user_include
    155 Better still to remove from user_include.h all irrelevant code such
    156 as that in the block starting with #ifdef _KERNEL. I am only leaving
    157 it in for the time being to see what functionality is in this file
    158 that kernel uses.
    159 
    160 Start copy: Copied code for __Userspace__ */
    161 #define	MALLOC_DEFINE(type, shortdesc, longdesc)			\
    162 	struct malloc_type type[1] = {					\
    163 		{ NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL,	\
    164 		    NULL, 0, NULL, NULL, 0, 0 }				\
    165 	}
    166 
    167 /* Removed "extern" in __Userspace__ code */
    168 /* If we need to use MALLOC_DECLARE before using MALLOC then
    169    we have to remove extern.
    170      In /usr/include/sys/malloc.h there is this definition:
    171      #define    MALLOC_DECLARE(type) \
    172         extern struct malloc_type type[1]
    173      and loader is unable to find the extern malloc_type because
    174      it may be defined in one of kernel object files.
    175      It seems that MALLOC_DECLARE and MALLOC_DEFINE cannot be used at
    176      the same time for same "type" variable. Also, in Randall's architecture
    177      document, where it specifies O/S specific macros and functions, it says
    178      that the name in SCTP_MALLOC does not have to be used.
    179 */
    180 #define	MALLOC_DECLARE(type) \
    181 	extern struct malloc_type type[1]
    182 
    183 #define	FREE(addr, type) free((addr))
    184 
    185 /* changed definitions of MALLOC and FREE */
    186 /* Using memset if flag M_ZERO is specified. Todo: M_WAITOK and M_NOWAIT */
    187 #define	MALLOC(space, cast, size, type, flags)                          \
    188     ((space) = (cast)malloc((u_long)(size)));                           \
    189     do {								\
    190         if(flags & M_ZERO) {                                            \
    191 	  memset(space,0,size);                                         \
    192 	}								\
    193     } while (0);
    194 
    195 
    196 /* End copy: Copied code for __Userspace__ */
    197 
    198 #if 0
    199 #ifdef _KERNEL
    200 #define	MALLOC_DEFINE(type, shortdesc, longdesc)			\
    201 	struct malloc_type type[1] = {					\
    202 		{ NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL,	\
    203 		    NULL, 0, NULL, NULL, 0, 0 }				\
    204 	};								\
    205 	SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init,	\
    206 	    type);							\
    207 	SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY,		\
    208 	    malloc_uninit, type)
    209 
    210 
    211 #define	MALLOC_DECLARE(type) \
    212 	extern struct malloc_type type[1]
    213 
    214 MALLOC_DECLARE(M_CACHE);
    215 MALLOC_DECLARE(M_DEVBUF);
    216 MALLOC_DECLARE(M_TEMP);
    217 
    218 MALLOC_DECLARE(M_IP6OPT); /* for INET6 */
    219 MALLOC_DECLARE(M_IP6NDP); /* for INET6 */
    220 
    221 /*
    222  * Deprecated macro versions of not-quite-malloc() and free().
    223  */
    224 #define	MALLOC(space, cast, size, type, flags) \
    225 	((space) = (cast)malloc((u_long)(size), (type), (flags)))
    226 #define	FREE(addr, type) free((addr), (type))
    227 
    228 /*
    229  * XXX this should be declared in <sys/uio.h>, but that tends to fail
    230  * because <sys/uio.h> is included in a header before the source file
    231  * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
    232  */
    233 MALLOC_DECLARE(M_IOV);
    234 
    235 extern struct mtx malloc_mtx;
    236 
    237 /* XXX struct malloc_type is unused for contig*(). */
    238 void	contigfree(void *addr, unsigned long size, struct malloc_type *type);
    239 void	*contigmalloc(unsigned long size, struct malloc_type *type, int flags,
    240 	    vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
    241 	    unsigned long boundary);
    242 void	free(void *addr, struct malloc_type *type);
    243 void	*malloc(unsigned long size, struct malloc_type *type, int flags);
    244 void	malloc_init(void *);
    245 int	malloc_last_fail(void);
    246 void	malloc_type_allocated(struct malloc_type *type, unsigned long size);
    247 void	malloc_type_freed(struct malloc_type *type, unsigned long size);
    248 void	malloc_uninit(void *);
    249 void	*realloc(void *addr, unsigned long size, struct malloc_type *type,
    250 	    int flags);
    251 void	*reallocf(void *addr, unsigned long size, struct malloc_type *type,
    252 	    int flags);
    253 
    254 
    255 #endif /* _KERNEL */
    256 #endif
    257 
    258 #endif /* !_SYS_MALLOC_H_ */
    259