Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework bbuff code with cleanups. #248

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/quo-vadis-pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ qv_pthread_scopes_free(
return QV_ERR_INVLD_ARG;
}
try {
qvi_scope_thfree(&scopes, nscopes);
qvi_scope_thdelete(&scopes, nscopes);
return QV_SUCCESS;
}
qvi_catch_and_return();
Expand Down
52 changes: 26 additions & 26 deletions src/qvi-bbuff-rmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define QVI_BBUFF_RMI_H

#include "qvi-common.h"
#include "qvi-bbuff.h"
#include "qvi-bbuff.h" // IWYU pragma: keep
#include "qvi-hwloc.h"
#include "qvi-hwpool.h"

Expand Down Expand Up @@ -299,7 +299,7 @@ qvi_bbuff_rmi_pack_item(
qvi_bbuff_t *buff,
size_t data
) {
return qvi_bbuff_append(buff, &data, sizeof(data));
return buff->append(&data, sizeof(data));
}

/**
Expand All @@ -310,7 +310,7 @@ qvi_bbuff_rmi_pack_item(
qvi_bbuff_t *buff,
int data
) {
return qvi_bbuff_append(buff, &data, sizeof(data));
return buff->append(&data, sizeof(data));
}

/**
Expand All @@ -322,7 +322,7 @@ qvi_bbuff_rmi_pack_item(
qv_scope_create_hints_t data
) {
const int dai = (int)data;
return qvi_bbuff_append(buff, &dai, sizeof(dai));
return buff->append(&dai, sizeof(dai));
}

/**
Expand All @@ -334,7 +334,7 @@ qvi_bbuff_rmi_pack_item(
qv_hw_obj_type_t data
) {
const int dai = (int)data;
return qvi_bbuff_append(buff, &dai, sizeof(dai));
return buff->append(&dai, sizeof(dai));
}

/**
Expand All @@ -346,7 +346,7 @@ qvi_bbuff_rmi_pack_item(
qv_device_id_type_t data
) {
const int dai = (int)data;
return qvi_bbuff_append(buff, &dai, sizeof(dai));
return buff->append(&dai, sizeof(dai));
}

/**
Expand All @@ -358,7 +358,7 @@ qvi_bbuff_rmi_pack_item(
qv_scope_intrinsic_t data
) {
const int dai = (int)data;
return qvi_bbuff_append(buff, &dai, sizeof(dai));
return buff->append(&dai, sizeof(dai));
}

#if QVI_SIZEOF_INT != QVI_SIZEOF_PID_T
Expand All @@ -371,7 +371,7 @@ qvi_bbuff_rmi_pack_item(
pid_t data
) {
const int dai = (int)data;
return qvi_bbuff_append(buff, &dai, sizeof(dai));
return buff->append(&dai, sizeof(dai));
}
#endif

Expand All @@ -380,7 +380,7 @@ qvi_bbuff_rmi_pack_item_impl(
qvi_bbuff_t *buff,
cstr_t data
) {
return qvi_bbuff_append(buff, data, strlen(data) + 1);
return buff->append(data, strlen(data) + 1);
}

/**
Expand Down Expand Up @@ -438,9 +438,9 @@ qvi_bbuff_rmi_pack_item(
// We store size then data so unpack has an easier time, but keep
// the user interface order as data then size.
size_t dsize = data.second;
const int rc = qvi_bbuff_append(buff, &dsize, sizeof(dsize));
if (rc != QV_SUCCESS) return rc;
return qvi_bbuff_append(buff, data.first, dsize);
const int rc = buff->append(&dsize, sizeof(dsize));
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
return buff->append(data.first, dsize);
}

/**
Expand All @@ -453,8 +453,8 @@ qvi_bbuff_rmi_pack_item_impl(
) {
// Protect against null data.
if (qvi_unlikely(!data)) {
return qvi_bbuff_append(
buff, QV_BUFF_RMI_NULL_CPUSET,
return buff->append(
QV_BUFF_RMI_NULL_CPUSET,
strlen(QV_BUFF_RMI_NULL_CPUSET) + 1
);
}
Expand All @@ -463,7 +463,7 @@ qvi_bbuff_rmi_pack_item_impl(
int rc = qvi_hwloc_bitmap_asprintf(data, &datas);
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// We are sending the string representation of the cpuset.
rc = qvi_bbuff_append(buff, datas, strlen(datas) + 1);
rc = buff->append(datas, strlen(datas) + 1);
free(datas);
return rc;
}
Expand Down Expand Up @@ -511,7 +511,7 @@ qvi_bbuff_rmi_pack_item(
) {
// Pack hints.
const int rc = qvi_bbuff_rmi_pack_item(buff, data.hints);
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;

return qvi_bbuff_rmi_pack_item(buff, data.cpuset);
}
Expand All @@ -524,25 +524,24 @@ qvi_bbuff_rmi_pack_item(
qvi_bbuff_t *buff,
qvi_hwpool_dev_s *data
) {
// TODO(skg) Move to device code.
// Pack device hints.
int rc = qvi_bbuff_rmi_pack_item(buff, data->hints);
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// Pack device affinity.
rc = qvi_bbuff_rmi_pack_item(buff, data->affinity);
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// Pack device type.
rc = qvi_bbuff_rmi_pack_item(buff, data->type);
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// Pack device ID.
rc = qvi_bbuff_rmi_pack_item(buff, data->m_id);
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// Pack device PCI bus ID.
rc = qvi_bbuff_rmi_pack_item(buff, data->pci_bus_id);
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// Pack device UUID.
rc = qvi_bbuff_rmi_pack_item(buff, data->uuid);
if (rc != QV_SUCCESS) return rc;
return rc;
return qvi_bbuff_rmi_pack_item(buff, data->uuid);
}

/**
Expand Down Expand Up @@ -583,7 +582,7 @@ qvi_bbuff_rmi_pack(
Types&&... args
) {
const int rc = qvi_bbuff_rmi_pack_item(buff, std::forward<T>(arg));
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
return qvi_bbuff_rmi_pack(buff, std::forward<Types>(args)...);
}

Expand Down Expand Up @@ -871,6 +870,7 @@ qvi_bbuff_rmi_unpack_item(
byte_t *buffpos,
size_t *bytes_written
) {
// TODO(skg) Move to dev code.
size_t bw = 0, total_bw = 0;

int rc = qvi_bbuff_rmi_unpack_item(
Expand Down Expand Up @@ -957,7 +957,7 @@ qvi_bbuff_rmi_unpack(
(byte_t *)data,
&bytes_written
);
if (rc != QV_SUCCESS) return rc;
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
pos += bytes_written;
return qvi_bbuff_rmi_unpack(pos, std::forward<Types>(args)...);
}
Expand Down
124 changes: 55 additions & 69 deletions src/qvi-bbuff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,98 +17,84 @@
#include "qvi-bbuff.h"
#include "qvi-utils.h"

struct qvi_bbuff_s {
/** Minimum growth in bytes for resizes, etc. */
static constexpr size_t min_growth = 256;
/** Current capacity of buffer. */
size_t capacity = 0;
/** Amount of data already stored. */
size_t size = 0;
/** Pointer to data backing store. */
void *data = nullptr;
/** Constructor. */
qvi_bbuff_s(void)
{
capacity = min_growth;
data = calloc(capacity, sizeof(byte_t));
if (qvi_unlikely(!data)) throw qvi_runtime_error();
}
/** Copy constructor. */
qvi_bbuff_s(
const qvi_bbuff_s &src
) : qvi_bbuff_s()
{
const int rc = qvi_bbuff_append(this, src.data, src.size);
if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error();
}
/** Destructor. */
~qvi_bbuff_s(void)
{
if (data) free(data);
}
};

