Lines Matching refs:mfp

88 static int  mf_write_block __ARGS((memfile_T *mfp, bhdr_T *hp, off_t offset, unsigned size));
125 memfile_T *mfp;
133 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
138 mfp->mf_fname = NULL;
139 mfp->mf_ffname = NULL;
140 mfp->mf_fd = -1;
144 mf_do_open(mfp, fname, flags); /* try to open the file */
147 if (mfp->mf_fd < 0)
149 vim_free(mfp);
154 mfp->mf_free_first = NULL; /* free list is empty */
155 mfp->mf_used_first = NULL; /* used list is empty */
156 mfp->mf_used_last = NULL;
157 mfp->mf_dirty = FALSE;
158 mfp->mf_used_count = 0;
161 mfp->mf_hash[i] = NULL; /* hash lists are empty */
162 mfp->mf_trans[i] = NULL; /* trans lists are empty */
164 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
166 mfp->mf_old_key = NULL;
177 if (mfp->mf_fd >= 0
178 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
181 mfp->mf_page_size = stf.F_BSIZE;
184 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
185 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
186 mfp->mf_blocknr_max = 0; /* no file or empty file */
188 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
189 / mfp->mf_page_size);
190 mfp->mf_blocknr_min = -1;
191 mfp->mf_neg_count = 0;
192 mfp->mf_infile_count = mfp->mf_blocknr_max;
201 unsigned page_size = mfp->mf_page_size;
208 mfp->mf_used_count_max = (p_mm << shift) / page_size;
209 if (mfp->mf_used_count_max < 10)
210 mfp->mf_used_count_max = 10;
213 return mfp;
227 mf_open_file(mfp, fname)
228 memfile_T *mfp;
231 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
233 if (mfp->mf_fd < 0)
236 mfp->mf_dirty = TRUE;
244 mf_close(mfp, del_file)
245 memfile_T *mfp;
252 if (mfp == NULL) /* safety check */
254 if (mfp->mf_fd >= 0)
256 if (close(mfp->mf_fd) < 0)
259 if (del_file && mfp->mf_fname != NULL)
260 mch_remove(mfp->mf_fname);
262 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
264 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
268 while (mfp->mf_free_first != NULL) /* free entries in free list */
269 vim_free(mf_rem_free(mfp));
271 for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
276 vim_free(mfp->mf_fname);
277 vim_free(mfp->mf_ffname);
278 vim_free(mfp);
289 memfile_T *mfp;
292 mfp = buf->b_ml.ml_mfp;
293 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
306 if (close(mfp->mf_fd) < 0) /* close the file */
308 mfp->mf_fd = -1;
310 if (mfp->mf_fname != NULL)
312 mch_remove(mfp->mf_fname); /* delete the swap file */
313 vim_free(mfp->mf_fname);
314 vim_free(mfp->mf_ffname);
315 mfp->mf_fname = NULL;
316 mfp->mf_ffname = NULL;
325 mf_new_page_size(mfp, new_size)
326 memfile_T *mfp;
331 total_mem_used += new_size - mfp->mf_page_size;
332 mfp->mf_page_size = new_size;
341 mf_new(mfp, negative, page_count)
342 memfile_T *mfp;
354 hp = mf_release(mfp, page_count);
362 freep = mfp->mf_free_first;
377 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
385 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
387 hp = mf_rem_free(mfp);
392 freep = mf_rem_free(mfp);
399 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
403 hp->bh_bnum = mfp->mf_blocknr_min--;
404 mfp->mf_neg_count++;
408 hp->bh_bnum = mfp->mf_blocknr_max;
409 mfp->mf_blocknr_max += page_count;
413 mfp->mf_dirty = TRUE;
415 mf_ins_used(mfp, hp);
416 mf_ins_hash(mfp, hp);
423 (size_t)mfp->mf_page_size * page_count);
434 mf_get(mfp, nr, page_count)
435 memfile_T *mfp;
441 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
447 hp = mf_find_hash(mfp, nr);
450 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
460 hp = mf_release(mfp, page_count);
461 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
467 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
475 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
476 mf_rem_hash(mfp, hp);
480 mf_ins_used(mfp, hp); /* put in front of used list */
481 mf_ins_hash(mfp, hp); /* put in front of hash list */
495 mf_put(mfp, hp, dirty, infile)
496 memfile_T *mfp;
511 mfp->mf_dirty = TRUE;
515 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
519 * block *hp is no longer in used, may put it in the free list of memfile *mfp
522 mf_free(mfp, hp)
523 memfile_T *mfp;
527 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
528 mf_rem_used(mfp, hp); /* get *hp out of the used list */
532 mfp->mf_neg_count--;
535 mf_ins_free(mfp, hp); /* put *hp in the free list */
550 * Sync the memory file *mfp to disk.
563 mf_sync(mfp, flags)
564 memfile_T *mfp;
574 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
576 mfp->mf_dirty = FALSE;
591 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
595 && hp->bh_bnum < mfp->mf_infile_count)))
599 if (mf_write(mfp, hp) == FAIL)
622 mfp->mf_dirty = FALSE;
642 if (fsync(mfp->mf_fd))
658 if (fsync(mfp->mf_fd))
663 if (_dos_commit(mfp->mf_fd))
671 if ((fd = dup(mfp->mf_fd)) >= 0)
677 if (fsync(mfp->mf_fd) != 0)
690 struct UFB *fp = chkufb(mfp->mf_fd);
706 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
713 Flush(_devtab[mfp->mf_fd].fd);
726 * For all blocks in memory file *mfp that have a positive block number set
731 mf_set_dirty(mfp)
732 memfile_T *mfp;
736 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
739 mfp->mf_dirty = TRUE;
743 * insert block *hp in front of hashlist of memfile *mfp
746 mf_ins_hash(mfp, hp)
747 memfile_T *mfp;
754 hhp = mfp->mf_hash[hash];
759 mfp->mf_hash[hash] = hp;
763 * remove block *hp from hashlist of memfile list *mfp
766 mf_rem_hash(mfp, hp)
767 memfile_T *mfp;
771 mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
780 * look in hash lists of memfile *mfp for block header with number 'nr'
783 mf_find_hash(mfp, nr)
784 memfile_T *mfp;
789 for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
796 * insert block *hp in front of used list of memfile *mfp
799 mf_ins_used(mfp, hp)
800 memfile_T *mfp;
803 hp->bh_next = mfp->mf_used_first;
804 mfp->mf_used_first = hp;
807 mfp->mf_used_last = hp;
810 mfp->mf_used_count += hp->bh_page_count;
811 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
815 * remove block *hp from used list of memfile *mfp
818 mf_rem_used(mfp, hp)
819 memfile_T *mfp;
823 mfp->mf_used_last = hp->bh_prev;
827 mfp->mf_used_first = hp->bh_next;
830 mfp->mf_used_count -= hp->bh_page_count;
831 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
842 mf_release(mfp, page_count)
843 memfile_T *mfp;
858 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
865 if (mfp->mf_fd < 0 && need_release && p_uc)
869 if (buf->b_ml.ml_mfp == mfp)
883 if (mfp->mf_fd < 0 || !need_release)
886 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
896 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
899 mf_rem_used(mfp, hp);
900 mf_rem_hash(mfp, hp);
909 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
929 memfile_T *mfp;
935 mfp = buf->b_ml.ml_mfp;
936 if (mfp != NULL)
939 if (mfp->mf_fd < 0 && buf->b_may_swap)
943 if (mfp->mf_fd >= 0)
945 for (hp = mfp->mf_used_last; hp != NULL; )
949 || mf_write(mfp, hp) != FAIL))
951 mf_rem_used(mfp, hp);
952 mf_rem_hash(mfp, hp);
954 hp = mfp->mf_used_last; /* re-start, list was changed */
970 mf_alloc_bhdr(mfp, page_count)
971 memfile_T *mfp;
978 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
1004 mf_ins_free(mfp, hp)
1005 memfile_T *mfp;
1008 hp->bh_next = mfp->mf_free_first;
1009 mfp->mf_free_first = hp;
1014 * Note: caller must check that mfp->mf_free_first is not NULL!
1017 mf_rem_free(mfp)
1018 memfile_T *mfp;
1022 hp = mfp->mf_free_first;
1023 mfp->mf_free_first = hp->bh_next;
1033 mf_read(mfp, hp)
1034 memfile_T *mfp;
1041 if (mfp->mf_fd < 0) /* there is no file, can't read */
1044 page_size = mfp->mf_page_size;
1047 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1052 if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
1060 if (*mfp->mf_buffer->b_p_key != NUL)
1061 ml_decrypt_data(mfp, hp->bh_data, offset, size);
1073 mf_write(mfp, hp)
1074 memfile_T *mfp;
1084 if (mfp->mf_fd < 0) /* there is no file, can't write */
1088 if (mf_trans_add(mfp, hp) == FAIL)
1091 page_size = mfp->mf_page_size;
1102 if (nr > mfp->mf_infile_count) /* beyond end of file */
1104 nr = mfp->mf_infile_count;
1105 hp2 = mf_find_hash(mfp, nr); /* NULL catched below */
1111 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1121 if (mf_write_block(mfp, hp2 == NULL ? hp : hp2, offset, size) == FAIL)
1138 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1139 mfp->mf_infile_count = nr + page_count;
1147 * Write block "hp" with data size "size" to file "mfp->mf_fd".
1152 mf_write_block(mfp, hp, offset, size)
1153 memfile_T *mfp;
1163 if (*mfp->mf_buffer->b_p_key != NUL)
1165 data = ml_encrypt_data(mfp, data, offset, size);
1171 if ((unsigned)vim_write(mfp->mf_fd, data, size) != size)
1188 mf_trans_add(mfp, hp)
1189 memfile_T *mfp;
1209 freep = mfp->mf_free_first;
1225 freep = mf_rem_free(mfp);
1231 new_bnum = mfp->mf_blocknr_max;
1232 mfp->mf_blocknr_max += page_count;
1238 mf_rem_hash(mfp, hp); /* remove from old hash list */
1240 mf_ins_hash(mfp, hp); /* insert in new hash list */
1243 np->nt_next = mfp->mf_trans[hash];
1244 mfp->mf_trans[hash] = np;
1258 mf_trans_del(mfp, old_nr)
1259 memfile_T *mfp;
1267 for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
1273 mfp->mf_neg_count--;
1278 mfp->mf_trans[hash] = np->nt_next;
1287 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1292 mf_set_ffname(mfp)
1293 memfile_T *mfp;
1295 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1303 mf_fullname(mfp)
1304 memfile_T *mfp;
1306 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1308 vim_free(mfp->mf_fname);
1309 mfp->mf_fname = mfp->mf_ffname;
1310 mfp->mf_ffname = NULL;
1315 * return TRUE if there are any translations pending for 'mfp'
1318 mf_need_trans(mfp)
1319 memfile_T *mfp;
1321 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1330 mf_do_open(mfp, fname, flags)
1331 memfile_T *mfp;
1339 mfp->mf_fname = fname;
1346 mf_set_ffname(mfp);
1352 mf_fullname(mfp);
1360 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1362 mfp->mf_fd = -1;
1378 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1384 if (mfp->mf_fd < 0)
1386 vim_free(mfp->mf_fname);
1387 vim_free(mfp->mf_ffname);
1388 mfp->mf_fname = NULL;
1389 mfp->mf_ffname = NULL;
1394 int fdflags = fcntl(mfp->mf_fd, F_GETFD);
1396 fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
1399 mch_copy_sec(fname, mfp->mf_fname);
1401 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */