Skip to content

BStkS: growable stack #1

Description

@vitiral

This would be nice to have as it would allow "infinite" return stacks/etc for fngi.

However, the complexity may not be worthwhile in the C implementation. Might only be worthwhile in the fngi bootstrapper. Don't implement unless it is useful

I got started on this before I decided to back up. This doesn't actually compile

In civ.h

// #################################
// # BStkS: an infinitely growable stack that uses blocks to grow/shrink

#define _BStk_BLOCK_AVAIL (BLOCK_SIZE - (sizeof(size_t) * 2))
#define BStk_CAP  (_BStk_BLOCK_AVAIL / sizeof(S))

typedef struct _BStkSNode {   
  struct _BStkSNode* next;
  struct _BStkSNode* prev;
  U2 sp; 
  U2 descendants; // count of descendants
  S dat[_BStk_BLOCK_AVAIL / sizeof(S)];
} BStkSNode;

typedef struct { BStkSNode* node; BA* ba; BANode* owned; } BStkS;
  
S       BStkS_len(BStkS* s);
S*      BStkS_topRef(BStkS* s);
void    BStkS_add(BStkS* s, S value);
S       BStkS_pop(BStkS* s); 

static inline BStkS  BStkS_alloc(BA* ba)  { return (BStkS) { .ba = ba }; }
static inline void   BStkS_free(BStkS* s) { BA_freeAll(s->ba, s->owned); }
static inline Sll*   BStk_asSll(BStkS* b) { return (Sll*) b; }

In civ.c

// #################################
// # BStkS: an infinitely growable stack that uses blocks to grow/shrink

DllRoot* BStkS_asDll(BStkS* s) { return (DllRoot*)&s->node; }

S  BStk_len(BStkS* s) {
  BStkSNode* n = s->node; if(not n) return 0;
  return (BStk_CAP - n->sp) + (BStk_CAP * n->descendants);
} 
  
S* BStk_topRef(BStkS* s) {
  BStkSNode* n = s->node;  ASSERT(n, "BStkS_topRef OOB");
  return &n->dat[n->sp];
}

BStkSNode* BStk_grow(BStkS* s) {
  BANode* baNode = BA_alloc(s->ba); if(not baNode) return NULL;
  Sll_add(
    
  n->next = s->node; ntmp->sp = BStkS_CAP;
  n->descendants = s->node ? (s->node.descendants + 1) : 0;
  s->node = n;
  return n;
}

void BStkS_add(BStkS* s, S value) {
  BStkSNode* n = s->node;
  if(not n or not n->sp) n = BStk_grow(s);
  ASSERT(stk->sp, "BStkS_add: BA is OOM");
  n->dat[-- n->sp] = value;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions