Comments (6)
I'm not sure it makes sense as a macro. Data_custom_val
returns a void*
pointer, so users are expected to cast it to the right type before manipulating the contents:
struct my_custom * data = Data_custom_val(v);
data->first = 0;
data->second = NULL;
This is tersely documented here. Maybe what's missing is that Data_custom_val
doesn't return the contents itself, but a pointer inside the custom block that can be used to modify the contents. For example, the snippet above assumes that a struct with two fields is embedded inside the custom block (without indirection), and sets the fields to zero.
from ocaml.
That's not my point. I want to set the pointer returned by Data_custom_val
to NULL
.
My use case is that I have a custom block with a pointer and a finalizer. However I may know before that I want to free the pointer. In which case I free the pointer and update the value returned by Data_custom_val
to NULL. If I don't update Data_custom_val
that will lead to a double free when the custom block is finalized.
from ocaml.
You can't set the pointer returned by Data_custom_val(v)
to NULL
, because it isn't stored anywhere. It's basically &(v->snd)
, where snd
is the second field of a C struct.
Either you are storing a full struct in the custom block (| header | custom_ops | struct_field1 | struct_field2 | ... |
), in which case the individual fields can be freed but the struct itself is not malloc'ed no there's nothing to free, or you are storing a pointer (| header | custom_ops | pointer_to_struct |
) in which case Data_custom_val
gives you a pointer to the pointer, and you should be able to set the pointer to NULL normally *((my_struct **)(Data_custom_val(v))) = NULL
.
from ocaml.
Ah I see. Thanks. I think I read too much in the wording here:
Blocks with tag Custom_tag contain both arbitrary user data and a pointer to a C struct, with type struct custom_operations, that associates user-provided finalization, comparison, hashing, serialization and deserialization functions to this block.
Somehow I thought this was a two field block. One field for the custom ops and one field for the pointer.
from ocaml.
Maybe the documentation could be clarified into:
Blocks with tag Custom_tag contain a pointer to "custom operations", followed by zero, one or several words of raw data. The custom operations are stored in a C struct of type
custom_operations
and contain user-provided finalization, comparison, hashing, serialization and deserialization functions to this custom block.
from ocaml.
I can do that at some point. Just reopening so that I don't forget.
from ocaml.
Related Issues (20)
- A minor oversight in pretty-printing raw identifiers (for new types in Pexp_function) HOT 4
- `value_kind` classification for `function` cases only looks at first case HOT 2
- Ground coercion is not composable HOT 1
- Regression with default GC settings between `4.14.2` and `5.1.1` HOT 7
- Doc typo in `Printexc` HOT 2
- Abnormal exit after upgrading to OCaml 5 HOT 4
- Restore native armv7 support for NetBSD 10.0 HOT 7
- Missing warning when an attribute is ignored or makes no sense HOT 9
- Support parameter constraints in module signature type equalities HOT 5
- Update the effect handlers manual page to use syntax HOT 3
- Simplify effect handler closure and reperform by dropping the `last_fiber` argument
- Module type substitutions in signature of recursive modules are not checked for cyclicity
- Sub-module identities inside an applicative functor are actually generative, leading to invalid signatures
- Strange error message with trunk HOT 5
- "β t is less general than ∀α. α t" with trunk HOT 14
- Please provide guidance whether the static linking exception applies to distributions like DkML HOT 12
- Semantics of toplevel let-bindings in classes HOT 12
- Make extensible variants more efficient and mashalable. HOT 4
- Library archive name change in OCaml 5 (e.g libunix.a -> libunixbyt.a & libunixnat.a) HOT 3
- Type-level module alias for functor parameter accepted HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ocaml.