1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/sctp/src/user_malloc.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,258 @@ 1.4 +/*- 1.5 + * Copyright (c) 1987, 1993 1.6 + * The Regents of the University of California. 1.7 + * Copyright (c) 2005 Robert N. M. Watson 1.8 + * All rights reserved. 1.9 + * 1.10 + * Redistribution and use in source and binary forms, with or without 1.11 + * modification, are permitted provided that the following conditions 1.12 + * are met: 1.13 + * 1. Redistributions of source code must retain the above copyright 1.14 + * notice, this list of conditions and the following disclaimer. 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 4. Neither the name of the University nor the names of its contributors 1.19 + * may be used to endorse or promote products derived from this software 1.20 + * without specific prior written permission. 1.21 + * 1.22 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1.23 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.24 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.25 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 1.26 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.27 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1.28 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.29 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 1.30 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 1.31 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 1.32 + * SUCH DAMAGE. 1.33 + * 1.34 + */ 1.35 + 1.36 +/* This file has been renamed user_malloc.h for Userspace */ 1.37 +#ifndef _USER_MALLOC_H_ 1.38 +#define _USER_MALLOC_H_ 1.39 + 1.40 +/*__Userspace__*/ 1.41 +#include <stdlib.h> 1.42 +#include <sys/types.h> 1.43 +#if !defined (__Userspace_os_Windows) 1.44 +#include <strings.h> 1.45 +#include <stdint.h> 1.46 +#else 1.47 +#include "netinet/sctp_os_userspace.h" 1.48 +#endif 1.49 + 1.50 +#define MINALLOCSIZE UMA_SMALLEST_UNIT 1.51 + 1.52 +/* 1.53 + * flags to malloc. 1.54 + */ 1.55 +#define M_NOWAIT 0x0001 /* do not block */ 1.56 +#define M_WAITOK 0x0002 /* ok to block */ 1.57 +#define M_ZERO 0x0100 /* bzero the allocation */ 1.58 +#define M_NOVM 0x0200 /* don't ask VM for pages */ 1.59 +#define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */ 1.60 + 1.61 +#define M_MAGIC 877983977 /* time when first defined :-) */ 1.62 + 1.63 +/* 1.64 + * Two malloc type structures are present: malloc_type, which is used by a 1.65 + * type owner to declare the type, and malloc_type_internal, which holds 1.66 + * malloc-owned statistics and other ABI-sensitive fields, such as the set of 1.67 + * malloc statistics indexed by the compile-time MAXCPU constant. 1.68 + * Applications should avoid introducing dependence on the allocator private 1.69 + * data layout and size. 1.70 + * 1.71 + * The malloc_type ks_next field is protected by malloc_mtx. Other fields in 1.72 + * malloc_type are static after initialization so unsynchronized. 1.73 + * 1.74 + * Statistics in malloc_type_stats are written only when holding a critical 1.75 + * section and running on the CPU associated with the index into the stat 1.76 + * array, but read lock-free resulting in possible (minor) races, which the 1.77 + * monitoring app should take into account. 1.78 + */ 1.79 +struct malloc_type_stats { 1.80 + uint64_t mts_memalloced; /* Bytes allocated on CPU. */ 1.81 + uint64_t mts_memfreed; /* Bytes freed on CPU. */ 1.82 + uint64_t mts_numallocs; /* Number of allocates on CPU. */ 1.83 + uint64_t mts_numfrees; /* number of frees on CPU. */ 1.84 + uint64_t mts_size; /* Bitmask of sizes allocated on CPU. */ 1.85 + uint64_t _mts_reserved1; /* Reserved field. */ 1.86 + uint64_t _mts_reserved2; /* Reserved field. */ 1.87 + uint64_t _mts_reserved3; /* Reserved field. */ 1.88 +}; 1.89 + 1.90 +#ifndef MAXCPU /* necessary on Linux */ 1.91 +#define MAXCPU 4 /* arbitrary? */ 1.92 +#endif 1.93 + 1.94 +struct malloc_type_internal { 1.95 + struct malloc_type_stats mti_stats[MAXCPU]; 1.96 +}; 1.97 + 1.98 +/* 1.99 + * ABI-compatible version of the old 'struct malloc_type', only all stats are 1.100 + * now malloc-managed in malloc-owned memory rather than in caller memory, so 1.101 + * as to avoid ABI issues. The ks_next pointer is reused as a pointer to the 1.102 + * internal data handle. 1.103 + */ 1.104 +struct malloc_type { 1.105 + struct malloc_type *ks_next; /* Next in global chain. */ 1.106 + u_long _ks_memuse; /* No longer used. */ 1.107 + u_long _ks_size; /* No longer used. */ 1.108 + u_long _ks_inuse; /* No longer used. */ 1.109 + uint64_t _ks_calls; /* No longer used. */ 1.110 + u_long _ks_maxused; /* No longer used. */ 1.111 + u_long ks_magic; /* Detect programmer error. */ 1.112 + const char *ks_shortdesc; /* Printable type name. */ 1.113 + 1.114 + /* 1.115 + * struct malloc_type was terminated with a struct mtx, which is no 1.116 + * longer required. For ABI reasons, continue to flesh out the full 1.117 + * size of the old structure, but reuse the _lo_class field for our 1.118 + * internal data handle. 1.119 + */ 1.120 + void *ks_handle; /* Priv. data, was lo_class. */ 1.121 + const char *_lo_name; 1.122 + const char *_lo_type; 1.123 + u_int _lo_flags; 1.124 + void *_lo_list_next; 1.125 + struct witness *_lo_witness; 1.126 + uintptr_t _mtx_lock; 1.127 + u_int _mtx_recurse; 1.128 +}; 1.129 + 1.130 +/* 1.131 + * Statistics structure headers for user space. The kern.malloc sysctl 1.132 + * exposes a structure stream consisting of a stream header, then a series of 1.133 + * malloc type headers and statistics structures (quantity maxcpus). For 1.134 + * convenience, the kernel will provide the current value of maxcpus at the 1.135 + * head of the stream. 1.136 + */ 1.137 +#define MALLOC_TYPE_STREAM_VERSION 0x00000001 1.138 +struct malloc_type_stream_header { 1.139 + uint32_t mtsh_version; /* Stream format version. */ 1.140 + uint32_t mtsh_maxcpus; /* Value of MAXCPU for stream. */ 1.141 + uint32_t mtsh_count; /* Number of records. */ 1.142 + uint32_t _mtsh_pad; /* Pad/reserved field. */ 1.143 +}; 1.144 + 1.145 +#define MALLOC_MAX_NAME 32 1.146 +struct malloc_type_header { 1.147 + char mth_name[MALLOC_MAX_NAME]; 1.148 +}; 1.149 + 1.150 +/* __Userspace__ 1.151 +Notice that at places it uses ifdef _KERNEL. That line cannot be 1.152 +removed because it causes conflicts with malloc definition in 1.153 +/usr/include/malloc.h, which essentially says that malloc.h has 1.154 +been overridden by stdlib.h. We will need to use names like 1.155 +user_malloc.h for isolating kernel interface headers. using 1.156 +original names like malloc.h in a user_include header can be 1.157 +confusing, All userspace header files are being placed in ./user_include 1.158 +Better still to remove from user_include.h all irrelevant code such 1.159 +as that in the block starting with #ifdef _KERNEL. I am only leaving 1.160 +it in for the time being to see what functionality is in this file 1.161 +that kernel uses. 1.162 + 1.163 +Start copy: Copied code for __Userspace__ */ 1.164 +#define MALLOC_DEFINE(type, shortdesc, longdesc) \ 1.165 + struct malloc_type type[1] = { \ 1.166 + { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL, \ 1.167 + NULL, 0, NULL, NULL, 0, 0 } \ 1.168 + } 1.169 + 1.170 +/* Removed "extern" in __Userspace__ code */ 1.171 +/* If we need to use MALLOC_DECLARE before using MALLOC then 1.172 + we have to remove extern. 1.173 + In /usr/include/sys/malloc.h there is this definition: 1.174 + #define MALLOC_DECLARE(type) \ 1.175 + extern struct malloc_type type[1] 1.176 + and loader is unable to find the extern malloc_type because 1.177 + it may be defined in one of kernel object files. 1.178 + It seems that MALLOC_DECLARE and MALLOC_DEFINE cannot be used at 1.179 + the same time for same "type" variable. Also, in Randall's architecture 1.180 + document, where it specifies O/S specific macros and functions, it says 1.181 + that the name in SCTP_MALLOC does not have to be used. 1.182 +*/ 1.183 +#define MALLOC_DECLARE(type) \ 1.184 + extern struct malloc_type type[1] 1.185 + 1.186 +#define FREE(addr, type) free((addr)) 1.187 + 1.188 +/* changed definitions of MALLOC and FREE */ 1.189 +/* Using memset if flag M_ZERO is specified. Todo: M_WAITOK and M_NOWAIT */ 1.190 +#define MALLOC(space, cast, size, type, flags) \ 1.191 + ((space) = (cast)malloc((u_long)(size))); \ 1.192 + do { \ 1.193 + if(flags & M_ZERO) { \ 1.194 + memset(space,0,size); \ 1.195 + } \ 1.196 + } while (0); 1.197 + 1.198 + 1.199 +/* End copy: Copied code for __Userspace__ */ 1.200 + 1.201 +#if 0 1.202 +#ifdef _KERNEL 1.203 +#define MALLOC_DEFINE(type, shortdesc, longdesc) \ 1.204 + struct malloc_type type[1] = { \ 1.205 + { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL, \ 1.206 + NULL, 0, NULL, NULL, 0, 0 } \ 1.207 + }; \ 1.208 + SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init, \ 1.209 + type); \ 1.210 + SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, \ 1.211 + malloc_uninit, type) 1.212 + 1.213 + 1.214 +#define MALLOC_DECLARE(type) \ 1.215 + extern struct malloc_type type[1] 1.216 + 1.217 +MALLOC_DECLARE(M_CACHE); 1.218 +MALLOC_DECLARE(M_DEVBUF); 1.219 +MALLOC_DECLARE(M_TEMP); 1.220 + 1.221 +MALLOC_DECLARE(M_IP6OPT); /* for INET6 */ 1.222 +MALLOC_DECLARE(M_IP6NDP); /* for INET6 */ 1.223 + 1.224 +/* 1.225 + * Deprecated macro versions of not-quite-malloc() and free(). 1.226 + */ 1.227 +#define MALLOC(space, cast, size, type, flags) \ 1.228 + ((space) = (cast)malloc((u_long)(size), (type), (flags))) 1.229 +#define FREE(addr, type) free((addr), (type)) 1.230 + 1.231 +/* 1.232 + * XXX this should be declared in <sys/uio.h>, but that tends to fail 1.233 + * because <sys/uio.h> is included in a header before the source file 1.234 + * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined. 1.235 + */ 1.236 +MALLOC_DECLARE(M_IOV); 1.237 + 1.238 +extern struct mtx malloc_mtx; 1.239 + 1.240 +/* XXX struct malloc_type is unused for contig*(). */ 1.241 +void contigfree(void *addr, unsigned long size, struct malloc_type *type); 1.242 +void *contigmalloc(unsigned long size, struct malloc_type *type, int flags, 1.243 + vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 1.244 + unsigned long boundary); 1.245 +void free(void *addr, struct malloc_type *type); 1.246 +void *malloc(unsigned long size, struct malloc_type *type, int flags); 1.247 +void malloc_init(void *); 1.248 +int malloc_last_fail(void); 1.249 +void malloc_type_allocated(struct malloc_type *type, unsigned long size); 1.250 +void malloc_type_freed(struct malloc_type *type, unsigned long size); 1.251 +void malloc_uninit(void *); 1.252 +void *realloc(void *addr, unsigned long size, struct malloc_type *type, 1.253 + int flags); 1.254 +void *reallocf(void *addr, unsigned long size, struct malloc_type *type, 1.255 + int flags); 1.256 + 1.257 + 1.258 +#endif /* _KERNEL */ 1.259 +#endif 1.260 + 1.261 +#endif /* !_SYS_MALLOC_H_ */