/ Check-in [7fca5a28]
Login

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

Overview
Comment:Add error logging to native Win32 heap support.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | winNativeHeap
Files: files | file ages | folders
SHA1: 7fca5a284cded6d7531060da6e99a57aed50cf8f
User & Date: mistachkin 2011-08-24 17:42:22
Context
2011-08-25
01:16
Make sure that SQLITE_FCNTL_SIZE_HINT on Windows does not shrink the file. check-in: d4f6437f user: mistachkin tags: winNativeHeap
2011-08-24
17:42
Add error logging to native Win32 heap support. check-in: 7fca5a28 user: mistachkin tags: winNativeHeap
16:13
Experimental work to allow SQLite to use the native Win32 heap API. check-in: bf3d0ab5 user: mistachkin tags: winNativeHeap
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Makefile.msc.

48
49
50
51
52
53
54

55
56
57
58
59
60
61
#
TCC = $(TCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS

#
# Use native Win32 heap.
#
TCC = $(TCC) -DSQLITE_WIN32_MALLOC=1


# The locations of the Tcl header and library files.  Also, the library that
# non-stubs enabled programs using Tcl must link against.  These variables
# (TCLINCDIR, TCLLIBDIR, and LIBTCL) may be overridden via the environment
# prior to running nmake in order to match the actual installed location and
# version on this machine.
#







>







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
TCC = $(TCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS

#
# Use native Win32 heap.
#
TCC = $(TCC) -DSQLITE_WIN32_MALLOC=1
# TCC = $(TCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1

# The locations of the Tcl header and library files.  Also, the library that
# non-stubs enabled programs using Tcl must link against.  These variables
# (TCLINCDIR, TCLLIBDIR, and LIBTCL) may be overridden via the environment
# prior to running nmake in order to match the actual installed location and
# version on this machine.
#

Changes to src/os_win.c.

150
151
152
153
154
155
156



157
158
159
160
161
162
163
...
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
243
244
245
246
247
248
249
250
251



252
253
254
255
256
257
258

259
260

261
262
263
264
265
266
267

268

269







270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288




289
290
291
292
293
294
295
...
298
299
300
301
302
303
304
305
306
307
308
309
310




311
312
313
314
315
316
317
...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

338
339
340
341
342
343
344
  BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
};

#define WINMEM_MAGIC     0x42b2830b

static struct winMemData win_mem_data = { WINMEM_MAGIC, NULL, FALSE };




static void *winMemMalloc(int nBytes);
static void winMemFree(void *pPrior);
static void *winMemRealloc(void *pPrior, int nBytes);
static int winMemSize(void *p);
static int winMemRoundup(int n);
static int winMemInit(void *pAppData);
static void winMemShutdown(void *pAppData);
................................................................................

#ifdef SQLITE_WIN32_MALLOC
/*
** Allocate nBytes of memory.
*/
static void *winMemMalloc(int nBytes){
  HANDLE hHeap;


  assert( win_mem_data.magic==WINMEM_MAGIC );

  hHeap = win_mem_data.hHeap;
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, NULL) );
#endif
  assert( nBytes>=0 );
  return HeapAlloc(hHeap, 0, (SIZE_T)nBytes);





}

/*
** Free memory.
*/
static void winMemFree(void *pPrior){
  HANDLE hHeap;

  assert( win_mem_data.magic==WINMEM_MAGIC );
  hHeap = win_mem_data.hHeap;
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, pPrior) );
#endif
  if (!pPrior) return; /* Passing NULL to HeapFree is undefined. */
  HeapFree(hHeap, 0, pPrior);



}

/*
** Change the size of an existing memory allocation
*/
static void *winMemRealloc(void *pPrior, int nBytes){
  HANDLE hHeap;


  assert( win_mem_data.magic==WINMEM_MAGIC );

  hHeap = win_mem_data.hHeap;
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, pPrior) );
#endif
  assert( nBytes>=0 );

  if (!pPrior) return HeapAlloc(hHeap, 0, (SIZE_T)nBytes);

  return HeapReAlloc(hHeap, 0, pPrior, (SIZE_T)nBytes);







}

/*
** Return the size of an outstanding allocation, in bytes.
*/
static int winMemSize(void *p){
  HANDLE hHeap;
  SIZE_T n;

  assert( win_mem_data.magic==WINMEM_MAGIC );
  hHeap = win_mem_data.hHeap;
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, NULL) );
#endif
  if (!p) return 0;
  n = HeapSize(hHeap, 0, p);
  assert( n<=INT_MAX );




  return (int)n;
}

/*
** Round up a request size to the next valid allocation size.
*/
static int winMemRoundup(int n){
................................................................................

/*
** Initialize this module.
*/
static int winMemInit(void *pAppData){
  winMemData *pWinMemData = (winMemData *)pAppData;

  if (!pWinMemData) return SQLITE_ERROR;
  assert( pWinMemData->magic==WINMEM_MAGIC );
  if (!pWinMemData->hHeap){
    pWinMemData->hHeap = HeapCreate(0, SQLITE_WIN32_HEAP_INIT_SIZE,
                                    SQLITE_WIN32_HEAP_MAX_SIZE);
    if (!pWinMemData->hHeap){




      return SQLITE_NOMEM;
    }
    pWinMemData->bOwned = TRUE;
  }
  assert( pWinMemData->hHeap!=0 );
  assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
................................................................................

/*
** Deinitialize this module.
*/
static void winMemShutdown(void *pAppData){
  winMemData *pWinMemData = (winMemData *)pAppData;

  if (!pWinMemData) return;
  if (pWinMemData->hHeap){
    assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
    assert( HeapValidate(pWinMemData->hHeap, 0, NULL) );
#endif
    if (pWinMemData->bOwned){
      if (!HeapDestroy(pWinMemData->hHeap)){
        /* TODO: Log this? */

      }
      pWinMemData->bOwned = FALSE;
    }
    pWinMemData->hHeap = NULL;
  }
}








>
>
>







 







>

<
>
|






|
>
>
>
>
>








|
|





|
|
>
>
>







>

<
>
|






>
|
>
|
>
>
>
>
>
>
>









|
|





|

|
>
>
>
>







 







|

|


|
>
>
>
>







 







|
|




|
|
|
>







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
...
221
222
223
224
225
226
227
228
229

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
};

#define WINMEM_MAGIC     0x42b2830b

static struct winMemData win_mem_data = { WINMEM_MAGIC, NULL, FALSE };

#define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
#define winMemGetHeap() win_mem_data.hHeap

static void *winMemMalloc(int nBytes);
static void winMemFree(void *pPrior);
static void *winMemRealloc(void *pPrior, int nBytes);
static int winMemSize(void *p);
static int winMemRoundup(int n);
static int winMemInit(void *pAppData);
static void winMemShutdown(void *pAppData);
................................................................................

#ifdef SQLITE_WIN32_MALLOC
/*
** Allocate nBytes of memory.
*/
static void *winMemMalloc(int nBytes){
  HANDLE hHeap;
  void *p;


  winMemAssertMagic();
  hHeap = winMemGetHeap();
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, NULL) );
#endif
  assert( nBytes>=0 );
  p = HeapAlloc(hHeap, 0, (SIZE_T)nBytes);
  if( !p ){
    sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
        nBytes, GetLastError(), (void*)hHeap);
  }
  return p;
}

/*
** Free memory.
*/
static void winMemFree(void *pPrior){
  HANDLE hHeap;

  winMemAssertMagic();
  hHeap = winMemGetHeap();
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, pPrior) );
#endif
  if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
  if( !HeapFree(hHeap, 0, pPrior) ){
    sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
        pPrior, GetLastError(), (void*)hHeap);
  }
}

/*
** Change the size of an existing memory allocation
*/
static void *winMemRealloc(void *pPrior, int nBytes){
  HANDLE hHeap;
  void *p;


  winMemAssertMagic();
  hHeap = winMemGetHeap();
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, pPrior) );
#endif
  assert( nBytes>=0 );
  if( !pPrior ){
    p = HeapAlloc(hHeap, 0, (SIZE_T)nBytes);
  }else{
    p = HeapReAlloc(hHeap, 0, pPrior, (SIZE_T)nBytes);
  }
  if( !p ){
    sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
        pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(),
        (void*)hHeap);
  }
  return p;
}

/*
** Return the size of an outstanding allocation, in bytes.
*/
static int winMemSize(void *p){
  HANDLE hHeap;
  SIZE_T n;

  winMemAssertMagic();
  hHeap = winMemGetHeap();
  assert( hHeap!=0 );
  assert( hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
  assert ( HeapValidate(hHeap, 0, NULL) );
#endif
  if( !p ) return 0;
  n = HeapSize(hHeap, 0, p);
  if( n==(SIZE_T)-1 ){
    sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
        p, GetLastError(), (void*)hHeap);
    return 0;
  }
  return (int)n;
}

/*
** Round up a request size to the next valid allocation size.
*/
static int winMemRoundup(int n){
................................................................................

/*
** Initialize this module.
*/
static int winMemInit(void *pAppData){
  winMemData *pWinMemData = (winMemData *)pAppData;

  if( !pWinMemData ) return SQLITE_ERROR;
  assert( pWinMemData->magic==WINMEM_MAGIC );
  if( !pWinMemData->hHeap ){
    pWinMemData->hHeap = HeapCreate(0, SQLITE_WIN32_HEAP_INIT_SIZE,
                                    SQLITE_WIN32_HEAP_MAX_SIZE);
    if( !pWinMemData->hHeap ){
      sqlite3_log(SQLITE_NOMEM,
          "failed to HeapCreate (%d), initSize=%u, maxSize=%u",
          GetLastError(), SQLITE_WIN32_HEAP_INIT_SIZE,
          SQLITE_WIN32_HEAP_MAX_SIZE);
      return SQLITE_NOMEM;
    }
    pWinMemData->bOwned = TRUE;
  }
  assert( pWinMemData->hHeap!=0 );
  assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
................................................................................

/*
** Deinitialize this module.
*/
static void winMemShutdown(void *pAppData){
  winMemData *pWinMemData = (winMemData *)pAppData;

  if( !pWinMemData ) return;
  if( pWinMemData->hHeap ){
    assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
    assert( HeapValidate(pWinMemData->hHeap, 0, NULL) );
#endif
    if( pWinMemData->bOwned ){
      if( !HeapDestroy(pWinMemData->hHeap) ){
        sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
            GetLastError(), (void*)pWinMemData->hHeap);
      }
      pWinMemData->bOwned = FALSE;
    }
    pWinMemData->hHeap = NULL;
  }
}