SQLite4
Check-in [69b21857ca]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Remove obsolete memory pool allocators from LSM.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 69b21857ca4728ebad02dfdd77f168f6f5479b6a
User & Date: drh 2013-01-01 19:55:16
Context
2013-01-09
18:15
Merge matchinfo branch with trunk. check-in: dbbce4e438 user: dan tags: trunk
2013-01-01
19:55
Remove obsolete memory pool allocators from LSM. check-in: 69b21857ca user: drh tags: trunk
19:02
Add the sqlite4_kvfactory typedef to the interface and use that typedef throughout the implementation. This check-in also includes some unrelated cleanup of the sqlite4.h file. check-in: a2630e5d90 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/lsmInt.h.

591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
int lsmTreeCursorSave(TreeCursor *pCsr);

void lsmFlagsToString(int flags, char *zFlags);

/* 
** Functions from file "mem.c".
*/
int lsmPoolNew(lsm_env *pEnv, Mempool **ppPool);
void lsmPoolDestroy(lsm_env *pEnv, Mempool *pPool);
void *lsmPoolMalloc(lsm_env *pEnv, Mempool *pPool, int nByte);
void *lsmPoolMallocZero(lsm_env *pEnv, Mempool *pPool, int nByte);
int lsmPoolUsed(Mempool *pPool);

void lsmPoolMark(Mempool *pPool, void **, int *);
void lsmPoolRollback(lsm_env *pEnv, Mempool *pPool, void *, int);

void *lsmMalloc(lsm_env*, size_t);
void lsmFree(lsm_env*, void *);
void *lsmRealloc(lsm_env*, void *, size_t);
void *lsmReallocOrFree(lsm_env*, void *, size_t);
void *lsmReallocOrFreeRc(lsm_env *, void *, size_t, int *);

void *lsmMallocZeroRc(lsm_env*, size_t, int *);







<
<
<
<
<
<
<
<
<







591
592
593
594
595
596
597









598
599
600
601
602
603
604
int lsmTreeCursorSave(TreeCursor *pCsr);

void lsmFlagsToString(int flags, char *zFlags);

/* 
** Functions from file "mem.c".
*/









void *lsmMalloc(lsm_env*, size_t);
void lsmFree(lsm_env*, void *);
void *lsmRealloc(lsm_env*, void *, size_t);
void *lsmReallocOrFree(lsm_env*, void *, size_t);
void *lsmReallocOrFreeRc(lsm_env *, void *, size_t, int *);

void *lsmMallocZeroRc(lsm_env*, size_t, int *);

Changes to src/lsm_mem.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
**
*************************************************************************
**
** Helper routines for memory allocation.
*/
#include "lsmInt.h"

/* Default allocation size. */
#define CHUNKSIZE 16*1024

typedef struct Chunk Chunk;

struct Chunk {
  int iOff;                       /* Offset of free space within pSpace */
  u8 *aData;                      /* Pointer to space for user allocations */
  int nData;                      /* Size of buffer aData, in bytes */
  Chunk *pNext;
};

struct Mempool {
  Chunk *pFirst;                  /* First in list of chunks */
  Chunk *pLast;                   /* Last in list of chunks */
  int nUsed;                      /* Total number of bytes allocated */
};

/*
** The following routines are called internally by LSM sub-routines. In
** this case a valid environment pointer must be supplied.
*/
void *lsmMalloc(lsm_env *pEnv, size_t N){
  assert( pEnv );
  return pEnv->xMalloc(pEnv, N);
................................................................................
  nByte = strlen(zIn);
  zRet = lsmMalloc(pEnv, nByte+1);
  if( zRet ){
    memcpy(zRet, zIn, nByte+1);
  }
  return zRet;
}


/*
** Allocate a new Chunk structure (using lsmMalloc()).
*/
static Chunk * poolChunkNew(lsm_env *pEnv, int nMin){
  Chunk *pChunk;
  int nAlloc = LSM_MAX(CHUNKSIZE, nMin + sizeof(Chunk));

  pChunk = (Chunk *)lsmMalloc(pEnv, nAlloc);
  if( pChunk ){
    pChunk->pNext = 0;
    pChunk->iOff = 0;
    pChunk->aData = (u8 *)&pChunk[1];
    pChunk->nData = nAlloc - sizeof(Chunk);
  }

  return pChunk;
}

