Skip to content

jim_begin() + jim_end() #6

Open

Description

@rexim

jim_begin() cleans up the state. jim_end() closes dangling scopes.

Activity

  1. amos692001 commented on Jul 6, 2021

    @amos692001

    What's up

  2. reluekiss commented on Jan 27, 2026

    @reluekiss

    properly cleans up the serialiser and deserialiser.
    found it leaked quite a bit when you have a long running networked program that uses json.

    diff --git a/jim2.h b/jim2.h
    index a314900..8af363b 100644
    --- a/jim2.h
    +++ b/jim2.h
    @@ -65,11 +65,19 @@ void jim_object_end(Jim *jim);
     static void jim_scope_push(Jim *jim, Jim_Scope_Kind kind)
     {
         if (jim->scopes_count >= jim->scopes_capacity) {
    -        if (jim->scopes_capacity == 0) jim->scopes_capacity = JIM_SCOPES_CAPACITY;
    -        else jim->scopes_capacity *= 2;
    -        jim->scopes = realloc(jim->scopes, sizeof(*jim->scopes)*jim->scopes_capacity);
    -        assert(jim->scopes);
    +        if (jim->scopes_capacity == 0)
    +            jim->scopes_capacity = JIM_SCOPES_CAPACITY;
    +        else
    +            jim->scopes_capacity *= 2;
    +
    +        void *p = realloc(
    +            jim->scopes,
    +            sizeof(*jim->scopes) * jim->scopes_capacity
    +        );
    +        assert(p);
    +        jim->scopes = p;
         }
    +
         jim->scopes[jim->scopes_count].kind = kind;
         jim->scopes[jim->scopes_count].tail = 0;
         jim->scopes[jim->scopes_count].key = 0;
    @@ -92,12 +100,22 @@ static Jim_Scope *jim_current_scope(Jim *jim)
    
     static void jim_write(Jim *jim, const char *buffer, size_t size)
     {
    -    while (jim->sink_count + size >= jim->sink_capacity) {
    -        // TODO: rename JIM_SCOPES_CAPACITY to something else since it's used by both sink and scopes
    -        if (jim->sink_capacity == 0) jim->sink_capacity = JIM_SCOPES_CAPACITY;
    -        else jim->sink_capacity *= 2;
    -        jim->sink = realloc(jim->sink, sizeof(*jim->sink)*jim->sink_capacity);
    +    size_t need = jim->sink_count + size;
    +
    +    if (need > jim->sink_capacity) {
    +        size_t new_cap = jim->sink_capacity;
    +
    +        if (new_cap == 0) new_cap = JIM_SCOPES_CAPACITY;
    +        while (need > new_cap) {
    +            new_cap *= 2;
    +        }
    +
    +        void *p = realloc(jim->sink, new_cap);
    +        assert(p);
    +        jim->sink = p;
    +        jim->sink_capacity = new_cap;
         }
    +
         memcpy(jim->sink + jim->sink_count, buffer, size);
         jim->sink_count += size;
     }
    @@ -122,8 +140,16 @@ static int jim_get_utf8_char_len(unsigned char ch)
    
     void jim_begin(Jim *jim)
     {
    +    free(jim->sink);
    +    free(jim->scopes);
    +
    +    jim->sink = NULL;
         jim->sink_count = 0;
    +    jim->sink_capacity = 0;
    +
    +    jim->scopes = NULL;
         jim->scopes_count = 0;
    +    jim->scopes_capacity = 0;
     }
    
     void jim_element_begin(Jim *jim)
    diff --git a/jimp.h b/jimp.h
    index 995a05c..e0404fb 100644
    --- a/jimp.h
    +++ b/jimp.h
    @@ -115,11 +115,19 @@ static void jimp__append_to_string(Jimp *jimp, char x);
    
     static void jimp__append_to_string(Jimp *jimp, char x)
     {
    -    if (jimp->string_count >= jimp->string_capacity) {
    -        if (jimp->string_capacity == 0) jimp->string_capacity = 1024;
    -        else jimp->string_capacity *= 2;
    -        jimp->string = realloc(jimp->string, jimp->string_capacity);
    +    if (jimp->string_count + 1 > jimp->string_capacity) {
    +        size_t new_cap = jimp->string_capacity;
    +        if (new_cap == 0) new_cap = 1024;
    +        while (jimp->string_count + 1 > new_cap) {
    +            new_cap *= 2;
    +        }
    +
    +        void *p = realloc(jimp->string, new_cap);
    +        assert(p);
    +        jimp->string = p;
    +        jimp->string_capacity = new_cap;
         }
    +
         jimp->string[jimp->string_count++] = x;
     }
    
    @@ -255,10 +263,20 @@ static bool jimp__get_token(Jimp *jimp)
    
     void jimp_begin(Jimp *jimp, const char *file_path, const char *input, size_t input_size)
     {
    +    free(jimp->string);
    +    jimp->string = NULL;
    +    jimp->string_count = 0;
    +    jimp->string_capacity = 0;
    +
         jimp->file_path = file_path;
         jimp->start     = input;
         jimp->end       = input + input_size;
         jimp->point     = input;
    +
    +    jimp->token = JIMP_INVALID;
    +    jimp->token_start = input;
    +    jimp->number = 0.0;
    +    jimp->boolean = false;
     }
    
     void jimp_diagf(Jimp *jimp, const char *fmt, ...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions