-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternative to Malloc for embedded device #253
Comments
It really depends on what you mean by that. You can:
When it comes to parsing, you don't get around using some allocator. The same goes for building JSON that you want to print later using As mentioned above, you can replace the global allocator completely, if this helps in your case. Use |
Hi @FSMaxB, I get JSON from API call from which I use Now since I cannot use malloc/free in this particular device, how can I create my own? My JSON string is always less than 5k which means I can do allocation statically. But never have used init hooks am at loss. TIA, |
here is a sample code: cJSON *root = NULL;
const cJSON *message = NULL;
const cJSON *success = NULL;
root = cJSON_Parse(api_json_string);
if(root == NULL)
{
show_error(cJSON_GetErrorPtr());
}
success = cJSON_GetObjectItemCaseSensitive(root, "success");
message = cJSON_GetObjectItemCaseSensitive(root, "message");
if(cJSON_IsBool(success) && cJSON_IsTrue(success))
{
show_success(message->valuestring);
}
cJSON_Delete(root); |
I have found that this device uses bare-metal approach and as such malloc is not available. Do you have advice on how I can use library for that? Is it outside use-case for this library? |
So more tests, reveals that I can use alloca(). Would simple replacement work? |
I would not recommend using Since you are using If I researched that correctly, your system has rather large memory and performance (16MB RAM, 180MHz) compared to some other systems I have seen people use cJSON on, this should definitely be enough if your JSON is not incredibly large. Probably you just have to turn on the Heap somehow, so you can use There are several malloc implementations in uClibc that you seem to be using btw. |
Hi,
Can you explain this comment? I want to avoid it, but then use it as a last resort if I fail to find any solution (since alloca works right now but malloc crashes in simple call. Not tested with CJSON yet). Do you mean I should copy
Currently I cannot use malloc. Using it even for the simplest code for sake of testing crashes. Being mainly a C++ programmer I have hard time to create statically allocated pool (especially managing global variables effectively)
You are right, memory is not an issue at all. I wonder why would malloc be a garbage in such a system with little memory constraint.
I have wasted good time trying both to no avail. I have also searched for libraries only to be confused. I will try the uclib's other malloc implementations and see if any helps. Thank you again! |
Nevermind, |
You don't need the There is no way around either fixing What you need to use a custom allocator in cJSON is to fill the Lines 75 to 79 in 0e0c463
Then you pass a pointer to that struct to Lines 135 to 136 in 0e0c463
|
Ok Thanks for the help. |
After fighting for two days, I think it is a lost battle. Malloc there does not work at all. Since I could not get Malloc to work and am not competent enough to write my own pool, am forced to find another library that is malloc free. It will be another waste of time but have no choice, sadly! I thank you for help and would keep using it in the other machines. |
Though am moving to another library, I suggest you think about non-malloc version of Thank you! |
It is simply impossible to implement I can't do any more here than provide a way to exchange the allocator that cJSON uses with your own implementation. (that will also improve in the future when you will be able to pass userdata around so you can create pool allocators with different memory pools). I doubt that it is at all possible to write a JSON library that can parse arbitrary JSON into a different data structure without requiring some kind of dynamic allocations at all. (Although it should be possible to write a parser that takes something like a JSON pointer or other kind of path and returns one single value given that, but cJSON just isn't that kind of library). I wish you good luck and please report back if you found a solution. |
Hi @mtangoo I'll like information on how you were able to parse json on the NEW8110 terminal so I may do the same. I'll appreciate libraries, sample code snippets and every other resources I can get. @FSMaxB I followed the thread above and managed to dig up two working implementations of malloc and free. When I called |
@arashifunsho are you dealing with NEW8110 too? I ended up using Tiny JSON. It is a non-malloc JSON that I have been using before diving into CJSon |
Yes, I stumbled on Tiny JSON few hours after posting that. Worked perfectly..... |
Hi,
Thanks for the useful library, it is great!
I have faced one problem with embedded device (NEW8110 EFT POS to be explicit) whereby using malloc() ends up with Data abort exception that have some code I cannot understand. Now since I want to use CJSON in this device, is there a way out? Can I use CJSON without invoking malloc at all?
Google plus searching issues here have not helped. The SDK documentation mentions nothing and the support have expired which means am on my own!
Device: NEW8110 EFT POS, ARM9
Compiler: arm-linux-uclib-gcc 4.1.2
Let me know if any more information is needed
TIA,
Stefano
The text was updated successfully, but these errors were encountered: