mem1.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. ** This file contains the C functions that implement a memory
  13. ** allocation subsystem for use by SQLite.
  14. **
  15. ** $Id: mem1.c,v 1.14 2007/11/29 18:36:49 drh Exp $
  16. */
  17. /*
  18. ** This version of the memory allocator is the default. It is
  19. ** used when no other memory allocator is specified using compile-time
  20. ** macros.
  21. */
  22. #if !defined(SQLITE_MEMDEBUG) && !defined(SQLITE_MEMORY_SIZE) \
  23. && !defined(SQLITE_MMAP_HEAP_SIZE)
  24. /*
  25. ** We will eventually construct multiple memory allocation subsystems
  26. ** suitable for use in various contexts:
  27. **
  28. ** * Normal multi-threaded builds
  29. ** * Normal single-threaded builds
  30. ** * Debugging builds
  31. **
  32. ** This initial version is suitable for use in normal multi-threaded
  33. ** builds. We envision that alternative versions will be stored in
  34. ** separate source files. #ifdefs will be used to select the code from
  35. ** one of the various memN.c source files for use in any given build.
  36. */
  37. #include "sqliteInt.h"
  38. /*
  39. ** All of the static variables used by this module are collected
  40. ** into a single structure named "mem". This is to keep the
  41. ** static variables organized and to reduce namespace pollution
  42. ** when this module is combined with other in the amalgamation.
  43. */
  44. static struct {
  45. /*
  46. ** The alarm callback and its arguments. The mem.mutex lock will
  47. ** be held while the callback is running. Recursive calls into
  48. ** the memory subsystem are allowed, but no new callbacks will be
  49. ** issued. The alarmBusy variable is set to prevent recursive
  50. ** callbacks.
  51. */
  52. sqlite3_int64 alarmThreshold;
  53. void (*alarmCallback)(void*, sqlite3_int64,int);
  54. void *alarmArg;
  55. int alarmBusy;
  56. /*
  57. ** Mutex to control access to the memory allocation subsystem.
  58. */
  59. sqlite3_mutex *mutex;
  60. /*
  61. ** Current allocation and high-water mark.
  62. */
  63. sqlite3_int64 nowUsed;
  64. sqlite3_int64 mxUsed;
  65. } mem;
  66. /*
  67. ** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
  68. */
  69. static void enterMem(void){
  70. if( mem.mutex==0 ){
  71. mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
  72. }
  73. sqlite3_mutex_enter(mem.mutex);
  74. }
  75. /*
  76. ** Return the amount of memory currently checked out.
  77. */
  78. sqlite3_int64 sqlite3_memory_used(void){
  79. sqlite3_int64 n;
  80. enterMem();
  81. n = mem.nowUsed;
  82. sqlite3_mutex_leave(mem.mutex);
  83. return n;
  84. }
  85. /*
  86. ** Return the maximum amount of memory that has ever been
  87. ** checked out since either the beginning of this process
  88. ** or since the most recent reset.
  89. */
  90. sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
  91. sqlite3_int64 n;
  92. enterMem();
  93. n = mem.mxUsed;
  94. if( resetFlag ){
  95. mem.mxUsed = mem.nowUsed;
  96. }
  97. sqlite3_mutex_leave(mem.mutex);
  98. return n;
  99. }
  100. /*
  101. ** Change the alarm callback
  102. */
  103. int sqlite3_memory_alarm(
  104. void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
  105. void *pArg,
  106. sqlite3_int64 iThreshold
  107. ){
  108. enterMem();
  109. mem.alarmCallback = xCallback;
  110. mem.alarmArg = pArg;
  111. mem.alarmThreshold = iThreshold;
  112. sqlite3_mutex_leave(mem.mutex);
  113. return SQLITE_OK;
  114. }
  115. /*
  116. ** Trigger the alarm
  117. */
  118. static void sqlite3MemsysAlarm(int nByte){
  119. void (*xCallback)(void*,sqlite3_int64,int);
  120. sqlite3_int64 nowUsed;
  121. void *pArg;
  122. if( mem.alarmCallback==0 || mem.alarmBusy ) return;
  123. mem.alarmBusy = 1;
  124. xCallback = mem.alarmCallback;
  125. nowUsed = mem.nowUsed;
  126. pArg = mem.alarmArg;
  127. sqlite3_mutex_leave(mem.mutex);
  128. xCallback(pArg, nowUsed, nByte);
  129. sqlite3_mutex_enter(mem.mutex);
  130. mem.alarmBusy = 0;
  131. }
  132. /*
  133. ** Allocate nBytes of memory
  134. */
  135. void *sqlite3_malloc(int nBytes){
  136. sqlite3_int64 *p = 0;
  137. if( nBytes>0 ){
  138. enterMem();
  139. if( mem.alarmCallback!=0 && mem.nowUsed+nBytes>=mem.alarmThreshold ){
  140. sqlite3MemsysAlarm(nBytes);
  141. }
  142. p = malloc(nBytes+8);
  143. if( p==0 ){
  144. sqlite3MemsysAlarm(nBytes);
  145. p = malloc(nBytes+8);
  146. }
  147. if( p ){
  148. p[0] = nBytes;
  149. p++;
  150. mem.nowUsed += nBytes;
  151. if( mem.nowUsed>mem.mxUsed ){
  152. mem.mxUsed = mem.nowUsed;
  153. }
  154. }
  155. sqlite3_mutex_leave(mem.mutex);
  156. }
  157. return (void*)p;
  158. }
  159. /*
  160. ** Free memory.
  161. */
  162. void sqlite3_free(void *pPrior){
  163. sqlite3_int64 *p;
  164. int nByte;
  165. if( pPrior==0 ){
  166. return;
  167. }
  168. assert( mem.mutex!=0 );
  169. p = pPrior;
  170. p--;
  171. nByte = (int)*p;
  172. sqlite3_mutex_enter(mem.mutex);
  173. mem.nowUsed -= nByte;
  174. free(p);
  175. sqlite3_mutex_leave(mem.mutex);
  176. }
  177. /*
  178. ** Change the size of an existing memory allocation
  179. */
  180. void *sqlite3_realloc(void *pPrior, int nBytes){
  181. int nOld;
  182. sqlite3_int64 *p;
  183. if( pPrior==0 ){
  184. return sqlite3_malloc(nBytes);
  185. }
  186. if( nBytes<=0 ){
  187. sqlite3_free(pPrior);
  188. return 0;
  189. }
  190. p = pPrior;
  191. p--;
  192. nOld = (int)p[0];
  193. assert( mem.mutex!=0 );
  194. sqlite3_mutex_enter(mem.mutex);
  195. if( mem.nowUsed+nBytes-nOld>=mem.alarmThreshold ){
  196. sqlite3MemsysAlarm(nBytes-nOld);
  197. }
  198. p = realloc(p, nBytes+8);
  199. if( p==0 ){
  200. sqlite3MemsysAlarm(nBytes);
  201. p = pPrior;
  202. p--;
  203. p = realloc(p, nBytes+8);
  204. }
  205. if( p ){
  206. p[0] = nBytes;
  207. p++;
  208. mem.nowUsed += nBytes-nOld;
  209. if( mem.nowUsed>mem.mxUsed ){
  210. mem.mxUsed = mem.nowUsed;
  211. }
  212. }
  213. sqlite3_mutex_leave(mem.mutex);
  214. return (void*)p;
  215. }
  216. #endif /* !SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */