Coverage Report

Created: 2026-08-31 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jansson/src/memory.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3
 * Copyright (c) 2011-2012 Basile Starynkevitch <basile@starynkevitch.net>
4
 *
5
 * Jansson is free software; you can redistribute it and/or modify it
6
 * under the terms of the MIT license. See LICENSE for details.
7
 */
8
9
#include <stdlib.h>
10
#include <string.h>
11
12
#include "jansson.h"
13
#include "jansson_private.h"
14
15
/* C89 allows these to be macros */
16
#undef malloc
17
#undef realloc
18
#undef free
19
20
/* memory function pointers */
21
static json_malloc_t do_malloc = malloc;
22
static json_realloc_t do_realloc = realloc;
23
static json_free_t do_free = free;
24
25
458k
void *jsonp_malloc(size_t size) {
26
458k
    if (!size)
27
0
        return NULL;
28
29
458k
    return (*do_malloc)(size);
30
458k
}
31
32
617k
void jsonp_free(void *ptr) {
33
617k
    if (!ptr)
34
158k
        return;
35
36
458k
    (*do_free)(ptr);
37
458k
}
38
39
8.02k
void *jsonp_realloc(void *ptr, size_t originalSize, size_t newSize) {
40
8.02k
    void *newMemory;
41
42
8.02k
    if (do_realloc)
43
8.02k
        return (*do_realloc)(ptr, newSize);
44
45
    // realloc emulation using malloc and free
46
0
    if (newSize == 0) {
47
0
        if (ptr != NULL)
48
0
            (*do_free)(ptr);
49
50
0
        return NULL;
51
0
    } else {
52
0
        newMemory = (*do_malloc)(newSize);
53
54
0
        if ((newMemory != NULL) && (ptr != NULL)) {
55
0
            memcpy(newMemory, ptr, (originalSize < newSize) ? originalSize : newSize);
56
57
0
            (*do_free)(ptr);
58
0
        }
59
60
0
        return newMemory;
61
0
    }
62
0
}
63
64
0
char *jsonp_strndup(const char *str, size_t len) {
65
0
    char *new_str;
66
67
0
    new_str = jsonp_malloc(len + 1);
68
0
    if (!new_str)
69
0
        return NULL;
70
71
0
    memcpy(new_str, str, len);
72
0
    new_str[len] = '\0';
73
0
    return new_str;
74
0
}
75
76
0
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) {
77
0
    do_malloc = malloc_fn;
78
0
    do_realloc = NULL;
79
0
    do_free = free_fn;
80
0
}
81
82
void json_set_alloc_funcs2(json_malloc_t malloc_fn, json_realloc_t realloc_fn,
83
0
                           json_free_t free_fn) {
84
0
    do_malloc = malloc_fn;
85
0
    do_realloc = realloc_fn;
86
0
    do_free = free_fn;
87
0
}
88
89
0
void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn) {
90
0
    if (malloc_fn)
91
0
        *malloc_fn = do_malloc;
92
0
    if (free_fn)
93
0
        *free_fn = do_free;
94
0
}
95
void json_get_alloc_funcs2(json_malloc_t *malloc_fn, json_realloc_t *realloc_fn,
96
0
                           json_free_t *free_fn) {
97
0
    if (malloc_fn)
98
0
        *malloc_fn = do_malloc;
99
0
    if (realloc_fn)
100
0
        *realloc_fn = do_realloc;
101
0
    if (free_fn)
102
0
        *free_fn = do_free;
103
0
}