Skip to content

Commit

Permalink
docs: update docs.md
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed May 21, 2024
1 parent b39cf8f commit b4638f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A module written in Rust and N-API provides interface (FFI) features for Node.js

This module aims to provide similar functionality to the node-ffi module but with a completely rewritten underlying codebase. The node-ffi module has been unmaintained for several years and is no longer usable, so ffi-rs was developed to fill that void.

## features
## Features

- High performance ✨
- Better type hints 🧐
Expand Down Expand Up @@ -47,7 +47,7 @@ Finished 2 cases!

```

## changelog
## Changelog

See [CHANGELOG.md](./CHANGELOG.md)

Expand Down Expand Up @@ -710,6 +710,29 @@ load({
// { errnoCode: 22, errnoMessage: 'Invalid argument (os error 22)', value: -1 }
```
## Memory Management
It's important to free the memory allocations during a single ffi call prevent memory leak.
What kinds of data memory are allocated in this?
- call parameters in Rust environment which are allocated in the heap like `String`
- return value which in C environment which are allocated in the heap like `char*`
At default, `ffi-rs` will free all of memory both call parameters and return value(except functionConstructor).
In some cases, the called c function also free memory has been allocated after ffi-call will cause repeated release error.
For avoid this error, there are two ways to prevent `ffi-rs` free memory automatically
- set `freeResultMemory: false` when call `load` method
If you set freeResultMemory to false, `ffi-rs` will not release the return result memory which was allocated in c environment
- Use `DataType.External` as paramsType or retType
If developers use `DataType.External` as paramsType or retType, please use `freePointer` to release the memory of pointer when this memory is no longer in use. ref [test.ts](./test.ts#170)
## runInNewThread
`ffi-rs` support run ffi task in a new thread without blocking the main thread which is useful for cpu intensive task.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"test": "esno ./test.ts",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add . && git commit -m \"docs: update changelog.md\" && git push origin master",
"pub": "npm version patch && git push origin master --tags && npm run changelog",
"pub:alpha": "npm version prerelease --preid=alpha && git push origin master --tags && npm run changelog"
"pub:alpha": "npm version prerelease --preid=alpha && git push origin master --tags"
},
"optionalDependencies": {
"@yuuang/ffi-rs-darwin-arm64": "1.0.55",
Expand Down

0 comments on commit b4638f1

Please sign in to comment.