| 169 | } |
| 170 | |
| 171 | CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) |
| 172 | { |
| 173 | if (hooks == NULL) |
| 174 | { |
| 175 | /* Reset hooks */ |
| 176 | global_hooks.allocate = malloc; |
| 177 | global_hooks.deallocate = free; |
| 178 | global_hooks.reallocate = realloc; |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | global_hooks.allocate = malloc; |
| 183 | if (hooks->malloc_fn != NULL) |
| 184 | { |
| 185 | global_hooks.allocate = hooks->malloc_fn; |
| 186 | } |
| 187 | |
| 188 | global_hooks.deallocate = free; |
| 189 | if (hooks->free_fn != NULL) |
| 190 | { |
| 191 | global_hooks.deallocate = hooks->free_fn; |
| 192 | } |
| 193 | |
| 194 | /* use realloc only if both free and malloc are used */ |
| 195 | global_hooks.reallocate = NULL; |
| 196 | if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) |
| 197 | { |
| 198 | global_hooks.reallocate = realloc; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* Internal constructor. */ |
| 203 | static cJSON *cJSON_New_Item(const internal_hooks * const hooks) |