1
1
mirror of https://github.com/urbit/shrub.git synced 2024-12-23 19:05:48 +03:00

u3: refactors custom allocator init for libraries

This commit is contained in:
Joe Bryan 2022-08-05 12:43:57 -04:00
parent f996844950
commit 7c9167b6ae
3 changed files with 63 additions and 98 deletions
pkg/urbit

View File

@ -1,8 +1,6 @@
#ifndef U3_ALLOCATE_H
#define U3_ALLOCATE_H
#include <openssl/opensslv.h>
#include "manage.h"
/** Constants.
@ -477,15 +475,6 @@
void*
u3a_malloc(size_t len_i);
/* u3a_malloc_ssl(): openssl-shaped malloc
*/
void*
u3a_malloc_ssl(size_t len_i
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
);
/* u3a_calloc(): aligned storage measured in bytes.
*/
void*
@ -496,39 +485,11 @@
void*
u3a_realloc(void* lag_v, size_t len_i);
/* u3a_realloc2(): gmp-shaped realloc.
*/
void*
u3a_realloc2(void* lag_v, size_t old_i, size_t new_i);
/* u3a_realloc_ssl(): openssl-shaped realloc.
*/
void*
u3a_realloc_ssl(void* lag_v, size_t len_i
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
);
/* u3a_free(): free for aligned malloc.
*/
void
u3a_free(void* tox_v);
/* u3a_free2(): gmp-shaped free.
*/
void
u3a_free2(void* tox_v, size_t siz_i);
/* u3a_free_ssl(): openssl-shaped free.
*/
void
u3a_free_ssl(void* tox_v
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
);
/* Reference and arena control.
*/
/* u3a_gain(): gain a reference count in normal space.

View File

@ -692,18 +692,6 @@ u3a_malloc(size_t len_i)
return out_w;
}
/* u3a_malloc_ssl(): openssl-shaped malloc
*/
void*
u3a_malloc_ssl(size_t len_i
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
)
{
return u3a_malloc(len_i);
}
/* u3a_cellblock(): allocate a block of cells on the hat.
*/
static c3_o
@ -885,26 +873,6 @@ u3a_realloc(void* lag_v, size_t len_i)
return u3a_wealloc(lag_v, (len_w + 3) >> 2);
}
/* u3a_realloc2(): gmp-shaped realloc.
*/
void*
u3a_realloc2(void* lag_v, size_t old_i, size_t new_i)
{
return u3a_realloc(lag_v, new_i);
}
/* u3a_realloc_ssl(): openssl-shaped realloc.
*/
void*
u3a_realloc_ssl(void* lag_v, size_t len_i
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
)
{
return u3a_realloc(lag_v, len_i);
}
/* u3a_free(): free for aligned malloc.
*/
void
@ -921,26 +889,6 @@ u3a_free(void* tox_v)
u3a_wfree(org_w);
}
/* u3a_free2(): gmp-shaped free.
*/
void
u3a_free2(void* tox_v, size_t siz_i)
{
return u3a_free(tox_v);
}
/* u3a_free_ssl(): openssl-shaped free.
*/
void
u3a_free_ssl(void* tox_v
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
)
{
return u3a_free(tox_v);
}
/* _me_wash_north(): clean up mug slots after copy.
*/
static void _me_wash_north(u3_noun dog);

View File

@ -1718,16 +1718,53 @@ _cm_signals(void)
# endif
}
extern void u3je_secp_init(void);
extern void u3je_secp_stop(void);
/* _cm_malloc_ssl(): openssl-shaped malloc
*/
static void*
_cm_malloc_ssl(size_t len_i
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
)
{
return u3a_malloc(len_i);
}
/* _cm_realloc_ssl(): openssl-shaped realloc.
*/
static void*
_cm_realloc_ssl(void* lag_v, size_t len_i
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
)
{
return u3a_realloc(lag_v, len_i);
}
/* _cm_free_ssl(): openssl-shaped free.
*/
static void
_cm_free_ssl(void* tox_v
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
, const char* file, int line
#endif
)
{
return u3a_free(tox_v);
}
extern void u3je_secp_init(void);
/* _cm_crypto(): initialize openssl and crypto jets.
*/
static void
_cm_crypto()
{
/* Initialize OpenSSL with loom allocation functions. */
if ( 0 == CRYPTO_set_mem_functions(&u3a_malloc_ssl,
&u3a_realloc_ssl,
&u3a_free_ssl) ) {
if ( 0 == CRYPTO_set_mem_functions(&_cm_malloc_ssl,
&_cm_realloc_ssl,
&_cm_free_ssl) ) {
u3l_log("%s\r\n", "openssl initialization failed");
abort();
}
@ -1735,6 +1772,22 @@ _cm_crypto()
u3je_secp_init();
}
/* _cm_realloc2(): gmp-shaped realloc.
*/
static void*
_cm_realloc2(void* lag_v, size_t old_i, size_t new_i)
{
return u3a_realloc(lag_v, new_i);
}
/* _cm_free2(): gmp-shaped free.
*/
static void
_cm_free2(void* tox_v, size_t siz_i)
{
return u3a_free(tox_v);
}
/* u3m_init(): start the environment.
*/
void
@ -1746,7 +1799,7 @@ u3m_init(void)
/* Make sure GMP uses our malloc.
*/
mp_set_memory_functions(u3a_malloc, u3a_realloc2, u3a_free2);
mp_set_memory_functions(u3a_malloc, _cm_realloc2, _cm_free2);
/* Map at fixed address.
*/
@ -1781,7 +1834,10 @@ u3m_init(void)
}
}
/* u3m_stop(): graceful shutdown cleanup. */
extern void u3je_secp_stop(void);
/* u3m_stop(): graceful shutdown cleanup.
*/
void
u3m_stop()
{