aboutsummaryrefslogtreecommitdiff
path: root/src/bufio/buffer.c
blob: 8963652518e801a30931b8bad8623cb545b33bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "impl.h"

// NewBuffer allocates a bytes buffer with size 'size'.
// This is unallocated usage.
// Returns the fat pointer by value.

void*
newbuffer(uintptr off, uintptr size)
{
    if (size == 0 || size > BUF·max) size = BUF·size;

    void   *mem = malloc(off + sizeof(Buffer) + size);
    Buffer *buf = mem + off;

    buf->eof  = 0;
    buf->off  = 0;
    buf->base = buf->b + BUF·ungets;
    buf->end  = buf->b + size;
    buf->size = buf->end - buf->base;

    memset(buf->b, 0, size);

    return mem;
}