Skip to content

Conversation

@Zhangshoukui
Copy link
Contributor

Summary

Increase the reference count of filep,fs_getfilep adds +1 to the reference count. After the call is complete, run the fs_putfilep command to release the reference count.When the reference count reaches 0, the file is actually closed.

Impact

This prevents the file from being closed while it is being read or written

Testing

struct file
{
int f_oflags; /* Open mode flags */
int f_refs; /* Reference count */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let us add a kconfig option to support disable this feature to save the code size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this add a lot of code size?It's not good to add too many restriction macros to code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can consider resolving similar issues in a more elegant way, eg:

#ifdef CONFIG_FS_REFCOUNT
int fs_putfilep(FAR struct file *filep)
#else
#  define fs_putfilep(f)
#endif

and also, any changes you need to consider the situation of resource-constrained devices. If you think this feature must exist and cannot be restricted by kconfig, then you should choose linux instead of nuttx

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20240829-124137

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Let me think about how to optimize。thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can consider resolving similar issues in a more elegant way, eg:

#ifdef CONFIG_FS_REFCOUNT
int fs_putfilep(FAR struct file *filep)
#else
#  define fs_putfilep(f)
#endif

and also, any changes you need to consider the situation of resource-constrained devices. If you think this feature must exist and cannot be restricted by kconfig, then you should choose linux instead of nuttx

but fs_putfilep need free filep if it's the last reference. Without reference count, it' hard to determine whether fs_putfilep should free or not free filep.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could keep the orignal free point with a special name(maybe fs_freefilep?), eg:

#ifdef CONFIG_FS_REFCOUNT
int fs_putfilep(FAR struct file *filep);
#  define fs_freefilep fs_putfilep
#else
int fs_freefilep(FAR struct file *filep);
#  define fs_putfilep(f)
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants