wip: phkmalloc calls into pma routines

This commit is contained in:
barter-simsum 2024-08-02 20:18:15 -04:00
parent 91be71d6dd
commit cf03d9ac8e
2 changed files with 380 additions and 316 deletions

9
rust/ares_pma/c-src/compile-phk.sh Normal file → Executable file
View File

@ -1 +1,8 @@
gcc -Wno-unused-result -pipe -O3 -finline-limit=5000 -fkeep-inline-functions -finline-functions -ffast-math -fomit-frame-pointer -DNDEBUG -UDEBUG -D_REENTRANT=1 -c phkmalloc.c
gcc -O0 -DDEBUG -g3 -c ./lib/checksum.c ./btree.c
gcc -Wno-unused-result -pipe -O0 -g3 -finline-limit=5000 -fkeep-inline-functions -finline-functions -ffast-math -fomit-frame-pointer -DNDEBUG -UDEBUG -D_REENTRANT=1 -c phkmalloc.c
gcc -O0 -DDEBUG -g3 ./btree.o ./checksum.o ./phkmalloc.o
# gcc -Wno-unused-result -pipe -O3 -finline-limit=5000 -fkeep-inline-functions -finline-functions -ffast-math -fomit-frame-pointer -DNDEBUG -UDEBUG -D_REENTRANT=1 phkmalloc.c

View File

@ -10,6 +10,17 @@
#include <sys/cdefs.h>
#include <stdint.h>
#include "btree.h"
#define PHKPMA
#define PHKPMA_PATH "./phkpma"
/* ;;: tmp. for main test function */
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
/* ;;: end */
/* <eric>__FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/stdlib/malloc.c,v 1.89 2004/07/04 16:11:01 stefanf Exp $"); */
/*
@ -177,7 +188,7 @@ struct pgfree {
#define MALLOC_MAGIC ((struct pginfo*) 4)
#ifndef malloc_pageshift
#define malloc_pageshift 12U
#define malloc_pageshift 14U /* ;;: Consider: should we make this the pma pagesize (14) */
#endif
#ifndef malloc_minsize
@ -231,6 +242,8 @@ static u_long last_index;
/* Pointer to page directory. Allocated "as if with" malloc */
static struct pginfo **page_dir;
static BT_state *bt_state;
/* How many slots in the page directory */
static unsigned malloc_ninfo;
@ -292,9 +305,13 @@ const char *_malloc_options;
static const char *malloc_func;
/* Macro for mmap */
#ifndef PHKPMA
#define MMAP(size) \
mmap(NULL, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
MMAP_FD, (off_t)0);
#else
#define MMAP(size) bt_malloc(bt_state, (size))
#endif
/*
* Necessary function declarations
@ -344,6 +361,7 @@ wrtwarning(char *p)
*/
static void *
map_pages(size_t pages)
#ifndef PHKPMA
{
caddr_t result, tail;
@ -367,6 +385,13 @@ map_pages(size_t pages)
return (result);
}
#else
{
/* ;;: I don't quite follow the conditional extension above. Ensure this is
correct */
return bt_malloc(bt_state, pages);
}
#endif
/*
* Extend page directory
@ -426,7 +451,7 @@ extend_pgdir(u_long index)
*/
static void
malloc_init(void)
{
{ /* ;;: call _pma_open() */
const char *p;
char b[64];
int i, j;
@ -494,8 +519,16 @@ malloc_init(void)
if (malloc_zero)
malloc_junk=1;
bt_state_new(&bt_state);
if (mkdir(PHKPMA_PATH, 0774) == -1)
abort();
if (bt_state_open(bt_state, PHKPMA_PATH, 0, 0644) != 0) {
fprintf(stderr, "Error opening PMA: %s\n", strerror(errno));
abort();
}
/* Allocate one page for the page directory */
page_dir = (struct pginfo **) MMAP(malloc_pagesize);
page_dir = (struct pginfo **) MMAP(malloc_pagesize); /* ;;: call _bt_malloc() */
if (page_dir == MAP_FAILED)
wrterror("mmap(2) failed, check limits\n");
@ -529,7 +562,7 @@ malloc_init(void)
*/
static void *
malloc_pages(size_t size)
{
{ /* ;;: call bt_malloc() */
void *p, *delay_free = NULL;
size_t i;
struct pgfree *pf;
@ -615,7 +648,7 @@ malloc_pages(size_t size)
static __inline int
malloc_make_chunks(int bits)
{
{ /* ;;: I'd guess we can avoid calling into bt routines here */
struct pginfo *bp;
void *pp;
int i, k, l;
@ -684,7 +717,7 @@ malloc_make_chunks(int bits)
*/
static void *
malloc_bytes(size_t size)
{
{ /* ;;: I'd guess we can avoid calling into bt routines here */
int i,j;
u_int u;
struct pginfo *bp;
@ -1190,6 +1223,8 @@ phkmalloc(size_t size) /* <eric> */
return (pubrealloc(NULL, size, " in malloc():"));
}
/* ;;: export a sync routine. Or I guess client program can just call pma_sync() */
void
phkfree(void *ptr) /* <eric> */
{
@ -1302,3 +1337,25 @@ phkmalloc_normalize (void * ptr) {
}
}
/* test code */
int main(int argc, char *argv[])
{
int *a, *b, *c;
a = phkmalloc(sizeof *a);
b = phkmalloc(sizeof *b);
c = phkmalloc(sizeof *c);
*a = 10;
*b = 20;
*c = 30;
fprintf(stderr, "a: %i, b: %i, c: %i", *a, *b, *c);
bt_state_close(bt_state);
if (bt_state_open(bt_state, PHKPMA_PATH, 0, 0644) != 0) {
fprintf(stderr, "Error opening PMA: %s\n", strerror(errno));
abort();
}
fprintf(stderr, "a: %i, b: %i, c: %i", *a, *b, *c);
bt_state_close(bt_state);
return 0;
}