urbit/n/a.c

912 lines
19 KiB
C
Raw Normal View History

2014-09-05 23:55:16 +04:00
/* n/a.c
**
** This file is in the public domain.
*/
#include "all.h"
/* _box_slot(): select the right free list to search for a block.
*/
c3_w
_box_slot(c3_w siz_w)
{
if ( siz_w < 8 ) {
return 0;
}
else {
c3_w i_w = 1;
while ( 1 ) {
2014-09-06 00:13:24 +04:00
if ( i_w == u3_cc_fbox_no ) {
2014-09-05 23:55:16 +04:00
return (i_w - 1);
}
if ( siz_w < 16 ) {
return i_w;
}
siz_w = (siz_w + 1) >> 1;
i_w += 1;
}
}
}
/* _box_make(): construct a box.
*/
2014-09-06 00:13:24 +04:00
u3_cs_box*
2014-09-05 23:55:16 +04:00
_box_make(void* box_v, c3_w siz_w, c3_w use_w)
{
2014-09-06 00:13:24 +04:00
u3_cs_box* box_u = box_v;
2014-09-05 23:55:16 +04:00
c3_w* box_w = box_v;
box_w[0] = siz_w;
box_w[siz_w - 1] = siz_w;
box_u->use_w = use_w;
# ifdef U2_MEMORY_DEBUG
box_u->cod_w = COD_w;
# endif
return box_u;
}
/* _box_attach(): attach a box to the free list.
*/
void
2014-09-06 00:13:24 +04:00
_box_attach(u3_cs_box* box_u)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(box_u->siz_w >= (1 + c3_wiseof(u3_cs_fbox)));
2014-09-05 23:55:16 +04:00
{
c3_w sel_w = _box_slot(box_u->siz_w);
2014-09-06 00:13:24 +04:00
u3_cs_fbox* fre_u = (void *)box_u;
u3_cs_fbox** pfr_u = &u3R->all.fre_u[sel_w];
u3_cs_fbox* nex_u = *pfr_u;
2014-09-05 23:55:16 +04:00
fre_u->pre_u = 0;
fre_u->nex_u = nex_u;
if ( fre_u->nex_u ) {
fre_u->nex_u->pre_u = fre_u;
}
(*pfr_u) = fre_u;
}
}
/* _box_detach(): detach a box from the free list.
*/
void
2014-09-06 00:13:24 +04:00
_box_detach(u3_cs_box* box_u)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_cs_fbox* fre_u = (void*) box_u;
u3_cs_fbox* pre_u = fre_u->pre_u;
u3_cs_fbox* nex_u = fre_u->nex_u;
2014-09-05 23:55:16 +04:00
if ( nex_u ) {
c3_assert(nex_u->pre_u == fre_u);
nex_u->pre_u = pre_u;
}
if ( pre_u ) {
c3_assert(pre_u->nex_u == fre_u);
pre_u->nex_u = nex_u;
}
else {
c3_w sel_w = _box_slot(box_u->siz_w);
2014-09-06 00:13:24 +04:00
c3_assert(fre_u == u3R->all.fre_u[sel_w]);
u3R->all.fre_u[sel_w] = nex_u;
2014-09-05 23:55:16 +04:00
}
}
2014-09-06 00:13:24 +04:00
/* _me_road_all_hat(): in u3R, allocate directly on the hat_w.
2014-09-05 23:55:16 +04:00
*/
static c3_w*
_me_road_all_hat(c3_w len_w)
{
2014-09-06 00:13:24 +04:00
if ( len_w > u3_co_open ) {
u3_cm_bail(c3__meme); return 0;
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_is_north ) {
2014-09-05 23:55:16 +04:00
c3_w* all_w;
2014-09-06 00:13:24 +04:00
all_w = u3R->hat_w;
u3R->hat_w += len_w;
2014-09-05 23:55:16 +04:00
return all_w;
}
else {
2014-09-06 00:13:24 +04:00
u3R->hat_w -= len_w;
return u3R->hat_w;
2014-09-05 23:55:16 +04:00
}
}
#if 0 // not yet used
2014-09-06 00:13:24 +04:00
/* _me_road_all_cap(): in u3R, allocate directly on the cap.
2014-09-05 23:55:16 +04:00
*/
static c3_w*
_me_road_all_cap(c3_w len_w)
{
2014-09-06 00:13:24 +04:00
if ( len_w > u3_co_open ) {
u3_cm_bail(c3__meme); return 0;
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 -= len_w;
return u3R->cap_w;
2014-09-05 23:55:16 +04:00
}
else {
c3_w* all_w;
2014-09-06 00:13:24 +04:00
all_w = u3R->cap_w;
u3R->cap_w += len_w;
2014-09-05 23:55:16 +04:00
return all_w;
}
}
#endif
2014-09-06 00:13:24 +04:00
/* u3_ca_walloc(): allocate storage words on hat_w.
2014-09-05 23:55:16 +04:00
*/
void*
2014-09-06 00:13:24 +04:00
u3_ca_walloc(c3_w len_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_w siz_w = c3_max(u3_cc_minimum, u3_co_boxed(len_w));
2014-09-05 23:55:16 +04:00
c3_w sel_w = _box_slot(siz_w);
// XX: this logic is totally bizarre, but preserve it.
//
2014-09-06 00:13:24 +04:00
if ( (sel_w != 0) && (sel_w != u3_cc_fbox_no - 1) ) {
2014-09-05 23:55:16 +04:00
sel_w += 1;
}
while ( 1 ) {
2014-09-06 00:13:24 +04:00
u3_cs_fbox** pfr_u = &u3R->all.fre_u[sel_w];
2014-09-05 23:55:16 +04:00
while ( 1 ) {
if ( 0 == *pfr_u ) {
2014-09-06 00:13:24 +04:00
if ( sel_w < (u3_cc_fbox_no - 1) ) {
2014-09-05 23:55:16 +04:00
sel_w += 1;
break;
}
else {
/* Nothing in top free list. Chip away at the hat_w.
*/
2014-09-06 00:13:24 +04:00
return u3_co_boxto(_box_make(_me_road_all_hat(siz_w), siz_w, 1));
2014-09-05 23:55:16 +04:00
}
}
else {
if ( siz_w > (*pfr_u)->box_u.siz_w ) {
/* This free block is too small. Continue searching.
*/
pfr_u = &((*pfr_u)->nex_u);
continue;
}
else {
2014-09-06 00:13:24 +04:00
u3_cs_box* box_u = &((*pfr_u)->box_u);
2014-09-05 23:55:16 +04:00
/* We have found a free block of adequate size. Remove it
** from the free list.
*/
{
{
c3_assert((0 == (*pfr_u)->pre_u) ||
(*pfr_u)->pre_u->nex_u == (*pfr_u));
c3_assert((0 == (*pfr_u)->nex_u) ||
(*pfr_u)->nex_u->pre_u == (*pfr_u));
}
if ( 0 != (*pfr_u)->nex_u ) {
(*pfr_u)->nex_u->pre_u = (*pfr_u)->pre_u;
}
*pfr_u = (*pfr_u)->nex_u;
}
/* If we can chop off another block, do it.
*/
2014-09-06 00:13:24 +04:00
if ( (siz_w + c3_wiseof(u3_cs_fbox) + 1) <= box_u->siz_w ) {
2014-09-05 23:55:16 +04:00
/* Split the block.
*/
c3_w* box_w = ((c3_w *)(void *)box_u);
c3_w* end_w = box_w + siz_w;
c3_w lef_w = (box_u->siz_w - siz_w);
_box_attach(_box_make(end_w, lef_w, 0));
2014-09-06 00:13:24 +04:00
return u3_co_boxto(_box_make(box_w, siz_w, 1));
2014-09-05 23:55:16 +04:00
}
else {
c3_assert(0 == box_u->use_w);
box_u->use_w = 1;
#ifdef U2_MEMORY_DEBUG
box_u->cod_w = COD_w;
#endif
2014-09-06 00:13:24 +04:00
return u3_co_boxto(box_u);
2014-09-05 23:55:16 +04:00
}
}
}
}
}
}
2014-09-06 00:13:24 +04:00
/* u3_ca_malloc(): allocate storage measured in bytes.
2014-09-05 23:55:16 +04:00
*/
void*
2014-09-06 00:13:24 +04:00
u3_ca_malloc(c3_w len_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_ca_walloc((len_w + 3) >> 2);
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_ca_wealloc(): realloc in words.
2014-09-05 23:55:16 +04:00
*/
void*
2014-09-06 00:13:24 +04:00
u3_ca_wealloc(void* lag_v, c3_w len_w)
2014-09-05 23:55:16 +04:00
{
if ( !lag_v ) {
2014-09-06 00:13:24 +04:00
return u3_ca_malloc(len_w);
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_cs_box* box_u = u3_co_botox(lag_v);
2014-09-05 23:55:16 +04:00
c3_w* old_w = lag_v;
c3_w tiz_w = c3_min(box_u->siz_w, len_w);
{
2014-09-06 00:13:24 +04:00
c3_w* new_w = u3_ca_walloc(len_w);
2014-09-05 23:55:16 +04:00
c3_w i_w;
for ( i_w = 0; i_w < tiz_w; i_w++ ) {
new_w[i_w] = old_w[i_w];
}
2014-09-06 00:13:24 +04:00
u3_ca_free(lag_v);
2014-09-05 23:55:16 +04:00
return new_w;
}
}
}
2014-09-06 00:13:24 +04:00
/* u3_ca_realloc(): realloc in bytes.
2014-09-05 23:55:16 +04:00
*/
void*
2014-09-06 00:13:24 +04:00
u3_ca_realloc(void* lag_v, c3_w len_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_ca_wealloc(lag_v, (len_w + 3) >> 2);
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_ca_free(): free storage.
2014-09-05 23:55:16 +04:00
*/
void
2014-09-06 00:13:24 +04:00
u3_ca_free(void* tox_v)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_cs_box* box_u = u3_co_botox(tox_v);
2014-09-05 23:55:16 +04:00
c3_w* box_w = (c3_w *)(void *)box_u;
c3_assert(box_u->use_w != 0);
box_u->use_w -= 1;
if ( 0 != box_u->use_w ) return;
2014-09-06 00:13:24 +04:00
c3_assert(u3_yes == u3_co_is_north);
2014-09-05 23:55:16 +04:00
#if 0
/* Clear the contents of the block, for debugging.
*/
{
c3_w i_w;
2014-09-06 00:13:24 +04:00
for ( i_w = c3_wiseof(u3_cs_box); (i_w + 1) < box_u->siz_w; i_w++ ) {
2014-09-05 23:55:16 +04:00
box_w[i_w] = 0xdeadbeef;
}
}
#endif
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_is_north ) {
2014-09-05 23:55:16 +04:00
/* Try to coalesce with the block below.
*/
2014-09-06 00:13:24 +04:00
if ( box_w != u3R->rut_w ) {
2014-09-05 23:55:16 +04:00
c3_w laz_w = *(box_w - 1);
2014-09-06 00:13:24 +04:00
u3_cs_box* pox_u = (u3_cs_box*)(void *)(box_w - laz_w);
2014-09-05 23:55:16 +04:00
if ( 0 == pox_u->use_w ) {
_box_detach(pox_u);
_box_make(pox_u, (laz_w + box_u->siz_w), 0);
box_u = pox_u;
box_w = (c3_w*)(void *)pox_u;
}
}
/* Try to coalesce with the block above, or the wilderness.
*/
2014-09-06 00:13:24 +04:00
if ( (box_w + box_u->siz_w) == u3R->hat_w ) {
u3R->hat_w = box_w;
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_cs_box* nox_u = (u3_cs_box*)(void *)(box_w + box_u->siz_w);
2014-09-05 23:55:16 +04:00
if ( 0 == nox_u->use_w ) {
_box_detach(nox_u);
_box_make(box_u, (box_u->siz_w + nox_u->siz_w), 0);
}
_box_attach(box_u);
}
}
else {
/* Try to coalesce with the block above.
*/
2014-09-06 00:13:24 +04:00
if ( (box_w + box_u->siz_w) != u3R->rut_w ) {
u3_cs_box* nox_u = (u3_cs_box*)(void *)(box_w + box_u->siz_w);
2014-09-05 23:55:16 +04:00
if ( 0 == nox_u->use_w ) {
_box_detach(nox_u);
_box_make(box_u, (box_u->siz_w + nox_u->siz_w), 0);
box_u = nox_u;
box_w = (c3_w*)(void *)nox_u;
}
}
/* Try to coalesce with the block below, or with the wilderness.
*/
2014-09-06 00:13:24 +04:00
if ( box_w == u3R->hat_w ) {
u3R->hat_w = (box_w + box_u->siz_w);
2014-09-05 23:55:16 +04:00
}
else {
c3_w laz_w = *(box_w - 1);
2014-09-06 00:13:24 +04:00
u3_cs_box* pox_u = (u3_cs_box*)(void *)(box_w - laz_w);
2014-09-05 23:55:16 +04:00
if ( 0 == pox_u->use_w ) {
_box_detach(pox_u);
_box_make(pox_u, (laz_w + box_u->siz_w), 0);
}
_box_attach(box_u);
}
}
}
/* _me_wash_north(): clean up mug slots after copy.
*/
2014-09-06 00:13:24 +04:00
static void _me_wash_north(u3_noun dog);
2014-09-05 23:55:16 +04:00
static void
2014-09-06 00:13:24 +04:00
_me_wash_north_in(u3_noun som)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_cat(som)) ) return;
if ( u3_ne(u3_co_north_is_junior(som)) ) return;
2014-09-05 23:55:16 +04:00
_me_wash_north(som);
}
static void
2014-09-06 00:13:24 +04:00
_me_wash_north(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3_co_is_dog(dog));
c3_assert(u3_yes == u3_co_north_is_junior(dog));
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_cs_noun* dog_u = u3_co_to_ptr(dog);
2014-09-05 23:55:16 +04:00
if ( dog_u->mug_w >> 31 ) { dog_u->mug_w = 0; }
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_pom(dog)) ) {
u3_cs_cell* god_u = (u3_cs_cell *)(void *)dog_u;
2014-09-05 23:55:16 +04:00
_me_wash_north_in(god_u->hed);
_me_wash_north_in(god_u->tel);
}
}
}
/* _me_wash_south(): clean up mug slots after copy.
*/
2014-09-06 00:13:24 +04:00
static void _me_wash_south(u3_noun dog);
2014-09-05 23:55:16 +04:00
static void
2014-09-06 00:13:24 +04:00
_me_wash_south_in(u3_noun som)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_cat(som)) ) return;
if ( u3_ne(u3_co_south_is_junior(som)) ) return;
2014-09-05 23:55:16 +04:00
_me_wash_south(som);
}
static void
2014-09-06 00:13:24 +04:00
_me_wash_south(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3_co_is_dog(dog));
c3_assert(u3_yes == u3_co_south_is_junior(dog));
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_cs_noun* dog_u = u3_co_to_ptr(dog);
2014-09-05 23:55:16 +04:00
if ( dog_u->mug_w >> 31 ) { dog_u->mug_w = 0; }
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_pom(dog)) ) {
u3_cs_cell* god_u = (u3_cs_cell *)(void *)dog_u;
2014-09-05 23:55:16 +04:00
_me_wash_south_in(god_u->hed);
_me_wash_south_in(god_u->tel);
}
}
}
/* _me_gain_use(): increment use count.
*/
static void
2014-09-06 00:13:24 +04:00
_me_gain_use(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_w* dog_w = u3_co_to_ptr(dog);
u3_cs_box* box_u = u3_co_botox(dog_w);
2014-09-05 23:55:16 +04:00
if ( 0xffffffff == box_u->use_w ) {
2014-09-06 00:13:24 +04:00
u3_cm_bail(c3__fail);
2014-09-05 23:55:16 +04:00
}
else {
box_u->use_w += 1;
}
}
/* _me_copy_north_in(): copy subjuniors on a north road.
*/
2014-09-06 00:13:24 +04:00
static u3_noun _me_copy_north(u3_noun);
static u3_noun
_me_copy_north_in(u3_noun som)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3_none != som);
if ( u3_so(u3_co_is_cat(som)) ) {
2014-09-05 23:55:16 +04:00
return som;
}
else {
2014-09-06 00:13:24 +04:00
u3_noun dog = som;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_north_is_senior(dog)) ) {
2014-09-05 23:55:16 +04:00
return dog;
}
2014-09-06 00:13:24 +04:00
else if ( u3_so(u3_co_north_is_junior(dog)) ) {
2014-09-05 23:55:16 +04:00
return _me_copy_north(dog);
}
else {
_me_gain_use(dog);
return dog;
}
}
}
/* _me_copy_north(): copy juniors on a north road.
*/
2014-09-06 00:13:24 +04:00
static u3_noun
_me_copy_north(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3_yes == u3_co_north_is_junior(dog));
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_ne(u3_co_north_is_junior(dog)) ) {
if ( u3_ne(u3_co_north_is_senior(dog)) ) {
2014-09-05 23:55:16 +04:00
_me_gain_use(dog);
}
return dog;
}
else {
2014-09-06 00:13:24 +04:00
u3_cs_noun* dog_u = u3_co_to_ptr(dog);
2014-09-05 23:55:16 +04:00
/* Borrow mug slot to record new destination.
*/
if ( dog_u->mug_w >> 31 ) {
2014-09-06 00:13:24 +04:00
u3_noun nov = (u3_noun) dog_u->mug_w;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
c3_assert(u3_so(u3_co_north_is_normal(nov)));
2014-09-05 23:55:16 +04:00
_me_gain_use(nov);
return nov;
}
else {
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_is_pom(dog) ) {
u3_cs_cell* old_u = u3_co_to_ptr(dog);
c3_w* new_w = u3_ca_walloc(c3_wiseof(u3_cs_cell));
u3_noun new = u3_co_de_twin(dog, new_w);
u3_cs_cell* new_u = (u3_cs_cell*)(void *)new_w;
2014-09-05 23:55:16 +04:00
new_u->mug_w = old_u->mug_w;
new_u->hed = _me_copy_north_in(old_u->hed);
new_u->tel = _me_copy_north_in(old_u->tel);
/* Borrow mug slot to record new destination.
*/
old_u->mug_w = new;
return new;
}
else {
2014-09-06 00:13:24 +04:00
u3_cs_atom* old_u = u3_co_to_ptr(dog);
c3_w* new_w = u3_ca_walloc(old_u->len_w + c3_wiseof(u3_cs_atom));
u3_noun new = u3_co_de_twin(dog, new_w);
u3_cs_atom* new_u = (u3_cs_atom*)(void *)new_w;
2014-09-05 23:55:16 +04:00
new_u->mug_w = old_u->mug_w;
new_u->len_w = old_u->len_w;
{
c3_w i_w;
for ( i_w=0; i_w < old_u->len_w; i_w++ ) {
new_u->buf_w[i_w] = old_u->buf_w[i_w];
}
}
/* Borrow mug slot to record new destination.
*/
old_u->mug_w = new;
return new;
}
}
}
}
/* _me_copy_south_in(): copy subjuniors on a south road.
*/
2014-09-06 00:13:24 +04:00
static u3_noun _me_copy_south(u3_noun);
static u3_noun
_me_copy_south_in(u3_noun som)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3_none != som);
if ( u3_so(u3_co_is_cat(som)) ) {
2014-09-05 23:55:16 +04:00
return som;
}
else {
2014-09-06 00:13:24 +04:00
u3_noun dog = som;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_south_is_senior(dog)) ) {
2014-09-05 23:55:16 +04:00
return dog;
}
2014-09-06 00:13:24 +04:00
else if ( u3_so(u3_co_south_is_junior(dog)) ) {
2014-09-05 23:55:16 +04:00
return _me_copy_south(dog);
}
else {
_me_gain_use(dog);
return dog;
}
}
}
/* _me_copy_south(): copy juniors on a south road.
*/
2014-09-06 00:13:24 +04:00
static u3_noun
_me_copy_south(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3_yes == u3_co_south_is_junior(dog));
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_ne(u3_co_south_is_junior(dog)) ) {
if ( u3_ne(u3_co_south_is_senior(dog)) ) {
2014-09-05 23:55:16 +04:00
_me_gain_use(dog);
}
return dog;
}
else {
2014-09-06 00:13:24 +04:00
u3_cs_noun* dog_u = u3_co_to_ptr(dog);
2014-09-05 23:55:16 +04:00
/* Borrow mug slot to record new destination.
*/
if ( dog_u->mug_w >> 31 ) {
2014-09-06 00:13:24 +04:00
u3_noun nov = (u3_noun) dog_u->mug_w;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
c3_assert(u3_so(u3_co_south_is_normal(nov)));
2014-09-05 23:55:16 +04:00
_me_gain_use(nov);
return nov;
}
else {
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_is_pom(dog) ) {
u3_cs_cell* old_u = u3_co_to_ptr(dog);
c3_w* new_w = u3_ca_walloc(c3_wiseof(u3_cs_cell));
u3_noun new = u3_co_de_twin(dog, new_w);
u3_cs_cell* new_u = (u3_cs_cell*)(void *)new_w;
2014-09-05 23:55:16 +04:00
new_u->mug_w = old_u->mug_w;
new_u->hed = _me_copy_south_in(old_u->hed);
new_u->tel = _me_copy_south_in(old_u->tel);
/* Borrow mug slot to record new destination.
*/
old_u->mug_w = new;
return new;
}
else {
2014-09-06 00:13:24 +04:00
u3_cs_atom* old_u = u3_co_to_ptr(dog);
c3_w* new_w = u3_ca_walloc(old_u->len_w + c3_wiseof(u3_cs_atom));
u3_noun new = u3_co_de_twin(dog, new_w);
u3_cs_atom* new_u = (u3_cs_atom*)(void *)new_w;
2014-09-05 23:55:16 +04:00
new_u->mug_w = old_u->mug_w;
new_u->len_w = old_u->len_w;
{
c3_w i_w;
for ( i_w=0; i_w < old_u->len_w; i_w++ ) {
new_u->buf_w[i_w] = old_u->buf_w[i_w];
}
}
/* Borrow mug slot to record new destination.
*/
old_u->mug_w = new;
return new;
}
}
}
}
/* _me_gain_north(): gain on a north road.
*/
2014-09-06 00:13:24 +04:00
static u3_noun
_me_gain_north(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_north_is_senior(dog) ) {
2014-09-05 23:55:16 +04:00
/* senior pointers are not refcounted
*/
return dog;
}
2014-09-06 00:13:24 +04:00
else if ( u3_yes == u3_co_north_is_junior(dog) ) {
2014-09-05 23:55:16 +04:00
/* junior pointers are copied
*/
2014-09-06 00:13:24 +04:00
u3_noun mos = _me_copy_north(dog);
2014-09-05 23:55:16 +04:00
_me_wash_north(dog);
return mos;
}
else {
/* normal pointers are refcounted
*/
_me_gain_use(dog);
return dog;
}
}
/* _me_gain_south(): gain on a south road.
*/
2014-09-06 00:13:24 +04:00
static u3_noun
_me_gain_south(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_south_is_senior(dog) ) {
2014-09-05 23:55:16 +04:00
/* senior pointers are not refcounted
*/
return dog;
}
2014-09-06 00:13:24 +04:00
else if ( u3_yes == u3_co_south_is_junior(dog) ) {
2014-09-05 23:55:16 +04:00
/* junior pointers are copied
*/
2014-09-06 00:13:24 +04:00
u3_noun mos = _me_copy_south(dog);
2014-09-05 23:55:16 +04:00
_me_wash_south(dog);
return mos;
}
else {
/* normal pointers are refcounted
*/
_me_gain_use(dog);
return dog;
}
}
/* _me_lose_north(): lose on a north road.
*/
static void
2014-09-06 00:13:24 +04:00
_me_lose_north(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
top:
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_north_is_normal(dog) ) {
c3_w* dog_w = u3_co_to_ptr(dog);
u3_cs_box* box_u = u3_co_botox(dog_w);
2014-09-05 23:55:16 +04:00
if ( box_u->use_w > 1 ) {
box_u->use_w -= 1;
}
else {
if ( 0 == box_u->use_w ) {
2014-09-06 00:13:24 +04:00
u3_cm_bail(c3__foul);
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_pom(dog)) ) {
u3_cs_cell* dog_u = (void *)dog_w;
u3_noun h_dog = dog_u->hed;
u3_noun t_dog = dog_u->tel;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_ne(u3_co_is_cat(h_dog)) ) {
2014-09-05 23:55:16 +04:00
_me_lose_north(h_dog);
}
2014-09-06 00:13:24 +04:00
u3_ca_free(dog_w);
if ( u3_ne(u3_co_is_cat(t_dog)) ) {
2014-09-05 23:55:16 +04:00
dog = t_dog;
goto top;
}
}
else {
2014-09-06 00:13:24 +04:00
u3_ca_free(dog_w);
2014-09-05 23:55:16 +04:00
}
}
}
}
}
/* _me_lose_south(): lose on a south road.
*/
static void
2014-09-06 00:13:24 +04:00
_me_lose_south(u3_noun dog)
2014-09-05 23:55:16 +04:00
{
top:
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_co_south_is_normal(dog) ) {
c3_w* dog_w = u3_co_to_ptr(dog);
u3_cs_box* box_u = u3_co_botox(dog_w);
2014-09-05 23:55:16 +04:00
if ( box_u->use_w > 1 ) {
box_u->use_w -= 1;
}
else {
if ( 0 == box_u->use_w ) {
2014-09-06 00:13:24 +04:00
u3_cm_bail(c3__foul);
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_pom(dog)) ) {
u3_cs_cell* dog_u = (void *)dog_w;
u3_noun h_dog = dog_u->hed;
u3_noun t_dog = dog_u->tel;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_ne(u3_co_is_cat(h_dog)) ) {
2014-09-05 23:55:16 +04:00
_me_lose_south(h_dog);
}
2014-09-06 00:13:24 +04:00
u3_ca_free(dog_w);
if ( u3_ne(u3_co_is_cat(t_dog)) ) {
2014-09-05 23:55:16 +04:00
dog = t_dog;
goto top;
}
}
else {
2014-09-06 00:13:24 +04:00
u3_ca_free(dog_w);
2014-09-05 23:55:16 +04:00
}
}
}
}
}
2014-09-06 00:13:24 +04:00
/* u3_ca_gain(): gain a reference count, and/or copy juniors.
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
u3_ca_gain(u3_noun som)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_assert(u3_none != som);
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_cat(som)) ) {
2014-09-05 23:55:16 +04:00
return som;
}
else {
2014-09-06 00:13:24 +04:00
return u3_so(u3_co_is_north)
2014-09-05 23:55:16 +04:00
? _me_gain_north(som)
: _me_gain_south(som);
}
}
2014-09-06 00:13:24 +04:00
/* u3_ca_lose(): lose a reference count.
2014-09-05 23:55:16 +04:00
*/
void
2014-09-06 00:13:24 +04:00
u3_ca_lose(u3_noun som)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_ne(u3_co_is_cat(som)) ) {
if ( u3_so(u3_co_is_north) ) {
2014-09-05 23:55:16 +04:00
_me_lose_north(som);
} else {
_me_lose_south(som);
}
}
}
2014-09-06 00:13:24 +04:00
/* u3_ca_use(): reference count.
2014-09-05 23:55:16 +04:00
*/
c3_w
2014-09-06 00:13:24 +04:00
u3_ca_use(u3_noun som)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_cat(som)) ) {
2014-09-05 23:55:16 +04:00
return 1;
}
else {
2014-09-06 00:13:24 +04:00
c3_w* dog_w = u3_co_to_ptr(som);
u3_cs_box* box_u = u3_co_botox(dog_w);
2014-09-05 23:55:16 +04:00
return box_u->use_w;
}
}
2014-09-06 00:13:24 +04:00
/* u3_ca_slab(): create a length-bounded proto-atom.
2014-09-05 23:55:16 +04:00
*/
c3_w*
2014-09-06 00:13:24 +04:00
u3_ca_slab(c3_w len_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_w* nov_w = u3_ca_walloc(len_w + c3_wiseof(u3_cs_atom));
u3_cs_atom* pug_u = (void *)nov_w;
2014-09-05 23:55:16 +04:00
pug_u->mug_w = 0;
pug_u->len_w = len_w;
/* Clear teh slab.
*/
{
c3_w i_w;
for ( i_w=0; i_w < len_w; i_w++ ) {
pug_u->buf_w[i_w] = 0;
}
}
return pug_u->buf_w;
}
2014-09-06 00:13:24 +04:00
/* u3_ca_slaq(): u3_ca_slaq() with a defined blocksize.
2014-09-05 23:55:16 +04:00
*/
c3_w*
2014-09-06 00:13:24 +04:00
u3_ca_slaq(c3_g met_g, c3_w len_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_ca_slab(((len_w << met_g) + 31) >> 5);
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_ca_malt(): measure and finish a proto-atom.
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
u3_ca_malt(c3_w* sal_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_w* nov_w = (sal_w - c3_wiseof(u3_cs_atom));
u3_cs_atom* nov_u = (void *)nov_w;
2014-09-05 23:55:16 +04:00
c3_w len_w;
for ( len_w = nov_u->len_w; len_w; len_w-- ) {
if ( 0 != nov_u->buf_w[len_w - 1] ) {
break;
}
}
2014-09-06 00:13:24 +04:00
return u3_ca_mint(sal_w, len_w);
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_ca_moot(): finish a pre-measured proto-atom; dangerous.
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
u3_ca_moot(c3_w* sal_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_w* nov_w = (sal_w - c3_wiseof(u3_cs_atom));
u3_cs_atom* nov_u = (void*)nov_w;
2014-09-05 23:55:16 +04:00
c3_w len_w = nov_u->len_w;
c3_w las_w = nov_u->buf_w[len_w - 1];
c3_assert(0 != len_w);
c3_assert(0 != las_w);
if ( 1 == len_w ) {
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_cat(las_w)) ) {
u3_ca_free(nov_w);
2014-09-05 23:55:16 +04:00
return las_w;
}
}
2014-09-06 00:13:24 +04:00
return u3_co_to_pug(u3_co_outa(nov_w));
2014-09-05 23:55:16 +04:00
}
2014-09-06 00:13:24 +04:00
/* u3_ca_mint(): finish a measured proto-atom.
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
u3_ca_mint(c3_w* sal_w, c3_w len_w)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
c3_w* nov_w = (sal_w - c3_wiseof(u3_cs_atom));
u3_cs_atom* nov_u = (void*)nov_w;
2014-09-05 23:55:16 +04:00
/* See if we can free the slab entirely.
*/
if ( len_w == 0 ) {
2014-09-06 00:13:24 +04:00
u3_ca_free(nov_w);
2014-09-05 23:55:16 +04:00
return 0;
}
else if ( len_w == 1 ) {
c3_w low_w = nov_u->buf_w[0];
2014-09-06 00:13:24 +04:00
if ( u3_so(u3_co_is_cat(low_w)) ) {
u3_ca_free(nov_w);
2014-09-05 23:55:16 +04:00
return low_w;
}
}
/* See if we can strip off a block on the end.
*/
{
c3_w old_w = nov_u->len_w;
c3_w dif_w = (old_w - len_w);
2014-09-06 00:13:24 +04:00
if ( dif_w >= u3_cc_minimum ) {
c3_w* box_w = (void *)u3_co_botox(nov_w);
c3_w* end_w = (nov_w + c3_wiseof(u3_cs_atom) + len_w + 1);
2014-09-05 23:55:16 +04:00
c3_w asz_w = (end_w - box_w);
c3_w bsz_w = box_w[0] - asz_w;
_box_attach(_box_make(end_w, bsz_w, 0));
box_w[0] = asz_w;
box_w[asz_w - 1] = asz_w;
}
nov_u->len_w = len_w;
}
2014-09-06 00:13:24 +04:00
return u3_co_to_pug(u3_co_outa(nov_w));
2014-09-05 23:55:16 +04:00
}