Program Listing for File cmb_buffer.h

Return to documentation for file (include/cmb_buffer.h)

/*
 * Copyright (c) Asbjørn M. Bonvik 2025-26.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CIMBA_CMB_BUFFER_H
#define CIMBA_CMB_BUFFER_H

#include <stdint.h>

#include "cmb_assert.h"
#include "cmb_resourceguard.h"
#include "cmb_timeseries.h"

#include "cmi_resourcebase.h"

struct cmb_buffer {
    struct cmi_resourcebase core;
    struct cmb_resourceguard front_guard;
    struct cmb_resourceguard rear_guard;
    uint64_t capacity;
    uint64_t level;
    bool is_recording;
    struct cmb_timeseries history;
};

extern struct cmb_buffer *cmb_buffer_create(void);

extern void cmb_buffer_initialize(struct cmb_buffer *bp,
                                  const char *name,
                                  uint64_t capacity);

extern void cmb_buffer_terminate(struct cmb_buffer *bp);

extern void cmb_buffer_destroy(struct cmb_buffer *bp);

extern int64_t cmb_buffer_get(struct cmb_buffer *bp, uint64_t *amntp);

extern int64_t cmb_buffer_put(struct cmb_buffer *bp, uint64_t *amntp);

[[maybe_unused]]
static inline const char *cmb_buffer_get_name(struct cmb_buffer *bp)
{
    cmb_assert_debug(bp != NULL);

    const struct cmi_resourcebase *rbp = (struct cmi_resourcebase *)bp;

    return rbp->name;
}

[[maybe_unused]]
static inline uint64_t cmb_buffer_level(struct cmb_buffer *bp)
{
    cmb_assert_debug(bp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)bp)->cookie == CMI_INITIALIZED);

    return bp->level;
}

[[maybe_unused]]
static inline uint64_t cmb_buffer_space(struct cmb_buffer *bp)
{
    cmb_assert_release(bp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)bp)->cookie == CMI_INITIALIZED);
    cmb_assert_debug(bp->level <= bp->capacity);

    return (bp->capacity - bp->level);
}

extern void cmb_buffer_recording_start(struct cmb_buffer *bp);

extern void cmb_buffer_recording_stop(struct cmb_buffer *bp);

extern struct cmb_timeseries *cmb_buffer_history(struct cmb_buffer *bp);

extern void cmb_buffer_print_report(struct cmb_buffer *bp, FILE *fp);

#endif /* CIMBA_CMB_BUFFER_H */