Skip to content

The api in the class LibcNativeMethods are bloated #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Vincent-X-Zhang opened this issue Apr 17, 2025 · 1 comment
Open

The api in the class LibcNativeMethods are bloated #120

Vincent-X-Zhang opened this issue Apr 17, 2025 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@Vincent-X-Zhang
Copy link

The api in the class LibcNativeMethods such as:

        /// <summary>
        /// Write the BcmCanFdMessage to the socket. Variant for 32-bit.
        /// </summary>
        /// <param name="socketHandle">Socket Handle Wrapper Instance</param>
        /// <param name="message">BCM Message to write</param>
        /// <param name="msgSize">Size of BCM Message in bytes</param>
        /// <returns>The number of bytes written on success, -1 on error</returns>
        [DllImport("libc", EntryPoint="write", SetLastError=true)]
        public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanFdMessage32 message, int msgSize);

        /// <summary>
        /// Write the BcmCanSingleMessage to the socket. Variant for 32-bit.
        /// </summary>
        /// <param name="socketHandle">Socket Handle Wrapper Instance</param>
        /// <param name="message">Special Single Frame BCM Message to write</param>
        /// <param name="msgSize">Size of BCM Message in bytes</param>
        /// <returns>The number of bytes written on success, -1 on error</returns>
        [DllImport("libc", EntryPoint="write", SetLastError=true)]
        public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanSingleMessage32 message, int msgSize);

They actually are the same native api:

ssize_t write(int fd, const void* buf, size_t count);

I mean, maybe there is a better way to unify them?

@derek-will
Copy link
Owner

derek-will commented Apr 22, 2025

Your observation is 100% correct. The reason why I provide so many overloads is to allow the .NET marshaller to do most of the heavy lifting for us.

In most cases, it was/is indeed possible to provide a single P/Invoke method signature per native function which takes a generic IntPtr parameter, but then we would need to call Marshal.AllocHGlobal(), Marshal.StructureToPtr() and Marshal.FreeHGlobal() many times over and over again. While it is more code regarding P/Invokes method signatures, it is less code everywhere else. In addition, using the Marshal methods incorrectly can lead to memory leaks and so the less of that we have the safer our code will be.

@derek-will derek-will self-assigned this Apr 22, 2025
@derek-will derek-will added the question Further information is requested label Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants