mem1.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. ** 2007 August 14
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. **
  13. ** This file contains low-level memory allocation drivers for when
  14. ** SQLite will use the standard C-library malloc/realloc/free interface
  15. ** to obtain the memory it needs.
  16. **
  17. ** This file contains implementations of the low-level memory allocation
  18. ** routines specified in the sqlite3_mem_methods object. The content of
  19. ** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
  20. ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
  21. ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
  22. ** default configuration is to use memory allocation routines in this
  23. ** file.
  24. **
  25. ** C-preprocessor macro summary:
  26. **
  27. ** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
  28. ** the malloc_usable_size() interface exists
  29. ** on the target platform. Or, this symbol
  30. ** can be set manually, if desired.
  31. ** If an equivalent interface exists by
  32. ** a different name, using a separate -D
  33. ** option to rename it.
  34. **
  35. ** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
  36. ** memory allocator. Set this symbol to enable
  37. ** building on older macs.
  38. **
  39. ** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
  40. ** _msize() on windows systems. This might
  41. ** be necessary when compiling for Delphi,
  42. ** for example.
  43. */
  44. #include "sqliteInt.h"
  45. /*
  46. ** This version of the memory allocator is the default. It is
  47. ** used when no other memory allocator is specified using compile-time
  48. ** macros.
  49. */
  50. #ifdef SQLITE_SYSTEM_MALLOC
  51. /*
  52. ** The MSVCRT has malloc_usable_size() but it is called _msize().
  53. ** The use of _msize() is automatic, but can be disabled by compiling
  54. ** with -DSQLITE_WITHOUT_MSIZE
  55. */
  56. #if defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
  57. # define SQLITE_MALLOCSIZE _msize
  58. #endif
  59. #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
  60. /*
  61. ** Use the zone allocator available on apple products unless the
  62. ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
  63. */
  64. #include <sys/sysctl.h>
  65. #include <malloc/malloc.h>
  66. #include <libkern/OSAtomic.h>
  67. static malloc_zone_t* _sqliteZone_;
  68. #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
  69. #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
  70. #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
  71. #define SQLITE_MALLOCSIZE(x) \
  72. (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
  73. #elif defined(SQLITE_OS_RTT)
  74. #include <rtthread.h>
  75. /*
  76. ** Use standard C library malloc and free on non-Apple systems.
  77. ** Also used by rt-thread systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
  78. */
  79. #define SQLITE_MALLOC(x) rt_malloc((rt_size_t)x)
  80. #define SQLITE_FREE(x) rt_free(x)
  81. #define SQLITE_REALLOC(x,y) rt_realloc((x),(rt_size_t)(y))
  82. #if (!defined(SQLITE_WITHOUT_MSIZE)) \
  83. && (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE))
  84. # error "not have malloc_usable_size()"
  85. #endif
  86. #ifdef HAVE_MALLOC_USABLE_SIZE
  87. # undef SQLITE_MALLOCSIZE
  88. #endif
  89. #else /* if not __APPLE__ */
  90. /*
  91. ** Use standard C library malloc and free on non-Apple systems.
  92. ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
  93. */
  94. #define SQLITE_MALLOC(x) malloc(x)
  95. #define SQLITE_FREE(x) free(x)
  96. #define SQLITE_REALLOC(x,y) realloc((x),(y))
  97. #if (defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)) \
  98. || (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE))
  99. # include <malloc.h> /* Needed for malloc_usable_size on linux */
  100. #endif
  101. #ifdef HAVE_MALLOC_USABLE_SIZE
  102. # ifndef SQLITE_MALLOCSIZE
  103. # define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
  104. # endif
  105. #else
  106. # undef SQLITE_MALLOCSIZE
  107. #endif
  108. #endif /* __APPLE__ or not __APPLE__ */
  109. /*
  110. ** Like malloc(), but remember the size of the allocation
  111. ** so that we can find it later using sqlite3MemSize().
  112. **
  113. ** For this low-level routine, we are guaranteed that nByte>0 because
  114. ** cases of nByte<=0 will be intercepted and dealt with by higher level
  115. ** routines.
  116. */
  117. static void *sqlite3MemMalloc(int nByte){
  118. #ifdef SQLITE_MALLOCSIZE
  119. void *p = SQLITE_MALLOC( nByte );
  120. if( p==0 ){
  121. testcase( sqlite3GlobalConfig.xLog!=0 );
  122. sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
  123. }
  124. return p;
  125. #else
  126. sqlite3_int64 *p;
  127. assert( nByte>0 );
  128. nByte = ROUND8(nByte);
  129. p = SQLITE_MALLOC( nByte+8 );
  130. if( p ){
  131. p[0] = nByte;
  132. p++;
  133. }else{
  134. testcase( sqlite3GlobalConfig.xLog!=0 );
  135. sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
  136. }
  137. return (void *)p;
  138. #endif
  139. }
  140. /*
  141. ** Like free() but works for allocations obtained from sqlite3MemMalloc()
  142. ** or sqlite3MemRealloc().
  143. **
  144. ** For this low-level routine, we already know that pPrior!=0 since
  145. ** cases where pPrior==0 will have been intecepted and dealt with
  146. ** by higher-level routines.
  147. */
  148. static void sqlite3MemFree(void *pPrior){
  149. #ifdef SQLITE_MALLOCSIZE
  150. SQLITE_FREE(pPrior);
  151. #else
  152. sqlite3_int64 *p = (sqlite3_int64*)pPrior;
  153. assert( pPrior!=0 );
  154. p--;
  155. SQLITE_FREE(p);
  156. #endif
  157. }
  158. /*
  159. ** Report the allocated size of a prior return from xMalloc()
  160. ** or xRealloc().
  161. */
  162. static int sqlite3MemSize(void *pPrior){
  163. #ifdef SQLITE_MALLOCSIZE
  164. return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
  165. #else
  166. sqlite3_int64 *p;
  167. if( pPrior==0 ) return 0;
  168. p = (sqlite3_int64*)pPrior;
  169. p--;
  170. return (int)p[0];
  171. #endif
  172. }
  173. /*
  174. ** Like realloc(). Resize an allocation previously obtained from
  175. ** sqlite3MemMalloc().
  176. **
  177. ** For this low-level interface, we know that pPrior!=0. Cases where
  178. ** pPrior==0 while have been intercepted by higher-level routine and
  179. ** redirected to xMalloc. Similarly, we know that nByte>0 becauses
  180. ** cases where nByte<=0 will have been intercepted by higher-level
  181. ** routines and redirected to xFree.
  182. */
  183. static void *sqlite3MemRealloc(void *pPrior, int nByte){
  184. #ifdef SQLITE_MALLOCSIZE
  185. void *p = SQLITE_REALLOC(pPrior, nByte);
  186. if( p==0 ){
  187. testcase( sqlite3GlobalConfig.xLog!=0 );
  188. sqlite3_log(SQLITE_NOMEM,
  189. "failed memory resize %u to %u bytes",
  190. SQLITE_MALLOCSIZE(pPrior), nByte);
  191. }
  192. return p;
  193. #else
  194. sqlite3_int64 *p = (sqlite3_int64*)pPrior;
  195. assert( pPrior!=0 && nByte>0 );
  196. assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
  197. p--;
  198. p = SQLITE_REALLOC(p, nByte+8 );
  199. if( p ){
  200. p[0] = nByte;
  201. p++;
  202. }else{
  203. testcase( sqlite3GlobalConfig.xLog!=0 );
  204. sqlite3_log(SQLITE_NOMEM,
  205. "failed memory resize %u to %u bytes",
  206. sqlite3MemSize(pPrior), nByte);
  207. }
  208. return (void*)p;
  209. #endif
  210. }
  211. /*
  212. ** Round up a request size to the next valid allocation size.
  213. */
  214. static int sqlite3MemRoundup(int n){
  215. return ROUND8(n);
  216. }
  217. /*
  218. ** Initialize this module.
  219. */
  220. static int sqlite3MemInit(void *NotUsed){
  221. #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
  222. int cpuCount;
  223. size_t len;
  224. if( _sqliteZone_ ){
  225. return SQLITE_OK;
  226. }
  227. len = sizeof(cpuCount);
  228. /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
  229. sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
  230. if( cpuCount>1 ){
  231. /* defer MT decisions to system malloc */
  232. _sqliteZone_ = malloc_default_zone();
  233. }else{
  234. /* only 1 core, use our own zone to contention over global locks,
  235. ** e.g. we have our own dedicated locks */
  236. bool success;
  237. malloc_zone_t* newzone = malloc_create_zone(4096, 0);
  238. malloc_set_zone_name(newzone, "Sqlite_Heap");
  239. do{
  240. success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
  241. (void * volatile *)&_sqliteZone_);
  242. }while(!_sqliteZone_);
  243. if( !success ){
  244. /* somebody registered a zone first */
  245. malloc_destroy_zone(newzone);
  246. }
  247. }
  248. #endif
  249. UNUSED_PARAMETER(NotUsed);
  250. return SQLITE_OK;
  251. }
  252. /*
  253. ** Deinitialize this module.
  254. */
  255. static void sqlite3MemShutdown(void *NotUsed){
  256. UNUSED_PARAMETER(NotUsed);
  257. return;
  258. }
  259. /*
  260. ** This routine is the only routine in this file with external linkage.
  261. **
  262. ** Populate the low-level memory allocation function pointers in
  263. ** sqlite3GlobalConfig.m with pointers to the routines in this file.
  264. */
  265. void sqlite3MemSetDefault(void){
  266. static const sqlite3_mem_methods defaultMethods = {
  267. sqlite3MemMalloc,
  268. sqlite3MemFree,
  269. sqlite3MemRealloc,
  270. sqlite3MemSize,
  271. sqlite3MemRoundup,
  272. sqlite3MemInit,
  273. sqlite3MemShutdown,
  274. 0
  275. };
  276. sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
  277. }
  278. #endif /* SQLITE_SYSTEM_MALLOC */