malloc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. ** 2001 September 15
  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. ** Memory allocation functions used throughout sqlite.
  13. **
  14. **
  15. ** $Id: malloc.c,v 1.14 2007/10/20 16:36:31 drh Exp $
  16. */
  17. #include "sqliteInt.h"
  18. #include <stdarg.h>
  19. #include <ctype.h>
  20. /*
  21. ** This routine runs when the memory allocator sees that the
  22. ** total memory allocation is about to exceed the soft heap
  23. ** limit.
  24. */
  25. static void softHeapLimitEnforcer(
  26. void *NotUsed,
  27. sqlite3_int64 inUse,
  28. int allocSize
  29. ){
  30. sqlite3_release_memory(allocSize);
  31. }
  32. /*
  33. ** Set the soft heap-size limit for the current thread. Passing a
  34. ** zero or negative value indicates no limit.
  35. */
  36. void sqlite3_soft_heap_limit(int n){
  37. sqlite3_uint64 iLimit;
  38. int overage;
  39. if( n<0 ){
  40. iLimit = 0;
  41. }else{
  42. iLimit = n;
  43. }
  44. if( iLimit>0 ){
  45. sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
  46. }else{
  47. sqlite3_memory_alarm(0, 0, 0);
  48. }
  49. overage = sqlite3_memory_used() - n;
  50. if( overage>0 ){
  51. sqlite3_release_memory(overage);
  52. }
  53. }
  54. /*
  55. ** Release memory held by SQLite instances created by the current thread.
  56. */
  57. int sqlite3_release_memory(int n){
  58. #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  59. return sqlite3PagerReleaseMemory(n);
  60. #else
  61. return SQLITE_OK;
  62. #endif
  63. }
  64. /*
  65. ** Allocate and zero memory.
  66. */
  67. void *sqlite3MallocZero(unsigned n){
  68. void *p = sqlite3_malloc(n);
  69. if( p ){
  70. memset(p, 0, n);
  71. }
  72. return p;
  73. }
  74. /*
  75. ** Allocate and zero memory. If the allocation fails, make
  76. ** the mallocFailed flag in the connection pointer.
  77. */
  78. void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
  79. void *p = sqlite3DbMallocRaw(db, n);
  80. if( p ){
  81. memset(p, 0, n);
  82. }
  83. return p;
  84. }
  85. /*
  86. ** Allocate and zero memory. If the allocation fails, make
  87. ** the mallocFailed flag in the connection pointer.
  88. */
  89. void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
  90. void *p = 0;
  91. if( !db || db->mallocFailed==0 ){
  92. p = sqlite3_malloc(n);
  93. if( !p && db ){
  94. db->mallocFailed = 1;
  95. }
  96. }
  97. return p;
  98. }
  99. /*
  100. ** Resize the block of memory pointed to by p to n bytes. If the
  101. ** resize fails, set the mallocFailed flag inthe connection object.
  102. */
  103. void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
  104. void *pNew = 0;
  105. if( db->mallocFailed==0 ){
  106. pNew = sqlite3_realloc(p, n);
  107. if( !pNew ){
  108. db->mallocFailed = 1;
  109. }
  110. }
  111. return pNew;
  112. }
  113. /*
  114. ** Attempt to reallocate p. If the reallocation fails, then free p
  115. ** and set the mallocFailed flag in the database connection.
  116. */
  117. void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
  118. void *pNew;
  119. pNew = sqlite3DbRealloc(db, p, n);
  120. if( !pNew ){
  121. sqlite3_free(p);
  122. }
  123. return pNew;
  124. }
  125. /*
  126. ** Make a copy of a string in memory obtained from sqliteMalloc(). These
  127. ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
  128. ** is because when memory debugging is turned on, these two functions are
  129. ** called via macros that record the current file and line number in the
  130. ** ThreadData structure.
  131. */
  132. char *sqlite3StrDup(const char *z){
  133. char *zNew;
  134. int n;
  135. if( z==0 ) return 0;
  136. n = strlen(z)+1;
  137. zNew = sqlite3_malloc(n);
  138. if( zNew ) memcpy(zNew, z, n);
  139. return zNew;
  140. }
  141. char *sqlite3StrNDup(const char *z, int n){
  142. char *zNew;
  143. if( z==0 ) return 0;
  144. zNew = sqlite3_malloc(n+1);
  145. if( zNew ){
  146. memcpy(zNew, z, n);
  147. zNew[n] = 0;
  148. }
  149. return zNew;
  150. }
  151. char *sqlite3DbStrDup(sqlite3 *db, const char *z){
  152. char *zNew = sqlite3StrDup(z);
  153. if( z && !zNew ){
  154. db->mallocFailed = 1;
  155. }
  156. return zNew;
  157. }
  158. char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
  159. char *zNew = sqlite3StrNDup(z, n);
  160. if( z && !zNew ){
  161. db->mallocFailed = 1;
  162. }
  163. return zNew;
  164. }
  165. /*
  166. ** Create a string from the 2nd and subsequent arguments (up to the
  167. ** first NULL argument), store the string in memory obtained from
  168. ** sqliteMalloc() and make the pointer indicated by the 1st argument
  169. ** point to that string. The 1st argument must either be NULL or
  170. ** point to memory obtained from sqliteMalloc().
  171. */
  172. void sqlite3SetString(char **pz, ...){
  173. va_list ap;
  174. int nByte;
  175. const char *z;
  176. char *zResult;
  177. assert( pz!=0 );
  178. nByte = 1;
  179. va_start(ap, pz);
  180. while( (z = va_arg(ap, const char*))!=0 ){
  181. nByte += strlen(z);
  182. }
  183. va_end(ap);
  184. sqlite3_free(*pz);
  185. *pz = zResult = sqlite3_malloc(nByte);
  186. if( zResult==0 ){
  187. return;
  188. }
  189. *zResult = 0;
  190. va_start(ap, pz);
  191. while( (z = va_arg(ap, const char*))!=0 ){
  192. int n = strlen(z);
  193. memcpy(zResult, z, n);
  194. zResult += n;
  195. }
  196. zResult[0] = 0;
  197. va_end(ap);
  198. }
  199. /*
  200. ** This function must be called before exiting any API function (i.e.
  201. ** returning control to the user) that has called sqlite3_malloc or
  202. ** sqlite3_realloc.
  203. **
  204. ** The returned value is normally a copy of the second argument to this
  205. ** function. However, if a malloc() failure has occured since the previous
  206. ** invocation SQLITE_NOMEM is returned instead.
  207. **
  208. ** If the first argument, db, is not NULL and a malloc() error has occured,
  209. ** then the connection error-code (the value returned by sqlite3_errcode())
  210. ** is set to SQLITE_NOMEM.
  211. */
  212. int sqlite3ApiExit(sqlite3* db, int rc){
  213. /* If the db handle is not NULL, then we must hold the connection handle
  214. ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
  215. ** is unsafe, as is the call to sqlite3Error().
  216. */
  217. assert( !db || sqlite3_mutex_held(db->mutex) );
  218. if( db && db->mallocFailed ){
  219. sqlite3Error(db, SQLITE_NOMEM, 0);
  220. db->mallocFailed = 0;
  221. rc = SQLITE_NOMEM;
  222. }
  223. return rc & (db ? db->errMask : 0xff);
  224. }