michael@0: /*- michael@0: * Copyright (c) 1987, 1993 michael@0: * The Regents of the University of California. michael@0: * Copyright (c) 2005 Robert N. M. Watson michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 4. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: /* This file has been renamed user_malloc.h for Userspace */ michael@0: #ifndef _USER_MALLOC_H_ michael@0: #define _USER_MALLOC_H_ michael@0: michael@0: /*__Userspace__*/ michael@0: #include michael@0: #include michael@0: #if !defined (__Userspace_os_Windows) michael@0: #include michael@0: #include michael@0: #else michael@0: #include "netinet/sctp_os_userspace.h" michael@0: #endif michael@0: michael@0: #define MINALLOCSIZE UMA_SMALLEST_UNIT michael@0: michael@0: /* michael@0: * flags to malloc. michael@0: */ michael@0: #define M_NOWAIT 0x0001 /* do not block */ michael@0: #define M_WAITOK 0x0002 /* ok to block */ michael@0: #define M_ZERO 0x0100 /* bzero the allocation */ michael@0: #define M_NOVM 0x0200 /* don't ask VM for pages */ michael@0: #define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */ michael@0: michael@0: #define M_MAGIC 877983977 /* time when first defined :-) */ michael@0: michael@0: /* michael@0: * Two malloc type structures are present: malloc_type, which is used by a michael@0: * type owner to declare the type, and malloc_type_internal, which holds michael@0: * malloc-owned statistics and other ABI-sensitive fields, such as the set of michael@0: * malloc statistics indexed by the compile-time MAXCPU constant. michael@0: * Applications should avoid introducing dependence on the allocator private michael@0: * data layout and size. michael@0: * michael@0: * The malloc_type ks_next field is protected by malloc_mtx. Other fields in michael@0: * malloc_type are static after initialization so unsynchronized. michael@0: * michael@0: * Statistics in malloc_type_stats are written only when holding a critical michael@0: * section and running on the CPU associated with the index into the stat michael@0: * array, but read lock-free resulting in possible (minor) races, which the michael@0: * monitoring app should take into account. michael@0: */ michael@0: struct malloc_type_stats { michael@0: uint64_t mts_memalloced; /* Bytes allocated on CPU. */ michael@0: uint64_t mts_memfreed; /* Bytes freed on CPU. */ michael@0: uint64_t mts_numallocs; /* Number of allocates on CPU. */ michael@0: uint64_t mts_numfrees; /* number of frees on CPU. */ michael@0: uint64_t mts_size; /* Bitmask of sizes allocated on CPU. */ michael@0: uint64_t _mts_reserved1; /* Reserved field. */ michael@0: uint64_t _mts_reserved2; /* Reserved field. */ michael@0: uint64_t _mts_reserved3; /* Reserved field. */ michael@0: }; michael@0: michael@0: #ifndef MAXCPU /* necessary on Linux */ michael@0: #define MAXCPU 4 /* arbitrary? */ michael@0: #endif michael@0: michael@0: struct malloc_type_internal { michael@0: struct malloc_type_stats mti_stats[MAXCPU]; michael@0: }; michael@0: michael@0: /* michael@0: * ABI-compatible version of the old 'struct malloc_type', only all stats are michael@0: * now malloc-managed in malloc-owned memory rather than in caller memory, so michael@0: * as to avoid ABI issues. The ks_next pointer is reused as a pointer to the michael@0: * internal data handle. michael@0: */ michael@0: struct malloc_type { michael@0: struct malloc_type *ks_next; /* Next in global chain. */ michael@0: u_long _ks_memuse; /* No longer used. */ michael@0: u_long _ks_size; /* No longer used. */ michael@0: u_long _ks_inuse; /* No longer used. */ michael@0: uint64_t _ks_calls; /* No longer used. */ michael@0: u_long _ks_maxused; /* No longer used. */ michael@0: u_long ks_magic; /* Detect programmer error. */ michael@0: const char *ks_shortdesc; /* Printable type name. */ michael@0: michael@0: /* michael@0: * struct malloc_type was terminated with a struct mtx, which is no michael@0: * longer required. For ABI reasons, continue to flesh out the full michael@0: * size of the old structure, but reuse the _lo_class field for our michael@0: * internal data handle. michael@0: */ michael@0: void *ks_handle; /* Priv. data, was lo_class. */ michael@0: const char *_lo_name; michael@0: const char *_lo_type; michael@0: u_int _lo_flags; michael@0: void *_lo_list_next; michael@0: struct witness *_lo_witness; michael@0: uintptr_t _mtx_lock; michael@0: u_int _mtx_recurse; michael@0: }; michael@0: michael@0: /* michael@0: * Statistics structure headers for user space. The kern.malloc sysctl michael@0: * exposes a structure stream consisting of a stream header, then a series of michael@0: * malloc type headers and statistics structures (quantity maxcpus). For michael@0: * convenience, the kernel will provide the current value of maxcpus at the michael@0: * head of the stream. michael@0: */ michael@0: #define MALLOC_TYPE_STREAM_VERSION 0x00000001 michael@0: struct malloc_type_stream_header { michael@0: uint32_t mtsh_version; /* Stream format version. */ michael@0: uint32_t mtsh_maxcpus; /* Value of MAXCPU for stream. */ michael@0: uint32_t mtsh_count; /* Number of records. */ michael@0: uint32_t _mtsh_pad; /* Pad/reserved field. */ michael@0: }; michael@0: michael@0: #define MALLOC_MAX_NAME 32 michael@0: struct malloc_type_header { michael@0: char mth_name[MALLOC_MAX_NAME]; michael@0: }; michael@0: michael@0: /* __Userspace__ michael@0: Notice that at places it uses ifdef _KERNEL. That line cannot be michael@0: removed because it causes conflicts with malloc definition in michael@0: /usr/include/malloc.h, which essentially says that malloc.h has michael@0: been overridden by stdlib.h. We will need to use names like michael@0: user_malloc.h for isolating kernel interface headers. using michael@0: original names like malloc.h in a user_include header can be michael@0: confusing, All userspace header files are being placed in ./user_include michael@0: Better still to remove from user_include.h all irrelevant code such michael@0: as that in the block starting with #ifdef _KERNEL. I am only leaving michael@0: it in for the time being to see what functionality is in this file michael@0: that kernel uses. michael@0: michael@0: Start copy: Copied code for __Userspace__ */ michael@0: #define MALLOC_DEFINE(type, shortdesc, longdesc) \ michael@0: struct malloc_type type[1] = { \ michael@0: { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL, \ michael@0: NULL, 0, NULL, NULL, 0, 0 } \ michael@0: } michael@0: michael@0: /* Removed "extern" in __Userspace__ code */ michael@0: /* If we need to use MALLOC_DECLARE before using MALLOC then michael@0: we have to remove extern. michael@0: In /usr/include/sys/malloc.h there is this definition: michael@0: #define MALLOC_DECLARE(type) \ michael@0: extern struct malloc_type type[1] michael@0: and loader is unable to find the extern malloc_type because michael@0: it may be defined in one of kernel object files. michael@0: It seems that MALLOC_DECLARE and MALLOC_DEFINE cannot be used at michael@0: the same time for same "type" variable. Also, in Randall's architecture michael@0: document, where it specifies O/S specific macros and functions, it says michael@0: that the name in SCTP_MALLOC does not have to be used. michael@0: */ michael@0: #define MALLOC_DECLARE(type) \ michael@0: extern struct malloc_type type[1] michael@0: michael@0: #define FREE(addr, type) free((addr)) michael@0: michael@0: /* changed definitions of MALLOC and FREE */ michael@0: /* Using memset if flag M_ZERO is specified. Todo: M_WAITOK and M_NOWAIT */ michael@0: #define MALLOC(space, cast, size, type, flags) \ michael@0: ((space) = (cast)malloc((u_long)(size))); \ michael@0: do { \ michael@0: if(flags & M_ZERO) { \ michael@0: memset(space,0,size); \ michael@0: } \ michael@0: } while (0); michael@0: michael@0: michael@0: /* End copy: Copied code for __Userspace__ */ michael@0: michael@0: #if 0 michael@0: #ifdef _KERNEL michael@0: #define MALLOC_DEFINE(type, shortdesc, longdesc) \ michael@0: struct malloc_type type[1] = { \ michael@0: { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL, \ michael@0: NULL, 0, NULL, NULL, 0, 0 } \ michael@0: }; \ michael@0: SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init, \ michael@0: type); \ michael@0: SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, \ michael@0: malloc_uninit, type) michael@0: michael@0: michael@0: #define MALLOC_DECLARE(type) \ michael@0: extern struct malloc_type type[1] michael@0: michael@0: MALLOC_DECLARE(M_CACHE); michael@0: MALLOC_DECLARE(M_DEVBUF); michael@0: MALLOC_DECLARE(M_TEMP); michael@0: michael@0: MALLOC_DECLARE(M_IP6OPT); /* for INET6 */ michael@0: MALLOC_DECLARE(M_IP6NDP); /* for INET6 */ michael@0: michael@0: /* michael@0: * Deprecated macro versions of not-quite-malloc() and free(). michael@0: */ michael@0: #define MALLOC(space, cast, size, type, flags) \ michael@0: ((space) = (cast)malloc((u_long)(size), (type), (flags))) michael@0: #define FREE(addr, type) free((addr), (type)) michael@0: michael@0: /* michael@0: * XXX this should be declared in , but that tends to fail michael@0: * because is included in a header before the source file michael@0: * has a chance to include to get MALLOC_DECLARE() defined. michael@0: */ michael@0: MALLOC_DECLARE(M_IOV); michael@0: michael@0: extern struct mtx malloc_mtx; michael@0: michael@0: /* XXX struct malloc_type is unused for contig*(). */ michael@0: void contigfree(void *addr, unsigned long size, struct malloc_type *type); michael@0: void *contigmalloc(unsigned long size, struct malloc_type *type, int flags, michael@0: vm_paddr_t low, vm_paddr_t high, unsigned long alignment, michael@0: unsigned long boundary); michael@0: void free(void *addr, struct malloc_type *type); michael@0: void *malloc(unsigned long size, struct malloc_type *type, int flags); michael@0: void malloc_init(void *); michael@0: int malloc_last_fail(void); michael@0: void malloc_type_allocated(struct malloc_type *type, unsigned long size); michael@0: void malloc_type_freed(struct malloc_type *type, unsigned long size); michael@0: void malloc_uninit(void *); michael@0: void *realloc(void *addr, unsigned long size, struct malloc_type *type, michael@0: int flags); michael@0: void *reallocf(void *addr, unsigned long size, struct malloc_type *type, michael@0: int flags); michael@0: michael@0: michael@0: #endif /* _KERNEL */ michael@0: #endif michael@0: michael@0: #endif /* !_SYS_MALLOC_H_ */