shrub/c/m.c

335 lines
6.5 KiB
C
Raw Normal View History

/* c/m.c
2014-09-05 23:55:16 +04:00
**
** This file is in the public domain.
*/
#include <fcntl.h>
#include <sys/stat.h>
2014-09-05 23:55:16 +04:00
#include "all.h"
/* u3_cm_file(): load file, as atom, or bail.
*/
static u3_noun
u3_cm_file(c3_c* pas_c)
{
struct stat buf_b;
c3_i fid_i = open(pas_c, O_RDONLY, 0644);
c3_w fln_w, red_w;
c3_y* pad_y;
if ( (fid_i < 0) || (fstat(fid_i, &buf_b) < 0) ) {
fprintf(stderr, "%s: %s\r\n", pas_c, strerror(errno));
return u3_cm_bail(c3__fail);
}
fln_w = buf_b.st_size;
pad_y = c3_malloc(buf_b.st_size);
red_w = read(fid_i, pad_y, fln_w);
close(fid_i);
if ( fln_w != red_w ) {
free(pad_y);
return u3_cm_bail(c3__fail);
}
else {
u3_noun pad = u3_ci_bytes(fln_w, (c3_y *)pad_y);
free(pad_y);
return pad;
}
}
2014-09-05 23:55:16 +04:00
/* _boot_north(): install a north road.
*/
2014-09-06 00:13:24 +04:00
static u3_road*
2014-09-05 23:55:16 +04:00
_boot_north(c3_w* mem_w, c3_w len_w)
{
c3_w* rut_w = mem_w;
c3_w* hat_w = rut_w;
2014-09-06 00:13:24 +04:00
c3_w* mat_w = ((mem_w + len_w) - c3_wiseof(u3_road));
2014-09-05 23:55:16 +04:00
c3_w* cap_w = mat_w;
2014-09-06 00:13:24 +04:00
u3_road* rod_u = (void*) mat_w;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
memset(rod_u, 0, sizeof(u3_road));
2014-09-05 23:55:16 +04:00
rod_u->rut_w = rut_w;
rod_u->hat_w = hat_w;
rod_u->mat_w = mat_w;
rod_u->cap_w = cap_w;
return rod_u;
}
/* _boot_south(): install a south road.
*/
2014-09-06 00:13:24 +04:00
static u3_road*
2014-09-05 23:55:16 +04:00
_boot_south(c3_w* mem_w, c3_w len_w)
{
c3_w* rut_w = mem_w;
c3_w* hat_w = rut_w;
2014-09-06 00:13:24 +04:00
c3_w* mat_w = ((mem_w + len_w) - c3_wiseof(u3_road));
2014-09-05 23:55:16 +04:00
c3_w* cap_w = mat_w;
2014-09-06 00:13:24 +04:00
u3_road* rod_u = (void*) mat_w;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
memset(rod_u, 0, sizeof(u3_road));
2014-09-05 23:55:16 +04:00
rod_u->rut_w = rut_w;
rod_u->hat_w = hat_w;
rod_u->mat_w = mat_w;
rod_u->cap_w = cap_w;
return rod_u;
}
2014-09-06 00:13:24 +04:00
/* u3_cm_boot(): make u3R and u3H from nothing.
2014-09-05 23:55:16 +04:00
*/
void
2014-09-06 00:13:24 +04:00
u3_cm_boot(c3_p adr_p, c3_w len_w)
2014-09-05 23:55:16 +04:00
{
void* map_v;
map_v = mmap((void *)(c3_p)adr_p,
(len_w << 2),
PROT_READ | PROT_WRITE,
(MAP_ANON | MAP_FIXED | MAP_PRIVATE),
-1, 0);
if ( -1 == (c3_ps)map_v ) {
map_v = mmap((void *)0,
(len_w << 2),
PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE,
-1, 0);
if ( -1 == (c3_ps)map_v ) {
fprintf(stderr, "map failed twice\n");
} else {
fprintf(stderr, "map failed - try U2_OS_LoomBase %p\n", map_v);
}
exit(1);
}
printf("loom: mapped %dMB\n", (len_w >> 18));
2014-09-06 00:13:24 +04:00
u3L = map_v;
u3H = u3R = _boot_north(map_v, len_w);
2014-09-05 23:55:16 +04:00
}
2014-09-07 04:09:29 +04:00
/* u3_cm_clear(): clear all allocated data in road.
*/
void
u3_cm_clear(void)
{
u3_ch_free(u3R->jed.har_u);
}
2014-09-05 23:55:16 +04:00
#if 0
static void
_road_sane(void)
{
c3_w i_w;
2014-09-06 00:13:24 +04:00
for ( i_w = 0; i_w < u3_cc_fbox_no; i_w++ ) {
u3_cs_fbox* fre_u = u3R->all.fre_u[i_w];
2014-09-05 23:55:16 +04:00
while ( fre_u ) {
2014-09-06 00:13:24 +04:00
if ( fre_u == u3R->all.fre_u[i_w] ) {
2014-09-05 23:55:16 +04:00
c3_assert(fre_u->pre_u == 0);
}
else {
c3_assert(fre_u->pre_u != 0);
c3_assert(fre_u->pre_u->nex_u == fre_u);
if ( fre_u->nex_u != 0 ) {
c3_assert(fre_u->nex_u->pre_u == fre_u);
}
}
fre_u = fre_u->nex_u;
}
}
}
#endif
void
2014-09-06 00:13:24 +04:00
u3_cm_dump(void)
2014-09-05 23:55:16 +04:00
{
c3_w hat_w;
c3_w fre_w = 0;
c3_w i_w;
2014-09-06 00:13:24 +04:00
hat_w = u3_so(u3_co_is_north) ? u3R->hat_w - u3R->rut_w
: u3R->hat_w - u3R->rut_w;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
for ( i_w = 0; i_w < u3_cc_fbox_no; i_w++ ) {
u3_cs_fbox* fre_u = u3R->all.fre_u[i_w];
2014-09-05 23:55:16 +04:00
while ( fre_u ) {
fre_w += fre_u->box_u.siz_w;
fre_u = fre_u->nex_u;
}
}
printf("dump: hat_w %x, fre_w %x, allocated %x\n",
hat_w, fre_w, (hat_w - fre_w));
if ( 0 != (hat_w - fre_w) ) {
2014-09-06 00:13:24 +04:00
c3_w* box_w = u3R->rut_w;
2014-09-05 23:55:16 +04:00
c3_w mem_w = 0;
2014-09-06 00:13:24 +04:00
while ( box_w < u3R->hat_w ) {
u3_cs_box* box_u = (void *)box_w;
2014-09-05 23:55:16 +04:00
if ( 0 != box_u->use_w ) {
2014-09-07 02:39:28 +04:00
#ifdef U3_MEMORY_DEBUG
printf("live %d words, code %x\n", box_u->siz_w, box_u->cod_w);
#endif
2014-09-05 23:55:16 +04:00
mem_w += box_u->siz_w;
}
box_w += box_u->siz_w;
}
printf("second count: %x\n", mem_w);
}
}
2014-09-06 00:13:24 +04:00
/* u3_cm_bail(): bail out. Does not return.
2014-09-05 23:55:16 +04:00
**
** Bail motes:
**
** %evil :: erroneous cryptography
** %exit :: semantic failure
** %oops :: assertion failure
** %intr :: interrupt
** %fail :: computability failure
** %need :: namespace block
** %meme :: out of memory
*/
c3_i
2014-09-06 00:13:24 +04:00
u3_cm_bail(c3_m how_m)
2014-09-05 23:55:16 +04:00
{
c3_c str_c[5];
str_c[0] = ((how_m >> 0) & 0xff);
str_c[1] = ((how_m >> 8) & 0xff);
str_c[2] = ((how_m >> 16) & 0xff);
str_c[3] = ((how_m >> 24) & 0xff);
str_c[4] = 0;
printf("bail: %s\n", str_c);
assert(0);
if ( c3__meme == how_m ) {
2014-09-06 00:13:24 +04:00
u3_cm_dump();
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
_longjmp(u3R->esc.buf, how_m);
2014-09-05 23:55:16 +04:00
return how_m;
}
2014-09-06 00:13:24 +04:00
/* u3_cm_error(): bail out with %exit, ct_pushing error.
2014-09-05 23:55:16 +04:00
*/
c3_i
2014-09-06 00:13:24 +04:00
u3_cm_error(c3_c* str_c)
2014-09-05 23:55:16 +04:00
{
printf("error: %s\n", str_c); // rong
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__exit);
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_cm_leap(): in u3R, create a new road within the existing one.
2014-09-05 23:55:16 +04:00
*/
void
2014-09-06 00:13:24 +04:00
u3_cm_leap()
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_road* rod_u;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_is_north ) {
rod_u = _boot_south(u3R->hat_w, (u3R->cap_w - u3R->hat_w));
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
rod_u = _boot_north(u3R->cap_w, (u3R->hat_w - u3R->cap_w));
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
c3_assert(0 == u3R->kid_u);
rod_u->par_u = u3R;
u3R->kid_u = rod_u;
u3R = rod_u;
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_cm_fall(): in u3R, return an inner road to its parent.
2014-09-05 23:55:16 +04:00
*/
void
2014-09-06 00:13:24 +04:00
u3_cm_fall()
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(0 != u3R->par_u);
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
u3R->par_u->cap_w = u3R->hat_w;
u3R = u3R->par_u;
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_cm_golf(): record cap_w length for u3_flog().
2014-09-05 23:55:16 +04:00
*/
c3_w
2014-09-06 00:13:24 +04:00
u3_cm_golf(void)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_is_north ) {
return u3R->mat_w - u3R->cap_w;
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
return u3R->cap_w - u3R->mat_w;
2014-09-05 23:55:16 +04:00
}
}
2014-09-06 00:13:24 +04:00
/* u3_cm_flog(): reset cap_w.
2014-09-05 23:55:16 +04:00
*/
void
2014-09-06 00:13:24 +04:00
u3_cm_flog(c3_w gof_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_is_north ) {
u3R->cap_w = u3R->mat_w - gof_w;
2014-09-05 23:55:16 +04:00
} else {
2014-09-06 00:13:24 +04:00
u3R->cap_w = u3R->mat_w + gof_w;
2014-09-05 23:55:16 +04:00
}
}
2014-09-06 00:13:24 +04:00
/* u3_cm_water(): produce watermarks.
2014-09-05 23:55:16 +04:00
*/
void
2014-09-06 00:13:24 +04:00
u3_cm_water(c3_w* low_w, c3_w* hig_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3R == u3H);
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
*low_w = (u3H->hat_w - u3H->rut_w);
*hig_w = (u3H->mat_w - u3H->cap_w) + c3_wiseof(u3_road);
2014-09-05 23:55:16 +04:00
}
/* u2_cm_soft(): system soft wrapper. unifies unix and nock errors.
**
** Produces [%$ result] or [%error (list tank)].
*/
u2_noun
u2_cm_soft(c3_w sec_w, u2_funk fun_f, u2_noun arg)
{
u2_noun why_a;
u3_cm_leap();
if ( u3_no == (why_a = u3_cm_trap()) ) {
u3_noun ton;
if ( 0 != u3R->net.nyd ) {
c3_assert(0); // XX actually, convert to error
}
else {
ton = u3nc(why_a, u3R->bug.tax);
}
u3_cm_fall();
ton = u3_ca_gain(ton);
u3_cm_flog(0);
}
else {
u3_noun pro = fun_f(arg);
u3_cm_fall();
ton = u3nc(0, u3_ca_gain(pro));
u3_cm_flog(0);
}
return ton;
}