标签:
Lua在载入lua文件的时候,读取过程中通过cache的方式,默认cache为512字节:
1、cache中包含数据时,直接将cache中数据返回;
2、cache中不包含数据的时候,每次读取512个字节,进行cache;
1 typedef struct LoadF { 2 int n; /* number of pre-read characters */ 3 FILE *f; /* file being read */ 4 char buff[LUAL_BUFFERSIZE]; /* area for reading file */ 5 } LoadF; 6 7 8 static const char *getF (lua_State *L, void *ud, size_t *size) { 9 LoadF *lf = (LoadF *)ud; 10 (void)L; /* not used */ 11 if (lf->n > 0) { /* are there pre-read characters to be read? */ 12 *size = lf->n; /* return them (chars already in buffer) */ 13 lf->n = 0; /* no more pre-read characters */ 14 } 15 else { /* read a block from file */ 16 /* ‘fread‘ can return > 0 *and* set the EOF flag. If next call to 17 ‘getF‘ called ‘fread‘, it might still wait for user input. 18 The next check avoids this problem. */ 19 if (feof(lf->f)) return NULL; 20 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ 21 } 22 return lf->buff; 23 }
在载入lua文件,进行解析文件时:
1 static void f_parser (lua_State *L, void *ud) { 2 int i; 3 Closure *cl; 4 struct SParser *p = cast(struct SParser *, ud); 5 int c = zgetc(p->z); /* read first character */ 6 ..... 7 } 8 9 #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 10 11 struct Zio { 12 size_t n; /* bytes still unread */ 13 const char *p; /* current position in buffer */ 14 lua_Reader reader; /* reader function */ 15 void* data; /* additional data */ 16 lua_State *L; /* Lua state (for reader) */ 17 }; 18 19 int luaZ_fill (ZIO *z) { 20 size_t size; 21 lua_State *L = z->L; 22 const char *buff; 23 lua_unlock(L); 24 buff = z->reader(L, z->data, &size); 25 lua_lock(L); 26 if (buff == NULL || size == 0) 27 return EOZ; 28 z->n = size - 1; /* discount char being returned */ 29 z->p = buff; 30 return cast_uchar(*(z->p++)); 31 }
lua_Reader reader在初始化是设置为getF,解析过程中采用cache的方式读取lua文件。
标签:
原文地址:http://www.cnblogs.com/zerozero/p/4189191.html