other benchmarks

This commit is contained in:
2026-04-10 02:50:17 +01:00
parent 913d17224b
commit dc9c8578b6
11 changed files with 4342 additions and 8059 deletions

View File

@@ -1,10 +1,13 @@
scp ../../start.S home-1:/home/akilan/cheri/output/sdk/bin/
# scp main.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc_test.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../link.ld home-1:/home/akilan/cheri/output/sdk/bin/
scp memaccess.c home-1:/home/akilan/cheri/output/sdk/bin/
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c memaccess.c'
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c memaccess.c'
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -DDEFINE_MALLOC -DDEFINE_FREE -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -T link.ld -o testC start.S memaccess.c malloc.c malloc_test.c'
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC'

View File

@@ -27,13 +27,12 @@ All rights reserved.
#define NPAD 7
#define MIN_WSS sizeof(struct l)
#define MAX_WSS 70
#define MAX_WSS 32 //2^32
void free(void * __capability ptr);
void * __capability malloc(size_t size);
// struct l{
// struct l *n;
// struct l *p;
@@ -277,7 +276,7 @@ void * walk(void * __capability param)
// printf("%d,%ld,%ld,%ld\n",data->thread_index,data->working_set_size, end-start, data->working_set_size/sizeof(struct l));
// free(data);
free(data);
return NULL;
}
@@ -348,7 +347,7 @@ int main(void)
free(root);
root = NULL;
// root = NULL;
return 0;

View File

@@ -0,0 +1,16 @@
scp ../../start.S home-1:/home/akilan/cheri/output/sdk/bin/
# scp main.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc_test.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../link.ld home-1:/home/akilan/cheri/output/sdk/bin/
scp richards.c home-1:/home/akilan/cheri/output/sdk/bin/
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c memaccess.c'
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -DDEFINE_MALLOC -DDEFINE_FREE -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -T link.ld -o testC start.S richards.c malloc.c malloc_test.c'
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC'
scp home-1:/home/akilan/cheri/output/sdk/bin/testC ../../../

View File

