2 #include <stdint.h> // for intptr_t
6 #include "pbc_memory.h"
9 /* guarantee zeroing the memory */
10 static void gmp_free(void *ptr
, size_t size
) {
16 static void* gmp_malloc(size_t size
) {
20 /* guarantee zeroing the memory
21 * realloc() is not suitable for use with secure memory
22 * because memory contents are not zeroed out. */
23 static void* gmp_realloc(void *old_ptr
, size_t old_size
, size_t new_size
) {
24 void *new_ptr
= malloc(new_size
);
25 if(new_ptr
&& old_ptr
)
26 memcpy(new_ptr
, old_ptr
, old_size
);
27 gmp_free(old_ptr
, old_size
);
31 static void gmp_guarantee_zero_memory(void) {
32 __gmp_set_memory_functions(gmp_malloc
, gmp_realloc
, gmp_free
);
35 __attribute__((constructor
)) void init(void) {
36 gmp_guarantee_zero_memory();
39 /* pbc_mem is a continuous memory keeping track of its size */
40 static inline size_t pbc_mem_get_size(size_t *p
) {
44 static inline void pbc_mem_set_size(size_t *p
, size_t size
) {
48 static inline void *pbc_mem_to_ptr(size_t *p
) {
52 static inline void *pbc_ptr_to_mem(size_t *p
) {
56 static void *pbc_mem_malloc(size_t size
) {
57 void *ptr
= malloc(size
+ sizeof(size_t));
59 pbc_mem_set_size(ptr
, size
);
63 static void pbc_mem_free(void *ptr
) {
64 memset(ptr
, 0, pbc_mem_get_size(ptr
) + sizeof(size_t));
68 static void *default_pbc_malloc(size_t size
) {
69 void *ptr
= pbc_mem_malloc(size
);
70 if(!ptr
) pbc_die("malloc() error");
71 return pbc_mem_to_ptr(ptr
);
74 static void *default_pbc_realloc(void *old
, size_t new_size
) {
75 void *new = pbc_mem_malloc(new_size
);
76 if(!new) pbc_die("realloc() error");
78 old
= pbc_ptr_to_mem(old
);
79 memcpy(pbc_mem_to_ptr(new), pbc_mem_to_ptr(old
), pbc_mem_get_size(old
));
82 return pbc_mem_to_ptr(new);
85 static void default_pbc_free(void *ptr
) {
87 pbc_mem_free(pbc_ptr_to_mem(ptr
));
90 static void *default_pbc_malloc(size_t size
) {
91 void *res
= malloc(size
);
92 if (!res
) pbc_die("malloc() error");
96 static void *default_pbc_realloc(void *ptr
, size_t size
) {
97 void *res
= realloc(ptr
, size
);
98 if (!res
) pbc_die("realloc() error");
102 static void default_pbc_free(void *ptr
) { free(ptr
); }
105 /* release memory got from pbc_malloc only by pbc_free(), do not use free() */
106 void *(*pbc_malloc
)(size_t) = default_pbc_malloc
;
107 /* pbc_realloc guarantees zeroing out the memory before moving old memory */
108 void *(*pbc_realloc
)(void *, size_t) = default_pbc_realloc
;
109 /* pbc_free guarantees zeroing out the memory */
110 void (*pbc_free
)(void *) = default_pbc_free
;
112 void pbc_set_memory_functions(void *(*malloc_fn
)(size_t),
113 void *(*realloc_fn
)(void *, size_t), void (*free_fn
)(void *)) {
114 pbc_malloc
= malloc_fn
;
115 pbc_realloc
= realloc_fn
;
119 void *pbc_calloc(size_t nmemb
, size_t size
) {
120 void *res
= pbc_malloc(nmemb
* size
);
121 if (!res
) pbc_die("calloc() error");
122 memset(res
, 0, nmemb
* size
);
126 char *pbc_strdup(const char *s
) {
127 size_t len
= strlen(s
);
128 char *res
= pbc_malloc(len
+ 1);