Small. Fast. Reliable.
Choose any three.

SQLite C 接口

内存分配函数
Memory Allocation Routines

typedef struct sqlite3_mem_methods sqlite3_mem_methods;
struct sqlite3_mem_methods {
  void *(*xMalloc)(int);         /* Memory allocation function */
  void (*xFree)(void*);          /* Free a prior allocation */
  void *(*xRealloc)(void*,int);  /* Resize an allocation */
  int (*xSize)(void*);           /* Return the size of an allocation */
  int (*xRoundup)(int);          /* Round up request size to allocation size */
  int (*xInit)(void*);           /* Initialize the memory allocator */
  void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
  void *pAppData;                /* Argument to xInit() and xShutdown() */
};

这个对象的实例定义了SQLite和底层内存分配函数之间的接口。
An instance of this object defines the interface between SQLite and low-level memory allocation routines.

这个对象只用于SQLite接口中的一个地方。当配置SQLITE_CONFIG_MALLOCSQLITE_CONFIG_GETMALLOC选项时,sqlite3_config()的一个参数是指向这个对象实例的指针。在配置中创建一个这个对象的实例,并传入到sqlite3_config(SQLITE_CONFIG_MALLOC)中,应用程序就可以给SQLite指定一个内存分配子系统来完成所有动态内存分配的工作。
This object is used in only one place in the SQLite interface. A pointer to an instance of this object is the argument to sqlite3_config() when the configuration option is SQLITE_CONFIG_MALLOC or SQLITE_CONFIG_GETMALLOC. By creating an instance of this object and passing it to sqlite3_config(SQLITE_CONFIG_MALLOC) during configuration, an application can specify an alternative memory allocation subsystem for SQLite to use for all of its dynamic memory needs.

注意,SQLite中有几个内置内存分配器,这些可以完美适用于绝大部分的应用程序,而这个对象只是适用于很小的一部分需要专用内存分配器的应用。这个对象经常用在SQLite测试中,用来指定一个可以模拟内存溢出等情况的内存分配器,以此来测试SQLite是否能从这种情况下恢复。
Note that SQLite comes with several built-in memory allocators that are perfectly adequate for the overwhelming majority of applications and that this object is only useful to a tiny minority of applications with specialized memory allocation requirements. This object is also used during testing of SQLite in order to specify an alternative memory allocator that simulates memory out-of-memory conditions in order to verify that SQLite recovers gracefully from such conditions.

xMalloc、xRealloc和xFree函数的功能与标准C库中malloc()、realloc()和free()函数一样。SQLite保证xRealloc的第二个参数总是一个之前xRoundup调用返回的值。
The xMalloc, xRealloc, and xFree methods must work like the malloc(), realloc() and free() functions from the standard C library. SQLite guarantees that the second argument to xRealloc is always a value returned by a prior call to xRoundup.

xSize可以返回之前从xMalloc 或 xRealloc中分配的内存的大小。分配的内存至少和需求一样大,不过也可能会大一些。
xSize should return the allocated size of a memory allocation previously obtained from xMalloc or xRealloc. The allocated size is always at least as big as the requested size but may be larger.

xRoundup函数给定一个所需的内存尺寸,返回实际会分配的内存大小。大多数的内存分配器至少将分配的内存大小向上增加到下一个8的倍数。一些分配器会增加到更大的倍数,甚至2的幂。每次通过sqlite3_malloc()sqlite3_realloc()分配内存时,会首先调用xRoundup。如果xRoundup返回0,则会导致相应的内存分配失败。
The xRoundup method returns what would be the allocated size of a memory allocation given a particular requested size. Most memory allocators round up memory allocations at least to the next multiple of 8. Some allocators round up to a larger multiple or to a power of 2. Every memory allocation request coming in through sqlite3_malloc() or sqlite3_realloc() first calls xRoundup. If xRoundup returns 0, that causes the corresponding memory allocation to fail.

xInit函数初始化内存分配。例如,可能会分配所有所需的互斥体,或者初始化内部数据结构。xShutdown函数由sqlite3_shutdown()间接执行,释放所有xInit请求的资源。pAppData指针是xInit和xShoutdown唯一的参数。
The xInit method initializes the memory allocator. For example, it might allocate any require mutexes or initialize internal data structures. The xShutdown method is invoked (indirectly) by sqlite3_shutdown() and should deallocate any resources acquired by xInit. The pAppData pointer is used as the only parameter to xInit and xShutdown.

当执行xInit函数时SQLite会持有SQLITE_MUTEX_STATIC_MASTER互斥锁,所以xInit函数不需要线程安全。xShutdown函数只在sqlite3_shutdown()中调用,所以也不需要线程安全。对于其他的函数,只要SQLITE_CONFIG_MEMSTATUS配置项是开启的(默认值),那么SQLite就会获取SQLITE_MUTEX_STATIC_MEM互斥锁,所以这些函数都是自动串行化的。但是,如果关闭了SQLITE_CONFIG_MEMSTATUS,那么其它的这些函数就必须保证线程安全,否则就让自行安排串行化。
SQLite holds the SQLITE_MUTEX_STATIC_MASTER mutex when it invokes the xInit method, so the xInit method need not be threadsafe. The xShutdown method is only called from sqlite3_shutdown() so it does not need to be threadsafe either. For all other methods, SQLite holds the SQLITE_MUTEX_STATIC_MEM mutex as long as the SQLITE_CONFIG_MEMSTATUS configuration option is turned on (which it is by default) and so the methods are automatically serialized. However, if SQLITE_CONFIG_MEMSTATUS is disabled, then the other methods must be threadsafe or else make their own arrangements for serialization.

SQLite永远不会在调用xShutdown()之前,连续调用xInit()一次以上。
SQLite will never invoke xInit() more than once without an intervening call to xShutdown().

另行参见ObjectsConstantsFunctions的列表。
See also lists of Objects, Constants, and Functions.