@@ -0,0 +1,452 @@
// clang-format off
/* C version of the systems programming language benchmark
** Author: M. J. Jordan Cambridge Computer Laboratory.
**
** Modified by: M. Richards, Nov 1996
** to be ANSI C and runnable on 64 bit machines + other minor changes
** Modified by: M. Richards, 20 Oct 1998
** made minor corrections to improve ANSI compliance (suggested
** by David Levine)
** Modified by: Jeremy Singer, 25 May 2022
** adapted to use uintptr_t for compatibility with architectures
** that do not have word-sized pointers (e.g. CHERI)
** also adapted to use Boehm-Demers-Weiser GC instead of system
** malloc, when compiled with -DGC
**
** Compile with, say
**
** gcc -o bench bench.c
**
** or
**
** gcc -o bench100 -Dbench100 bench.c (for a version that obeys
** the main loop 100x more often)
*/
// #include <stdio.h>
#include <stdint.h>
#include <stddef.h>
// #include <stdlib.h>
// #include "harness.h"
// #include "stddefines.h"
// #ifdef GC
// #include "gc.h"
// #define MALLOC GC_MALLOC
// #else
// #define MALLOC malloc
// #endif // GC
// #define malloc tiny_malloc
// #define free tiny_free
void* tiny_malloc(size_t);
void tiny_free(void*);
#ifdef bench100
#define Count 10000*100
#define Qpktcountval 2326410
#define Holdcountval 930563
#else
#define Count 10000
#define Qpktcountval 23246
#define Holdcountval 9297
#endif
#define TRUE 1
#define FALSE 0
#define MAXINT 32767
#define BUFSIZE 3
#define I_IDLE 1
#define I_WORK 2
#define I_HANDLERA 3
#define I_HANDLERB 4
#define I_DEVA 5
#define I_DEVB 6
#define PKTBIT 1
#define WAITBIT 2
#define HOLDBIT 4
#define NOTPKTBIT !1
#define NOTWAITBIT !2
#define NOTHOLDBIT 0XFFFB
#define S_RUN 0
#define S_RUNPKT 1
#define S_WAIT 2
#define S_WAITPKT 3
#define S_HOLD 4
#define S_HOLDPKT 5
#define S_HOLDWAIT 6
#define S_HOLDWAITPKT 7
#define K_DEV 1000
#define K_WORK 1001
struct packet
{
struct packet *p_link;
int p_id;
int p_kind;
int p_a1;
char p_a2[BUFSIZE+1];
};
struct task
{
struct task *t_link;
int t_id;
int t_pri;
struct packet *t_wkq;
int t_state;
struct task *(*t_fn)(struct packet *);
uintptr_t t_v1;
uintptr_t t_v2;
};
char alphabet[28] = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct task *tasktab[11] = {(struct task *)10,0,0,0,0,0,0,0,0,0,0};
struct task *tasklist = 0;
struct task *tcb;
long taskid;
uintptr_t v1;
uintptr_t v2;
int qpktcount = 0;
int holdcount = 0;
int tracing = 0;
int layout = 0;
void append(struct packet *pkt, struct packet *ptr);
void createtask(int id,
int pri,
struct packet *wkq,
int state,
struct task *(*fn)(struct packet *),
uintptr_t v1,
uintptr_t v2)
{
struct task *t = (struct task *)tiny_malloc(sizeof(struct task));
tasktab[id] = t;
t->t_link = tasklist;
t->t_id = id;
t->t_pri = pri;
t->t_wkq = wkq;
t->t_state = state;
t->t_fn = fn;
t->t_v1 = v1;
t->t_v2 = v2;
tasklist = t;
}
struct packet *pkt(struct packet *link, int id, int kind)
{
int i;
struct packet *p = (struct packet *)tiny_malloc(sizeof(struct packet));
for (i=0; i<=BUFSIZE; i++)
p->p_a2[i] = 0;
p->p_link = link;
p->p_id = id;
p->p_kind = kind;
p->p_a1 = 0;
return (p);
}
void trace(char a)
{
if ( --layout <= 0 )
{
// printf("\n");
layout = 50;
}
// printf("%c", a);
}
void schedule()
{
while ( tcb != 0 )
{
struct packet *pkt;
struct task *newtcb;
pkt=0;
switch ( tcb->t_state )
{
case S_WAITPKT:
pkt = tcb->t_wkq;
tcb->t_wkq = pkt->p_link;
tcb->t_state = tcb->t_wkq == 0 ? S_RUN : S_RUNPKT;
case S_RUN:
case S_RUNPKT:
taskid = tcb->t_id;
v1 = tcb->t_v1;
v2 = tcb->t_v2;
if (tracing) {
trace(taskid+'0');
}
newtcb = (*(tcb->t_fn))(pkt);
tcb->t_v1 = v1;
tcb->t_v2 = v2;
tcb = newtcb;
break;
case S_WAIT:
case S_HOLD:
case S_HOLDPKT:
case S_HOLDWAIT:
case S_HOLDWAITPKT:
tcb = tcb->t_link;
break;
default:
return;
}
}
}
struct task *wait_task(void)
{
tcb->t_state |= WAITBIT;
return (tcb);
}
struct task *holdself(void)
{
++holdcount;
tcb->t_state |= HOLDBIT;
return (tcb->t_link) ;
}
struct task *findtcb(int id)
{
struct task *t = 0;
if (1<=id && id<=(long)tasktab[0])
t = tasktab[id];
// if (t==0) printf("\nBad task id %d\n", id);
return(t);
}
struct task *release(int id)
{
struct task *t;
t = findtcb(id);
if ( t==0 ) return (0);
t->t_state &= NOTHOLDBIT;
if ( t->t_pri > tcb->t_pri ) return (t);
return (tcb) ;
}
struct task *qpkt(struct packet *pkt)
{
struct task *t;
t = findtcb(pkt->p_id);
if (t==0) return (t);
qpktcount++;
pkt->p_link = 0;
pkt->p_id = taskid;
if (t->t_wkq==0)
{
t->t_wkq = pkt;
t->t_state |= PKTBIT;
if (t->t_pri > tcb->t_pri) return (t);
}
else
{
append(pkt, (struct packet *)&(t->t_wkq));
}
return (tcb);
}
struct task *idlefn(struct packet *pkt)
{
if ( --v2==0 ) return ( holdself() );
if ( (v1&1) == 0 )
{
v1 = ( v1>>1) & MAXINT;
return ( release(I_DEVA) );
}
else
{
v1 = ( (v1>>1) & MAXINT) ^ 0XD008;
return ( release(I_DEVB) );
}
}
struct task *workfn(struct packet *pkt)
{
if ( pkt==0 ) return ( wait_task() );
else
{
int i;
v1 = I_HANDLERA + I_HANDLERB - v1;
pkt->p_id = v1;
pkt->p_a1 = 0;
for (i=0; i<=BUFSIZE; i++)
{
v2++;
if ( v2 > 26 ) v2 = 1;
(pkt->p_a2)[i] = alphabet[v2];
}
return ( qpkt(pkt) );
}
}
struct task *handlerfn(struct packet *pkt)
{
if ( pkt!=0) {
append(pkt, (struct packet *)(pkt->p_kind==K_WORK ? &v1 : &v2));
}
if ( v1!=0 ) {
int count;
struct packet *workpkt = (struct packet *)v1;
count = workpkt->p_a1;
if ( count > BUFSIZE ) {
v1 = (uintptr_t)(((struct packet *)v1)->p_link);
return ( qpkt(workpkt) );
}
if ( v2!=0 ) {
struct packet *devpkt;
devpkt = (struct packet *)v2;
v2 = (uintptr_t)(((struct packet *)v2)->p_link);
devpkt->p_a1 = workpkt->p_a2[count];
workpkt->p_a1 = count+1;
return( qpkt(devpkt) );
}
}
return wait_task();
}
struct task *devfn(struct packet *pkt)
{
if ( pkt==0 )
{
if ( v1==0 ) return ( wait_task() );
pkt = (struct packet *)v1;
v1 = 0;
return ( qpkt(pkt) );
}
else
{
v1 = (uintptr_t)pkt;
if (tracing) trace(pkt->p_a1);
return ( holdself() );
}
}
void append(struct packet *pkt, struct packet *ptr)
{
pkt->p_link = 0;
while ( ptr->p_link ) ptr = ptr->p_link;
ptr->p_link = pkt;
}
int bench() {
struct packet *wkq = 0;
// printf("Bench mark starting\n");
createtask(I_IDLE, 0, wkq, S_RUN, idlefn, 1, Count);
wkq = pkt(0, 0, K_WORK);
wkq = pkt(wkq, 0, K_WORK);
createtask(I_WORK, 1000, wkq, S_WAITPKT, workfn, I_HANDLERA, 0);
wkq = pkt(0, I_DEVA, K_DEV);
wkq = pkt(wkq, I_DEVA, K_DEV);
wkq = pkt(wkq, I_DEVA, K_DEV);
createtask(I_HANDLERA, 2000, wkq, S_WAITPKT, handlerfn, 0, 0);
wkq = pkt(0, I_DEVB, K_DEV);
wkq = pkt(wkq, I_DEVB, K_DEV);
wkq = pkt(wkq, I_DEVB, K_DEV);
createtask(I_HANDLERB, 3000, wkq, S_WAITPKT, handlerfn, 0, 0);
wkq = 0;
createtask(I_DEVA, 4000, wkq, S_WAIT, devfn, 0, 0);
createtask(I_DEVB, 5000, wkq, S_WAIT, devfn, 0, 0);
tcb = tasklist;
qpktcount = holdcount = 0;
// printf("Starting\n");
tracing = FALSE;
layout = 0;
schedule();
// printf("\nfinished\n");
// if (!(qpktcount == Qpktcountval && holdcount == Holdcountval)) {
// printf("qpkt count = %d holdcount = %d\n", qpktcount, holdcount);
// printf("These results are incorrect");
// exit(1);
// }
// printf("\nend of run\n");
return qpktcount;
}
int inner_loop(int inner) {
int r = 0;
while (inner > 0) {
r += bench();
inner--;
}
return r;
}
int main(int argc, char* argv[])
{
//INITREGULARALLOC();
int iterations = 100;
int warmup = 0;
int inner_iterations = 100;
// parse_argv(argc, argv, &iterations, &warmup, &inner_iterations);
int result = 0;
while (iterations > 0) {
// unsigned long start = microseconds();
result += inner_loop(inner_iterations);
// unsigned long elapsed = microseconds() - start;
// printf("Richards: iterations=1 runtime: %lu%s\n", elapsed, "us");
// print(elapsed);
iterations--;
}
}

