Rizin
unix-like reverse engineering framework and cli tools
alloc.c File Reference
#include "alloc.h"
#include <stdlib.h>

Go to the source code of this file.

Functions

static void * ts_malloc_default (size_t size)
 
static void * ts_calloc_default (size_t count, size_t size)
 
static void * ts_realloc_default (void *buffer, size_t size)
 
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 *))
 

Variables

void *(* ts_current_malloc )(size_t) = ts_malloc_default
 
void *(* ts_current_calloc )(size_t, size_t) = ts_calloc_default
 
void *(* ts_current_realloc )(void *, size_t) = ts_realloc_default
 
void(* ts_current_free )(void *) = free
 

Function Documentation

◆ ts_calloc_default()

static void* ts_calloc_default ( size_t  count,
size_t  size 
)
static

Definition at line 13 of file alloc.c.

13  {
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 }
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
voidpf void uLong size
Definition: ioapi.h:138
void * calloc(size_t number, size_t size)
Definition: malloc.c:102

References calloc(), count, and test-lz4-list::exit.

Referenced by ts_set_allocator().

◆ ts_malloc_default()

static void* ts_malloc_default ( size_t  size)
static

Definition at line 4 of file alloc.c.

4  {
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 }
void * malloc(size_t size)
Definition: malloc.c:123

References test-lz4-list::exit, and malloc().

Referenced by ts_set_allocator().

◆ ts_realloc_default()

static void* ts_realloc_default ( void *  buffer,
size_t  size 
)
static

Definition at line 22 of file alloc.c.

22  {
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 }
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
Definition: buffer.h:15

References test-lz4-list::exit, and realloc().

Referenced by ts_set_allocator().

◆ ts_set_allocator()

void ts_set_allocator ( void *(*)(size_t new_malloc,
void *(*)(size_t, size_t new_calloc,
void *(*)(void *, size_t new_realloc,
void(*)(void *)  new_free 
)

Set the allocation functions used by the library.

By default, Tree-sitter uses the standard libc allocation functions, but aborts the process when an allocation fails. This function lets you supply alternative allocation functions at runtime.

If you pass NULL for any parameter, Tree-sitter will switch back to its default implementation of that function.

If you call this function after the library has already been used, then you must ensure that either:

  1. All the existing objects have been freed.
  2. The new allocator shares its state with the old one, so it is capable of freeing memory that was allocated by the old allocator.

Definition at line 37 of file alloc.c.

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 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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

References free(), ts_calloc_default(), ts_current_calloc, ts_current_free, ts_current_malloc, ts_current_realloc, ts_malloc_default(), and ts_realloc_default().

Variable Documentation

◆ ts_current_calloc

void*(* ts_current_calloc) (size_t, size_t) ( size_t  ,
size_t   
) = ts_calloc_default

Definition at line 33 of file alloc.c.

Referenced by ts_set_allocator().

◆ ts_current_free

void(* ts_current_free) (void *) ( void *  ) = free

Definition at line 35 of file alloc.c.

Referenced by ts_set_allocator().

◆ ts_current_malloc

void*(* ts_current_malloc) (size_t) ( size_t  ) = ts_malloc_default

Definition at line 32 of file alloc.c.

Referenced by ts_set_allocator().

◆ ts_current_realloc

void*(* ts_current_realloc) (void *, size_t) ( void *  ,
size_t   
) = ts_realloc_default

Definition at line 34 of file alloc.c.

Referenced by ts_set_allocator().