From 7a8402ac31aa2e155baafbc925c48527511c1e92 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 28 May 2023 08:06:30 +0800 Subject: vim-patch:9.0.1583: get E304 when using 'cryptmethod' "xchacha20v2" (#23790) Problem: Get E304 when using 'cryptmethod' "xchacha20v2". (Steve Mynott) Solution: Add 4th crypt method to block zero ID check. Avoid syncing a swap file before reading the file. (closes vim/vim#12433) https://github.com/vim/vim/commit/3a2a60ce4a8e73594bca16814672fcc243d093ac Co-authored-by: Bram Moolenaar --- src/nvim/memfile_defs.h | 68 +++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'src/nvim/memfile_defs.h') diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h index 53152c28f8..917dd6a905 100644 --- a/src/nvim/memfile_defs.h +++ b/src/nvim/memfile_defs.h @@ -38,13 +38,13 @@ typedef struct mf_hashitem { /// mf_hashitem_T which contains the key and linked list pointers. List of items /// in each bucket is doubly-linked. typedef struct mf_hashtab { - size_t mht_mask; /// mask used to mod hash value to array index - /// (nr of items in array is 'mht_mask + 1') - size_t mht_count; /// number of items inserted - mf_hashitem_T **mht_buckets; /// points to the array of buckets (can be - /// mht_small_buckets or a newly allocated array - /// when mht_small_buckets becomes too small) - mf_hashitem_T *mht_small_buckets[MHT_INIT_SIZE]; /// initial buckets + size_t mht_mask; ///< mask used to mod hash value to array index + ///< (nr of items in array is 'mht_mask + 1') + size_t mht_count; ///< number of items inserted + mf_hashitem_T **mht_buckets; ///< points to the array of buckets (can be + ///< mht_small_buckets or a newly allocated array + ///< when mht_small_buckets becomes too small) + mf_hashitem_T *mht_small_buckets[MHT_INIT_SIZE]; ///< initial buckets } mf_hashtab_T; /// A block header. @@ -61,17 +61,17 @@ typedef struct mf_hashtab { /// The blocks in the free list have no block of memory allocated and /// the contents of the block in the file (if any) is irrelevant. typedef struct bhdr { - mf_hashitem_T bh_hashitem; /// header for hash table and key -#define bh_bnum bh_hashitem.mhi_key /// block number, part of bh_hashitem + mf_hashitem_T bh_hashitem; ///< header for hash table and key +#define bh_bnum bh_hashitem.mhi_key ///< block number, part of bh_hashitem - struct bhdr *bh_next; /// next block header in free or used list - struct bhdr *bh_prev; /// previous block header in used list - void *bh_data; /// pointer to memory (for used block) - unsigned bh_page_count; /// number of pages in this block + struct bhdr *bh_next; ///< next block header in free or used list + struct bhdr *bh_prev; ///< previous block header in used list + void *bh_data; ///< pointer to memory (for used block) + unsigned bh_page_count; ///< number of pages in this block #define BH_DIRTY 1U #define BH_LOCKED 2U - unsigned bh_flags; // BH_DIRTY or BH_LOCKED + unsigned bh_flags; ///< BH_DIRTY or BH_LOCKED } bhdr_T; /// A block number translation list item. @@ -81,27 +81,33 @@ typedef struct bhdr { /// number, we remember the translation to the new positive number in the /// double linked trans lists. The structure is the same as the hash lists. typedef struct mf_blocknr_trans_item { - mf_hashitem_T nt_hashitem; /// header for hash table and key -#define nt_old_bnum nt_hashitem.mhi_key /// old, negative, number - blocknr_T nt_new_bnum; /// new, positive, number + mf_hashitem_T nt_hashitem; ///< header for hash table and key +#define nt_old_bnum nt_hashitem.mhi_key ///< old, negative, number + blocknr_T nt_new_bnum; ///< new, positive, number } mf_blocknr_trans_item_T; +typedef enum { + MF_DIRTY_NO = 0, ///< no dirty blocks + MF_DIRTY_YES, ///< there are dirty blocks + MF_DIRTY_YES_NOSYNC, ///< there are dirty blocks, do not sync yet +} mfdirty_T; + /// A memory file. typedef struct memfile { - char *mf_fname; /// name of the file - char *mf_ffname; /// idem, full path - int mf_fd; /// file descriptor - bhdr_T *mf_free_first; /// first block header in free list - bhdr_T *mf_used_first; /// mru block header in used list - bhdr_T *mf_used_last; /// lru block header in used list - mf_hashtab_T mf_hash; /// hash lists - mf_hashtab_T mf_trans; /// trans lists - blocknr_T mf_blocknr_max; /// highest positive block number + 1 - blocknr_T mf_blocknr_min; /// lowest negative block number - 1 - blocknr_T mf_neg_count; /// number of negative blocks numbers - blocknr_T mf_infile_count; /// number of pages in the file - unsigned mf_page_size; /// number of bytes in a page - bool mf_dirty; /// true if there are dirty blocks + char *mf_fname; ///< name of the file + char *mf_ffname; ///< idem, full path + int mf_fd; ///< file descriptor + bhdr_T *mf_free_first; ///< first block header in free list + bhdr_T *mf_used_first; ///< mru block header in used list + bhdr_T *mf_used_last; ///< lru block header in used list + mf_hashtab_T mf_hash; ///< hash lists + mf_hashtab_T mf_trans; ///< trans lists + blocknr_T mf_blocknr_max; ///< highest positive block number + 1 + blocknr_T mf_blocknr_min; ///< lowest negative block number - 1 + blocknr_T mf_neg_count; ///< number of negative blocks numbers + blocknr_T mf_infile_count; ///< number of pages in the file + unsigned mf_page_size; ///< number of bytes in a page + mfdirty_T mf_dirty; } memfile_T; #endif // NVIM_MEMFILE_DEFS_H -- cgit From 87cde88c41d003988e7d5dbc4ddb26687d24923d Mon Sep 17 00:00:00 2001 From: bfredl Date: Fri, 25 Aug 2023 23:00:29 +0200 Subject: refactor(memfile): change mf_trans and mf_hash from ad-hoc hashtable to Map Memfile used a private implementation of an open hash table with intrusive collision chains, but there is no reason to assume the standard khash_t based Map won't work just fine. Yes, we are taking full ownership and maintenance over memline and memfile. No one is going to maintain it for us. Trust the plan. --- src/nvim/memfile_defs.h | 66 +++++++++---------------------------------------- 1 file changed, 12 insertions(+), 54 deletions(-) (limited to 'src/nvim/memfile_defs.h') diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h index 917dd6a905..bf9bb208a4 100644 --- a/src/nvim/memfile_defs.h +++ b/src/nvim/memfile_defs.h @@ -5,6 +5,7 @@ #include #include +#include "nvim/map.h" #include "nvim/pos.h" #include "nvim/types.h" @@ -15,57 +16,20 @@ /// with negative numbers are currently in memory only. typedef int64_t blocknr_T; -/// A hash item. -/// -/// Items' keys are block numbers. -/// Items in the same bucket are organized into a doubly-linked list. -/// -/// Therefore, items can be arbitrary data structures beginning with pointers -/// for the list and and a block number key. -typedef struct mf_hashitem { - struct mf_hashitem *mhi_next; - struct mf_hashitem *mhi_prev; - blocknr_T mhi_key; -} mf_hashitem_T; - -/// Initial size for a hashtable. -#define MHT_INIT_SIZE 64 - -/// A chained hashtable with block numbers as keys and arbitrary data structures -/// as items. -/// -/// This is an intrusive data structure: we require that items begin with -/// mf_hashitem_T which contains the key and linked list pointers. List of items -/// in each bucket is doubly-linked. -typedef struct mf_hashtab { - size_t mht_mask; ///< mask used to mod hash value to array index - ///< (nr of items in array is 'mht_mask + 1') - size_t mht_count; ///< number of items inserted - mf_hashitem_T **mht_buckets; ///< points to the array of buckets (can be - ///< mht_small_buckets or a newly allocated array - ///< when mht_small_buckets becomes too small) - mf_hashitem_T *mht_small_buckets[MHT_INIT_SIZE]; ///< initial buckets -} mf_hashtab_T; - /// A block header. /// /// There is a block header for each previously used block in the memfile. /// /// The block may be linked in the used list OR in the free list. -/// The used blocks are also kept in hash lists. /// /// The used list is a doubly linked list, most recently used block first. /// The blocks in the used list have a block of memory allocated. -/// The hash lists are used to quickly find a block in the used list. /// The free list is a single linked list, not sorted. /// The blocks in the free list have no block of memory allocated and /// the contents of the block in the file (if any) is irrelevant. typedef struct bhdr { - mf_hashitem_T bh_hashitem; ///< header for hash table and key -#define bh_bnum bh_hashitem.mhi_key ///< block number, part of bh_hashitem + blocknr_T bh_bnum; ///< key used in hash table - struct bhdr *bh_next; ///< next block header in free or used list - struct bhdr *bh_prev; ///< previous block header in used list void *bh_data; ///< pointer to memory (for used block) unsigned bh_page_count; ///< number of pages in this block @@ -74,18 +38,6 @@ typedef struct bhdr { unsigned bh_flags; ///< BH_DIRTY or BH_LOCKED } bhdr_T; -/// A block number translation list item. -/// -/// When a block with a negative number is flushed to the file, it gets -/// a positive number. Because the reference to the block is still the negative -/// number, we remember the translation to the new positive number in the -/// double linked trans lists. The structure is the same as the hash lists. -typedef struct mf_blocknr_trans_item { - mf_hashitem_T nt_hashitem; ///< header for hash table and key -#define nt_old_bnum nt_hashitem.mhi_key ///< old, negative, number - blocknr_T nt_new_bnum; ///< new, positive, number -} mf_blocknr_trans_item_T; - typedef enum { MF_DIRTY_NO = 0, ///< no dirty blocks MF_DIRTY_YES, ///< there are dirty blocks @@ -98,10 +50,16 @@ typedef struct memfile { char *mf_ffname; ///< idem, full path int mf_fd; ///< file descriptor bhdr_T *mf_free_first; ///< first block header in free list - bhdr_T *mf_used_first; ///< mru block header in used list - bhdr_T *mf_used_last; ///< lru block header in used list - mf_hashtab_T mf_hash; ///< hash lists - mf_hashtab_T mf_trans; ///< trans lists + + /// The used blocks are kept in mf_hash. + /// mf_hash are used to quickly find a block in the used list. + PMap(int64_t) mf_hash; + + /// When a block with a negative number is flushed to the file, it gets + /// a positive number. Because the reference to the block is still the negative + /// number, we remember the translation to the new positive number. + Map(int64_t, int64_t) mf_trans; + blocknr_T mf_blocknr_max; ///< highest positive block number + 1 blocknr_T mf_blocknr_min; ///< lowest negative block number - 1 blocknr_T mf_neg_count; ///< number of negative blocks numbers -- cgit From 4f8941c1a5f1ef6caa410feeb52e343db22763ce Mon Sep 17 00:00:00 2001 From: dundargoc Date: Fri, 10 Nov 2023 12:23:42 +0100 Subject: refactor: replace manual header guards with #pragma once It is less error-prone than manually defining header guards. Pretty much all compilers support it even if it's not part of the C standard. --- src/nvim/memfile_defs.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/nvim/memfile_defs.h') diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h index bf9bb208a4..1ed5a1cb62 100644 --- a/src/nvim/memfile_defs.h +++ b/src/nvim/memfile_defs.h @@ -1,5 +1,4 @@ -#ifndef NVIM_MEMFILE_DEFS_H -#define NVIM_MEMFILE_DEFS_H +#pragma once #include #include @@ -67,5 +66,3 @@ typedef struct memfile { unsigned mf_page_size; ///< number of bytes in a page mfdirty_T mf_dirty; } memfile_T; - -#endif // NVIM_MEMFILE_DEFS_H -- cgit From f4aedbae4cb1f206f5b7c6142697b71dd473059b Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 27 Nov 2023 18:39:38 +0100 Subject: build(IWYU): fix includes for undo_defs.h --- src/nvim/memfile_defs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/memfile_defs.h') diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h index 1ed5a1cb62..58ffa8ff74 100644 --- a/src/nvim/memfile_defs.h +++ b/src/nvim/memfile_defs.h @@ -5,7 +5,7 @@ #include #include "nvim/map.h" -#include "nvim/pos.h" +#include "nvim/pos_defs.h" #include "nvim/types.h" /// A block number. -- cgit From 6c14ae6bfaf51415b555e9a6b85d1d280976358d Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 27 Nov 2023 20:27:32 +0100 Subject: refactor: rename types.h to types_defs.h --- src/nvim/memfile_defs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/memfile_defs.h') diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h index 58ffa8ff74..dd7d4324e1 100644 --- a/src/nvim/memfile_defs.h +++ b/src/nvim/memfile_defs.h @@ -6,7 +6,7 @@ #include "nvim/map.h" #include "nvim/pos_defs.h" -#include "nvim/types.h" +#include "nvim/types_defs.h" /// A block number. /// -- cgit From 79b6ff28ad1204fbb4199b9092f5c578d88cb28e Mon Sep 17 00:00:00 2001 From: dundargoc Date: Tue, 28 Nov 2023 20:31:00 +0100 Subject: refactor: fix headers with IWYU --- src/nvim/memfile_defs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/memfile_defs.h') diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h index dd7d4324e1..db68ecf039 100644 --- a/src/nvim/memfile_defs.h +++ b/src/nvim/memfile_defs.h @@ -4,7 +4,7 @@ #include #include -#include "nvim/map.h" +#include "nvim/map_defs.h" #include "nvim/pos_defs.h" #include "nvim/types_defs.h" -- cgit