From ceac95c81aa8ab08530b03c3d8ac5d5e081c82be Mon Sep 17 00:00:00 2001 From: hackerchai Date: Tue, 30 Jul 2024 15:18:54 +0800 Subject: [PATCH 1/4] fix(c/libuv): Add libuv fs struct new func Signed-off-by: hackerchai --- c/libuv/_wrap/fs.c | 6 ++++++ c/libuv/fs.go | 3 +++ c/libuv/libuv.go | 1 + 3 files changed, 10 insertions(+) create mode 100644 c/libuv/_wrap/fs.c diff --git a/c/libuv/_wrap/fs.c b/c/libuv/_wrap/fs.c new file mode 100644 index 000000000..6728e4ee8 --- /dev/null +++ b/c/libuv/_wrap/fs.c @@ -0,0 +1,6 @@ +#include + +uv_fs_t uv_fs_new() { + uv_fs_t req; + return req; +} \ No newline at end of file diff --git a/c/libuv/fs.go b/c/libuv/fs.go index 62a3c1935..b29c815b3 100644 --- a/c/libuv/fs.go +++ b/c/libuv/fs.go @@ -106,6 +106,9 @@ type FsPollCb func(handle *FsPoll, status c.Int, events c.Int) /* Fs related function and method */ +//go:linkname FsNew C.uv_fs_new +func FsNew() Fs + //go:linkname FsGetType C.uv_fs_get_type func FsGetType(req *Fs) FsType diff --git a/c/libuv/libuv.go b/c/libuv/libuv.go index 9465f00e7..b11d6ad95 100644 --- a/c/libuv/libuv.go +++ b/c/libuv/libuv.go @@ -9,6 +9,7 @@ import ( const ( LLGoPackage = "link: $(pkg-config --libs libuv); -luv" + LLGoFiles = "$(pkg-config --cflags libuv): _wrap/fs.c" ) // ---------------------------------------------- From acd09d24d541f7a5f43fcefa59010dac6327c664 Mon Sep 17 00:00:00 2001 From: hackerchai Date: Tue, 30 Jul 2024 15:19:31 +0800 Subject: [PATCH 2/4] fix(c/libuv): Fix async_fs demo return 255 error & pointer not allocated error Signed-off-by: hackerchai fix(c/libuv): Mv LLGoFiles declaration Signed-off-by: hackerchai fix(c/libuv/demo): Fix return 255 error & pointer not allocated error Signed-off-by: hackerchai refactor(c/libuv): Rewrite FsNew() logic Signed-off-by: hackerchai --- c/libuv/_demo/async_fs/async_fs.go | 63 +++++++++++++++++++++--------- c/libuv/_wrap/fs.c | 7 ++-- c/libuv/fs.go | 8 +++- c/libuv/libuv.go | 1 - 4 files changed, 55 insertions(+), 24 deletions(-) diff --git a/c/libuv/_demo/async_fs/async_fs.go b/c/libuv/_demo/async_fs/async_fs.go index 5c8b42e6f..760cbe634 100644 --- a/c/libuv/_demo/async_fs/async_fs.go +++ b/c/libuv/_demo/async_fs/async_fs.go @@ -12,12 +12,12 @@ const BUFFER_SIZE = 1024 var ( loop *libuv.Loop - openReq libuv.Fs - readReq libuv.Fs - closeReq libuv.Fs + openReq *libuv.Fs + closeReq *libuv.Fs buffer [BUFFER_SIZE]c.Char iov libuv.Buf + file libuv.File ) func main() { @@ -27,11 +27,23 @@ func main() { // Initialize the loop loop = libuv.DefaultLoop() + // Initialize the requests + openReq = libuv.FsNew() + closeReq = libuv.FsNew() + if openReq == nil || closeReq == nil { + c.Fprintf(c.Stderr, c.Str("Error in FsNew\n")) + return + } + // Open the file - libuv.FsOpen(loop, &openReq, c.Str("example.txt"), os.O_RDONLY, 0, onOpen) + libuv.FsOpen(loop, openReq, c.Str("example.txt"), os.O_RDONLY, 0, onOpen) // Run the loop - libuv.Run(loop, libuv.RUN_DEFAULT) + result := libuv.Run(loop, libuv.RUN_DEFAULT) + + if result != 0 { + c.Fprintf(c.Stderr, c.Str("Error in Run: %s\n"), libuv.Strerror(libuv.Errno(result))) + } // Cleanup defer cleanup() @@ -40,32 +52,45 @@ func main() { func onOpen(req *libuv.Fs) { // Check for errors if libuv.FsGetResult(req) < 0 { - c.Fprintf(c.Stderr, c.Str("Error opening file: %s\n"), libuv.Strerror(libuv.Errno(libuv.LoopClose(loop)))) + c.Fprintf(c.Stderr, c.Str("Error opening file: %s\n"), libuv.Strerror(libuv.Errno(libuv.FsGetResult(req)))) libuv.LoopClose(loop) return } + + // Store the file descriptor + file = libuv.File(libuv.FsGetResult(req)) + // Init buffer iov = libuv.InitBuf((*c.Char)(unsafe.Pointer(&buffer[0])), c.Uint(unsafe.Sizeof(buffer))) + // Read the file - readRes := libuv.FsRead(loop, &readReq, libuv.File(libuv.FsGetResult(req)), &iov, 1, -1, onRead) + readFile() + +} + +func readFile() { + // Initialize the request + readReq := libuv.FsNew() + + // Read the file + readRes := libuv.FsRead(loop, readReq, file, &iov, 1, -1, onRead) if readRes != 0 { c.Printf(c.Str("Error in FsRead: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(readRes)), readRes) + libuv.FsReqCleanup(readReq) libuv.LoopClose(loop) - return } } func onRead(req *libuv.Fs) { + // Cleanup the request + defer libuv.FsReqCleanup(req) // Check for errors if libuv.FsGetResult(req) < 0 { c.Fprintf(c.Stderr, c.Str("Read error: %s\n"), libuv.Strerror(libuv.Errno(libuv.FsGetResult(req)))) - libuv.LoopClose(loop) } else if libuv.FsGetResult(req) == 0 { - c.Printf(c.Str("EOF\n")) // Close the file - closeRes := libuv.FsClose(loop, &closeReq, libuv.File(libuv.FsGetResult(&openReq)), onClose) + closeRes := libuv.FsClose(loop, closeReq, libuv.File(libuv.FsGetResult(openReq)), onClose) if closeRes != 0 { - // Print the content c.Printf(c.Str("Error in FsClose: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(closeRes)), closeRes) libuv.LoopClose(loop) return @@ -73,7 +98,8 @@ func onRead(req *libuv.Fs) { } else { c.Printf(c.Str("Read %d bytes\n"), libuv.FsGetResult(req)) c.Printf(c.Str("Read content: %.*s\n"), libuv.FsGetResult(req), (*c.Char)(unsafe.Pointer(&buffer[0]))) - libuv.LoopClose(loop) + // Read the file again + readFile() } } @@ -84,14 +110,15 @@ func onClose(req *libuv.Fs) { } else { c.Printf(c.Str("\nFile closed successfully.\n")) } - libuv.LoopClose(loop) } func cleanup() { // Cleanup the requests - libuv.FsReqCleanup(&openReq) - libuv.FsReqCleanup(&readReq) - libuv.FsReqCleanup(&closeReq) + libuv.FsReqCleanup(openReq) + libuv.FsReqCleanup(closeReq) // Close the loop - libuv.LoopClose(loop) + result := libuv.LoopClose(loop) + if result != 0 { + c.Fprintf(c.Stderr, c.Str("Error in LoopClose: %s\n"), libuv.Strerror(libuv.Errno(result))) + } } diff --git a/c/libuv/_wrap/fs.c b/c/libuv/_wrap/fs.c index 6728e4ee8..e3469edce 100644 --- a/c/libuv/_wrap/fs.c +++ b/c/libuv/_wrap/fs.c @@ -1,6 +1,7 @@ +#include #include -uv_fs_t uv_fs_new() { - uv_fs_t req; - return req; +uv_fs_t* uv_fs_new() { + uv_fs_t* req = malloc(sizeof(uv_fs_t)); + return req; } \ No newline at end of file diff --git a/c/libuv/fs.go b/c/libuv/fs.go index b29c815b3..1c4dfc25f 100644 --- a/c/libuv/fs.go +++ b/c/libuv/fs.go @@ -6,6 +6,10 @@ import ( "github.com/goplus/llgo/c" ) +const ( + LLGoFiles = "$(pkg-config --cflags libuv): _wrap/fs.c" +) + const ( FS_UNKNOWN FsType = -1 FS_CUSTOM FsType = 0 @@ -69,7 +73,7 @@ type File c.Int /* Handle types. */ type Fs struct { - Unused [0]byte + Unused [440]byte } type FsEvent struct { @@ -107,7 +111,7 @@ type FsPollCb func(handle *FsPoll, status c.Int, events c.Int) /* Fs related function and method */ //go:linkname FsNew C.uv_fs_new -func FsNew() Fs +func FsNew() *Fs //go:linkname FsGetType C.uv_fs_get_type func FsGetType(req *Fs) FsType diff --git a/c/libuv/libuv.go b/c/libuv/libuv.go index b11d6ad95..9465f00e7 100644 --- a/c/libuv/libuv.go +++ b/c/libuv/libuv.go @@ -9,7 +9,6 @@ import ( const ( LLGoPackage = "link: $(pkg-config --libs libuv); -luv" - LLGoFiles = "$(pkg-config --cflags libuv): _wrap/fs.c" ) // ---------------------------------------------- From f253e4fabec7106573b43c44425b012bc3890cf6 Mon Sep 17 00:00:00 2001 From: hackerchai Date: Thu, 1 Aug 2024 10:27:59 +0800 Subject: [PATCH 3/4] Revert "fix(c/libuv): Add libuv fs struct new func" This reverts commit 5fdb0a9634b5ecc29ddd50b6e5cce9938bcb7934. # Conflicts: # c/libuv/_wrap/fs.c # c/libuv/fs.go --- c/libuv/_wrap/fs.c | 7 ------- c/libuv/fs.go | 7 ------- 2 files changed, 14 deletions(-) delete mode 100644 c/libuv/_wrap/fs.c diff --git a/c/libuv/_wrap/fs.c b/c/libuv/_wrap/fs.c deleted file mode 100644 index e3469edce..000000000 --- a/c/libuv/_wrap/fs.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -uv_fs_t* uv_fs_new() { - uv_fs_t* req = malloc(sizeof(uv_fs_t)); - return req; -} \ No newline at end of file diff --git a/c/libuv/fs.go b/c/libuv/fs.go index 1c4dfc25f..9bb8f57f7 100644 --- a/c/libuv/fs.go +++ b/c/libuv/fs.go @@ -6,10 +6,6 @@ import ( "github.com/goplus/llgo/c" ) -const ( - LLGoFiles = "$(pkg-config --cflags libuv): _wrap/fs.c" -) - const ( FS_UNKNOWN FsType = -1 FS_CUSTOM FsType = 0 @@ -110,9 +106,6 @@ type FsPollCb func(handle *FsPoll, status c.Int, events c.Int) /* Fs related function and method */ -//go:linkname FsNew C.uv_fs_new -func FsNew() *Fs - //go:linkname FsGetType C.uv_fs_get_type func FsGetType(req *Fs) FsType From 5dd5494f93ea3bfb6f001b8618e3b010e944108b Mon Sep 17 00:00:00 2001 From: hackerchai Date: Thu, 1 Aug 2024 10:52:59 +0800 Subject: [PATCH 4/4] refactor(c/libuv): Adapt libuv.Fs struct --- c/libuv/_demo/async_fs/async_fs.go | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/c/libuv/_demo/async_fs/async_fs.go b/c/libuv/_demo/async_fs/async_fs.go index 760cbe634..14b50ecde 100644 --- a/c/libuv/_demo/async_fs/async_fs.go +++ b/c/libuv/_demo/async_fs/async_fs.go @@ -12,8 +12,8 @@ const BUFFER_SIZE = 1024 var ( loop *libuv.Loop - openReq *libuv.Fs - closeReq *libuv.Fs + openReq libuv.Fs + closeReq libuv.Fs buffer [BUFFER_SIZE]c.Char iov libuv.Buf @@ -27,16 +27,8 @@ func main() { // Initialize the loop loop = libuv.DefaultLoop() - // Initialize the requests - openReq = libuv.FsNew() - closeReq = libuv.FsNew() - if openReq == nil || closeReq == nil { - c.Fprintf(c.Stderr, c.Str("Error in FsNew\n")) - return - } - // Open the file - libuv.FsOpen(loop, openReq, c.Str("example.txt"), os.O_RDONLY, 0, onOpen) + libuv.FsOpen(loop, &openReq, c.Str("example.txt"), os.O_RDONLY, 0, onOpen) // Run the loop result := libuv.Run(loop, libuv.RUN_DEFAULT) @@ -69,14 +61,14 @@ func onOpen(req *libuv.Fs) { } func readFile() { - // Initialize the request - readReq := libuv.FsNew() + // Initialize the request every time + var readReq libuv.Fs // Read the file - readRes := libuv.FsRead(loop, readReq, file, &iov, 1, -1, onRead) + readRes := libuv.FsRead(loop, &readReq, file, &iov, 1, -1, onRead) if readRes != 0 { c.Printf(c.Str("Error in FsRead: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(readRes)), readRes) - libuv.FsReqCleanup(readReq) + libuv.FsReqCleanup(&readReq) libuv.LoopClose(loop) } } @@ -89,7 +81,7 @@ func onRead(req *libuv.Fs) { c.Fprintf(c.Stderr, c.Str("Read error: %s\n"), libuv.Strerror(libuv.Errno(libuv.FsGetResult(req)))) } else if libuv.FsGetResult(req) == 0 { // Close the file - closeRes := libuv.FsClose(loop, closeReq, libuv.File(libuv.FsGetResult(openReq)), onClose) + closeRes := libuv.FsClose(loop, &closeReq, libuv.File(libuv.FsGetResult(&openReq)), onClose) if closeRes != 0 { c.Printf(c.Str("Error in FsClose: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(closeRes)), closeRes) libuv.LoopClose(loop) @@ -114,8 +106,8 @@ func onClose(req *libuv.Fs) { func cleanup() { // Cleanup the requests - libuv.FsReqCleanup(openReq) - libuv.FsReqCleanup(closeReq) + libuv.FsReqCleanup(&openReq) + libuv.FsReqCleanup(&closeReq) // Close the loop result := libuv.LoopClose(loop) if result != 0 {