jemalloc_internal.h.in revision 84f7cdb0c588322dfd50a26497fc1cb54b792018
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/mman.h> 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/param.h> 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/time.h> 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/types.h> 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/sysctl.h> 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/uio.h> 7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <errno.h> 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <limits.h> 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef SIZE_T_MAX 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# define SIZE_T_MAX SIZE_MAX 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <pthread.h> 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <sched.h> 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdarg.h> 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdbool.h> 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h> 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdlib.h> 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdint.h> 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h> 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef offsetof 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# define offsetof(type, member) ((size_t)&(((type *)NULL)->member)) 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <inttypes.h> 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string.h> 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <strings.h> 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <ctype.h> 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <unistd.h> 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <fcntl.h> 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <pthread.h> 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <math.h> 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define JEMALLOC_NO_DEMANGLE 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "../jemalloc@install_suffix@.h" 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "jemalloc/internal/private_namespace.h" 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef JEMALLOC_CC_SILENCE 39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define UNUSED JEMALLOC_ATTR(unused) 40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#else 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define UNUSED 42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const bool config_debug = 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef JEMALLOC_DEBUG 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) true 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) false 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ; 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const bool config_dss = 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef JEMALLOC_DSS 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) true 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) false 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ; 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static const bool config_dynamic_page_shift = 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifdef JEMALLOC_DYNAMIC_PAGE_SHIFT 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) true 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) false 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ; 65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)static const bool config_fill = 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef JEMALLOC_FILL 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) true 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) false 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ; 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const bool config_lazy_lock = 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef JEMALLOC_LAZY_LOCK 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) true 75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else 76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) false 77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ; 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const bool config_prof = 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef JEMALLOC_PROF 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) true 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) false 844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ; 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static const bool config_prof_libgcc = 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifdef JEMALLOC_PROF_LIBGCC 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) true 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) false 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ; 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static const bool config_prof_libunwind = 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifdef JEMALLOC_PROF_LIBUNWIND 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) true 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) false 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ; 1004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static const bool config_stats = 1014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifdef JEMALLOC_STATS 1024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) true 1034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#else 1044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) false 1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif 1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ; 1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static const bool config_tcache = 1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifdef JEMALLOC_TCACHE 1094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) true 1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#else 1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) false 112a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif 1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ; 1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static const bool config_tls = 1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifdef JEMALLOC_TLS 1164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) true 117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#else 118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) false 1194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif 1204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ; 1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static const bool config_xmalloc = 1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifdef JEMALLOC_XMALLOC 1234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) true 1244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#else 1254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) false 1264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif 1274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ; 1284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static const bool config_ivsalloc = 1294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifdef JEMALLOC_IVSALLOC 1304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) true 1314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#else 1324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) false 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ; 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN)) 137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <libkern/OSAtomic.h> 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef JEMALLOC_ZONE 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <mach/mach_error.h> 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <mach/mach_init.h> 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <mach/vm_map.h> 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <malloc/malloc.h> 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define RB_COMPACT 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jemalloc/internal/rb.h" 149a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "jemalloc/internal/qr.h" 150a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "jemalloc/internal/ql.h" 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void (*je_malloc_message)(void *wcbopaque, const char *s); 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/* 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Define a custom assert() in order to reduce the chances of deadlock during 156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * assertion failure. 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef assert 1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# ifdef JEMALLOC_DEBUG 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# define assert(e) do { \ 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!(e)) { \ 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) char line_buf[UMAX2S_BUFSIZE]; \ 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) malloc_write("<jemalloc>: "); \ 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) malloc_write(__FILE__); \ 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) malloc_write(":"); \ 1663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) malloc_write(u2s(__LINE__, 10, line_buf)); \ 1673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) malloc_write(": Failed assertion: "); \ 1683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) malloc_write("\""); \ 1693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) malloc_write(#e); \ 1703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) malloc_write("\"\n"); \ 171e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch abort(); \ 172e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch } \ 173c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch} while (0) 174c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch# else 1751675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch# define assert(e) 1761675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch# endif 177c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#endif 178c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch 179c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch/* Use to assert a particular configuration, e.g., cassert(config_debug). */ 180c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#define cassert(c) do { \ 181a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if ((c) == false) \ 182a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) assert(false); \ 183a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)} while (0) 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * jemalloc can conceptually be broken into components (arena, tcache, etc.), 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * but there are circular dependencies that cannot be broken without 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * substantial performance degradation. In order to reduce the effect on 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * visual code flow, read the header files in multiple passes, with one of the 190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * following cpp variables defined during each pass: 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * JEMALLOC_H_TYPES : Preprocessor-defined constants and psuedo-opaque data 1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * types. 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * JEMALLOC_H_STRUCTS : Data structures. 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes. 1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * JEMALLOC_H_INLINES : Inline functions. 1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */ 1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/******************************************************************************/ 1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define JEMALLOC_H_TYPES 2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ALLOCM_LG_ALIGN_MASK ((int)0x3f) 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ZU(z) ((size_t)z) 204f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 205f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef __DECONST 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) 207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifdef JEMALLOC_DEBUG 2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /* Disable inlining to make debugging easier. */ 2114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)# define JEMALLOC_INLINE 2124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)# define inline 2134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#else 2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# define JEMALLOC_ENABLE_INLINE 2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# define JEMALLOC_INLINE static inline 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Size of stack-allocated buffer passed to buferror(). */ 2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define BUFERROR_BUF 64 2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Smallest size class to support. */ 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LG_TINY_MIN 3 223a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define TINY_MIN (1U << LG_TINY_MIN) 2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 225a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)/* 226a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * Minimum alignment of allocations is 2^LG_QUANTUM bytes (ignoring tiny size 227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * classes). 228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) */ 229f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef LG_QUANTUM 230f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# ifdef __i386__ 231f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# define LG_QUANTUM 4 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# endif 2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# ifdef __ia64__ 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# define LG_QUANTUM 4 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# endif 23690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)# ifdef __alpha__ 23790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)# define LG_QUANTUM 4 23890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)# endif 23990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)# ifdef __sparc64__ 240a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# define LG_QUANTUM 4 241a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# endif 242a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# if (defined(__amd64__) || defined(__x86_64__)) 243a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# define LG_QUANTUM 4 244a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# endif 245a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# ifdef __arm__ 246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# define LG_QUANTUM 3 247a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# endif 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# ifdef __mips__ 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# define LG_QUANTUM 3 250a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# endif 2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# ifdef __powerpc__ 2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# define LG_QUANTUM 4 2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# endif 2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# ifdef __s390x__ 2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# define LG_QUANTUM 4 2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# endif 2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# ifdef __tile__ 2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# define LG_QUANTUM 4 2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# endif 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# ifndef LG_QUANTUM 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# error "No LG_QUANTUM definition for architecture; specify via CPPFLAGS" 262a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)# endif 2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define QUANTUM ((size_t)(1U << LG_QUANTUM)) 2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define QUANTUM_MASK (QUANTUM - 1) 2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Return the smallest quantum multiple that is >= a. */ 2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define QUANTUM_CEILING(a) \ 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (((a) + QUANTUM_MASK) & ~QUANTUM_MASK) 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 272a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define LONG ((size_t)(1U << LG_SIZEOF_LONG)) 2734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define LONG_MASK (LONG - 1) 2744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/* Return the smallest long multiple that is >= a. */ 2764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define LONG_CEILING(a) \ 2774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) (((a) + LONG_MASK) & ~LONG_MASK) 2784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define SIZEOF_PTR (1U << LG_SIZEOF_PTR) 280a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define PTR_MASK (SIZEOF_PTR - 1) 2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Return the smallest (void *) multiple that is >= a. */ 2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define PTR_CEILING(a) \ 2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) (((a) + PTR_MASK) & ~PTR_MASK) 2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* 2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Maximum size of L1 cache line. This is used to avoid cache line aliasing. 2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * In addition, this controls the spacing of cacheline-spaced size classes. 2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */ 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LG_CACHELINE 6 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CACHELINE ((size_t)(1U << LG_CACHELINE)) 292a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define CACHELINE_MASK (CACHELINE - 1) 2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Return the smallest cacheline multiple that is >= s. */ 2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define CACHELINE_CEILING(s) \ 2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) (((s) + CACHELINE_MASK) & ~CACHELINE_MASK) 2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Page size. STATIC_PAGE_SHIFT is determined by the configure script. */ 2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define STATIC_PAGE_SIZE ((size_t)(1U << STATIC_PAGE_SHIFT)) 3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define STATIC_PAGE_MASK ((size_t)(STATIC_PAGE_SIZE - 1)) 3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef PAGE_SHIFT 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# undef PAGE_SHIFT 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 304a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#ifdef PAGE_SIZE 3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# undef PAGE_SIZE 3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef PAGE_MASK 3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# undef PAGE_MASK 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PAGE_SHIFT STATIC_PAGE_SHIFT 311a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define PAGE_SIZE STATIC_PAGE_SIZE 3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define PAGE_MASK STATIC_PAGE_MASK 3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Return the smallest pagesize multiple that is >= s. */ 3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define PAGE_CEILING(s) \ 3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) (((s) + PAGE_MASK) & ~PAGE_MASK) 3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "jemalloc/internal/atomic.h" 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jemalloc/internal/prng.h" 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jemalloc/internal/ckh.h" 321c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "jemalloc/internal/size_classes.h" 322c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "jemalloc/internal/stats.h" 323c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "jemalloc/internal/ctl.h" 324c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "jemalloc/internal/mutex.h" 325c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "jemalloc/internal/mb.h" 326c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "jemalloc/internal/extent.h" 327c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "jemalloc/internal/arena.h" 3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/bitmap.h" 3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/base.h" 3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/chunk.h" 3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/huge.h" 3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/rtree.h" 3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/tcache.h" 3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/hash.h" 3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifdef JEMALLOC_ZONE 3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "jemalloc/internal/zone.h" 3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif 338a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "jemalloc/internal/prof.h" 3394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 3404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#undef JEMALLOC_H_TYPES 3414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/******************************************************************************/ 3424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define JEMALLOC_H_STRUCTS 3434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 3444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/atomic.h" 345a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "jemalloc/internal/prng.h" 3464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/ckh.h" 3474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/size_classes.h" 3484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/stats.h" 3494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/ctl.h" 3504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/mutex.h" 3514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/mb.h" 3524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/bitmap.h" 3534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/extent.h" 3544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jemalloc/internal/arena.h" 3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "jemalloc/internal/base.h" 356#include "jemalloc/internal/chunk.h" 357#include "jemalloc/internal/huge.h" 358#include "jemalloc/internal/rtree.h" 359#include "jemalloc/internal/tcache.h" 360#include "jemalloc/internal/hash.h" 361#ifdef JEMALLOC_ZONE 362#include "jemalloc/internal/zone.h" 363#endif 364#include "jemalloc/internal/prof.h" 365 366typedef struct { 367 uint64_t allocated; 368 uint64_t deallocated; 369} thread_allocated_t; 370 371#undef JEMALLOC_H_STRUCTS 372/******************************************************************************/ 373#define JEMALLOC_H_EXTERNS 374 375extern bool opt_abort; 376extern bool opt_junk; 377extern bool opt_xmalloc; 378extern bool opt_zero; 379extern size_t opt_narenas; 380 381#ifdef DYNAMIC_PAGE_SHIFT 382extern size_t pagesize; 383extern size_t pagesize_mask; 384extern size_t lg_pagesize; 385#endif 386 387/* Number of CPUs. */ 388extern unsigned ncpus; 389 390extern malloc_mutex_t arenas_lock; /* Protects arenas initialization. */ 391extern pthread_key_t arenas_tsd; 392#ifndef NO_TLS 393/* 394 * Map of pthread_self() --> arenas[???], used for selecting an arena to use 395 * for allocations. 396 */ 397extern __thread arena_t *arenas_tls JEMALLOC_ATTR(tls_model("initial-exec")); 398# define ARENA_GET() arenas_tls 399# define ARENA_SET(v) do { \ 400 arenas_tls = (v); \ 401 pthread_setspecific(arenas_tsd, (void *)(v)); \ 402} while (0) 403#else 404# define ARENA_GET() ((arena_t *)pthread_getspecific(arenas_tsd)) 405# define ARENA_SET(v) do { \ 406 pthread_setspecific(arenas_tsd, (void *)(v)); \ 407} while (0) 408#endif 409 410/* 411 * Arenas that are used to service external requests. Not all elements of the 412 * arenas array are necessarily used; arenas are created lazily as needed. 413 */ 414extern arena_t **arenas; 415extern unsigned narenas; 416 417#ifndef NO_TLS 418extern __thread thread_allocated_t thread_allocated_tls; 419# define ALLOCATED_GET() (thread_allocated_tls.allocated) 420# define ALLOCATEDP_GET() (&thread_allocated_tls.allocated) 421# define DEALLOCATED_GET() (thread_allocated_tls.deallocated) 422# define DEALLOCATEDP_GET() (&thread_allocated_tls.deallocated) 423# define ALLOCATED_ADD(a, d) do { \ 424 thread_allocated_tls.allocated += a; \ 425 thread_allocated_tls.deallocated += d; \ 426} while (0) 427#else 428# define ALLOCATED_GET() (thread_allocated_get()->allocated) 429# define ALLOCATEDP_GET() (&thread_allocated_get()->allocated) 430# define DEALLOCATED_GET() (thread_allocated_get()->deallocated) 431# define DEALLOCATEDP_GET() (&thread_allocated_get()->deallocated) 432# define ALLOCATED_ADD(a, d) do { \ 433 thread_allocated_t *thread_allocated = thread_allocated_get(); \ 434 thread_allocated->allocated += (a); \ 435 thread_allocated->deallocated += (d); \ 436} while (0) 437#endif 438extern pthread_key_t thread_allocated_tsd; 439thread_allocated_t *thread_allocated_get_hard(void); 440 441arena_t *arenas_extend(unsigned ind); 442arena_t *choose_arena_hard(void); 443int buferror(int errnum, char *buf, size_t buflen); 444void jemalloc_prefork(void); 445void jemalloc_postfork(void); 446 447#include "jemalloc/internal/atomic.h" 448#include "jemalloc/internal/prng.h" 449#include "jemalloc/internal/ckh.h" 450#include "jemalloc/internal/size_classes.h" 451#include "jemalloc/internal/stats.h" 452#include "jemalloc/internal/ctl.h" 453#include "jemalloc/internal/mutex.h" 454#include "jemalloc/internal/mb.h" 455#include "jemalloc/internal/bitmap.h" 456#include "jemalloc/internal/extent.h" 457#include "jemalloc/internal/arena.h" 458#include "jemalloc/internal/base.h" 459#include "jemalloc/internal/chunk.h" 460#include "jemalloc/internal/huge.h" 461#include "jemalloc/internal/rtree.h" 462#include "jemalloc/internal/tcache.h" 463#include "jemalloc/internal/hash.h" 464#ifdef JEMALLOC_ZONE 465#include "jemalloc/internal/zone.h" 466#endif 467#include "jemalloc/internal/prof.h" 468 469#undef JEMALLOC_H_EXTERNS 470/******************************************************************************/ 471#define JEMALLOC_H_INLINES 472 473#include "jemalloc/internal/atomic.h" 474#include "jemalloc/internal/prng.h" 475#include "jemalloc/internal/ckh.h" 476#include "jemalloc/internal/size_classes.h" 477#include "jemalloc/internal/stats.h" 478#include "jemalloc/internal/ctl.h" 479#include "jemalloc/internal/mutex.h" 480#include "jemalloc/internal/mb.h" 481#include "jemalloc/internal/extent.h" 482#include "jemalloc/internal/base.h" 483#include "jemalloc/internal/chunk.h" 484#include "jemalloc/internal/huge.h" 485 486#ifndef JEMALLOC_ENABLE_INLINE 487size_t pow2_ceil(size_t x); 488size_t s2u(size_t size); 489size_t sa2u(size_t size, size_t alignment, size_t *run_size_p); 490void malloc_write(const char *s); 491arena_t *choose_arena(void); 492thread_allocated_t *thread_allocated_get(void); 493#endif 494 495#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_)) 496/* Compute the smallest power of 2 that is >= x. */ 497JEMALLOC_INLINE size_t 498pow2_ceil(size_t x) 499{ 500 501 x--; 502 x |= x >> 1; 503 x |= x >> 2; 504 x |= x >> 4; 505 x |= x >> 8; 506 x |= x >> 16; 507#if (LG_SIZEOF_PTR == 3) 508 x |= x >> 32; 509#endif 510 x++; 511 return (x); 512} 513 514/* 515 * Compute usable size that would result from allocating an object with the 516 * specified size. 517 */ 518JEMALLOC_INLINE size_t 519s2u(size_t size) 520{ 521 522 if (size <= SMALL_MAXCLASS) 523 return (arena_bin_info[SMALL_SIZE2BIN(size)].reg_size); 524 if (size <= arena_maxclass) 525 return (PAGE_CEILING(size)); 526 return (CHUNK_CEILING(size)); 527} 528 529/* 530 * Compute usable size that would result from allocating an object with the 531 * specified size and alignment. 532 */ 533JEMALLOC_INLINE size_t 534sa2u(size_t size, size_t alignment, size_t *run_size_p) 535{ 536 size_t usize; 537 538 /* 539 * Round size up to the nearest multiple of alignment. 540 * 541 * This done, we can take advantage of the fact that for each small 542 * size class, every object is aligned at the smallest power of two 543 * that is non-zero in the base two representation of the size. For 544 * example: 545 * 546 * Size | Base 2 | Minimum alignment 547 * -----+----------+------------------ 548 * 96 | 1100000 | 32 549 * 144 | 10100000 | 32 550 * 192 | 11000000 | 64 551 */ 552 usize = (size + (alignment - 1)) & (-alignment); 553 /* 554 * (usize < size) protects against the combination of maximal 555 * alignment and size greater than maximal alignment. 556 */ 557 if (usize < size) { 558 /* size_t overflow. */ 559 return (0); 560 } 561 562 if (usize <= arena_maxclass && alignment <= PAGE_SIZE) { 563 if (usize <= SMALL_MAXCLASS) 564 return (arena_bin_info[SMALL_SIZE2BIN(usize)].reg_size); 565 return (PAGE_CEILING(usize)); 566 } else { 567 size_t run_size; 568 569 /* 570 * We can't achieve subpage alignment, so round up alignment 571 * permanently; it makes later calculations simpler. 572 */ 573 alignment = PAGE_CEILING(alignment); 574 usize = PAGE_CEILING(size); 575 /* 576 * (usize < size) protects against very large sizes within 577 * PAGE_SIZE of SIZE_T_MAX. 578 * 579 * (usize + alignment < usize) protects against the 580 * combination of maximal alignment and usize large enough 581 * to cause overflow. This is similar to the first overflow 582 * check above, but it needs to be repeated due to the new 583 * usize value, which may now be *equal* to maximal 584 * alignment, whereas before we only detected overflow if the 585 * original size was *greater* than maximal alignment. 586 */ 587 if (usize < size || usize + alignment < usize) { 588 /* size_t overflow. */ 589 return (0); 590 } 591 592 /* 593 * Calculate the size of the over-size run that arena_palloc() 594 * would need to allocate in order to guarantee the alignment. 595 */ 596 if (usize >= alignment) 597 run_size = usize + alignment - PAGE_SIZE; 598 else { 599 /* 600 * It is possible that (alignment << 1) will cause 601 * overflow, but it doesn't matter because we also 602 * subtract PAGE_SIZE, which in the case of overflow 603 * leaves us with a very large run_size. That causes 604 * the first conditional below to fail, which means 605 * that the bogus run_size value never gets used for 606 * anything important. 607 */ 608 run_size = (alignment << 1) - PAGE_SIZE; 609 } 610 if (run_size_p != NULL) 611 *run_size_p = run_size; 612 613 if (run_size <= arena_maxclass) 614 return (PAGE_CEILING(usize)); 615 return (CHUNK_CEILING(usize)); 616 } 617} 618 619/* 620 * Wrapper around malloc_message() that avoids the need for 621 * je_malloc_message(...) throughout the code. 622 */ 623JEMALLOC_INLINE void 624malloc_write(const char *s) 625{ 626 627 je_malloc_message(NULL, s); 628} 629 630/* 631 * Choose an arena based on a per-thread value (fast-path code, calls slow-path 632 * code if necessary). 633 */ 634JEMALLOC_INLINE arena_t * 635choose_arena(void) 636{ 637 arena_t *ret; 638 639 ret = ARENA_GET(); 640 if (ret == NULL) { 641 ret = choose_arena_hard(); 642 assert(ret != NULL); 643 } 644 645 return (ret); 646} 647 648JEMALLOC_INLINE thread_allocated_t * 649thread_allocated_get(void) 650{ 651 thread_allocated_t *thread_allocated = (thread_allocated_t *) 652 pthread_getspecific(thread_allocated_tsd); 653 654 if (thread_allocated == NULL) 655 return (thread_allocated_get_hard()); 656 return (thread_allocated); 657} 658#endif 659 660#include "jemalloc/internal/bitmap.h" 661#include "jemalloc/internal/rtree.h" 662#include "jemalloc/internal/tcache.h" 663#include "jemalloc/internal/arena.h" 664#include "jemalloc/internal/hash.h" 665#ifdef JEMALLOC_ZONE 666#include "jemalloc/internal/zone.h" 667#endif 668 669#ifndef JEMALLOC_ENABLE_INLINE 670void *imalloc(size_t size); 671void *icalloc(size_t size); 672void *ipalloc(size_t usize, size_t alignment, bool zero); 673size_t isalloc(const void *ptr); 674size_t ivsalloc(const void *ptr); 675void idalloc(void *ptr); 676void *iralloc(void *ptr, size_t size, size_t extra, size_t alignment, 677 bool zero, bool no_move); 678#endif 679 680#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_)) 681JEMALLOC_INLINE void * 682imalloc(size_t size) 683{ 684 685 assert(size != 0); 686 687 if (size <= arena_maxclass) 688 return (arena_malloc(size, false)); 689 else 690 return (huge_malloc(size, false)); 691} 692 693JEMALLOC_INLINE void * 694icalloc(size_t size) 695{ 696 697 if (size <= arena_maxclass) 698 return (arena_malloc(size, true)); 699 else 700 return (huge_malloc(size, true)); 701} 702 703JEMALLOC_INLINE void * 704ipalloc(size_t usize, size_t alignment, bool zero) 705{ 706 void *ret; 707 708 assert(usize != 0); 709 assert(usize == sa2u(usize, alignment, NULL)); 710 711 if (usize <= arena_maxclass && alignment <= PAGE_SIZE) 712 ret = arena_malloc(usize, zero); 713 else { 714 size_t run_size 715#ifdef JEMALLOC_CC_SILENCE 716 = 0 717#endif 718 ; 719 720 /* 721 * Ideally we would only ever call sa2u() once per aligned 722 * allocation request, and the caller of this function has 723 * already done so once. However, it's rather burdensome to 724 * require every caller to pass in run_size, especially given 725 * that it's only relevant to large allocations. Therefore, 726 * just call it again here in order to get run_size. 727 */ 728 sa2u(usize, alignment, &run_size); 729 if (run_size <= arena_maxclass) { 730 ret = arena_palloc(choose_arena(), usize, run_size, 731 alignment, zero); 732 } else if (alignment <= chunksize) 733 ret = huge_malloc(usize, zero); 734 else 735 ret = huge_palloc(usize, alignment, zero); 736 } 737 738 assert(((uintptr_t)ret & (alignment - 1)) == 0); 739 return (ret); 740} 741 742JEMALLOC_INLINE size_t 743isalloc(const void *ptr) 744{ 745 size_t ret; 746 arena_chunk_t *chunk; 747 748 assert(ptr != NULL); 749 750 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); 751 if (chunk != ptr) { 752 /* Region. */ 753 if (config_prof) 754 ret = arena_salloc_demote(ptr); 755 else 756 ret = arena_salloc(ptr); 757 } else 758 ret = huge_salloc(ptr); 759 760 return (ret); 761} 762 763JEMALLOC_INLINE size_t 764ivsalloc(const void *ptr) 765{ 766 767 /* Return 0 if ptr is not within a chunk managed by jemalloc. */ 768 if (rtree_get(chunks_rtree, (uintptr_t)CHUNK_ADDR2BASE(ptr)) == NULL) 769 return (0); 770 771 return (isalloc(ptr)); 772} 773 774JEMALLOC_INLINE void 775idalloc(void *ptr) 776{ 777 arena_chunk_t *chunk; 778 779 assert(ptr != NULL); 780 781 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); 782 if (chunk != ptr) 783 arena_dalloc(chunk->arena, chunk, ptr); 784 else 785 huge_dalloc(ptr, true); 786} 787 788JEMALLOC_INLINE void * 789iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero, 790 bool no_move) 791{ 792 void *ret; 793 size_t oldsize; 794 795 assert(ptr != NULL); 796 assert(size != 0); 797 798 oldsize = isalloc(ptr); 799 800 if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1)) 801 != 0) { 802 size_t usize, copysize; 803 804 /* 805 * Existing object alignment is inadquate; allocate new space 806 * and copy. 807 */ 808 if (no_move) 809 return (NULL); 810 usize = sa2u(size + extra, alignment, NULL); 811 if (usize == 0) 812 return (NULL); 813 ret = ipalloc(usize, alignment, zero); 814 if (ret == NULL) { 815 if (extra == 0) 816 return (NULL); 817 /* Try again, without extra this time. */ 818 usize = sa2u(size, alignment, NULL); 819 if (usize == 0) 820 return (NULL); 821 ret = ipalloc(usize, alignment, zero); 822 if (ret == NULL) 823 return (NULL); 824 } 825 /* 826 * Copy at most size bytes (not size+extra), since the caller 827 * has no expectation that the extra bytes will be reliably 828 * preserved. 829 */ 830 copysize = (size < oldsize) ? size : oldsize; 831 memcpy(ret, ptr, copysize); 832 idalloc(ptr); 833 return (ret); 834 } 835 836 if (no_move) { 837 if (size <= arena_maxclass) { 838 return (arena_ralloc_no_move(ptr, oldsize, size, 839 extra, zero)); 840 } else { 841 return (huge_ralloc_no_move(ptr, oldsize, size, 842 extra)); 843 } 844 } else { 845 if (size + extra <= arena_maxclass) { 846 return (arena_ralloc(ptr, oldsize, size, extra, 847 alignment, zero)); 848 } else { 849 return (huge_ralloc(ptr, oldsize, size, extra, 850 alignment, zero)); 851 } 852 } 853} 854#endif 855 856#include "jemalloc/internal/prof.h" 857 858#undef JEMALLOC_H_INLINES 859/******************************************************************************/ 860