OLD | NEW |
1 /* | 1 /* |
2 ** 2007 August 14 | 2 ** 2007 August 14 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** | 12 ** |
13 ** This file contains low-level memory allocation drivers for when | 13 ** This file contains low-level memory allocation drivers for when |
14 ** SQLite will use the standard C-library malloc/realloc/free interface | 14 ** SQLite will use the standard C-library malloc/realloc/free interface |
15 ** to obtain the memory it needs. | 15 ** to obtain the memory it needs. |
16 ** | 16 ** |
17 ** This file contains implementations of the low-level memory allocation | 17 ** This file contains implementations of the low-level memory allocation |
18 ** routines specified in the sqlite3_mem_methods object. | 18 ** routines specified in the sqlite3_mem_methods object. |
19 ** | |
20 ** $Id: mem1.c,v 1.30 2009/03/23 04:33:33 danielk1977 Exp $ | |
21 */ | 19 */ |
22 #include "sqliteInt.h" | 20 #include "sqliteInt.h" |
23 | 21 |
24 /* | 22 /* |
25 ** This version of the memory allocator is the default. It is | 23 ** This version of the memory allocator is the default. It is |
26 ** used when no other memory allocator is specified using compile-time | 24 ** used when no other memory allocator is specified using compile-time |
27 ** macros. | 25 ** macros. |
28 */ | 26 */ |
29 #ifdef SQLITE_SYSTEM_MALLOC | 27 #ifdef SQLITE_SYSTEM_MALLOC |
30 | 28 |
31 /* | 29 /* |
32 ** Like malloc(), but remember the size of the allocation | 30 ** Like malloc(), but remember the size of the allocation |
33 ** so that we can find it later using sqlite3MemSize(). | 31 ** so that we can find it later using sqlite3MemSize(). |
34 ** | 32 ** |
35 ** For this low-level routine, we are guaranteed that nByte>0 because | 33 ** For this low-level routine, we are guaranteed that nByte>0 because |
36 ** cases of nByte<=0 will be intercepted and dealt with by higher level | 34 ** cases of nByte<=0 will be intercepted and dealt with by higher level |
37 ** routines. | 35 ** routines. |
38 */ | 36 */ |
39 static void *sqlite3MemMalloc(int nByte){ | 37 static void *sqlite3MemMalloc(int nByte){ |
40 sqlite3_int64 *p; | 38 sqlite3_int64 *p; |
41 assert( nByte>0 ); | 39 assert( nByte>0 ); |
42 nByte = ROUND8(nByte); | 40 nByte = ROUND8(nByte); |
43 p = malloc( nByte+8 ); | 41 p = malloc( nByte+8 ); |
44 if( p ){ | 42 if( p ){ |
45 p[0] = nByte; | 43 p[0] = nByte; |
46 p++; | 44 p++; |
| 45 }else{ |
| 46 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 47 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); |
47 } | 48 } |
48 return (void *)p; | 49 return (void *)p; |
49 } | 50 } |
50 | 51 |
51 /* | 52 /* |
52 ** Like free() but works for allocations obtained from sqlite3MemMalloc() | 53 ** Like free() but works for allocations obtained from sqlite3MemMalloc() |
53 ** or sqlite3MemRealloc(). | 54 ** or sqlite3MemRealloc(). |
54 ** | 55 ** |
55 ** For this low-level routine, we already know that pPrior!=0 since | 56 ** For this low-level routine, we already know that pPrior!=0 since |
56 ** cases where pPrior==0 will have been intecepted and dealt with | 57 ** cases where pPrior==0 will have been intecepted and dealt with |
57 ** by higher-level routines. | 58 ** by higher-level routines. |
58 */ | 59 */ |
59 static void sqlite3MemFree(void *pPrior){ | 60 static void sqlite3MemFree(void *pPrior){ |
60 sqlite3_int64 *p = (sqlite3_int64*)pPrior; | 61 sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
61 assert( pPrior!=0 ); | 62 assert( pPrior!=0 ); |
62 p--; | 63 p--; |
63 free(p); | 64 free(p); |
64 } | 65 } |
65 | 66 |
66 /* | 67 /* |
| 68 ** Report the allocated size of a prior return from xMalloc() |
| 69 ** or xRealloc(). |
| 70 */ |
| 71 static int sqlite3MemSize(void *pPrior){ |
| 72 sqlite3_int64 *p; |
| 73 if( pPrior==0 ) return 0; |
| 74 p = (sqlite3_int64*)pPrior; |
| 75 p--; |
| 76 return (int)p[0]; |
| 77 } |
| 78 |
| 79 /* |
67 ** Like realloc(). Resize an allocation previously obtained from | 80 ** Like realloc(). Resize an allocation previously obtained from |
68 ** sqlite3MemMalloc(). | 81 ** sqlite3MemMalloc(). |
69 ** | 82 ** |
70 ** For this low-level interface, we know that pPrior!=0. Cases where | 83 ** For this low-level interface, we know that pPrior!=0. Cases where |
71 ** pPrior==0 while have been intercepted by higher-level routine and | 84 ** pPrior==0 while have been intercepted by higher-level routine and |
72 ** redirected to xMalloc. Similarly, we know that nByte>0 becauses | 85 ** redirected to xMalloc. Similarly, we know that nByte>0 becauses |
73 ** cases where nByte<=0 will have been intercepted by higher-level | 86 ** cases where nByte<=0 will have been intercepted by higher-level |
74 ** routines and redirected to xFree. | 87 ** routines and redirected to xFree. |
75 */ | 88 */ |
76 static void *sqlite3MemRealloc(void *pPrior, int nByte){ | 89 static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
77 sqlite3_int64 *p = (sqlite3_int64*)pPrior; | 90 sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
78 assert( pPrior!=0 && nByte>0 ); | 91 assert( pPrior!=0 && nByte>0 ); |
79 nByte = ROUND8(nByte); | 92 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ |
80 p = (sqlite3_int64*)pPrior; | |
81 p--; | 93 p--; |
82 p = realloc(p, nByte+8 ); | 94 p = realloc(p, nByte+8 ); |
83 if( p ){ | 95 if( p ){ |
84 p[0] = nByte; | 96 p[0] = nByte; |
85 p++; | 97 p++; |
| 98 }else{ |
| 99 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 100 sqlite3_log(SQLITE_NOMEM, |
| 101 "failed memory resize %u to %u bytes", |
| 102 sqlite3MemSize(pPrior), nByte); |
86 } | 103 } |
87 return (void*)p; | 104 return (void*)p; |
88 } | 105 } |
89 | 106 |
90 /* | 107 /* |
91 ** Report the allocated size of a prior return from xMalloc() | |
92 ** or xRealloc(). | |
93 */ | |
94 static int sqlite3MemSize(void *pPrior){ | |
95 sqlite3_int64 *p; | |
96 if( pPrior==0 ) return 0; | |
97 p = (sqlite3_int64*)pPrior; | |
98 p--; | |
99 return (int)p[0]; | |
100 } | |
101 | |
102 /* | |
103 ** Round up a request size to the next valid allocation size. | 108 ** Round up a request size to the next valid allocation size. |
104 */ | 109 */ |
105 static int sqlite3MemRoundup(int n){ | 110 static int sqlite3MemRoundup(int n){ |
106 return ROUND8(n); | 111 return ROUND8(n); |
107 } | 112 } |
108 | 113 |
109 /* | 114 /* |
110 ** Initialize this module. | 115 ** Initialize this module. |
111 */ | 116 */ |
112 static int sqlite3MemInit(void *NotUsed){ | 117 static int sqlite3MemInit(void *NotUsed){ |
(...skipping 23 matching lines...) Expand all Loading... |
136 sqlite3MemSize, | 141 sqlite3MemSize, |
137 sqlite3MemRoundup, | 142 sqlite3MemRoundup, |
138 sqlite3MemInit, | 143 sqlite3MemInit, |
139 sqlite3MemShutdown, | 144 sqlite3MemShutdown, |
140 0 | 145 0 |
141 }; | 146 }; |
142 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); | 147 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); |
143 } | 148 } |
144 | 149 |
145 #endif /* SQLITE_SYSTEM_MALLOC */ | 150 #endif /* SQLITE_SYSTEM_MALLOC */ |
OLD | NEW |