View File

@@ -1,10 +1,13 @@
scp ../../start.S home-1:/home/akilan/cheri/output/sdk/bin/
# scp main.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc_test.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../link.ld home-1:/home/akilan/cheri/output/sdk/bin/
scp glibc.c home-1:/home/akilan/cheri/output/sdk/bin/
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c glibc.c'
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c memaccess.c'
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -DDEFINE_MALLOC -DDEFINE_FREE -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -T link.ld -o testC start.S glibc.c malloc.c malloc_test.c'
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC'

View File

@@ -0,0 +1,16 @@
scp ../../start.S home-1:/home/akilan/cheri/output/sdk/bin/
# scp main.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../malloc_test.c home-1:/home/akilan/cheri/output/sdk/bin/
scp ../../link.ld home-1:/home/akilan/cheri/output/sdk/bin/
scp kmeans.c home-1:/home/akilan/cheri/output/sdk/bin/
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c memaccess.c'
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -DDEFINE_MALLOC -DDEFINE_FREE -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -T link.ld -o testC start.S kmeans.c malloc.c malloc_test.c'
# ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC'
scp home-1:/home/akilan/cheri/output/sdk/bin/testC ../../../

View File

@@ -0,0 +1,123 @@
// #include <cheri_init_globals.h>
// #include <cheritypes.h>
#include <cheriintrin.h>
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
#include <float.h>
// #include <math.h>
/* --- Simple Bare-Metal CHERI Malloc --- */
// #define HEAP_SIZE 0x10000
// static uint8_t raw_heap[HEAP_SIZE] __attribute__((aligned(16)));
// static size_t heap_ptr = 0;
// void* cheri_malloc(size_t size) {
// // Ensure 16-byte alignment for CHERI capability representability
// size = (size + 15) & ~15;
// if (heap_ptr + size > HEAP_SIZE) return NULL;
// // Get a capability to the heap slice
// void *ptr = cheri_get_base(&raw_heap[heap_ptr]);
// // Set strict bounds on the returned capability
// ptr = cheri_set_bounds(ptr, size);
// heap_ptr += size;
// return ptr;
// }
void free(void * __capability ptr);
void * __capability malloc(size_t size);
/* --- K-Means Hybrid Structures --- */
typedef struct {
float x, y;
} Point;
typedef struct {
float x, y;
int count;
} Centroid;
float get_distance(Point p, Centroid c) {
float dx = p.x - c.x;
float dy = p.y - c.y;
return (dx * dx) + (dy * dy); // Squared Euclidean
}
/* --- Main Logic --- */
void run_kmeans(Point * __capability points, int num_points, int k, int iterations) {
// Allocate centroids using our CHERI-bounded malloc
Centroid * __capability centroids = (Centroid * __capability)malloc(sizeof(Centroid) * k);
int * __capability assignments = (int * __capability)malloc(sizeof(int) * num_points);
if (!centroids || !assignments) return;
// Initialize Centroids (Simple sequential pick)
for (int i = 0; i < k; i++) {
centroids[i].x = points[i].x;
centroids[i].y = points[i].y;
}
for (int iter = 0; iter < iterations; iter++) {
// 1. Assignment Step
for (int i = 0; i < num_points; i++) {
float min_dist = FLT_MAX;
int best_cluster = 0;
for (int j = 0; j < k; j++) {
float d = get_distance(points[i], centroids[j]);
if (d < min_dist) {
min_dist = d;
best_cluster = j;
}
}
assignments[i] = best_cluster;
}
// 2. Update Step
for (int i = 0; i < k; i++) {
centroids[i].x = 0;
centroids[i].y = 0;
centroids[i].count = 0;
}
for (int i = 0; i < num_points; i++) {
int cluster = assignments[i];
centroids[cluster].x += points[i].x;
centroids[cluster].y += points[i].y;
centroids[cluster].count++;
}
for (int i = 0; i < k; i++) {
if (centroids[i].count > 0) {
centroids[i].x /= centroids[i].count;
centroids[i].y /= centroids[i].count;
}
}
}
}
int main() {
int n = 10;
int k = 3;
// Allocate point data on our CHERI heap
Point * __capability data = (Point * __capability)malloc(sizeof(Point) * n);
if (data) {
// Mock data initialization
for(int i = 0; i < n; i++) {
data[i].x = (float)(i % 10);
data[i].y = (float)(i / 10);
}
run_kmeans(data, n, k, 10);
}
return 0;
}

