Commit cda19591 authored by Éric Thiébaut's avatar Éric Thiébaut
Browse files

Normalize API names for i/o buffers

parent e74c4bb0
......@@ -65,7 +65,7 @@ static size_t next_buffer_size(
// Having a zero-filled structure is correctly interpreted as an initialized
// empty static buffer. This has the same effects as initailizing the buffer
// contents with the macro TAO_BUFFER_INITIALIZER.
void tao_initialize_static_buffer(
void tao_buffer_initialize(
tao_buffer* buf)
{
buf->data = NULL;
......@@ -75,7 +75,7 @@ void tao_initialize_static_buffer(
buf->flags = 0;
}
tao_buffer* tao_create_buffer(
tao_buffer* tao_buffer_create(
size_t size)
{
tao_buffer* buf = (tao_buffer*)tao_malloc(sizeof(tao_buffer));
......@@ -103,7 +103,7 @@ tao_buffer* tao_create_buffer(
return buf;
}
void tao_finalize_buffer(
void tao_buffer_destroy(
tao_buffer* buf)
{
if (buf != NULL) {
......@@ -114,7 +114,7 @@ void tao_finalize_buffer(
free((void*)buf);
} else {
// Reset the container.
tao_initialize_static_buffer(buf);
tao_buffer_initialize(buf);
}
if (data != NULL && (flags & VOLATILE_DATA) != 0) {
// Free the contents.
......@@ -123,7 +123,7 @@ void tao_finalize_buffer(
}
}
tao_status tao_resize_buffer(
tao_status tao_buffer_resize(
tao_buffer* buf,
size_t cnt)
{
......@@ -144,7 +144,7 @@ tao_status tao_resize_buffer(
if (cnt > avail) {
if (buf->pending + cnt <= buf->size) {
// Buffer size is sufficient, just move the contents.
tao_flush_buffer(buf);
tao_buffer_flush(buf);
} else {
// Reallocate the buffer.
size_t minsize = buf->pending + cnt;
......@@ -181,7 +181,7 @@ tao_status tao_resize_buffer(
return TAO_OK;
}
void tao_flush_buffer(
void tao_buffer_flush(
tao_buffer* buf)
{
if (buf->offset > 0 && buf->pending > 0) {
......@@ -191,20 +191,20 @@ void tao_flush_buffer(
}
}
void tao_clear_buffer(
void tao_buffer_clear(
tao_buffer* buf)
{
buf->pending = 0;
buf->offset = 0;
}
size_t tao_get_buffer_contents_size(
size_t tao_buffer_get_contents_size(
tao_buffer* buf)
{
return buf->pending;
}
void* tao_get_buffer_contents(
void* tao_buffer_get_contents(
const tao_buffer* buf,
size_t* sizptr)
{
......@@ -214,19 +214,19 @@ void* tao_get_buffer_contents(
return buf->data + buf->offset;
}
size_t tao_get_buffer_unused_size(
size_t tao_buffer_get_unused_size(
const tao_buffer* buf)
{
return buf->size - (buf->offset + buf->pending);
}
size_t tao_get_buffer_total_unused_size(
size_t tao_buffer_get_total_unused_size(
const tao_buffer* buf)
{
return buf->size - buf->pending;
}
size_t tao_get_buffer_unused_part(
size_t tao_buffer_get_unused_part(
const tao_buffer* buf,
void** data)
{
......@@ -236,7 +236,7 @@ size_t tao_get_buffer_unused_part(
return buf->size - offset;
}
tao_status tao_adjust_buffer_contents_size(
tao_status tao_buffer_adjust_contents_size(
tao_buffer* buf,
ssize_t adj)
{
......@@ -276,9 +276,9 @@ tao_status tao_adjust_buffer_contents_size(
return TAO_OK;
}
ssize_t tao_read_to_buffer(
int fd,
ssize_t tao_buffer_read_from_fd(
tao_buffer* buf,
int fd,
size_t cnt)
{
// Quick return?
......@@ -298,10 +298,10 @@ ssize_t tao_read_to_buffer(
if (cnt > avail) {
if (buf->pending + cnt <= buf->size) {
// Buffer size is sufficient, just move the contents.
tao_flush_buffer(buf);
tao_buffer_flush(buf);
} else {
// Reallocate the buffer.
if (tao_resize_buffer(buf, cnt) == TAO_ERROR) {
if (tao_buffer_resize(buf, cnt) == TAO_ERROR) {
return -1;
}
}
......@@ -326,9 +326,9 @@ ssize_t tao_read_to_buffer(
return nr;
}
ssize_t tao_write_from_buffer(
int fd,
tao_buffer* buf)
ssize_t tao_buffer_write_to_fd(
tao_buffer* buf,
int fd)
{
// Check arguments.
if (check_buffer_struct(buf) != TAO_OK) {
......@@ -371,7 +371,7 @@ ssize_t tao_write_from_buffer(
return 0;
}
tao_status tao_print_to_buffer(
tao_status tao_buffer_printf(
tao_buffer* buf,
const char* format,
...)
......@@ -380,12 +380,12 @@ tao_status tao_print_to_buffer(
va_list args;
va_start(args, format);
status = tao_vprint_to_buffer(buf, format, args);
status = tao_buffer_vprintf(buf, format, args);
va_end(args);
return status;
}
tao_status tao_vprint_to_buffer(
tao_status tao_buffer_vprintf(
tao_buffer* buf,
const char* format,
va_list args)
......@@ -416,19 +416,19 @@ tao_status tao_vprint_to_buffer(
}
}
// Unused part was too small. Enlarge it before retrying.
if (tao_resize_buffer(buf, len + 1) == TAO_ERROR) {
if (tao_buffer_resize(buf, len + 1) == TAO_ERROR) {
return TAO_ERROR;
}
}
}
tao_status tao_append_to_buffer(
tao_status tao_buffer_append_bytes(
tao_buffer* buf,
const void* ptr,
long siz)
{
if (siz > 0) {
if (tao_resize_buffer(buf, siz) == TAO_ERROR) {
if (tao_buffer_resize(buf, siz) == TAO_ERROR) {
return TAO_ERROR;
}
memcpy(buf->data + buf->offset + buf->pending, ptr, siz);
......@@ -437,7 +437,7 @@ tao_status tao_append_to_buffer(
return TAO_OK;
}
tao_status tao_put_string_to_buffer(
tao_status tao_buffer_append_string(
tao_buffer* buf,
const char* str,
long len)
......@@ -445,7 +445,7 @@ tao_status tao_put_string_to_buffer(
if (len < 0) {
len = TAO_STRLEN(str);
}
if (tao_resize_buffer(buf, len + 1) == TAO_ERROR) {
if (tao_buffer_resize(buf, len + 1) == TAO_ERROR) {
return TAO_ERROR;
}
long end = buf->offset + buf->pending;
......@@ -457,11 +457,11 @@ tao_status tao_put_string_to_buffer(
return TAO_OK;
}
tao_status tao_put_char_to_buffer(
tao_status tao_buffer_append_char(
tao_buffer* buf,
int c)
{
if (tao_resize_buffer(buf, 2) == TAO_ERROR) {
if (tao_buffer_resize(buf, 2) == TAO_ERROR) {
return TAO_ERROR;
}
long end = buf->offset + buf->pending;
......
......@@ -251,7 +251,7 @@ tao_status tao_pack_words(
// can be used to write the result. This information must be updated every
// time we need to resize the unused part.
char* buf;
long avail = tao_get_buffer_unused_part(dest, (void**)&buf);
long avail = tao_buffer_get_unused_part(dest, (void**)&buf);
// Encode all words of the list.
for (int iarg = 0; (iarg < argc) || (argc == -1 &&
......@@ -312,10 +312,10 @@ tao_status tao_pack_words(
if (avail < minsize) {
// Not enough space is available. Resize the buffer and retrieve
// the new position and size of its unused part.
if (tao_resize_buffer(dest, minsize) != TAO_OK) {
if (tao_buffer_resize(dest, minsize) != TAO_OK) {
return TAO_ERROR;
}
avail = tao_get_buffer_unused_part(dest, (void**)&buf);
avail = tao_buffer_get_unused_part(dest, (void**)&buf);
ASSERT(avail >= minsize, return TAO_ERROR);
}
......@@ -365,14 +365,14 @@ tao_status tao_pack_words(
// one more character can be written. This should be always the case
// unless an empty word list was given.
if (avail < off + 1) {
if (tao_resize_buffer(dest, off + 1) != TAO_OK) {
if (tao_buffer_resize(dest, off + 1) != TAO_OK) {
return TAO_ERROR;
}
avail = tao_get_buffer_unused_part(dest, (void**)&buf);
avail = tao_buffer_get_unused_part(dest, (void**)&buf);
ASSERT(avail >= off + 1, return TAO_ERROR);
}
buf[off++] = '\n';
// Adjust the size of the buffer contents to account for the added command.
return tao_adjust_buffer_contents_size(dest, off);
return tao_buffer_adjust_contents_size(dest, off);
}
......@@ -73,7 +73,7 @@ static tao_status buffer_reporter(
int code)
{
reporter_context* ctx = data;
return tao_print_to_buffer((tao_buffer*)ctx->output, error_format,
return tao_buffer_printf((tao_buffer*)ctx->output, error_format,
ctx->prefix, reason, func, info, ctx->suffix);
}
......
......@@ -140,7 +140,7 @@ static void error(
va_start(args, format);
if (buf != NULL) {
if (tao_vprint_to_buffer(buf, format, args) != TAO_OK) {
if (tao_buffer_vprintf(buf, format, args) != TAO_OK) {
tao_report_error();
}
} else {
......
......@@ -30,7 +30,7 @@ static bool initialized = false;
void tao_clear_server_buffer(
tao_server* srv)
{
tao_clear_buffer(&srv->databuf);
tao_buffer_clear(&srv->databuf);
}
tao_status tao_put_string_to_server_buffer(
......@@ -38,7 +38,7 @@ tao_status tao_put_string_to_server_buffer(
const char* str,
long len)
{
return tao_put_string_to_buffer(&srv->databuf, str, len);
return tao_buffer_append_string(&srv->databuf, str, len);
}
tao_status tao_print_to_server_buffer(
......@@ -50,7 +50,7 @@ tao_status tao_print_to_server_buffer(
va_list args;
va_start(args, format);
status = tao_vprint_to_buffer(&srv->databuf, format, args);
status = tao_buffer_vprintf(&srv->databuf, format, args);
va_end(args);
return status;
}
......@@ -60,7 +60,7 @@ tao_status tao_append_to_server_buffer(
const void* ptr,
long siz)
{
return tao_append_to_buffer(&srv->databuf, ptr, siz);
return tao_buffer_append_bytes(&srv->databuf, ptr, siz);
}
void tao_set_static_reply_message(
......@@ -75,12 +75,12 @@ tao_status tao_set_reply_message(
const char* msg,
long len)
{
tao_clear_buffer(&srv->mesgbuf);
if (tao_put_string_to_buffer(&srv->mesgbuf, msg, len) != TAO_OK) {
tao_buffer_clear(&srv->mesgbuf);
if (tao_buffer_append_string(&srv->mesgbuf, msg, len) != TAO_OK) {
srv->message = NULL;
return TAO_ERROR;
}
srv->message = tao_get_buffer_contents(&srv->mesgbuf, NULL);
srv->message = tao_buffer_get_contents(&srv->mesgbuf, NULL);
return TAO_OK;
}
......@@ -91,15 +91,15 @@ tao_status tao_format_reply_message(
{
tao_status status;
va_list args;
tao_clear_buffer(&srv->mesgbuf);
tao_buffer_clear(&srv->mesgbuf);
va_start(args, format);
status = tao_vprint_to_buffer(&srv->mesgbuf, format, args);
status = tao_buffer_vprintf(&srv->mesgbuf, format, args);
va_end(args);
if (status != TAO_OK) {
srv->message = NULL;
return TAO_ERROR;
}
srv->message = tao_get_buffer_contents(&srv->mesgbuf, NULL);
srv->message = tao_buffer_get_contents(&srv->mesgbuf, NULL);
return TAO_OK;
}
......@@ -118,7 +118,7 @@ static const char* get_error_message(
} else {
// Write error messages in the embedded dynamic buffer.
tao_error* err = tao_get_last_error();
tao_clear_buffer(&srv->mesgbuf);
tao_buffer_clear(&srv->mesgbuf);
if (err->code != TAO_SUCCESS) {
// Print error message to the i/o buffer. A fatal error
// results in case of failure here.
......@@ -126,7 +126,7 @@ static const char* get_error_message(
&srv->mesgbuf, err, NULL, NULL) != TAO_OK) {
tao_panic();
}
message = tao_get_buffer_contents(&srv->mesgbuf, NULL);
message = tao_buffer_get_contents(&srv->mesgbuf, NULL);
} else {
// There were no errors!
message = "Failure reported but no error information!";
......@@ -182,8 +182,8 @@ static int send_callback(
if (tao_get_last_error()->code != TAO_SUCCESS) {
tao_report_error();
}
tao_clear_buffer(&srv->databuf);
tao_clear_buffer(&srv->mesgbuf);
tao_buffer_clear(&srv->databuf);
tao_buffer_clear(&srv->mesgbuf);
if (srv->debuglevel > 1) {
fprintf(stderr, "send: \"%s\"\n", command);
}
......@@ -243,8 +243,8 @@ static int recv_callback(
if (tao_get_last_error()->code != TAO_SUCCESS) {
tao_report_error();
}
tao_clear_buffer(&srv->databuf);
tao_clear_buffer(&srv->mesgbuf);
tao_buffer_clear(&srv->databuf);
tao_buffer_clear(&srv->mesgbuf);
if (srv->debuglevel > 1) {
fprintf(stderr, "recv: \"%s\" [%ld byte(s)]\n", command, (long)siz);
}
......@@ -293,14 +293,14 @@ tao_status tao_set_reply_data_from_buffer(
bool newline)
{
size_t len;
char* buf = tao_get_buffer_contents(&srv->databuf, &len);
char* buf = tao_buffer_get_contents(&srv->databuf, &len);
if (newline && len > 0 && buf[len-1] != '\n') {
// Append a newline for more readable output when xapset/xpaget are
// used from the command line.
if (tao_put_char_to_buffer(&srv->databuf, '\n') != TAO_OK) {
if (tao_buffer_append_char(&srv->databuf, '\n') != TAO_OK) {
return TAO_ERROR;
}
buf = tao_get_buffer_contents(&srv->databuf, &len);
buf = tao_buffer_get_contents(&srv->databuf, &len);
}
*bufptr = buf;
*lenptr = len;
......@@ -347,8 +347,8 @@ tao_server* tao_create_server(
if (srv == NULL) {
return NULL;
}
tao_initialize_static_buffer(&srv->databuf);
tao_initialize_static_buffer(&srv->mesgbuf);
tao_buffer_initialize(&srv->databuf);
tao_buffer_initialize(&srv->mesgbuf);
srv->running = false;
srv->debuglevel = 0;
srv->send = send;
......@@ -420,8 +420,8 @@ void tao_destroy_server(
if (srv->address != NULL) {
free((void*)srv->address);
}
tao_finalize_buffer(&srv->databuf);
tao_finalize_buffer(&srv->mesgbuf);
tao_buffer_destroy(&srv->databuf);
tao_buffer_destroy(&srv->mesgbuf);
free((void*)srv);
}
}
......@@ -628,7 +628,7 @@ tao_status tao_serve_send_command(
return TAO_ERROR;
}
#if 0 // FIXME: do we automatically recover data from the server buffer?
*bufptr = tao_get_buffer_contents(tao_get_server_buffer(srv), sizptr);
*bufptr = tao_buffer_get_contents(tao_get_server_buffer(srv), sizptr);
#endif
return TAO_OK;
}
......
......@@ -64,7 +64,7 @@ typedef struct tao_buffer_ {
*
* ~~~~~{.c}
* static tao_buffer buf;
* tao_initialize_static_buffer(&buf);
* tao_buffer_initialize(&buf);
* ~~~~~
*/
#define TAO_BUFFER_INITIALIZER { NULL, 0, 0, 0, 0}
......@@ -74,18 +74,18 @@ typedef struct tao_buffer_ {
*
* Use this function to initialize statically an i/o buffer structure. When no
* longer needed, the internal resources which may have been allocated must be
* released by calling tao_finalize_buffer(). The structure itself will not be
* freed by tao_finalize_buffer() which will reset its contents as if just
* released by calling tao_buffer_destroy(). The structure itself will not be
* freed by tao_buffer_destroy() which will reset its contents as if just
* initialized instead.
*
* @warning Do not call this function on a buffer created by
* tao_create_buffer().
* tao_buffer_create().
*
* @param buf Address of a static i/o buffer structure.
*
* @see TAO_BUFFER_INITIALIZER, tao_create_buffer().
* @see TAO_BUFFER_INITIALIZER, tao_buffer_create().
*/
extern void tao_initialize_static_buffer(
extern void tao_buffer_initialize(
tao_buffer* buf);
/**
......@@ -94,7 +94,7 @@ extern void tao_initialize_static_buffer(
* This function creates a new i/o buffer. Both the container (the buffer
* structure) and the contents (the data stored by the buffer) will be
* dynamically allocated. When no longer needed, the caller is responsible for
* calling tao_finalize_buffer() to release all the resources allocated for
* calling tao_buffer_destroy() to release all the resources allocated for
* the buffer (that is, the container and the contents).
*
* @param size Initial number of bytes of the buffer (actual number of
......@@ -102,23 +102,23 @@ extern void tao_initialize_static_buffer(
*
* @return The address of the new buffer; `NULL` in case of failure.
*/
extern tao_buffer* tao_create_buffer(
extern tao_buffer* tao_buffer_create(
size_t size);
/**
* Destroy dynamic resources of an i/o buffer.
*
* This function frees any dynamic resources used by the i/o buffer @b buf.
* If the buffer has been initialized by tao_initialize_static_buffer(), only
* If the buffer has been initialized by tao_buffer_initialize(), only
* the contents of the buffer may be destroyed and the buffer is reset to have
* an empty contents, just as done by tao_initialize_static_buffer(), and can
* be safely re-used. If the buffer has been created by tao_create_buffer(),
* an empty contents, just as done by tao_buffer_initialize(), and can
* be safely re-used. If the buffer has been created by tao_buffer_create(),
* the contents and the container (that is, the structure itself) are destroyed
* and @b buf must no longer be used.
*
* @param buf Address of the i/o buffer to destroy (can be `NULL`).
*/
extern void tao_finalize_buffer(
extern void tao_buffer_destroy(
tao_buffer* buf);
/**
......@@ -135,11 +135,12 @@ extern void tao_finalize_buffer(
* calling this function.
*
* @param buf Address of the i/o buffer.
*
* @param cnt Minimum size (in bytes) of unused space.
*
* @return @ref TAO_OK on success, @ref TAO_ERROR in case of failure.
*/
extern tao_status tao_resize_buffer(
extern tao_status tao_buffer_resize(
tao_buffer* buf,
size_t cnt);
......@@ -155,7 +156,7 @@ extern tao_status tao_resize_buffer(
*
* @param buf Address of the i/o buffer.
*/
extern void tao_flush_buffer(
extern void tao_buffer_flush(
tao_buffer* buf);
/**
......@@ -168,15 +169,15 @@ extern void tao_flush_buffer(
*
* @param buf Address of the i/o buffer.
*/
extern void tao_clear_buffer(
extern void tao_buffer_clear(
tao_buffer* buf);
/**
* Get the size of the contents of an i/o buffer.
*
* This function yields the size of the contents (the pending data) of an i/o
* buffer. Use tao_get_buffer_contents() to retrieve the address of the
* contents of the buffer and tao_adjust_buffer_contents_size() to remove the
* buffer. Use tao_buffer_get_contents() to retrieve the address of the
* contents of the buffer and tao_buffer_adjust_contents_size() to remove the
* bytes you may have consumed.
* @warning This function is meant to be fast. It assumes its argument is
......@@ -186,9 +187,9 @@ extern void tao_clear_buffer(
*
* @return The number of pending bytes in the buffer.
*
* @see tao_get_buffer_contents, tao_adjust_buffer_contents_size.
* @see tao_buffer_get_contents, tao_buffer_adjust_contents_size.
*/
extern size_t tao_get_buffer_contents_size(
extern size_t tao_buffer_get_contents_size(
tao_buffer* buf);
/**
......@@ -196,7 +197,7 @@ extern size_t tao_get_buffer_contents_size(
*
* This function yields the address and, optionally the size, of the contents
* (the pending bytes) stored in an i/o buffer and which has yet not been
* consumed. Call tao_adjust_buffer_contents_size() to remove the bytes you
* consumed. Call tao_buffer_adjust_contents_size() to remove the bytes you
* may have consumed.
*
* @warning The returned information is only valid until no operations that may
......@@ -207,15 +208,16 @@ extern size_t tao_get_buffer_contents_size(
* correct and makes no error checking.
*
* @param buf Address of the i/o buffer.
*
* @param sizptr Address to store the number of pending bytes. Can be `NULL`
* to not retrieve this information. The number of pending
* bytes is also given by tao_get_buffer_contents_size().
* bytes is also given by tao_buffer_get_contents_size().
*
* @return The address of the first pending byte.
*
* @see tao_get_buffer_contents_size, tao_adjust_buffer_contents_size.
* @see tao_buffer_get_contents_size, tao_buffer_adjust_contents_size.
*/
extern void* tao_get_buffer_contents(
extern void* tao_buffer_get_contents(
const tao_buffer* buf,
size_t* sizptr);
......@@ -224,9 +226,9 @@ extern void* tao_get_buffer_contents(
*
* This function returns the number of unused bytes after the contents of an
* i/o buffer. These bytes are directly available to add more contents to the
* buffer. You may call tao_resize_buffer() to make sure enough unused space
* is available. Call tao_get_buffer_unused_part() to retrieve the address of
* the unused part and then, possibly, tao_adjust_buffer_contents_size() to
* buffer. You may call tao_buffer_resize() to make sure enough unused space
* is available. Call tao_buffer_get_unused_part() to retrieve the address of
* the unused part and then, possibly, tao_buffer_adjust_contents_size() to
* indicate the number of bytes that have been added.
* @warning This function is meant to be fast. It assumes its argument is
......@@ -236,10 +238,10 @@ extern void* tao_get_buffer_contents(
*
* @return The number of unused bytes after the contents of the buffer.
*
* @see tao_resize_buffer, tao_flush_buffer, tao_get_buffer_unused_part,
* tao_get_buffer_total_unused_size, tao_adjust_buffer_contents_size.
* @see tao_buffer_resize, tao_buffer_flush, tao_buffer_get_unused_part,
* tao_buffer_get_total_unused_size, tao_buffer_adjust_contents_size.
*/
extern size_t tao_get_buffer_unused_size(
extern size_t tao_buffer_get_unused_size(
const tao_buffer* buf);
/**
......@@ -247,7 +249,7 @@ extern size_t tao_get_buffer_unused_size(
*
* This function yields the total number of unused bytes in an i/o buffer.
* That is, the number of bytes that would be unused after the contents of the
* buffer if tao_flush_buffer() have been called.
* buffer if tao_buffer_flush() have been called.
*
* @warning This function is meant to be fast. It assumes its argument is
* correct and makes no error checking.
......@@ -256,9 +258,9 @@ extern size_t tao_get_buffer_unused_size(
*
* @return The total number of unused bytes in the buffer.
*
* @see tao_flush_buffer, tao_get_buffer_unused_size.
* @see tao_buffer_flush, tao_buffer_get_unused_size.
*/
extern size_t tao_get_buffer_total_unused_size(
extern size_t tao_buffer_get_total_unused_size(
const tao_buffer* buf);
/**
......@@ -267,7 +269,7 @@ extern size_t tao_get_buffer_total_unused_size(
* This function yields the address and the size of the unused space at the end
* an i/o buffer and which can directly be used to append more data. If you
* write some bytes in the returned space, call
* tao_adjust_buffer_contents_size() to change the size of the contents of the
* tao_buffer_adjust_contents_size() to change the size of the contents of the
* i/o buffer.