int
qvi_bbuff_new(
qvi_bbuff_t **buff
) {
return qvi_new(buff);
qvi_bbuff_s::qvi_bbuff_s(void)
{
m_capacity = s_min_growth;
m_data = calloc(m_capacity, sizeof(byte_t));
if (qvi_unlikely(!m_data)) throw qvi_runtime_error();
}

int
qvi_bbuff_dup(
const qvi_bbuff_t *const src,
qvi_bbuff_t **buff
) {
return qvi_dup(*src, buff);
qvi_bbuff_s::qvi_bbuff_s(
const qvi_bbuff_s &src
) : qvi_bbuff_s()
{
const int rc = append(src.m_data, src.m_size);
if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error();
}

void
qvi_bbuff_delete(
qvi_bbuff_t **buff
) {
qvi_delete(buff);
qvi_bbuff_s::~qvi_bbuff_s(void)
{
if (m_data) free(m_data);
}

void *
qvi_bbuff_data(
qvi_bbuff_t *buff
) {
return buff->data;
size_t
qvi_bbuff_s::size(void) const
{
return m_size;
}

size_t
qvi_bbuff_size(
const qvi_bbuff_t *buff
) {
return buff->size;
void *
qvi_bbuff_s::data(void)
{
return m_data;
}

int
qvi_bbuff_append(
qvi_bbuff_t *buff,
qvi_bbuff_s::append(
const void *const data,
size_t size
) {
const size_t req_capacity = size + buff->size;
if (req_capacity > buff->capacity) {
const size_t req_capacity = size + m_size;
if (req_capacity > m_capacity) {
// New capacity.
const size_t new_capacity = req_capacity + buff->min_growth;
const size_t new_capacity = req_capacity + s_min_growth;
void *new_data = calloc(new_capacity, sizeof(byte_t));
if (qvi_unlikely(!new_data)) return QV_ERR_OOR;
// Memory allocation successful.
memmove(new_data, buff->data, buff->size);
free(buff->data);
buff->capacity = new_capacity;
buff->data = new_data;
memmove(new_data, m_data, m_size);
free(m_data);
m_capacity = new_capacity;
m_data = new_data;
}
byte_t *dest = (byte_t *)buff->data;
dest += buff->size;
byte_t *dest = (byte_t *)m_data;
dest += m_size;
memmove(dest, data, size);
buff->size += size;
m_size += size;
return QV_SUCCESS;
}

int
qvi_bbuff_new(
qvi_bbuff_t **buff
) {
return qvi_new(buff);
}

int
qvi_bbuff_dup(
const qvi_bbuff_t &src,
qvi_bbuff_t **buff
) {
return qvi_dup(src, buff);
}

void
qvi_bbuff_delete(
qvi_bbuff_t **buff
) {
qvi_delete(buff);
}

/*
* vim: ft=cpp ts=4 sts=4 sw=4 expandtab
*/
Loading
Loading