Rizin
unix-like reverse engineering framework and cli tools
alloc.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2019-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 #include <rz_util/rz_alloc.h>
6 
7 RZ_API void rz_alloc_init(void) {
8 #if RZ_MALLOC_WRAPPER
9  rz_alloc_hooks(malloc, calloc, realloc, free);
10 #endif
11 }
12 
13 #if RZ_MALLOC_WRAPPER
14 
15 #if RZ_MALLOC_GLOBAL
20 
21 RZ_API void rz_alloc_hooks(RMalloc m, RCalloc c, RRealloc r, RFree f) {
22  rz_return_if_fail(m && c && r && f);
23  rz_malloc = m;
24  rz_calloc = c;
25  rz_realloc = r;
26  rz_free = f;
27 }
28 
29 #else
30 
31 static RMalloc *_r_malloc = malloc;
32 static RCalloc *_r_calloc = calloc;
33 static RRealloc *_r_realloc = realloc;
34 static RFree *_r_free = free;
35 
36 RZ_API void rz_alloc_hooks(RMalloc m, RCalloc c, RRealloc r, RFree f) {
37  rz_return_if_fail(m && c && r && f);
38  _r_malloc = m;
39  _r_calloc = c;
40  _r_realloc = r;
41  _r_free = f;
42 }
43 
44 RZ_API void *rz_malloc(size_t sz) {
45  return _r_malloc(sz);
46 }
47 
48 RZ_API void *rz_calloc(size_t count, size_t sz) {
49  return _r_calloc(count, sz);
50 }
51 
52 RZ_API void *rz_realloc(void *p, size_t sz) {
53  return _r_realloc(p, sz);
54 }
55 
56 RZ_API void rz_free(void *p) {
57  return _r_free(p);
58 }
59 #endif
60 #endif
61 
62 RZ_API void *rz_malloc_aligned(size_t size, size_t alignment) {
63  int offset = alignment - 1 + sizeof(void *);
64  void *p1 = _r_malloc(size + offset);
65  if (!p1) {
66  return NULL;
67  }
68  void **p2 = (void **)(((size_t)(p1) + offset) & ~(alignment - 1));
69  p2[-1] = p1;
70  return p2;
71 }
72 
73 RZ_API void rz_free_aligned(void *p) {
74  _r_free(((void **)p)[-1]);
75 }
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
voidpf uLong offset
Definition: ioapi.h:144
void * p
Definition: libc.cpp:67
RZ_API void rz_free_aligned(void *p)
Definition: alloc.c:73
RZ_API void * rz_malloc_aligned(size_t size, size_t alignment)
Definition: alloc.c:62
RZ_API void rz_alloc_init(void)
Definition: alloc.c:7
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
#define rz_malloc(x)
Definition: rz_alloc.h:41
#define rz_free(x)
Definition: rz_alloc.h:44
void() RFree(void *)
Definition: rz_alloc.h:14
void *() RRealloc(void *, size_t)
Definition: rz_alloc.h:13
#define _r_realloc
Definition: rz_alloc.h:49
#define rz_realloc(x, y)
Definition: rz_alloc.h:43
#define _r_free
Definition: rz_alloc.h:48
#define _r_calloc
Definition: rz_alloc.h:47
void *() RMalloc(size_t)
Definition: rz_alloc.h:11
#define _r_malloc
Definition: rz_alloc.h:46
#define rz_calloc(x, y)
Definition: rz_alloc.h:42
void *() RCalloc(size_t, size_t)
Definition: rz_alloc.h:12
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define f(i)
Definition: sha256.c:46
#define c(i)
Definition: sha256.c:43