BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
memlib.c File Reference
#include "blitzlib.h"
#include "blitzexec.h"

Data Structures

struct  memblock
struct  memlist

Macros

#define BUCKET_K   8
#define BUCKET_SIZE   (4+8*BUCKET_K)
#define BUCKET_LAST   (4+8*(BUCKET_K-1))

Functions

void localloc (void)
 localloc ($C000) - allocate some memory in the local region!
void locfree (void)
 locfree ($C001) - free memory of the current local list.
void globalloc (void)
 globalloc ($C002) - like localloc, but in the global (last) list; globfree ($C003) - like locfree, in the global list.
void globfree (void)
void newmem (void)
 newmem ($C004, at procedure entry) - create a new node (for local stuff): a new current local list.
void freelast (void)
 freelast ($C005, at procedure exit) - free up most recent node with all its blocks.
void memlib_codeinit (void)
void memlib_codefinit (void)
static void codefinit_c (void)
 Library finit and token $C006 - free ALL memory allocated: every list (local and global) with all its blocks.
static void err1 (void)
static ULONG codeinit_c (void)
 Library init - start with one list (the global one).

Variables

char membase []
static struct memlist * firstdata
static struct memlist * bklast
static UWORD memerr
static const char mess1 [] = "Out of Memory!"
static const char mess2 [] = "Unable to Free Memory"

Macro Definition Documentation

◆ BUCKET_K

#define BUCKET_K   8

◆ BUCKET_LAST

#define BUCKET_LAST   (4+8*(BUCKET_K-1))

◆ BUCKET_SIZE

#define BUCKET_SIZE   (4+8*BUCKET_K)

Function Documentation

◆ codefinit_c()

void codefinit_c ( void )
static

Library finit and token $C006 - free ALL memory allocated: every list (local and global) with all its blocks.

freelast until there is no list (freelast's Z is firstdata = 0); the freelast of the last list frees the buckets too. Keeps all registers (the thunk; freelast keeps d0/d2-d7/a2-a6).

◆ codeinit_c()

ULONG codeinit_c ( void )
static

Library init - start with one list (the global one).

Returns
d0: the init value, the jump table membase (localloc, locfree, globalloc, globfree; !libs 65530,la6 of other libraries).

◆ err1()

void err1 ( void )
static

◆ freelast()

void freelast ( void )

freelast ($C005, at procedure exit) - free up most recent node with all its blocks.

The list header goes back to its bucket, then every block is freed with its stored length (FreeMem). When the header was the first entry of its bucket, that bucket is empty: the spare above it, if any, is freed (the last two buckets were empty), and the bucket itself stays as the spare, unless it held the last list (then it is freed too: no list, no bucket). Keeps d0 and d2-d7/a2-a6: the compiler calls it at the end of a procedure with the result of a Function in d0 (perf-analysis thunks.md 1.4); d1, a0 and a1 are free (the original keeps them), no third-party library calls it.

An assembler entry, the original's code with d0/a2/a6 saved: C cannot keep d0 nor return a flag, and the thunk around the C function saved d0-d1/a0-a1.

Result
Z = 1 if there was none (the original's MOVEQ #-1,d0 / the BEQ on firstdata; internal, not a Blitz truth value).

◆ globalloc()

void globalloc ( void )

globalloc ($C002) - like localloc, but in the global (last) list; globfree ($C003) - like locfree, in the global list.

The original's frame: firstdata is saved, set to the global list (the last one, the original's findglob, here inline) and restored behind the call of localloc / locfree. globalloc keeps d2-d7/a0-a3/a6, globfree d1-d7/a0/a2-a3/a6. Errors as localloc / locfree.

Third-party libraries (XBCiaTrackerLib, XBSoundSystemLib, RIARexxLib, EFMUILib, and banklib AllocMem's callers) test the result of globalloc with beq right after the call, without a tst: the original leaves Z clear (its last flag-setting instruction moves the list head, never 0, and a failure does not return). Fixed (#1059): globalloc ends with tst.l d0, so the flags do not depend on the caller's registers. Third-party callers keep a0/a1 across globalloc and d1/a0 across globfree (analyse-internal-token-callers.md).

globalloc:

  • d0 size, the size in bytes.
  • d1 flags, the AllocMem flags.
    Result
    d0: the memory, the flags of tst.l d0 (Z clear after a successful allocation).
    globfree:
  • a1 mem, the memory (d0 = length, ignored).

◆ globfree()

void globfree ( void )

◆ localloc()

void localloc ( void )

localloc ($C000) - allocate some memory in the local region!

The block (8 bytes longer: next, length + 8) goes into the current (first) list. Keeps d2-d7/a0-a3/a6 (exec keeps d2-d7/a2-a5).

An assembler entry, the original's code: a thunk around a C function saved a0/a1 and the C function a6 and d2 again, behind a second call level.

Errors: out of memory - err1: all blocks are freed (the empty global list stays) and the runtime error "Out of Memory!" raised. DIFF: the original never returns from err1 (its saved registers stay on the stack); here the result is 0.

  • d0 size, the size in bytes.
  • d1 flags, the AllocMem flags.
    Result
    d0: the memory; Z clear after a successful allocation (the flags of the final ADDQ, as in the original, whose failure does not return; the same as globalloc), Z set with 0.

◆ locfree()

void locfree ( void )

locfree ($C001) - free memory of the current local list.

The block is looked up in the list and freed with its stored length. Keeps d1-d7/a0/a2-a3/a6.

An assembler entry, the original's code (a thunk around a C function saved d1/a0 and the C function a6 again).

Errors: a block that is not in the list - the runtime error "Unable to Free Memory", reported only once (memerr). DIFF (K): if the trap returns, the original runs on into newmem with locfree's saved registers on the stack; here locfree returns.

  • a1 mem, the memory (d0 = length, ignored!).

◆ memlib_codefinit()

void memlib_codefinit ( void )

◆ memlib_codeinit()

void memlib_codeinit ( void )

◆ newmem()

void newmem ( void )

newmem ($C004, at procedure entry) - create a new node (for local stuff): a new current local list.

The list header (8 bytes, no blocks) in front of firstdata. It is the next entry of the top bucket (see the buckets above); only when that bucket is full and has no spare is a new bucket allocated (AllocMem(BUCKET_SIZE, MEMF_PUBLIC)). Keeps d0-d7/a2-a6: the compiler calls it in the procedure prologue with the parameters in d0-d5 (perf-analysis thunks.md 1.4); a0 and a1 are free (the original keeps them), no third-party library calls it.

An assembler entry: the fast path uses a0/a1 only; the bucket change saves d0-d1/a6 around AllocMem.

Errors: no memory for a bucket - err1: all blocks are freed (the empty global list stays) and the runtime error "Out of Memory!" raised (DIFF: err1 returns, see there).

Variable Documentation

◆ bklast

struct memlist* bklast
static

◆ firstdata

struct memlist* firstdata
static

◆ membase

void membase ( )
extern

◆ memerr

UWORD memerr
static

◆ mess1

const char mess1[] = "Out of Memory!"
static

◆ mess2

const char mess2[] = "Unable to Free Memory"
static