Adjust posix symbols from rtld-elf/malloc.c with the __crt_ prefix.

This allows to reuse the allocator in other environments that get
malloc(3) and related functions from libc or interposer.

MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D18988
This commit is contained in:
Konstantin Belousov 2019-01-29 22:40:42 +00:00
parent e34c029020
commit 1a3b2ebf95
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=343564
4 changed files with 59 additions and 16 deletions

View File

@ -153,7 +153,7 @@ botch(s)
*/
void *
malloc(size_t nbytes)
__crt_malloc(size_t nbytes)
{
union overhead *op;
int bucket;
@ -236,7 +236,7 @@ malloc(size_t nbytes)
}
void *
calloc(size_t num, size_t size)
__crt_calloc(size_t num, size_t size)
{
void *ret;
@ -245,7 +245,7 @@ calloc(size_t num, size_t size)
return (NULL);
}
if ((ret = malloc(num * size)) != NULL)
if ((ret = __crt_malloc(num * size)) != NULL)
memset(ret, 0, num * size);
return (ret);
@ -298,7 +298,7 @@ morecore(int bucket)
}
void
free(void * cp)
__crt_free(void *cp)
{
int size;
union overhead *op;
@ -339,7 +339,7 @@ free(void * cp)
static int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
void *
realloc(void *cp, size_t nbytes)
__crt_realloc(void *cp, size_t nbytes)
{
u_int onb;
int i;
@ -348,7 +348,7 @@ realloc(void *cp, size_t nbytes)
int was_alloced = 0;
if (cp == NULL)
return (malloc(nbytes));
return (__crt_malloc(nbytes));
op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
if (op->ov_magic == MAGIC) {
was_alloced++;
@ -393,10 +393,10 @@ realloc(void *cp, size_t nbytes)
#endif
return(cp);
} else
free(cp);
__crt_free(cp);
}
if ((res = malloc(nbytes)) == NULL)
return (NULL);
if ((res = __crt_malloc(nbytes)) == NULL)
return (NULL);
if (cp != res) /* common optimization if "compacting" */
bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
return (res);
@ -467,9 +467,11 @@ morepages(int n)
caddr_t addr = (caddr_t)
(((long)pagepool_start + pagesz - 1) & ~(pagesz - 1));
if (munmap(addr, pagepool_end - addr) != 0) {
#ifdef IN_RTLD
rtld_fdprintf(STDERR_FILENO, _BASENAME_RTLD ": "
"morepages: cannot munmap %p: %s\n",
addr, rtld_strerror(errno));
#endif
}
}
@ -478,9 +480,11 @@ morepages(int n)
if ((pagepool_start = mmap(0, n * pagesz,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, fd, 0)) == (caddr_t)-1) {
#ifdef IN_RTLD
rtld_fdprintf(STDERR_FILENO, _BASENAME_RTLD ": morepages: "
"cannot mmap anonymous memory: %s\n",
rtld_strerror(errno));
#endif
return 0;
}
pagepool_end = pagepool_start + n * pagesz;

View File

@ -66,6 +66,7 @@ __FBSDID("$FreeBSD$");
#include "paths.h"
#include "rtld_tls.h"
#include "rtld_printf.h"
#include "rtld_malloc.h"
#include "rtld_utrace.h"
#include "notes.h"
@ -5637,3 +5638,32 @@ bzero(void *dest, size_t len)
for (i = 0; i < len; i++)
((char *)dest)[i] = 0;
}
/* malloc */
void *
malloc(size_t nbytes)
{
return (__crt_malloc(nbytes));
}
void *
calloc(size_t num, size_t size)
{
return (__crt_calloc(num, size));
}
void
free(void *cp)
{
__crt_free(cp);
}
void *
realloc(void *cp, size_t nbytes)
{
return (__crt_realloc(cp, nbytes));
}

View File

@ -409,4 +409,9 @@ void pre_init(void);
void init_pltgot(Obj_Entry *);
void allocate_initial_tls(Obj_Entry *);
void *__crt_calloc(size_t num, size_t size);
void __crt_free(void *cp);
void *__crt_malloc(size_t nbytes);
void *__crt_realloc(void *cp, size_t nbytes);
#endif /* } */

View File

@ -33,13 +33,14 @@
#include <unistd.h>
#include "rtld.h"
#include "rtld_printf.h"
#include "rtld_malloc.h"
void *
xcalloc(size_t number, size_t size)
{
void *p;
p = calloc(number, size);
p = __crt_calloc(number, size);
if (p == NULL) {
rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
_exit(1);
@ -50,12 +51,15 @@ xcalloc(size_t number, size_t size)
void *
xmalloc(size_t size)
{
void *p = malloc(size);
if (p == NULL) {
rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
_exit(1);
}
return p;
void *p;
p = __crt_malloc(size);
if (p == NULL) {
rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
_exit(1);
}
return (p);
}
char *