Programming Macaw OS

Memory

Every program has its own memory, with real addresses. You can ask for some with malloc, read and write single bytes with peek and poke, and give it back with free. Your window's picture is in there too, so poking the right address changes a pixel.

Each program's memory is its own. A bad address in one program can't touch another one, or anything outside Macaw OS. It just stops that program with an error saying which address was wrong.

The machine

The Corvus has 512 MB of memory. Everything every program has asked for counts against it, along with the memory Macaw OS uses itself. About Macaw OS and the Profiler show how much is in use. When it's all used, malloc returns 0.

Memory you've asked for doesn't take up room on the real computer until you write to it. Until then it reads as zeros. So a program can ask for a big block and use a little of it.

Addresses

AddressesWhat's there
0x00000000 to 0x0000FFFFNothing. Reading or writing here is an error, so a pointer that's 0 (NULL) fails straight away
0x00010000 onwardsYour window's picture, one byte per pixel, row by row. Each byte is a colour number
0x01000000 onwardsThe heap, where malloc gives out memory

Anywhere else, and heap memory that malloc hasn't given out, is an error to touch.

Numbers bigger than a byte are stored with the most significant byte first, as on a PowerPC. So poke4(a, 0x01020304) puts 1 at a and 4 at a + 3.

Reading and writing

CallWhat it does
peek(addr) / poke(addr, n)Reads or writes one byte, 0 to 255. Bigger numbers keep their low 8 bits, so poke(a, -1) writes 255
peek2(addr) / poke2(addr, n)Two bytes, 0 to 65535
peek4(addr) / poke4(addr, n)Four bytes, 0 to 4294967295. Negative numbers wrap round, so poke4(a, -1) reads back as 4294967295
peekf(addr) / pokef(addr, x)A 4-byte floating point number (a C float)
peekd(addr) / poked(addr, x)An 8-byte floating point number (a C double)
peekstr(addr, [max])The bytes from addr up to the first 0, as a string. With max, at most that many
pokestr(addr, s)Writes the string's bytes and then a 0
memset(addr, n, count)Writes the byte n into count bytes
memcpy(to, from, count)Copies count bytes. It works even when the two overlap

Asking for memory

CallWhat it does
malloc(size)Gives you size bytes and returns their address, or 0 if there isn't room. The address is always a multiple of 16
calloc(count, size)The same for count things of size bytes each, set to zero
realloc(addr, size)Makes a block bigger or smaller, keeping what's in it. It may move, so use the address it returns
free(addr)Gives a block back. free(0) does nothing. Freeing an address malloc didn't give you, or freeing a block twice, is an error
memory()How many bytes this program has asked for, how much of the machine is in use, and how big it is
local list = malloc(100 * 4)         -- room for 100 four-byte numbers
for i = 0, 99 do poke4(list + i * 4, i * i) end
print(peek4(list + 10 * 4))          -- 100
free(list)

Drawing through memory

screenmem() returns your window picture's address, its width and its height. The pixel at x, y is at addr + y * width + x.

WINDOW = { w = 160, h = 100, title = "Stripes", realtime = true }

function _draw()
  local addr, w, h = screenmem()
  for y = 0, h - 1 do
    for x = 0, w - 1 do
      poke(addr + y * w + x, (x // 8 + y // 8) % 2 == 0 and colors.navy or colors.sky)
    end
  end
end

It's the same picture the drawing calls draw into, so you can mix the two. A window that's resized gets a new picture, so ask screenmem() again rather than keeping the size.