1 /******************************************************************************
3 *****************************************************************************/
17 static real_close_t real_close
;
18 static real_write_t real_write
;
19 static real_read_t real_read
;
21 static real_fclose_t real_fclose
;
22 static real_fwrite_t real_fwrite
;
23 static real_fread_t real_fread
;
24 static real_fwrite_t real_fwrite_unlocked
;
25 static real_fread_t real_fread_unlocked
;
29 static int fadvise_init_done
=0;
30 static int fadvise_debug
=0;
31 static int fadvise_advice
=POSIX_FADV_DONTNEED
;
33 void do_log(char *str
, ...)
37 va_start(arglist
, str
);
38 vfprintf(stderr
, str
, arglist
);
45 static void lib_init(void)
49 if(fadvise_init_done
) return;
52 *(void **) (&real_close
) = dlsym(RTLD_NEXT
, "close");
55 fprintf(stderr
, "(fadvise) Cannot load symbol 'close' %s\n", dlerror());
58 *(void **) (&real_write
) = dlsym(RTLD_NEXT
, "write");
61 fprintf(stderr
, "(fadvise) Cannot load symbol 'write' %s\n", dlerror());
64 *(void **) (&real_read
) = dlsym(RTLD_NEXT
, "read");
67 fprintf(stderr
, "(fadvise) Cannot load symbol 'read' %s\n", dlerror());
70 *(void **) (&real_fclose
) = dlsym(RTLD_NEXT
, "fclose");
73 fprintf(stderr
, "(fadvise) Cannot load symbol 'fclose' %s\n", dlerror());
76 *(void **) (&real_fwrite
) = dlsym(RTLD_NEXT
, "fwrite");
79 fprintf(stderr
, "(fadvise) Cannot load symbol 'fwrite' %s\n", dlerror());
82 *(void **) (&real_fread
) = dlsym(RTLD_NEXT
, "fread");
85 fprintf(stderr
, "(fadvise) Cannot load symbol 'fread' %s\n", dlerror());
88 *(void **) (&real_fwrite_unlocked
) = dlsym(RTLD_NEXT
, "fwrite_unlocked");
89 if(!real_fwrite_unlocked
)
91 fprintf(stderr
, "(fadvise) Cannot load symbol 'fwrite_unlocked' %s\n", dlerror());
94 *(void **) (&real_fread_unlocked
) = dlsym(RTLD_NEXT
, "fread_unlocked");
95 if(!real_fread_unlocked
)
97 fprintf(stderr
, "(fadvise) Cannot load symbol 'fread_unlocked' %s\n", dlerror());
101 temp
= getenv("FADVISE_ADVICE"); if(!temp
) temp
= getenv("FADV");
104 if(strcasestr(temp
, "NORM")) fadvise_advice
= POSIX_FADV_NORMAL
;
105 else if(strcasestr(temp
, "SEQ")) fadvise_advice
= POSIX_FADV_SEQUENTIAL
;
106 else if(strcasestr(temp
, "RAND")) fadvise_advice
= POSIX_FADV_RANDOM
;
107 else if(strcasestr(temp
, "NORE")) fadvise_advice
= POSIX_FADV_NOREUSE
;
108 else if(strcasestr(temp
, "WILL")) fadvise_advice
= POSIX_FADV_WILLNEED
;
109 else if(strcasestr(temp
, "DONT")) fadvise_advice
= POSIX_FADV_DONTNEED
;
113 temp
= getenv("FADVISE_DEBUG"); if(!temp
) temp
= getenv("FADD");
114 if(temp
) fadvise_debug
= strtoul(temp
, NULL
, 10);
122 ssize_t
write(int fd
, const void *buf
, size_t count
)
127 DEBUG("write(%d, ,%u)\n", fd
, count
);
128 if(!fadvise_init_done
) lib_init();
130 ret
= real_write(fd
, buf
, count
);
134 pos
= lseek(fd
, 0, SEEK_CUR
);
135 if((off_t
)-1 == pos
) pos
= 0;
136 posix_fadvise(fd
, 0, pos
, fadvise_advice
);
144 size_t fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
149 DEBUG("fwrite(, %u, %u, %d)\n", size
, nmemb
, stream
? fileno(stream
) : -1);
150 if(!fadvise_init_done
) lib_init();
152 ret
= real_fwrite(ptr
, size
, nmemb
, stream
);
156 pos
= (long) ftell(stream
);
157 if(-1 == pos
) pos
= 0;
158 posix_fadvise(fileno(stream
), 0, pos
, fadvise_advice
);
166 size_t fwrite_unlocked(const void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
171 DEBUG("fwrite_unlocked(, %u, %u, %d)\n", size
, nmemb
, stream
? fileno(stream
) : -1);
172 if(!fadvise_init_done
) lib_init();
174 ret
= real_fwrite_unlocked(ptr
, size
, nmemb
, stream
);
178 pos
= (long) ftell(stream
);
179 if(-1 == pos
) pos
= 0;
180 posix_fadvise(fileno(stream
), 0, pos
, fadvise_advice
);
188 ssize_t
read(int fd
, void *buf
, size_t count
)
193 DEBUG("read(%d, ,%u)\n", fd
, count
);
194 if(!fadvise_init_done
) lib_init();
196 ret
= real_read(fd
, buf
, count
);
200 pos
= lseek(fd
, 0, SEEK_CUR
);
201 if((off_t
)-1 == pos
) pos
= 0;
202 posix_fadvise(fd
, 0, pos
, fadvise_advice
);
210 size_t fread(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
215 DEBUG("fread(, %u, %u, %d)\n", size
, nmemb
, stream
? fileno(stream
) : -1);
216 if(!fadvise_init_done
) lib_init();
218 ret
= real_fread(ptr
, size
, nmemb
, stream
);
222 pos
= (long) ftell(stream
);
223 if(-1 == pos
) pos
= 0;
224 posix_fadvise(fileno(stream
), 0, pos
, fadvise_advice
);
232 size_t fread_unlocked(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
237 DEBUG("fread_unlocked(, %u, %u, %d)\n", size
, nmemb
, stream
? fileno(stream
) : -1);
238 if(!fadvise_init_done
) lib_init();
240 ret
= real_fread_unlocked(ptr
, size
, nmemb
, stream
);
244 pos
= (long) ftell(stream
);
245 if(-1 == pos
) pos
= 0;
246 posix_fadvise(fileno(stream
), 0, pos
, fadvise_advice
);
259 DEBUG("close(%d)\n", fd
);
260 if(!fadvise_init_done
) lib_init();
262 pos
= lseek(fd
, 0, SEEK_CUR
);
263 if((off_t
)-1 == pos
) pos
= 0;
264 posix_fadvise(fd
, 0, pos
, fadvise_advice
);
265 return real_close(fd
);
274 DEBUG("fclose(%d)\n", fp
? fileno(fp
) : -1);
275 if(!fadvise_init_done
) lib_init();
277 pos
= (long) ftell(fp
);
278 if(-1 == pos
) pos
= 0;
279 posix_fadvise(fileno(fp
), 0, pos
, fadvise_advice
);
280 return real_fclose(fp
);