/*
** Allocate sz bytes from chunk pChunk.
*/
static u8 *poolChunkAlloc(Chunk *pChunk, int sz){
  u8 *pRet;                       /* Pointer value to return */
  assert( sz<=(pChunk->nData - pChunk->iOff) );
  pRet = &pChunk->aData[pChunk->iOff];
  pChunk->iOff += sz;
  return pRet;
}


int lsmPoolNew(lsm_env *pEnv, Mempool **ppPool){
  int rc = LSM_NOMEM;
  Mempool *pPool = 0;
  Chunk *pChunk;

  pChunk = poolChunkNew(pEnv, sizeof(Mempool));
  if( pChunk ){
    pPool = (Mempool *)poolChunkAlloc(pChunk, sizeof(Mempool));
    pPool->pFirst = pChunk;
    pPool->pLast = pChunk;
    pPool->nUsed = 0;
    rc = LSM_OK;
  }

  *ppPool = pPool;
  return rc;
}

void lsmPoolDestroy(lsm_env *pEnv, Mempool *pPool){
  if( pPool ){
    Chunk *pChunk = pPool->pFirst;
    while( pChunk ){
      Chunk *pFree = pChunk;
      pChunk = pChunk->pNext;
      lsmFree(pEnv, pFree);
    }
  }
}

void *lsmPoolMalloc(lsm_env *pEnv, Mempool *pPool, int nByte){
  u8 *pRet = 0;
  Chunk *pLast = pPool->pLast;

  nByte = ROUND8(nByte);
  if( nByte > (pLast->nData - pLast->iOff) ){
    Chunk *pNew = poolChunkNew(pEnv, nByte);
    if( !pNew ) return 0;
    pLast->pNext = pNew;
    pPool->pLast = pNew;
    pLast = pNew;
  }

  if( pLast ){
    pRet = poolChunkAlloc(pLast, nByte);
    pPool->nUsed += nByte;
  }
  return (void *)pRet;
}

void *lsmPoolMallocZero(lsm_env *pEnv, Mempool *pPool, int nByte){
  void *pRet = lsmPoolMalloc(pEnv, pPool, nByte);
  if( pRet ) memset(pRet, 0, nByte);
  return pRet;
}

/*
** Return the amount of memory currently allocated from this pool.
*/
int lsmPoolUsed(Mempool *pPool){
  return pPool->nUsed;
}

void lsmPoolMark(Mempool *pPool, void **ppChunk, int *piOff){
  *ppChunk = (void *)pPool->pLast;
  *piOff = pPool->pLast->iOff;
}

void lsmPoolRollback(lsm_env *pEnv, Mempool *pPool, void *pChunk, int iOff){
  Chunk *pLast = (Chunk *)pChunk;
  Chunk *p;
  Chunk *pNext;

#ifdef LSM_EXPENSIVE_DEBUG
  /* Check that pLast is actually in the list of chunks */
  for(p=pPool->pFirst; p!=pLast; p=p->pNext);
#endif

  pPool->nUsed -= (pLast->iOff - iOff);
  for(p=pLast->pNext; p; p=pNext){
    pPool->nUsed -= p->iOff;
    pNext = p->pNext;
    lsmFree(pEnv, p);
  }

  pLast->pNext = 0;
  pLast->iOff = iOff;
  pPool->pLast = pLast;
}







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
10
11
12
13
14
15
16


















17
18
19
20
21
22
23
..
98
99
100
101
102
103
104
























































































































**
*************************************************************************
**
** Helper routines for memory allocation.
*/
#include "lsmInt.h"



















/*
** The following routines are called internally by LSM sub-routines. In
** this case a valid environment pointer must be supplied.
*/
void *lsmMalloc(lsm_env *pEnv, size_t N){
  assert( pEnv );
  return pEnv->xMalloc(pEnv, N);
................................................................................
  nByte = strlen(zIn);
  zRet = lsmMalloc(pEnv, nByte+1);
  if( zRet ){
    memcpy(zRet, zIn, nByte+1);
  }
  return zRet;
}