blob: 7522fc8d2b47605d3db8d0093db012b7be56f036 [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.
19**
danielk197762c14b32008-11-19 09:05:26 +000020** $Id: mem1.c,v 1.28 2008/11/19 09:05:27 danielk1977 Exp $
drh90f6a5b2007-08-15 13:04:54 +000021*/
drh0d180202008-02-14 23:26:56 +000022#include "sqliteInt.h"
drh90f6a5b2007-08-15 13:04:54 +000023
24/*
drh4c3645c2007-08-15 17:07:57 +000025** This version of the memory allocator is the default. It is
26** used when no other memory allocator is specified using compile-time
27** macros.
28*/
drh0d180202008-02-14 23:26:56 +000029#ifdef SQLITE_SYSTEM_MALLOC
drh90f6a5b2007-08-15 13:04:54 +000030
drh90f6a5b2007-08-15 13:04:54 +000031/*
drhfec00ea2008-06-14 16:56:21 +000032** Like malloc(), but remember the size of the allocation
33** so that we can find it later using sqlite3MemSize().
34**
35** 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
37** routines.
drh90f6a5b2007-08-15 13:04:54 +000038*/
drhfec00ea2008-06-14 16:56:21 +000039static void *sqlite3MemMalloc(int nByte){
drh153c62c2007-08-24 03:51:33 +000040 sqlite3_int64 *p;
drhfec00ea2008-06-14 16:56:21 +000041 assert( nByte>0 );
42 nByte = (nByte+7)&~7;
43 p = malloc( nByte+8 );
danielk1977950292f2008-06-23 15:10:24 +000044 if( p ){
45 p[0] = nByte;
46 p++;
47 }
48 return (void *)p;
drhfec00ea2008-06-14 16:56:21 +000049}
50
51/*
52** Like free() but works for allocations obtained from sqlite3MemMalloc()
53** or sqlite3MemRealloc().
54**
55** 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** by higher-level routines.
58*/
59static void sqlite3MemFree(void *pPrior){
drhfec00ea2008-06-14 16:56:21 +000060 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
danielk1977834a5aa2008-06-23 14:40:18 +000061 assert( pPrior!=0 );
drh90f6a5b2007-08-15 13:04:54 +000062 p--;
drh90f6a5b2007-08-15 13:04:54 +000063 free(p);
drh90f6a5b2007-08-15 13:04:54 +000064}
65
66/*
drhfec00ea2008-06-14 16:56:21 +000067** Like realloc(). Resize an allocation previously obtained from
68** sqlite3MemMalloc().
69**
70** For this low-level interface, we know that pPrior!=0. Cases where
71** pPrior==0 while have been intercepted by higher-level routine and
72** redirected to xMalloc. Similarly, we know that nByte>0 becauses
73** cases where nByte<=0 will have been intercepted by higher-level
74** routines and redirected to xFree.
danielk1977a7a8e142008-02-13 18:25:27 +000075*/
drhfec00ea2008-06-14 16:56:21 +000076static void *sqlite3MemRealloc(void *pPrior, int nByte){
77 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
78 assert( pPrior!=0 && nByte>0 );
79 nByte = (nByte+7)&~7;
80 p = (sqlite3_int64*)pPrior;
81 p--;
82 p = realloc(p, nByte+8 );
83 if( p ){
84 p[0] = nByte;
85 p++;
86 }
87 return (void*)p;
danielk1977a7a8e142008-02-13 18:25:27 +000088}
89
90/*
drhfec00ea2008-06-14 16:56:21 +000091** Report the allocated size of a prior return from xMalloc()
92** or xRealloc().
drh40257ff2008-06-13 18:24:27 +000093*/
drhfec00ea2008-06-14 16:56:21 +000094static int sqlite3MemSize(void *pPrior){
95 sqlite3_int64 *p;
96 if( pPrior==0 ) return 0;
97 p = (sqlite3_int64*)pPrior;
98 p--;
99 return p[0];
100}
101
102/*
103** Round up a request size to the next valid allocation size.
104*/
105static int sqlite3MemRoundup(int n){
106 return (n+7) & ~7;
107}
108
109/*
110** Initialize this module.
111*/
112static int sqlite3MemInit(void *NotUsed){
danielk197762c14b32008-11-19 09:05:26 +0000113 UNUSED_PARAMETER(NotUsed);
drh40257ff2008-06-13 18:24:27 +0000114 return SQLITE_OK;
115}
116
117/*
drhfec00ea2008-06-14 16:56:21 +0000118** Deinitialize this module.
drh90f6a5b2007-08-15 13:04:54 +0000119*/
drhfec00ea2008-06-14 16:56:21 +0000120static void sqlite3MemShutdown(void *NotUsed){
danielk197762c14b32008-11-19 09:05:26 +0000121 UNUSED_PARAMETER(NotUsed);
drhfec00ea2008-06-14 16:56:21 +0000122 return;
123}
124
drhd1370b62008-10-28 18:58:20 +0000125/*
126** This routine is the only routine in this file with external linkage.
127**
128** Populate the low-level memory allocation function pointers in
129** sqlite3GlobalConfig.m with pointers to the routines in this file.
130*/
131void sqlite3MemSetDefault(void){
drhfec00ea2008-06-14 16:56:21 +0000132 static const sqlite3_mem_methods defaultMethods = {
133 sqlite3MemMalloc,
134 sqlite3MemFree,
135 sqlite3MemRealloc,
136 sqlite3MemSize,
137 sqlite3MemRoundup,
138 sqlite3MemInit,
139 sqlite3MemShutdown,
140 0
141 };
drhd1370b62008-10-28 18:58:20 +0000142 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
drh90f6a5b2007-08-15 13:04:54 +0000143}
drh4c3645c2007-08-15 17:07:57 +0000144
drh0d180202008-02-14 23:26:56 +0000145#endif /* SQLITE_SYSTEM_MALLOC */