xref: /sqlite-3.40.0/src/mem1.c (revision fec00eab)
190f6a5beSdrh /*
290f6a5beSdrh ** 2007 August 14
390f6a5beSdrh **
490f6a5beSdrh ** The author disclaims copyright to this source code.  In place of
590f6a5beSdrh ** a legal notice, here is a blessing:
690f6a5beSdrh **
790f6a5beSdrh **    May you do good and not evil.
890f6a5beSdrh **    May you find forgiveness for yourself and forgive others.
990f6a5beSdrh **    May you share freely, never taking more than you give.
1090f6a5beSdrh **
1190f6a5beSdrh *************************************************************************
1290f6a5beSdrh **
13*fec00eabSdrh ** This file contains low-level memory allocation drivers for when
14*fec00eabSdrh ** SQLite will use the standard C-library malloc/realloc/free interface
15*fec00eabSdrh ** to obtain the memory it needs.
16*fec00eabSdrh **
17*fec00eabSdrh ** This file contains implementations of the low-level memory allocation
18*fec00eabSdrh ** routines specified in the sqlite3_mem_methods object.
19*fec00eabSdrh **
20*fec00eabSdrh ** $Id: mem1.c,v 1.19 2008/06/14 16:56:22 drh Exp $
2190f6a5beSdrh */
220d18020bSdrh #include "sqliteInt.h"
2390f6a5beSdrh 
2490f6a5beSdrh /*
254c3645c6Sdrh ** This version of the memory allocator is the default.  It is
264c3645c6Sdrh ** used when no other memory allocator is specified using compile-time
274c3645c6Sdrh ** macros.
284c3645c6Sdrh */
290d18020bSdrh #ifdef SQLITE_SYSTEM_MALLOC
3090f6a5beSdrh 
3190f6a5beSdrh /*
32*fec00eabSdrh ** Like malloc(), but remember the size of the allocation
33*fec00eabSdrh ** so that we can find it later using sqlite3MemSize().
34*fec00eabSdrh **
35*fec00eabSdrh ** For this low-level routine, we are guaranteed that nByte>0 because
36*fec00eabSdrh ** cases of nByte<=0 will be intercepted and dealt with by higher level
37*fec00eabSdrh ** routines.
3890f6a5beSdrh */
39*fec00eabSdrh static void *sqlite3MemMalloc(int nByte){
40*fec00eabSdrh   sqlite3_int64 *p;
41*fec00eabSdrh   assert( nByte>0 );
42*fec00eabSdrh   nByte = (nByte+7)&~7;
43*fec00eabSdrh   p = malloc( nByte+8 );
44*fec00eabSdrh   p[0] = nByte;
45*fec00eabSdrh   return (void*)&p[1];
46ca0c8971Sdanielk1977 }
4790f6a5beSdrh 
4890f6a5beSdrh /*
49*fec00eabSdrh ** Like free() but works for allocations obtained from sqlite3MemMalloc()
50*fec00eabSdrh ** or sqlite3MemRealloc().
51*fec00eabSdrh **
52*fec00eabSdrh ** For this low-level routine, we already know that pPrior!=0 since
53*fec00eabSdrh ** cases where pPrior==0 will have been intecepted and dealt with
54*fec00eabSdrh ** by higher-level routines.
5590f6a5beSdrh */
56*fec00eabSdrh static void sqlite3MemFree(void *pPrior){
57*fec00eabSdrh   assert( pPrior!=0 );
58*fec00eabSdrh   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
59*fec00eabSdrh   p--;
60*fec00eabSdrh   free(p);
6190f6a5beSdrh }
6290f6a5beSdrh 
6390f6a5beSdrh /*
64*fec00eabSdrh ** Like realloc().  Resize an allocation previously obtained from
65*fec00eabSdrh ** sqlite3MemMalloc().
66*fec00eabSdrh **
67*fec00eabSdrh ** For this low-level interface, we know that pPrior!=0.  Cases where
68*fec00eabSdrh ** pPrior==0 while have been intercepted by higher-level routine and
69*fec00eabSdrh ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
70*fec00eabSdrh ** cases where nByte<=0 will have been intercepted by higher-level
71*fec00eabSdrh ** routines and redirected to xFree.
7290f6a5beSdrh */
73*fec00eabSdrh static void *sqlite3MemRealloc(void *pPrior, int nByte){
74*fec00eabSdrh   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
75*fec00eabSdrh   assert( pPrior!=0 && nByte>0 );
76*fec00eabSdrh   nByte = (nByte+7)&~7;
77*fec00eabSdrh   p = (sqlite3_int64*)pPrior;
78*fec00eabSdrh   p--;
79*fec00eabSdrh   p = realloc(p, nByte+8 );
8090f6a5beSdrh   if( p ){
81*fec00eabSdrh     p[0] = nByte;
8290f6a5beSdrh     p++;
83ca0c8971Sdanielk1977   }
8490f6a5beSdrh   return (void*)p;
8590f6a5beSdrh }
8690f6a5beSdrh 
8790f6a5beSdrh /*
88*fec00eabSdrh ** Report the allocated size of a prior return from xMalloc()
89*fec00eabSdrh ** or xRealloc().
9090f6a5beSdrh */
91*fec00eabSdrh static int sqlite3MemSize(void *pPrior){
92153c62c4Sdrh   sqlite3_int64 *p;
93*fec00eabSdrh   if( pPrior==0 ) return 0;
94*fec00eabSdrh   p = (sqlite3_int64*)pPrior;
95*fec00eabSdrh   p--;
96*fec00eabSdrh   return p[0];
97*fec00eabSdrh }
98*fec00eabSdrh 
99*fec00eabSdrh /*
100*fec00eabSdrh ** Round up a request size to the next valid allocation size.
101*fec00eabSdrh */
102*fec00eabSdrh static int sqlite3MemRoundup(int n){
103*fec00eabSdrh   return (n+7) & ~7;
104*fec00eabSdrh }
105*fec00eabSdrh 
106*fec00eabSdrh /*
107*fec00eabSdrh ** Initialize this module.
108*fec00eabSdrh */
109*fec00eabSdrh static int sqlite3MemInit(void *NotUsed){
110*fec00eabSdrh   return SQLITE_OK;
111*fec00eabSdrh }
112*fec00eabSdrh 
113*fec00eabSdrh /*
114*fec00eabSdrh ** Deinitialize this module.
115*fec00eabSdrh */
116*fec00eabSdrh static void sqlite3MemShutdown(void *NotUsed){
11790f6a5beSdrh   return;
11890f6a5beSdrh }
11990f6a5beSdrh 
12090f6a5beSdrh /*
121*fec00eabSdrh ** This routine is the only routine in this file with external linkage.
122*fec00eabSdrh **
123*fec00eabSdrh ** Populate the low-level memory allocation function pointers in
124*fec00eabSdrh ** sqlite3Config.m with pointers to the routines in this file.
125a7a8e14bSdanielk1977 */
126*fec00eabSdrh void sqlite3MemSetDefault(void){
127*fec00eabSdrh   static const sqlite3_mem_methods defaultMethods = {
128*fec00eabSdrh      sqlite3MemMalloc,
129*fec00eabSdrh      sqlite3MemFree,
130*fec00eabSdrh      sqlite3MemRealloc,
131*fec00eabSdrh      sqlite3MemSize,
132*fec00eabSdrh      sqlite3MemRoundup,
133*fec00eabSdrh      sqlite3MemInit,
134*fec00eabSdrh      sqlite3MemShutdown,
135*fec00eabSdrh      0
136*fec00eabSdrh   };
137*fec00eabSdrh   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
13890f6a5beSdrh }
1394c3645c6Sdrh 
1400d18020bSdrh #endif /* SQLITE_SYSTEM_MALLOC */
141