View File

@@ -16,6 +16,8 @@
void* tiny_malloc(size_t);
void tiny_free(void*);
#define ALIGN 16
// #define HEAP_SIZE 65536 // 64 KB heap
// #define PHYS_BASE 0x80000000
@@ -57,7 +59,11 @@ void * __capability malloc(size_t size) {
cap = cheri_bounds_set(cap, size);
cap = add_delta(cap, 12);
int delta = 12;
delta = (delta + ALIGN - 1) & ~(ALIGN - 1);
cap = add_delta(cap, delta);
// // Align to 8 bytes (important for capability safety)
// size = (size + 7) & ~7;

View File

@@ -64,17 +64,17 @@
void* tiny_malloc(size_t);
void tiny_free(void*);
// void* realloc(void*, size_t);
// void* memalign(size_t, size_t);
// void* valloc(size_t);
// void* pvalloc(size_t);
// void* calloc(size_t, size_t);
// void cfree(void*);
// int malloc_trim(size_t);
// size_t malloc_usable_size(void*);
// void malloc_stats(void);
// int mallopt(int, int);
// struct mallinfo mallinfo(void);
void* realloc(void*, size_t);
void* memalign(size_t, size_t);
void* valloc(size_t);
void* pvalloc(size_t);
void* calloc(size_t, size_t);
void cfree(void*);
int malloc_trim(size_t);
size_t malloc_usable_size(void*);
void malloc_stats(void);
int mallopt(int, int);
struct mallinfo mallinfo(void);
typedef struct freelist_entry {
size_t size;
@@ -273,329 +273,329 @@ tiny_free (void *block_p)
}
#endif
// #ifdef DEFINE_REALLOC
// void *
// realloc (void *block_p, size_t sz)
// {
// fle block = (fle)((size_t) block_p - offsetof (struct freelist_entry, next));
// size_t real_size = REAL_SIZE (sz);
// size_t old_real_size;
#ifdef DEFINE_REALLOC
void *
realloc (void *block_p, size_t sz)
{
fle block = (fle)((size_t) block_p - offsetof (struct freelist_entry, next));
size_t real_size = REAL_SIZE (sz);
size_t old_real_size;
// if (block_p == NULL)
// return malloc (sz);
if (block_p == NULL)
return malloc (sz);
// old_real_size = block->size;
old_real_size = block->size;
// /* Perhaps we need to allocate more space. */
// if (old_real_size < real_size)
// {
// void *result;
// size_t old_size = old_real_size - sizeof (size_t);
/* Perhaps we need to allocate more space. */
if (old_real_size < real_size)
{
void *result;
size_t old_size = old_real_size - sizeof (size_t);
// /* Need to allocate, copy, and free. */
// result = malloc (sz);
// if (result == NULL)
// return NULL;
// memcpy (result, block_p, old_size < sz ? old_size : sz);
// free (block_p);
// return result;
// }
// /* Perhaps we can free some space. */
// if (old_real_size - real_size >= sizeof (struct freelist_entry))
// {
// fle newblock = (fle)((size_t)block + real_size);
// block->size = real_size;
// newblock->size = old_real_size - real_size;
// free (&newblock->next);
// }
// return block_p;
// }
// #endif
/* Need to allocate, copy, and free. */
result = malloc (sz);
if (result == NULL)
return NULL;
memcpy (result, block_p, old_size < sz ? old_size : sz);
free (block_p);
return result;
}
/* Perhaps we can free some space. */
if (old_real_size - real_size >= sizeof (struct freelist_entry))
{
fle newblock = (fle)((size_t)block + real_size);
block->size = real_size;
newblock->size = old_real_size - real_size;
free (&newblock->next);
}
return block_p;
}
#endif
// #ifdef DEFINE_CALLOC
// void *
// calloc (size_t n, size_t elem_size)
// {
// void *result;
// size_t sz = n * elem_size;
// result = malloc (sz);
// if (result != NULL)
// memset (result, 0, sz);
// return result;
// }
// #endif
#ifdef DEFINE_CALLOC
void *
calloc (size_t n, size_t elem_size)
{
void *result;
size_t sz = n * elem_size;
result = malloc (sz);
if (result != NULL)
memset (result, 0, sz);
return result;
}
#endif
// #ifdef DEFINE_CFREE
// void
// cfree (void *p)
// {
// free (p);
// }
// #endif
#ifdef DEFINE_CFREE
void
cfree (void *p)
{
free (p);
}
#endif
// #ifdef DEFINE_MEMALIGN
// void *
// memalign (size_t align, size_t sz)
// {
// fle *nextfree;
// fle block;
#ifdef DEFINE_MEMALIGN
void *
memalign (size_t align, size_t sz)
{
fle *nextfree;
fle block;
// /* real_size is the size we actually have to allocate, allowing for
// overhead and alignment. */
// size_t real_size = REAL_SIZE (sz);
/* real_size is the size we actually have to allocate, allowing for
overhead and alignment. */
size_t real_size = REAL_SIZE (sz);
// /* Some sanity checking on 'align'. */
// if ((align & (align - 1)) != 0
// || align <= 0)
// return NULL;
/* Some sanity checking on 'align'. */
if ((align & (align - 1)) != 0
|| align <= 0)
return NULL;
// /* Look for the first block on the freelist that is large enough. */
// /* One tricky part is this: We want the result to be a valid pointer
// to free. That means that there has to be room for a size_t
// before the block. If there's additional space before the block,
// it should go on the freelist, or it'll be lost---we could add it
// to the size of the block before it in memory, but finding the
// previous block is expensive. */
// for (nextfree = &__malloc_freelist;
// ;
// nextfree = &(*nextfree)->next)
// {
// size_t before_size;
// size_t old_size;
/* Look for the first block on the freelist that is large enough. */
/* One tricky part is this: We want the result to be a valid pointer
to free. That means that there has to be room for a size_t
before the block. If there's additional space before the block,
it should go on the freelist, or it'll be lost---we could add it
to the size of the block before it in memory, but finding the
previous block is expensive. */
for (nextfree = &__malloc_freelist;
;
nextfree = &(*nextfree)->next)
{
size_t before_size;
size_t old_size;
// /* If we've run out of free blocks, allocate more space. */
// if (! *nextfree)
// {
// old_size = real_size;
// if (MALLOC_DIRECTION < 0)
// {
// old_size += M_ALIGN_SUB (((size_t)__malloc_end
// - old_size + sizeof (size_t)),
// align);
// if (! CAN_ALLOC_P (old_size))
// return NULL;
// block = __malloc_end = (void *)((size_t)__malloc_end - old_size);
// }
// else
// {
// block = __malloc_end;
// old_size += M_ALIGN ((size_t)__malloc_end + sizeof (size_t),
// align);
// if (! CAN_ALLOC_P (old_size))
// return NULL;
// __malloc_end = (void *)((size_t)__malloc_end + old_size);
// }
// *nextfree = block;
// block->size = old_size;
// block->next = NULL;
// }
// else
// {
// block = *nextfree;
// old_size = block->size;
// }
/* If we've run out of free blocks, allocate more space. */
if (! *nextfree)
{
old_size = real_size;
if (MALLOC_DIRECTION < 0)
{
old_size += M_ALIGN_SUB (((size_t)__malloc_end
- old_size + sizeof (size_t)),
align);
if (! CAN_ALLOC_P (old_size))
return NULL;
block = __malloc_end = (void *)((size_t)__malloc_end - old_size);
}
else
{
block = __malloc_end;
old_size += M_ALIGN ((size_t)__malloc_end + sizeof (size_t),
align);
if (! CAN_ALLOC_P (old_size))
return NULL;
__malloc_end = (void *)((size_t)__malloc_end + old_size);
}
*nextfree = block;
block->size = old_size;
block->next = NULL;
}
else
{
block = *nextfree;
old_size = block->size;
}
// before_size = M_ALIGN (&block->next, align);
// if (before_size != 0)
// before_size = sizeof (*block) + M_ALIGN (&(block+1)->next, align);
before_size = M_ALIGN (&block->next, align);
if (before_size != 0)
before_size = sizeof (*block) + M_ALIGN (&(block+1)->next, align);
// /* If this is the last block on the freelist, and it is too small,
// enlarge it. */
// if (! block->next
// && old_size < real_size + before_size
// && __malloc_end == (void *)((size_t)block + block->size))
// {
// if (MALLOC_DIRECTION < 0)
// {
// size_t moresize = real_size - block->size;
// moresize += M_ALIGN_SUB ((size_t)&block->next - moresize, align);
// if (! CAN_ALLOC_P (moresize))
// return NULL;
// block = __malloc_end = (void *)((size_t)block - moresize);
// block->next = NULL;
// block->size = old_size = old_size + moresize;
// before_size = 0;
// }
// else
// {
// if (! CAN_ALLOC_P (before_size + real_size - block->size))
// return NULL;
// __malloc_end = (void *)((size_t)block + before_size + real_size);
// block->size = old_size = before_size + real_size;
// }
/* If this is the last block on the freelist, and it is too small,
enlarge it. */
if (! block->next
&& old_size < real_size + before_size
&& __malloc_end == (void *)((size_t)block + block->size))
{
if (MALLOC_DIRECTION < 0)
{
size_t moresize = real_size - block->size;
moresize += M_ALIGN_SUB ((size_t)&block->next - moresize, align);
if (! CAN_ALLOC_P (moresize))
return NULL;
block = __malloc_end = (void *)((size_t)block - moresize);
block->next = NULL;
block->size = old_size = old_size + moresize;
before_size = 0;
}
else
{
if (! CAN_ALLOC_P (before_size + real_size - block->size))
return NULL;
__malloc_end = (void *)((size_t)block + before_size + real_size);
block->size = old_size = before_size + real_size;
}
// /* Two out of the four cases below will now be possible; which
// two depends on MALLOC_DIRECTION. */
// }
/* Two out of the four cases below will now be possible; which
two depends on MALLOC_DIRECTION. */
}
// if (old_size >= real_size + before_size)
// {
// /* This block will do. If there needs to be space before it,
// split the block. */
// if (before_size != 0)
// {
// fle old_block = block;
if (old_size >= real_size + before_size)
{
/* This block will do. If there needs to be space before it,
split the block. */
if (before_size != 0)
{
fle old_block = block;
// old_block->size = before_size;
// block = (fle)((size_t)block + before_size);
old_block->size = before_size;
block = (fle)((size_t)block + before_size);
// /* If there's no space after the block, we're now nearly
// done; just make a note of the size required.
// Otherwise, we need to create a new free space block. */
// if (old_size - before_size
// <= real_size + sizeof (struct freelist_entry))
// {
// block->size = old_size - before_size;
// return (void *)&block->next;
// }
// else
// {
// fle new_block;
// new_block = (fle)((size_t)block + real_size);
// new_block->size = old_size - before_size - real_size;
// if (MALLOC_DIRECTION > 0)
// {
// new_block->next = old_block->next;
// old_block->next = new_block;
// }
// else
// {
// new_block->next = old_block;
// *nextfree = new_block;
// }
// goto done;
// }
// }
// else
// {
// /* If the block found is just the right size, remove it from
// the free list. Otherwise, split it. */
// if (old_size <= real_size + sizeof (struct freelist_entry))
// {
// *nextfree = block->next;
// return (void *)&block->next;
// }
// else
// {
// size_t newsize = old_size - real_size;
// fle newnext = block->next;
// *nextfree = (fle)((size_t)block + real_size);
// (*nextfree)->size = newsize;
// (*nextfree)->next = newnext;
// goto done;
// }
// }
// }
// }
/* If there's no space after the block, we're now nearly
done; just make a note of the size required.
Otherwise, we need to create a new free space block. */
if (old_size - before_size
<= real_size + sizeof (struct freelist_entry))
{
block->size = old_size - before_size;
return (void *)&block->next;
}
else
{
fle new_block;
new_block = (fle)((size_t)block + real_size);
new_block->size = old_size - before_size - real_size;
if (MALLOC_DIRECTION > 0)
{
new_block->next = old_block->next;
old_block->next = new_block;
}
else
{
new_block->next = old_block;
*nextfree = new_block;
}
goto done;
}
}
else
{
/* If the block found is just the right size, remove it from
the free list. Otherwise, split it. */
if (old_size <= real_size + sizeof (struct freelist_entry))
{
*nextfree = block->next;
return (void *)&block->next;
}
else
{
size_t newsize = old_size - real_size;
fle newnext = block->next;
*nextfree = (fle)((size_t)block + real_size);
(*nextfree)->size = newsize;
(*nextfree)->next = newnext;
goto done;
}
}
}
}
// done:
// block->size = real_size;
// return (void *)&block->next;
// }
// #endif
done:
block->size = real_size;
return (void *)&block->next;
}
#endif
// #ifdef DEFINE_VALLOC
// void *
// valloc (size_t sz)
// {
// return memalign (128, sz);
// }
// #endif
// #ifdef DEFINE_PVALLOC
// void *
// pvalloc (size_t sz)
// {
// return memalign (128, sz + M_ALIGN (sz, 128));
// }
// #endif
#ifdef DEFINE_VALLOC
void *
valloc (size_t sz)
{
return memalign (128, sz);
}
#endif
#ifdef DEFINE_PVALLOC
void *
pvalloc (size_t sz)
{
return memalign (128, sz + M_ALIGN (sz, 128));
}
#endif
// #ifdef DEFINE_MALLINFO
// #include "malloc.h"
#ifdef DEFINE_MALLINFO
#include "malloc.h"
// struct mallinfo
// mallinfo (void)
// {
// struct mallinfo r;
// fle fr;
// size_t free_size;
// size_t total_size;
// size_t free_blocks;
struct mallinfo
mallinfo (void)
{
struct mallinfo r;
fle fr;
size_t free_size;
size_t total_size;
size_t free_blocks;
// memset (&r, 0, sizeof (r));
memset (&r, 0, sizeof (r));
// free_size = 0;
// free_blocks = 0;
// for (fr = __malloc_freelist; fr; fr = fr->next)
// {
// free_size += fr->size;
// free_blocks++;
// if (! fr->next)
// {
// int atend;
// if (MALLOC_DIRECTION > 0)
// atend = (void *)((size_t)fr + fr->size) == __malloc_end;
// else
// atend = (void *)fr == __malloc_end;
// if (atend)
// r.keepcost = fr->size;
// }
// }
free_size = 0;
free_blocks = 0;
for (fr = __malloc_freelist; fr; fr = fr->next)
{
free_size += fr->size;
free_blocks++;
if (! fr->next)
{
int atend;
if (MALLOC_DIRECTION > 0)
atend = (void *)((size_t)fr + fr->size) == __malloc_end;
else
atend = (void *)fr == __malloc_end;
if (atend)
r.keepcost = fr->size;
}
}
// if (MALLOC_DIRECTION > 0)
// total_size = (char *)__malloc_end - (char *)&__malloc_start;
// else
// total_size = (char *)&__malloc_start - (char *)__malloc_end;
if (MALLOC_DIRECTION > 0)
total_size = (char *)__malloc_end - (char *)&__malloc_start;
else
total_size = (char *)&__malloc_start - (char *)__malloc_end;
// #ifdef DEBUG
// /* Fixme: should walk through all the in-use blocks and see if
// they're valid. */
// #endif
#ifdef DEBUG
/* Fixme: should walk through all the in-use blocks and see if
they're valid. */
#endif
// r.arena = total_size;
// r.fordblks = free_size;
// r.uordblks = total_size - free_size;
// r.ordblks = free_blocks;
// return r;
// }
// #endif
r.arena = total_size;
r.fordblks = free_size;
r.uordblks = total_size - free_size;
r.ordblks = free_blocks;
return r;
}
#endif
// #ifdef DEFINE_MALLOC_STATS
// #include "malloc.h"
// #include <stdio.h>
#ifdef DEFINE_MALLOC_STATS
#include "malloc.h"
#include <stdio.h>
// void
// malloc_stats(void)
// {
// struct mallinfo i;
// FILE *fp;
void
malloc_stats(void)
{
struct mallinfo i;
FILE *fp;
// fp = stderr;
// i = mallinfo();
// fprintf (fp, "malloc has reserved %u bytes between %p and %p\n",
// i.arena, &__malloc_start, __malloc_end);
// fprintf (fp, "there are %u bytes free in %u chunks\n",
// i.fordblks, i.ordblks);
// fprintf (fp, "of which %u bytes are at the end of the reserved space\n",
// i.keepcost);
// fprintf (fp, "and %u bytes are in use.\n", i.uordblks);
// }
// #endif
fp = stderr;
i = mallinfo();
fprintf (fp, "malloc has reserved %u bytes between %p and %p\n",
i.arena, &__malloc_start, __malloc_end);
fprintf (fp, "there are %u bytes free in %u chunks\n",
i.fordblks, i.ordblks);
fprintf (fp, "of which %u bytes are at the end of the reserved space\n",
i.keepcost);
fprintf (fp, "and %u bytes are in use.\n", i.uordblks);
}
#endif
// #ifdef DEFINE_MALLOC_USABLE_SIZE
// size_t
// malloc_usable_size (void *block_p)
// {
// fle block = (fle)((size_t) block_p - offsetof (struct freelist_entry, next));
// return block->size - sizeof (size_t);
// }
// #endif
#ifdef DEFINE_MALLOC_USABLE_SIZE
size_t
malloc_usable_size (void *block_p)
{
fle block = (fle)((size_t) block_p - offsetof (struct freelist_entry, next));
return block->size - sizeof (size_t);
}
#endif
// #ifdef DEFINE_MALLOPT
// int
// mallopt (int n, int v)
// {
// (void)n; (void)v;
// return 0;
// }
// #endif
#ifdef DEFINE_MALLOPT
int
mallopt (int n, int v)
{
(void)n; (void)v;
return 0;
}
#endif

Binary file not shown.

File diff suppressed because it is too large Load Diff