- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一个学校项目开发一些 ext2 文件系统的东西(实现 ls、mkdir 之类的东西),并且发现我正在为需要遍历 inode 的 i_block 的任务生成大量冗余代码。我有函数来计算 dir 条目的数量、搜索 dir 条目以查找 strcmp 名称匹配、读取数据、写入数据...遍历 i_block 似乎是许多问题的常见问题。我正在尝试为 i_block 编写类似于迭代器的东西来删除这种冗余。
我想知道什么是执行此操作的好方法?有没有在Linux系统代码中完成此操作或类似操作的示例?或者这只是一个坏主意。
到目前为止我想出的代码:
// returns block number located at iter position
// accepts a minode which is a struct wrapping an inode (in memory inode)
// accepts an iter which will self mutate and should start at 0
int iter_i_block(minode *mip, int *iter) {
static char buf[BLKSIZE]; // static buffer
// buffer number used to check if a new block needs to be read in
static int bufno;
// inode number used to determine if we are working on a new inode
static int ino;
// block number to return
int bno;
// flag for if this a different inode than last time
int new_ino = 0;
if (ino != mip->ino) {
ino = mip->ino;
new_ino = 1;
}
// direct blocks
if (*iter < 12) {
bno = mip->inode.i_block[*iter];
(*iter)++;
bufno = bno;
return bno;
}
// indirect blocks
if (*iter < 12 + BLKSIZE_1024 / sizeof(int)) {
if (!mip->inode.i_block[12])
return 0;
if (new_ino || bufno != 12)
get_block(mip->mount_entry, mip->inode.i_block[12], buf);
bufno = 12;
bno = *((int *)buf + (*iter - 12));
(*iter)++;
return bno;
}
// double indirect blocks (not shown)
// triple indirect blocks (not shown)
return 0;
}
如有任何建议,我们将不胜感激!谢谢
最佳答案
这就是我现在要做的
谢谢Gil Hamilton建议使用结构
typedef struct blk_iter {
struct minode *mip;
// buf contains the nth block
unsigned int nth;
// direct block (buf), indirection block(map1),
// double indirection(map2), triple indirection(map3);
char buf[BLKSIZE_1024], map1[BLKSIZE_1024], map2[BLKSIZE_1024],
map3[BLKSIZE_1024];
} blk_iter;
// returns a char* buffer of BLKSIZE on success
// null on failure (nothing more to read)
// must start from nth = -1
char *get_blk(blk_iter *it, int lbk) {
// calculations for convience, could be macros
int blks_per = BLKSIZE_1024 / sizeof(int);
int direct_start = 0, direct_end = 12, indirect_start = direct_end,
indirect_end = direct_end + blks_per, double_start = indirect_end,
double_end = indirect_end + blks_per * blks_per,
triple_start = double_end,
triple_end = double_end + blks_per * blks_per * blks_per;
// pointers for shorter names
unsigned int *i_block = it->mip->inode.i_block;
mount_entry *me = it->mip->mount_entry;
// null check
if (!it || !it->mip)
return 0;
// get blocks based on lbk
if (lbk < direct_end) {
// get direct block
get_block(me, i_block[lbk], it->buf);
} else if (lbk < indirect_end) {
// get indirect block
if (!(it->nth >= indirect_start && it->nth < indirect_end))
// check if map1 cached
get_block(me, i_block[12], it->map1);
get_block(me, it->map1[lbk - indirect_start], it->buf);
} else if (lbk < double_end) {
// get double indirect block
if (!(it->nth >= double_start && it->nth < double_end))
// check if map2 cached
get_block(me, i_block[13], it->map2);
if (!((lbk - double_start) / blks_per ==
(it->nth - double_start) / blks_per))
// check if map1 cached
get_block(me, it->map2[(lbk - double_start) / blks_per], it->map1);
get_block(me, it->map1[(lbk - double_start) % blks_per], it->buf);
} else if (lbk < triple_end) {
// triple indirect blocks
if (!(it->nth >= triple_start && it->nth < triple_end))
// check if map3 cached
get_block(me, i_block[12], it->map3);
if (!((lbk - triple_start) / (blks_per * blks_per) ==
(it->nth - triple_start) / (blks_per * blks_per)))
// check if map2 cached
get_block(me, it->map3[(lbk - triple_start) / (blks_per * blks_per)],
it->map2);
if (!((lbk - triple_start) / blks_per ==
(it->nth - triple_start) / blks_per))
// check if map1 cached
get_block(me, it->map2[(lbk - triple_start) / blks_per], it->map1);
get_block(me, it->map1[(lbk - triple_start) % blks_per], it->buf);
}
it->nth = lbk;
return it->buf;
}
关于c - 有没有办法在 C 中为 i_block 创建迭代器风格的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55626887/
Textmate 语法(.tmLanguage 文件)有时以 XML 格式表示。 我想转换为更易读的格式(即 JSON 或 YAML)以集成到 VS Code 语法突出显示扩展中。 为了澄清我的意思,
如何通过 pandas 样式隐藏列标签?有一个 hide_index() 方法可以删除索引行,不幸的是 hide_column() 标签会删除整个列(标题和数据)。我只想隐藏标题。谢谢! 最佳答案 s
我正在考虑为一组服务使用 SOA 架构来支持我咨询的业务,以前我们使用数据库集成,其中每个应用程序从共享的 MS SQL 数据库中挑选出它需要的东西并使用它等等。我们有各种与怪物数据库(包括 java
所以我有以下代码,我想知道 Objective-C 中哪种“风格”被认为更好。 选项 1: id temp = [dictionary objectForKey: @"aBooleanValue"];
当创建一个没有类参数的对象时,我很难决定是否应该包含空括号。一个具体的例子:我正在与现有的 Java 代码交互,并创建一个实现名为 EventResponder 的接口(interface)的对象。我
我有一个抽象类Stack和一个扩展它的类:MyStack。我需要为 MyStack 创建一个复制构造函数。只传入 MyStack 对象更好,还是传入任何 Stack 对象更好? public MySt
我正在考虑将那些在函数体中未修改的 Python 函数参数拼写为 ALL_UPPERCASE,向此类 API 的用户发出信号,表明传递的值不会被修改(如果一切都如广告所言,无论如何) )。我不知道这会
我的 build.gradle 文件、staging、stable 和 production 以及默认构建类型 debug 和 release。对于其中的每一个,我都有不同的 AAR 文件,例如,我有
假设我有以下文件: main.cpp 例程.cpp 例程.h 进一步假设 main.cpp 调用了在 routine.cpp 中定义的函数 routine(),但是 routine.cpp 还包含仅由
我对此进行了一些搜索,但实际上我还没有找到 MySQL 中用于创建外键的样式概念是什么 - 在创建表定义中或在 alter 语句中。谢谢。 最佳答案 何时创建外键: 如果在创建表时明确需要外键,则在创
您好,我正在尝试将 Android 应用风格(免费且完整)实现为动态壁纸。在 Eclipse 中,我曾经使用以下代码从我自己的 Android Activity 打开动态壁纸预览: I
我的 Android 应用程序有两种不同的风格,lite 和 pro。在应用程序中,我有一个名为 customFragment.java 的类,它包含在 main 中(不同风格之间没有区别)并且还包含
我有一个包含多个子目录的项目,如下所示: /opt/exampleProject/src ├── __init__.py ├── dir1 │ ├── __init__.py │ ├──
假设我们有类似的东西 int f(int n); .... do{ int a = b; int b = f(a); } 这样说有没有风险 do{ int b = f(b);
是否有风格指导或理由来选择其中一种模式而不是另一种? 最小化上下文管理器下的代码量“感觉”更干净,但我无法指出具体原因。这可能只是偏好,并没有关于此事的官方指导。 1) 里面的所有代码都有上下文。 w
module Hints module Designer def self.message "Hello, World!" end
我正在开发一个具有多种风格的 android 项目。 这很好用,我可以自定义应用程序的元素,例如颜色和字符串资源。 我想让一些风格基于 AppCompat 浅色主题,一些基于 AppCompat 深色
因此,这不起作用,因为 seatsAvailable 是最终的。如何使用更多的 lambda 风格的从头开始的方式来完成我想要完成的事情? final boolean seatsAvailable =
考虑以下代码: cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); sched_setaffinity(0, sizeof(cpuset
从历史上看,我总是这样编写我的异常处理代码: Cursor cursor = null; try { cursor = db.openCursor(null, null
我是一名优秀的程序员,十分优秀!