Comments (5)
Hey @Sucuturdean
An interesting albeit unusual suggestion! Are there any live examples of this implementation in other libraries?
Also, zpl supports multiple different allocation strategies, and yes, the standard heap allocation is one of them. And those strategies allow a lot of flexibility to the end user.
from zpl.
Hello @inlife
I have taken inspiration from the standard library that zig uses. In that std, you provide the size to the free function because in that language allocating returns a slice (which is a struct containing a pointer and size). In c there isn't a concept of slice, but I thought that since that is slightly faster, someone might want to use that version. I am also aware that this library is trying to be simple and a function like this might not be good for it (that's why I opened an issue first).
Just realised while writing this another reason for this. If someone already keeps tabs on the size of their allocated object, then why duplicate that variable when the user can simply pass it to the free function?
from zpl.
Let's also wait for an answer from @zaklaus, he should be back in a couple of days, and decide then.
from zpl.
Hi, thank you for the proposal!
While I understand that such a change could be helpful in some instances, I'm unfortunately unable to accept it as it doesn't fit within this library.
Let's first explore how zpl deals with memory allocation and what restrictions or requirements the library currently imposes on users to understand better why this enhancement wouldn't work in zpl:
ZPL offers multiple memory allocation strategies, all relying on a unified API to allocate, resize, and free memory; each allocation strategy has its own rules and restrictions.
The heap allocator is the simplest one, as it solely depends on your runtime's memory allocator to manage memory. Therefore it's wholly reliant on your system. Providing the size
to the free method wouldn't benefit here as it would be unused internally.
The arena/linear allocator disallows us to free allocated blobs since we don't track nor partition the memory. Instead, it offers memory snapshot capturing to imitate temporary memory allocation regions and those already "cache" the used memory and use it if we want to restore a snapshot. The size
argument wouldn't apply here since the free method is incompatible with this allocator.
The pool allocator uses a linked list of equally allocated blobs to manage the memory. Since we know the blob size beforehand, the size
argument is not required when we free memory.
The stack allocator prepends the allocated memory with a header that stores the allocated size so that when we call the free method, we already know the size we need to free. Therefore the size
argument isn't required here.
The scratch memory (ring buffer) allocator does not allow us to free individual allocations (since it wraps a fixed-size block of memory). Therefore the size
argument isn't required as the free method isn't compatible here.
In conclusion, each allocation strategy either doesn't directly support the ability to free memory, or we already know the size we need to de-allocate beforehand.
Let me know if you have any questions or ideas, and I'd gladly discuss them!
from zpl.
I will close this issue now, but if you have other suggestions, feel free to re-open it!
from zpl.
Related Issues (20)
- zpl_adt_node returned by zpl_json_parse() could have more useful parents HOT 4
- zpl_opts module should make use of ADT parser to process input HOT 1
- ADT URL parser addition
- Off-by-one in zpl_json_write_string/zpl_csv_write_string_delimiter HOT 1
- Result of zpl_alloc() is often not checked HOT 16
- Completion of error handling HOT 4
- zpl_array_fill ZPL_ASSERT error HOT 1
- zpl_opts_load_adt: allows the ability to load ADT node to fulfill CLI options
- ADT parsing of 'x' results in a ZPL_ADT_TYPE_INTEGER HOT 4
- `zpl_file_open` and functions that call it do not error when passed a directory HOT 5
- `name` and `string` fields of `zpl_json_object` have garbage values for `$schema` key HOT 6
- zpl_thread join/is_running
- Unicode support HOT 2
- pthread_join must be called for PTHREAD_CREATE_JOINABLE thread
- add 'zpl_semaphore_trywait'
- Undecorated 'cast' macro in helpers.h HOT 1
- Global-buffer-overflow in zpl_strlcpy in base64_enc HOT 2
- CSV parser assumes (unquoted) IP addresses are floating point HOT 3
- zpl_random_gen_isize / zpl_random_range_isize are not 32bit safe HOT 3
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 zpl.