Program Listing for File cmb_objectqueue.h

Return to documentation for file (include/cmb_objectqueue.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_OBJECTQUEUE_H
#define CIMBA_CMB_OBJECTQUEUE_H

#include <stdint.h>

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

#include "cmi_resourcebase.h"

struct cmb_objectqueue {
    struct cmi_resourcebase core;
    struct cmb_resourceguard front_guard;
    struct cmb_resourceguard rear_guard;
    struct queue_tag *queue_head;
    struct queue_tag *queue_end;
    uint64_t capacity;
    uint64_t length;
    bool is_recording;
    struct cmb_timeseries history;
};

extern struct cmb_objectqueue *cmb_objectqueue_create(void);

extern void cmb_objectqueue_initialize(struct cmb_objectqueue *oqp,
                                       const char *name,
                                       uint64_t capacity);

extern void cmb_objectqueue_terminate(struct cmb_objectqueue *oqp);

extern void cmb_objectqueue_destroy(struct cmb_objectqueue *oqp);

extern int64_t cmb_objectqueue_get(struct cmb_objectqueue *oqp,
                                   void **objectloc);

extern int64_t cmb_objectqueue_put(struct cmb_objectqueue *oqp, void *object);

[[maybe_unused]]
static inline const char *cmb_objectqueue_name(struct cmb_objectqueue *oqp)
{
    cmb_assert_debug(oqp != NULL);

    const struct cmi_resourcebase *rbp = (struct cmi_resourcebase *)oqp;
    cmb_assert_release(rbp->cookie == CMI_INITIALIZED);

    return rbp->name;
}

[[maybe_unused]]
static inline uint64_t cmb_objectqueue_length(struct cmb_objectqueue *oqp)
{
    cmb_assert_debug(oqp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)oqp)->cookie == CMI_INITIALIZED);

    return oqp->length;
}

[[maybe_unused]]
static inline uint64_t cmb_objectqueue_space(struct cmb_objectqueue *oqp)
{
    cmb_assert_release(oqp != NULL);
    cmb_assert_release(((struct cmi_resourcebase *)oqp)->cookie == CMI_INITIALIZED);
    cmb_assert_debug(oqp->length <= oqp->capacity);

    return (oqp->capacity - oqp->length);
}

extern uint64_t cmb_objectqueue_position(struct cmb_objectqueue *oqp, void *object);

extern void cmb_objectqueue_recording_start(struct cmb_objectqueue *oqp);

extern void cmb_objectqueue_recording_stop(struct cmb_objectqueue *oqp);

extern struct cmb_timeseries *cmb_objectqueue_history(struct cmb_objectqueue *oqp);

extern void cmb_objectqueue_report_print(struct cmb_objectqueue *oqp, FILE *fp);

#endif /* CIMBA_CMB_OBJECTQUEUE_H */