+#else
+ fprintf(stderr, "sorry mincore only works with mmap");
+#endif
+}
+#endif /* defined DEBUG && DEBUG > 1 */
+
+/*
+ * get exclusive lock to whole file.
+ * lock gets removed when we close the file
+ *
+ * returns 0 on success
+ */
+int rrd_lock(
+ rrd_file_t *rrd_file)
+{
+ int rcstat;
+ rrd_simple_file_t *rrd_simple_file;
+ rrd_simple_file = (rrd_simple_file_t *)rrd_file->pvt;
+
+ {
+#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
+ struct _stat st;
+
+ if (_fstat(rrd_simple_file->fd, &st) == 0) {
+ rcstat = _locking(rrd_simple_file->fd, _LK_NBLCK, st.st_size);
+ } else {
+ rcstat = -1;
+ }
+#else
+ struct flock lock;
+
+ lock.l_type = F_WRLCK; /* exclusive write lock */
+ lock.l_len = 0; /* whole file */
+ lock.l_start = 0; /* start of file */
+ lock.l_whence = SEEK_SET; /* end of file */
+
+ rcstat = fcntl(rrd_simple_file->fd, F_SETLK, &lock);
+#endif
+ }
+
+ return (rcstat);
+}
+
+
+/* drop cache except for the header and the active pages */
+void rrd_dontneed(
+ rrd_file_t *rrd_file,
+ rrd_t *rrd)
+{
+ rrd_simple_file_t *rrd_simple_file = (rrd_simple_file_t *)rrd_file->pvt;
+#if defined USE_MADVISE || defined HAVE_POSIX_FADVISE
+ off_t dontneed_start;
+ off_t rra_start;
+ off_t active_block;
+ unsigned long i;
+ ssize_t _page_size = sysconf(_SC_PAGESIZE);
+
+ if (rrd_file == NULL) {
+#if defined DEBUG && DEBUG
+ fprintf (stderr, "rrd_dontneed: Argument 'rrd_file' is NULL.\n");
+#endif
+ return;
+ }
+
+#if defined DEBUG && DEBUG > 1
+ mincore_print(rrd_file, "before");
+#endif