blob: e0d1dd6e8f29983e75989bdfa1174b8a4d7e3212 [file] [log] [blame]
drh90f6a5b2007-08-15 13:04:54 +00001/*
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*************************************************************************
drh90f6a5b2007-08-15 13:04:54 +000012**
drhfec00ea2008-06-14 16:56:21 +000013** 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.
drh90f6a5b2007-08-15 13:04:54 +000019*/
drh0d180202008-02-14 23:26:56 +000020#include "sqliteInt.h"
drh90f6a5b2007-08-15 13:04:54 +000021
22/*
drh4c3645c2007-08-15 17:07:57 +000023** This version of the memory allocator is the default. It is
24** used when no other memory allocator is specified using compile-time
25** macros.
26*/
drh0d180202008-02-14 23:26:56 +000027#ifdef SQLITE_SYSTEM_MALLOC
drh90f6a5b2007-08-15 13:04:54 +000028
drh6a8ab6d2011-11-09 01:53:25 +000029#ifdef HAVE_MALLOC_USABLE_SIZE
30#include <malloc.h>
31#endif
32
drh90f6a5b2007-08-15 13:04:54 +000033/*
drhfec00ea2008-06-14 16:56:21 +000034** Like malloc(), but remember the size of the allocation
35** so that we can find it later using sqlite3MemSize().
36**
37** For this low-level routine, we are guaranteed that nByte>0 because
38** cases of nByte<=0 will be intercepted and dealt with by higher level
39** routines.
drh90f6a5b2007-08-15 13:04:54 +000040*/
drhfec00ea2008-06-14 16:56:21 +000041static void *sqlite3MemMalloc(int nByte){
drh6a8ab6d2011-11-09 01:53:25 +000042#ifdef HAVE_MALLOC_USABLE_SIZE
43 void *p = malloc( nByte );
44 if( p==0 ){
45 testcase( sqlite3GlobalConfig.xLog!=0 );
46 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
47 }
48 return p;
49#else
drh153c62c2007-08-24 03:51:33 +000050 sqlite3_int64 *p;
drhfec00ea2008-06-14 16:56:21 +000051 assert( nByte>0 );
danielk1977bc739712009-03-23 04:33:32 +000052 nByte = ROUND8(nByte);
drhfec00ea2008-06-14 16:56:21 +000053 p = malloc( nByte+8 );
danielk1977950292f2008-06-23 15:10:24 +000054 if( p ){
55 p[0] = nByte;
56 p++;
drh413c3d32010-02-23 20:11:56 +000057 }else{
drhaf46dc12010-02-24 21:44:07 +000058 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +000059 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
danielk1977950292f2008-06-23 15:10:24 +000060 }
61 return (void *)p;
drh6a8ab6d2011-11-09 01:53:25 +000062#endif
drhfec00ea2008-06-14 16:56:21 +000063}
64
65/*
66** Like free() but works for allocations obtained from sqlite3MemMalloc()
67** or sqlite3MemRealloc().
68**
69** For this low-level routine, we already know that pPrior!=0 since
70** cases where pPrior==0 will have been intecepted and dealt with
71** by higher-level routines.
72*/
73static void sqlite3MemFree(void *pPrior){
drh6a8ab6d2011-11-09 01:53:25 +000074#if HAVE_MALLOC_USABLE_SIZE
75 free(pPrior);
76#else
drhfec00ea2008-06-14 16:56:21 +000077 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
danielk1977834a5aa2008-06-23 14:40:18 +000078 assert( pPrior!=0 );
drh90f6a5b2007-08-15 13:04:54 +000079 p--;
drh90f6a5b2007-08-15 13:04:54 +000080 free(p);
drh6a8ab6d2011-11-09 01:53:25 +000081#endif
drh90f6a5b2007-08-15 13:04:54 +000082}
83
84/*
drh413c3d32010-02-23 20:11:56 +000085** Report the allocated size of a prior return from xMalloc()
86** or xRealloc().
87*/
88static int sqlite3MemSize(void *pPrior){
drh6a8ab6d2011-11-09 01:53:25 +000089#if HAVE_MALLOC_USABLE_SIZE
90 return pPrior ? (int)malloc_usable_size(pPrior) : 0;
91#else
drh413c3d32010-02-23 20:11:56 +000092 sqlite3_int64 *p;
93 if( pPrior==0 ) return 0;
94 p = (sqlite3_int64*)pPrior;
95 p--;
96 return (int)p[0];
drh6a8ab6d2011-11-09 01:53:25 +000097#endif
drh413c3d32010-02-23 20:11:56 +000098}
99
100/*
drhfec00ea2008-06-14 16:56:21 +0000101** Like realloc(). Resize an allocation previously obtained from
102** sqlite3MemMalloc().
103**
104** For this low-level interface, we know that pPrior!=0. Cases where
105** pPrior==0 while have been intercepted by higher-level routine and
106** redirected to xMalloc. Similarly, we know that nByte>0 becauses
107** cases where nByte<=0 will have been intercepted by higher-level
108** routines and redirected to xFree.
danielk1977a7a8e142008-02-13 18:25:27 +0000109*/
drhfec00ea2008-06-14 16:56:21 +0000110static void *sqlite3MemRealloc(void *pPrior, int nByte){
drh6a8ab6d2011-11-09 01:53:25 +0000111#if HAVE_MALLOC_USABLE_SIZE
112 void *p = realloc(pPrior, nByte);
113 if( p==0 ){
114 testcase( sqlite3GlobalConfig.xLog!=0 );
115 sqlite3_log(SQLITE_NOMEM,
116 "failed memory resize %u to %u bytes",
117 malloc_usable_size(pPrior), nByte);
118 }
119 return p;
120#else
drhfec00ea2008-06-14 16:56:21 +0000121 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
122 assert( pPrior!=0 && nByte>0 );
drh9f129f42010-08-31 15:27:32 +0000123 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
drhfec00ea2008-06-14 16:56:21 +0000124 p--;
125 p = realloc(p, nByte+8 );
126 if( p ){
127 p[0] = nByte;
128 p++;
drh413c3d32010-02-23 20:11:56 +0000129 }else{
drhaf46dc12010-02-24 21:44:07 +0000130 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000131 sqlite3_log(SQLITE_NOMEM,
132 "failed memory resize %u to %u bytes",
133 sqlite3MemSize(pPrior), nByte);
drhfec00ea2008-06-14 16:56:21 +0000134 }
135 return (void*)p;
drh6a8ab6d2011-11-09 01:53:25 +0000136#endif
danielk1977a7a8e142008-02-13 18:25:27 +0000137}
138
139/*
drhfec00ea2008-06-14 16:56:21 +0000140** Round up a request size to the next valid allocation size.
141*/
142static int sqlite3MemRoundup(int n){
danielk1977bc739712009-03-23 04:33:32 +0000143 return ROUND8(n);
drhfec00ea2008-06-14 16:56:21 +0000144}
145
146/*
147** Initialize this module.
148*/
149static int sqlite3MemInit(void *NotUsed){
danielk197762c14b32008-11-19 09:05:26 +0000150 UNUSED_PARAMETER(NotUsed);
drh40257ff2008-06-13 18:24:27 +0000151 return SQLITE_OK;
152}
153
154/*
drhfec00ea2008-06-14 16:56:21 +0000155** Deinitialize this module.
drh90f6a5b2007-08-15 13:04:54 +0000156*/
drhfec00ea2008-06-14 16:56:21 +0000157static void sqlite3MemShutdown(void *NotUsed){
danielk197762c14b32008-11-19 09:05:26 +0000158 UNUSED_PARAMETER(NotUsed);
drhfec00ea2008-06-14 16:56:21 +0000159 return;
160}
161
drhd1370b62008-10-28 18:58:20 +0000162/*
163** This routine is the only routine in this file with external linkage.
164**
165** Populate the low-level memory allocation function pointers in
166** sqlite3GlobalConfig.m with pointers to the routines in this file.
167*/
168void sqlite3MemSetDefault(void){
drhfec00ea2008-06-14 16:56:21 +0000169 static const sqlite3_mem_methods defaultMethods = {
170 sqlite3MemMalloc,
171 sqlite3MemFree,
172 sqlite3MemRealloc,
173 sqlite3MemSize,
174 sqlite3MemRoundup,
175 sqlite3MemInit,
176 sqlite3MemShutdown,
177 0
178 };
drhd1370b62008-10-28 18:58:20 +0000179 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
drh90f6a5b2007-08-15 13:04:54 +0000180}
drh4c3645c2007-08-15 17:07:57 +0000181
drh0d180202008-02-14 23:26:56 +0000182#endif /* SQLITE_SYSTEM_MALLOC */