Comments (13)
You should implement cJSON_Hooks and discourage them from doing the same.
from cjson.
I propose adding cJSON_Hooks as optional parameter to all the functions that are doing frees/allocations. That would be part of the breaking changes for 1.0.0
from cjson.
What is passed will be a pointer to cJSON_Hooks
and if null, regular malloc
and free
will be used.
from cjson.
Nice.
from cjson.
While you're there, it might be wise to remove unbuffered printing entirely, especially since you have ensure() working nicely now :)
from cjson.
I would suggest rename cJSON_Delete to cJSON_DeleteObject and create a new API cJSON_DeleteBuffer which frees the print buffer using hook. Let me know I can create patch for that.
from cjson.
No. By allowing the user to pass in a pointer to an instance of cJSON_Hooks
, the user of the library knows which allocator was used for allocating the printbuffer, so no special function is required for that, just use the deallocator that fits the allocator you gave to cJSON for printing.
from cjson.
Is there currently any way using cJSON_Hooks to pass along another void pointer that could go to the allocator? I can't see a way. This would be necessary to do something like using a memory pool allocator? Obviously this would require major upheaval to the api, but seems if this is going to happen, now would be the time. Is there any interest in looking at that now, or would it be too invasive to make sense?
from cjson.
This sounds feasible for v2. My idea was to have an extended cJSON_Hooks
struct that get's passed in (as pointer) to every function that allocates memory. If left NULL
, defaults would be used. That new cJSON_Hooks is also supposed to have a pointer to a realloc function (optional) and may also contain the error pointer (or rather error position).
Allocator functions that take a void pointer could be added as separate fields to this struct (or be the default, not sure).
I will definitely put this feature on the list.
from cjson.
Yes.. this approach makes sense... the other way to do it.. which probably has value, is to include a concept of a "parent" object, and work the api to pull that void pointer from the parent object. Having access to the parent to be able to walk back up to the root json may also have other unexpected advantages. Anyway all food for thought. We are not using anything like this now, but have at least talked about how it would be useful.
from cjson.
Interesting idea. But I don't think this works for allocators because any object can be detached from the tree and become it's own root at any time.
from cjson.
I added cJSON_malloc
and cJSON_free
on the develop branch. Since version 2 is still way ahead I decided to just add simple wrappers that call the internal functions. They will be removed again in version 2.
This is still quite prone to race conditions though.
from cjson.
cJSON_malloc
and cJSON_free
are now available with version 1.5.0
from cjson.
Related Issues (20)
- Help - Get value of a key
- Inputs not validated HOT 2
- cJSON_ParseWithOpts("{xx}", return_parse_end, 0) sets *return_parse_end to "x}"
- cJSON_GetArraySize overflow HOT 1
- Enhancement: Functions to get object reference to children starting with prefix/suffix
- OOB access in `parse_string` HOT 1
- bug for cJSON_InsertItemInArray function HOT 4
- bug for cJSON_SetValuestring HOT 4
- heap-buffer-overflow in cJSON_ParseWithLengthOpts HOT 1
- An outside the range found in cJSON_SetNumberHelper HOT 1
- An 'outside the range of int' bug found in cJSON_CreateNumber HOT 2
- An infinite loop found in cJSON_DeleteItemFromObjectCaseSensitive HOT 2
- Can't link from CMake HOT 1
- Installing with CMake's FetchContent creates problems with header files
- CMake ≥ 3.13 - Policy CMP0077 (option() behavior)
- decode_pointer_inplace is not working as it should
- Strange behavior observed in cJSON_ReplaceItemInArray HOT 1
- const-qualify cJSON * in cJSON_PrintPreallocated
- Configuring allocators when using FreeRTOS? HOT 1
- Inconsistent error in cJSON_AddItemReferenceToObject
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 cjson.