- \ref XBT_swag
- \ref XBT_heap
- \ref XBT_peer
+ - @ref xbt_strbuff
- \ref XBT_misc
- \ref XBT_graph
SG_BEGIN_DECL()
-/** Buffer code **/
+/** @defgroup xbt_strbuff String buffers
+ * @ingroup XBT_adt
+ *
+ * This data container is very similar to the Java StringBuffer:
+ * that's a string to which you can add content with a lesser performance
+ * penalty than if you recreate a new string from scratch. Once done building
+ * your string, you must retrieve the content and free its container.
+ *
+ * @{
+ */
+
+/** @brief Buffer data container **/
struct xbt_strbuff {
char *data;
int used, size;
typedef struct xbt_strbuff s_xbt_strbuff_t;
typedef struct xbt_strbuff* xbt_strbuff_t;
-XBT_PUBLIC(void) xbt_strbuff_empty(xbt_strbuff_t b);
+XBT_PUBLIC(void) xbt_strbuff_clear(xbt_strbuff_t b);
XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new(void);
XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new_from(const char *s);
XBT_PUBLIC(void) xbt_strbuff_free(xbt_strbuff_t b);
XBT_PUBLIC(void) xbt_strbuff_free_container(xbt_strbuff_t b);
XBT_PUBLIC(void) xbt_strbuff_append(xbt_strbuff_t b, const char *toadd);
+XBT_PUBLIC(void) xbt_strbuff_printf(xbt_strbuff_t b, const char *fmt, ...);
XBT_PUBLIC(void) xbt_strbuff_chomp(xbt_strbuff_t b);
XBT_PUBLIC(void) xbt_strbuff_trim(xbt_strbuff_t b);
XBT_PUBLIC(void) xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns);
+/** @} */
SG_END_DECL()
#endif
}
}
-static void get_hierarchy_string(jed_simgrid_container_t container, char *outbuf) {
+static void get_hierarchy_string(jed_simgrid_container_t container, char *outbuf, int bufsize) {
char buf[STR_BUF_SIZE];
xbt_dynar_t hier_list;
unsigned int iter;
} else {
snprintf(buf, STR_BUF_SIZE, "%d", number);
}
- strncat(outbuf, buf, STR_BUF_SIZE-strlen(outbuf));
+ strncat(outbuf, buf, bufsize-strlen(outbuf));
}
xbt_dynar_free(&hier_list);
unsigned int res_nb;
unsigned int i;
char *res_name;
- char resid[STR_BUF_SIZE];
+ char resid[STR_BUF_SIZE];
xbt_assert( resource_parent->resource_list != NULL );
res_nb = xbt_dynar_length(resource_parent->resource_list);
- get_hierarchy_string(resource_parent, resid);
+ get_hierarchy_string(resource_parent, resid, STR_BUF_SIZE);
fprintf(jed_file, " <rset id=\"%s\" nb=\"%d\" names=\"", resid, res_nb);
xbt_dynar_foreach(resource_parent->resource_list, i, res_name) {
int end = subset->start_idx + subset->nres - 1;
char resid[STR_BUF_SIZE];
- get_hierarchy_string(subset->parent, resid);
+ get_hierarchy_string(subset->parent, resid, STR_BUF_SIZE);
fprintf(jed_file, " <select resources=\"");
fprintf(jed_file, "%s", resid);
fprintf(jed_file, ".[%d-%d]", start, end);
dst = (SD_task_t)xbt_dict_get_or_null(jobs, dst_name);
if (size > 0) {
- int namesize=(strlen(src_name)+strlen(dst_name)+6);
- char *name = (char*)xbt_malloc(namesize*sizeof(char*));
- snprintf(name,namesize, "%s->%s", src_name, dst_name);
+ char *name = bprintf("%s->%s", src_name, dst_name);
XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
if (seq_or_par == sequential)
}
/* build description line with possible values */
-static void describe_model(char *result,
+static void describe_model(char *result,int resultsize,
const s_surf_model_description_t model_description[],
const char *name,
const char *description)
{
- char *p = result +
- snprintf(result,1024-strlen(result), "%s. Possible values: %s", description,
+ char *p = result;
+ p += snprintf(result,resultsize-1, "%s. Possible values: %s", description,
model_description[0].name ? model_description[0].name : "n/a");
for (int i = 1; model_description[i].name; i++)
- p += snprintf(p,1024, ", %s", model_description[i].name);
- snprintf(p,1024, ".\n (use 'help' as a value to see the long description of each %s)", name);
+ p += snprintf(p,resultsize-(p-result)-1, ", %s", model_description[i].name);
+ p += snprintf(p,resultsize-(p-result)-1, ".\n (use 'help' as a value to see the long description of each %s)", name);
+
+ xbt_assert(p<result+resultsize-1,"Buffer too small to display the model description of %s",name);
}
/* create the config set, register what should be and parse the command line*/
void sg_config_init(int *argc, char **argv)
{
+ int descsize = 1024;
char description[1024];
/* Create the configuration support */
if (_sg_cfg_init_status == 0) { /* Only create stuff if not already inited */
/* Plugins configuration */
- describe_model(description, surf_plugin_description, "plugin", "The plugins");
+ describe_model(description,descsize, surf_plugin_description, "plugin", "The plugins");
xbt_cfg_register_string("plugin", nullptr, &_sg_cfg_cb__plugin, description);
- describe_model(description, surf_cpu_model_description, "model", "The model to use for the CPU");
+ describe_model(description,descsize, surf_cpu_model_description, "model", "The model to use for the CPU");
xbt_cfg_register_string("cpu/model", "Cas01", &_sg_cfg_cb__cpu_model, description);
- describe_model(description, surf_optimization_mode_description, "optimization mode", "The optimization modes to use for the CPU");
+ describe_model(description,descsize, surf_optimization_mode_description, "optimization mode", "The optimization modes to use for the CPU");
xbt_cfg_register_string("cpu/optim", "Lazy", &_sg_cfg_cb__optimization_mode, description);
- describe_model(description, surf_storage_model_description, "model", "The model to use for the storage");
+ describe_model(description,descsize, surf_storage_model_description, "model", "The model to use for the storage");
xbt_cfg_register_string("storage/model", "default", &_sg_cfg_cb__storage_mode, description);
- describe_model(description, surf_network_model_description, "model", "The model to use for the network");
+ describe_model(description,descsize, surf_network_model_description, "model", "The model to use for the network");
xbt_cfg_register_string("network/model", "LV08", &_sg_cfg_cb__network_model, description);
- describe_model(description, surf_optimization_mode_description, "optimization mode", "The optimization modes to use for the network");
+ describe_model(description,descsize, surf_optimization_mode_description, "optimization mode", "The optimization modes to use for the network");
xbt_cfg_register_string("network/optim", "Lazy", &_sg_cfg_cb__optimization_mode, description);
- describe_model(description, surf_host_model_description, "model", "The model to use for the host");
+ describe_model(description,descsize, surf_host_model_description, "model", "The model to use for the host");
xbt_cfg_register_string("host/model", "default", &_sg_cfg_cb__host_model, description);
- describe_model(description, surf_vm_model_description, "model", "The model to use for the vm");
+ describe_model(description,descsize, surf_vm_model_description, "model", "The model to use for the vm");
xbt_cfg_register_string("vm/model", "default", &_sg_cfg_cb__vm_model, description);
simgrid::config::bindFlag(sg_tcp_gamma = 4194304.0,
#include "xbt/sysdep.h"
#include "xbt/log.h"
+#include "xbt/strbuff.h"
#include "xbt/mallocator.h"
#include "maxmin_private.hpp"
#include <stdlib.h>
xbt_swag_t cnst_list = NULL;
xbt_swag_t var_list = NULL;
xbt_swag_t elem_list = NULL;
- char print_buf[1024];
- char *trace_buf = (char*) xbt_malloc0(sizeof(char));
+ xbt_strbuff_t buf = xbt_strbuff_new();
double sum = 0.0;
/* Printing Objective */
var_list = &(sys->variable_set);
- snprintf(print_buf,11, "MAX-MIN ( ");
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
+ xbt_strbuff_append(buf, "MAX-MIN ( ");
xbt_swag_foreach(_var, var_list) {
- var = (lmm_variable_t)_var;
- snprintf(print_buf,1024, "'%d'(%f) ", var->id_int, var->weight);
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
+ var = (lmm_variable_t)_var;
+ xbt_strbuff_printf(buf, "'%d'(%f) ", var->id_int, var->weight);
}
- snprintf(print_buf,2, ")");
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
- XBT_DEBUG("%20s", trace_buf);
- trace_buf[0] = '\000';
+ xbt_strbuff_append(buf, ")");
+ XBT_DEBUG("%20s", buf->data);
+ xbt_strbuff_clear(buf);
XBT_DEBUG("Constraints");
/* Printing Constraints */
sum = 0.0;
//Show the enabled variables
elem_list = &(cnst->enabled_element_set);
- snprintf(print_buf,2, "\t");
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
- snprintf(print_buf,1024, "%s(",(cnst->sharing_policy)?"":"max");
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
+ xbt_strbuff_append(buf, "\t");
+ xbt_strbuff_printf(buf, "%s(", (cnst->sharing_policy)?"":"max");
xbt_swag_foreach(_elem, elem_list) {
elem = (lmm_element_t)_elem;
- snprintf(print_buf,1024, "%f.'%d'(%f) %s ", elem->value,
+ xbt_strbuff_printf(buf, "%f.'%d'(%f) %s ", elem->value,
elem->variable->id_int, elem->variable->value,(cnst->sharing_policy)?"+":",");
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
if(cnst->sharing_policy)
sum += elem->value * elem->variable->value;
else
elem_list = &(cnst->disabled_element_set);
xbt_swag_foreach(_elem, elem_list) {
elem = (lmm_element_t)_elem;
- snprintf(print_buf,1024, "%f.'%d'(%f) %s ", elem->value,
+ xbt_strbuff_printf(buf, "%f.'%d'(%f) %s ", elem->value,
elem->variable->id_int, elem->variable->value,(cnst->sharing_policy)?"+":",");
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
if(cnst->sharing_policy)
sum += elem->value * elem->variable->value;
else
sum = MAX(sum,elem->value * elem->variable->value);
}
- snprintf(print_buf,1024, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
+ xbt_strbuff_printf(buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
if (!cnst->sharing_policy) {
- snprintf(print_buf,1024, " [MAX-Constraint]");
- trace_buf = (char*) xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
- strncat(trace_buf, print_buf, strlen(print_buf));
+ xbt_strbuff_printf(buf, " [MAX-Constraint]");
}
- XBT_DEBUG("%s", trace_buf);
- trace_buf[0] = '\000';
- xbt_assert(!double_positive(sum - cnst->bound, cnst->bound*sg_maxmin_precision),
- "Incorrect value (%f is not smaller than %f): %g", sum, cnst->bound, sum - cnst->bound);
+ XBT_DEBUG("%s", buf->data);
+ xbt_strbuff_clear(buf);
+ xbt_assert(!double_positive(sum - cnst->bound, cnst->bound*sg_maxmin_precision),
+ "Incorrect value (%f is not smaller than %f): %g", sum, cnst->bound, sum - cnst->bound);
//if(double_positive(sum - cnst->bound, cnst->bound*sg_maxmin_precision))
//XBT_ERROR("Incorrect value (%f is not smaller than %f): %g",sum, cnst->bound, sum - cnst->bound);
}
}
}
- free(trace_buf);
+ xbt_strbuff_free(buf);
}
void lmm_solve(lmm_system_t sys)
* under the terms of the license (GNU LGPL) which comes with this package. */
#include "xbt/strbuff.h"
+#include <stdarg.h>
#define minimal_increment 512
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(strbuff, xbt, "String buffers");
-inline void xbt_strbuff_empty(xbt_strbuff_t b)
+/** @brief Remove any content from the buffer */
+inline void xbt_strbuff_clear(xbt_strbuff_t b)
{
b->used = 0;
b->data[0] = '\0';
}
+/** @brief Constructor */
xbt_strbuff_t xbt_strbuff_new(void)
{
xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t));
res->data = xbt_malloc(512);
res->size = 512;
- xbt_strbuff_empty(res);
+ xbt_strbuff_clear(res);
return res;
}
}
}
+/** @brief Adds some content at the end of the buffer */
void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
{
int addlen;
b->used += addlen;
}
+/** @brief format some content and push it at the end of the buffer */
+void xbt_strbuff_printf(xbt_strbuff_t b, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ char *data = bvprintf(fmt, ap);
+ xbt_strbuff_append(b, data);
+ xbt_free(data);
+ va_end(ap);
+}
+
/** @brief Replaces a set of variables by their values
*
* @param b buffer to modify