Rizin
unix-like reverse engineering framework and cli tools
alloc.c
Go to the documentation of this file.
1 #include "alloc.h"
2 #include <stdlib.h>
3 
4 static void *ts_malloc_default(size_t size) {
5  void *result = malloc(size);
6  if (size > 0 && !result) {
7  fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size);
8  exit(1);
9  }
10  return result;
11 }
12 
13 static void *ts_calloc_default(size_t count, size_t size) {
14  void *result = calloc(count, size);
15  if (count > 0 && !result) {
16  fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size);
17  exit(1);
18  }
19  return result;
20 }
21 
22 static void *ts_realloc_default(void *buffer, size_t size) {
23  void *result = realloc(buffer, size);
24  if (size > 0 && !result) {
25  fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size);
26  exit(1);
27  }
28  return result;
29 }
30 
31 // Allow clients to override allocation functions dynamically
32 void *(*ts_current_malloc)(size_t) = ts_malloc_default;
33 void *(*ts_current_calloc)(size_t, size_t) = ts_calloc_default;
34 void *(*ts_current_realloc)(void *, size_t) = ts_realloc_default;
35 void (*ts_current_free)(void *) = free;
36 
38  void *(*new_malloc)(size_t),
39  void *(*new_calloc)(size_t, size_t),
40  void *(*new_realloc)(void *, size_t),
41  void (*new_free)(void *)
42 ) {
43  ts_current_malloc = new_malloc ? new_malloc : ts_malloc_default;
44  ts_current_calloc = new_calloc ? new_calloc : ts_calloc_default;
45  ts_current_realloc = new_realloc ? new_realloc : ts_realloc_default;
46  ts_current_free = new_free ? new_free : free;
47 }
48 
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
int size_t
Definition: sftypes.h:40
Definition: buffer.h:15
static void * ts_realloc_default(void *buffer, size_t size)
Definition: alloc.c:22
static void * ts_calloc_default(size_t count, size_t size)
Definition: alloc.c:13
void(* ts_current_free)(void *)
Definition: alloc.c:35
static void * ts_malloc_default(size_t size)
Definition: alloc.c:4
void *(* ts_current_malloc)(size_t)
Definition: alloc.c:32
void *(* ts_current_calloc)(size_t, size_t)
Definition: alloc.c:33
void *(* ts_current_realloc)(void *, size_t)
Definition: alloc.c:34
void ts_set_allocator(void *(*new_malloc)(size_t), void *(*new_calloc)(size_t, size_t), void *(*new_realloc)(void *, size_t), void(*new_free)(void *))
Definition: alloc.c:37