C
ctx2002
hi guys:
I am reading Sqlite code at moment, my c language skill not good
enough , hope some one here can help me.
In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr
*pPg);
there is a line:
int iOff = (char *)pLink - (char *)pPg;
from my understanding, a pointer is just a memory address, so one
memory address minus an other
memory adress equal to what? what that iOff stand for?
I thought author of sqlite thought pLink will greater than pPg, it
that right?
also what is result of cast a pointer of struct into a pointer of
char. if the struct pLink has data inside,
what is result of (char *)pLink++?
also can any one explains to me what this is mean?
PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)
[iOff]);
thanks very much.
following are code copied from sqlite;
typedef struct PagerLruList PagerLruList;
struct PagerLruList {
PgHdr *pFirst; /* First page in LRU list */
PgHdr *pLast; /* Last page in LRU list (the most recently
used) */
PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0
*/
};
/*
** The following structure contains the next and previous pointers
used
** to link a PgHdr structure into a PagerLruList linked list.
*/
typedef struct PagerLruLink PagerLruLink;
struct PagerLruLink {
PgHdr *pNext;
PgHdr *pPrev;
};
/*
** Add page pPg to the end of the linked list managed by structure
** pList (pPg becomes the last entry in the list - the most recently
** used). Argument pLink should point to either pPg->free or pPg-
** global LRU list.
*/
static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr
*pPg){
pLink->pNext = 0;
pLink->pPrev = pList->pLast;
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
assert(pLink==&pPg->free || pLink==&pPg->gfree);
assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
#endif
if( pList->pLast ){
int iOff = (char *)pLink - (char *)pPg;
PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)
[iOff]);
pLastLink->pNext = pPg;
}else{
assert(!pList->pFirst);
pList->pFirst = pPg;
}
pList->pLast = pPg;
if( !pList->pFirstSynced && pPg->needSync==0 ){
pList->pFirstSynced = pPg;
}
}
I am reading Sqlite code at moment, my c language skill not good
enough , hope some one here can help me.
In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr
*pPg);
there is a line:
int iOff = (char *)pLink - (char *)pPg;
from my understanding, a pointer is just a memory address, so one
memory address minus an other
memory adress equal to what? what that iOff stand for?
I thought author of sqlite thought pLink will greater than pPg, it
that right?
also what is result of cast a pointer of struct into a pointer of
char. if the struct pLink has data inside,
what is result of (char *)pLink++?
also can any one explains to me what this is mean?
PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)
[iOff]);
thanks very much.
following are code copied from sqlite;
typedef struct PagerLruList PagerLruList;
struct PagerLruList {
PgHdr *pFirst; /* First page in LRU list */
PgHdr *pLast; /* Last page in LRU list (the most recently
used) */
PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0
*/
};
/*
** The following structure contains the next and previous pointers
used
** to link a PgHdr structure into a PagerLruList linked list.
*/
typedef struct PagerLruLink PagerLruLink;
struct PagerLruLink {
PgHdr *pNext;
PgHdr *pPrev;
};
/*
** Add page pPg to the end of the linked list managed by structure
** pList (pPg becomes the last entry in the list - the most recently
** used). Argument pLink should point to either pPg->free or pPg-
** depending on whether pPg is being added to the pager-specific orgfree,
** global LRU list.
*/
static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr
*pPg){
pLink->pNext = 0;
pLink->pPrev = pList->pLast;
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
assert(pLink==&pPg->free || pLink==&pPg->gfree);
assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
#endif
if( pList->pLast ){
int iOff = (char *)pLink - (char *)pPg;
PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)
[iOff]);
pLastLink->pNext = pPg;
}else{
assert(!pList->pFirst);
pList->pFirst = pPg;
}
pList->pLast = pPg;
if( !pList->pFirstSynced && pPg->needSync==0 ){
pList->pFirstSynced = pPg;
}
}