3. Callbacks for cmp

CMP uses a callback interface to allow the messaging system to be read/written to in many ways. The callbacks below are written specifically to be useful for monolith.

3.1. get_size

The get_size callback is used when determining how many bytes are required to be allocated. Instead of writing to a buffer, get_size will increment an integer variable.

<<moncmp_callbacks>>=
static size_t get_size(cmp_ctx_t *ctx,
                       const void *ud,
                       size_t count)
{
    size_t *size;
    size = (size_t *)ctx->buf;
    *size += count;
    return count;
}

3.2. memwrite

This callback is used for writing values to a memory buffer.

<<moncmp_callbacks>>=
static size_t memwrite(cmp_ctx_t *ctx, const void *ud, size_t count)
{
    size_t n;
    moncmp_d *m;
    const uint8_t *bytes;

    m = ctx->buf;
    bytes = ud;
    for(n = 0; n < count; n++) {
        m->buf[m->pos + n] = bytes[n];
    }
    m->pos += count;
    return count;
}

3.3. memread

Used to read values from a buffer.

<<moncmp_callbacks>>=
static bool memread(cmp_ctx_t *ctx, void *data, size_t limit)
{
    size_t n;
    moncmp_d *m;
    uint8_t *bytes;

    m = ctx->buf;
    bytes = data;

    if (m->pos + limit > m->sz) {
        fprintf(stderr,
                "Read %ld out of range of size %ld\n",
                m->pos + limit, m->sz);
        return false;
    }

    for (n = 0; n < limit; n++) {
        bytes[n] = m->buf[m->pos + n];
    }
    m->pos += limit;
    return true;
}



prev | home | next