blob: 1a0183999278bdee66f4bc37f7293cb2bd071dcd [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
drh90f6a5b2007-08-15 13:04:54 +000029/*
drhfec00ea2008-06-14 16:56:21 +000030** Like malloc(), but remember the size of the allocation
31** so that we can find it later using sqlite3MemSize().
32**
33** For this low-level routine, we are guaranteed that nByte>0 because
34** cases of nByte<=0 will be intercepted and dealt with by higher level
35** routines.
drh90f6a5b2007-08-15 13:04:54 +000036*/
drhfec00ea2008-06-14 16:56:21 +000037static void *sqlite3MemMalloc(int nByte){
drh153c62c2007-08-24 03:51:33 +000038 sqlite3_int64 *p;
drhfec00ea2008-06-14 16:56:21 +000039 assert( nByte>0 );
danielk1977bc739712009-03-23 04:33:32 +000040 nByte = ROUND8(nByte);
drhfec00ea2008-06-14 16:56:21 +000041 p = malloc( nByte+8 );
danielk1977950292f2008-06-23 15:10:24 +000042 if( p ){
43 p[0] = nByte;
44 p++;
drh413c3d32010-02-23 20:11:56 +000045 }else{
drhaf46dc12010-02-24 21:44:07 +000046 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +000047 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
danielk1977950292f2008-06-23 15:10:24 +000048 }
49 return (void *)p;
drhfec00ea2008-06-14 16:56:21 +000050}
51
52/*
53** Like free() but works for allocations obtained from sqlite3MemMalloc()
54** or sqlite3MemRealloc().
55**
56** For this low-level routine, we already know that pPrior!=0 since
57** cases where pPrior==0 will have been intecepted and dealt with
58** by higher-level routines.
59*/
60static void sqlite3MemFree(void *pPrior){
drhfec00ea2008-06-14 16:56:21 +000061 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
danielk1977834a5aa2008-06-23 14:40:18 +000062 assert( pPrior!=0 );
drh90f6a5b2007-08-15 13:04:54 +000063 p--;
drh90f6a5b2007-08-15 13:04:54 +000064 free(p);
drh90f6a5b2007-08-15 13:04:54 +000065}
66
67/*
drh413c3d32010-02-23 20:11:56 +000068** Report the allocated size of a prior return from xMalloc()
69** or xRealloc().
70*/
71static 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/*
drhfec00ea2008-06-14 16:56:21 +000080** Like realloc(). Resize an allocation previously obtained from
81** sqlite3MemMalloc().
82**
83** For this low-level interface, we know that pPrior!=0. Cases where
84** pPrior==0 while have been intercepted by higher-level routine and
85** redirected to xMalloc. Similarly, we know that nByte>0 becauses
86** cases where nByte<=0 will have been intercepted by higher-level
87** routines and redirected to xFree.
danielk1977a7a8e142008-02-13 18:25:27 +000088*/
drhfec00ea2008-06-14 16:56:21 +000089static void *sqlite3MemRealloc(void *pPrior, int nByte){
90 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
91 assert( pPrior!=0 && nByte>0 );
danielk1977bc739712009-03-23 04:33:32 +000092 nByte = ROUND8(nByte);
drhfec00ea2008-06-14 16:56:21 +000093 p--;
94 p = realloc(p, nByte+8 );
95 if( p ){
96 p[0] = nByte;
97 p++;
drh413c3d32010-02-23 20:11:56 +000098 }else{
drhaf46dc12010-02-24 21:44:07 +000099 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000100 sqlite3_log(SQLITE_NOMEM,
101 "failed memory resize %u to %u bytes",
102 sqlite3MemSize(pPrior), nByte);
drhfec00ea2008-06-14 16:56:21 +0000103 }
104 return (void*)p;
danielk1977a7a8e142008-02-13 18:25:27 +0000105}
106
107/*
drhfec00ea2008-06-14 16:56:21 +0000108** Round up a request size to the next valid allocation size.
109*/
110static int sqlite3MemRoundup(int n){
danielk1977bc739712009-03-23 04:33:32 +0000111 return ROUND8(n);
drhfec00ea2008-06-14 16:56:21 +0000112}
113
114/*
115** Initialize this module.
116*/
117static int sqlite3MemInit(void *NotUsed){
danielk197762c14b32008-11-19 09:05:26 +0000118 UNUSED_PARAMETER(NotUsed);
drh40257ff2008-06-13 18:24:27 +0000119 return SQLITE_OK;
120}
121
122/*
drhfec00ea2008-06-14 16:56:21 +0000123** Deinitialize this module.
drh90f6a5b2007-08-15 13:04:54 +0000124*/
drhfec00ea2008-06-14 16:56:21 +0000125static void sqlite3MemShutdown(void *NotUsed){
danielk197762c14b32008-11-19 09:05:26 +0000126 UNUSED_PARAMETER(NotUsed);
drhfec00ea2008-06-14 16:56:21 +0000127 return;
128}
129
drhd1370b62008-10-28 18:58:20 +0000130/*
131** This routine is the only routine in this file with external linkage.
132**
133** Populate the low-level memory allocation function pointers in
134** sqlite3GlobalConfig.m with pointers to the routines in this file.
135*/
136void sqlite3MemSetDefault(void){
drhfec00ea2008-06-14 16:56:21 +0000137 static const sqlite3_mem_methods defaultMethods = {
138 sqlite3MemMalloc,
139 sqlite3MemFree,
140 sqlite3MemRealloc,
141 sqlite3MemSize,
142 sqlite3MemRoundup,
143 sqlite3MemInit,
144 sqlite3MemShutdown,
145 0
146 };
drhd1370b62008-10-28 18:58:20 +0000147 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
drh90f6a5b2007-08-15 13:04:54 +0000148}
drh4c3645c2007-08-15 17:07:57 +0000149
drh0d180202008-02-14 23:26:56 +0000150#endif /* SQLITE_SYSTEM_MALLOC */