Coder Social home page Coder Social logo

Custom allocators with context. about parson HOT 3 CLOSED

kgabis avatar kgabis commented on August 23, 2024
Custom allocators with context.

from parson.

Comments (3)

kgabis avatar kgabis commented on August 23, 2024

In this implementation you're still using a static variable but it's hidden behind parson's API. How is that better than just referencing some static context from custom malloc/free functions? e.g.

static custom_alloc_ctx_t ctx;

void* custom_malloc_wrapper(size_t s)
{
    return custom_malloc(&ctx, s);
}

void custom_free_wrapper(void *p)
{
    custom_free(&ctx, p);
}

int main()
{
    json_set_allocation_functions(custom_malloc_wrapper, custom_free_wrapper);
}

I might be missing something but I don't think adding json_set_allocation_ctx_functions to the API is necessary because you could achieve same functionality with existing API.

from parson.

Matthew-Jemielity avatar Matthew-Jemielity commented on August 23, 2024

My thinking was that the users may not want or be able to use static variables in their code to hold the context.
You're right that it's hiding static variables in parson. Currently parson holds the allocator function pointers as static variables and my implementation adds pointer to context as I was aiming for smallest changes required for this to work. To really fix that parson would need to have some state object returned to user and passed to virtually all functions and remove the use of static variables altogether. Something like:

typedef struct {
    JSON_Allocator_Ctx * ctx;
    JSON_Malloc_Ctx_Function alloc_;
    JSON_Free_Ctx_Function free_;
} JSON_Parson_State;

int main(void)
{
    JSON_Parson_State state = json_init_parson_state();
    json_set_allocation_functions(&state, custom_malloc_wrapper, custom_free_wrapper);

    JSON_Value * v = json_parse_file(&state, "file.json");
    /* ... */
    json_value_free(&state, v);
    /* and so on */
}

So this would mean far greater changes to the API. If you agree to such changes, I could prepare a patch.

from parson.

kgabis avatar kgabis commented on August 23, 2024

I realize that the only way would be to add some context to every function call but I strongly object against such change. I'm going to close this issue because the original functionality you suggested can be easily achieved using current API and "users may not want or be able to use static variables" is not very convincing.

from parson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.