/* ** Change the size of an existing memory allocation */ static void *memsys3Realloc(sqlite4_env*pEnv, void *pPrior, int nBytes){ int nOld; void *p; if( pPrior==0 ){ return sqlite4_malloc(pEnv, nBytes); } if( nBytes<=0 ){ sqlite4_free(pEnv, pPrior); return 0; } nOld = memsys3Size(pPrior); if( nBytes<=nOld && nBytes>=nOld-128 ){ return pPrior; } memsys3Enter(); p = memsys3MallocUnsafe(nBytes); if( p ){ if( nOld<nBytes ){ memcpy(p, pPrior, nOld); }else{ memcpy(p, pPrior, nBytes); } memsys3FreeUnsafe(pPrior); } memsys3Leave(); return p; }
//重新分配*pPrior指向的内存空间大小为n字节。 static void *memsys3Realloc(void *pPrior, int nBytes) { int nOld; void *p; if( pPrior==0 ) { //原来的内存为0,则直接分配n字节 return sqlite3_malloc(nBytes); } if( nBytes<=0 ) { sqlite3_free(pPrior); //释放从由sqlite3Malloc获得的内存空间 return 0; } nOld = memsys3Size(pPrior); //获取未完成分配的内存空间 if( nBytes<=nOld && nBytes>=nOld-128 ) { return pPrior; } memsys3Enter(); p = memsys3MallocUnsafe(nBytes); //申请n字节的内存空间 if( p ) { if( nOld<nBytes ) { memcpy(p, pPrior, nOld); } else { memcpy(p, pPrior, nBytes); } memsys3FreeUnsafe(pPrior); } memsys3Leave(); return p; }
/* ** Allocate nBytes of memory. */ static void *memsys3Malloc(int nBytes){ sqlite4_int64 *p; assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */ memsys3Enter(); p = memsys3MallocUnsafe(nBytes); memsys3Leave(); return (void*)p; }
//申请分配n字节的内存空间。 static void *memsys3Malloc(int nBytes) { //分配n字节的内存 sqlite3_int64 *p; assert( nBytes>0 ); /* malloc.c filters out 0 byte requests *///请求字节为0则终止程序 memsys3Enter(); //获取共享锁 p = memsys3MallocUnsafe(nBytes); //分配内存 memsys3Leave(); //释放锁 return (void*)p; //返回空指针 }